Mock clipboard
MODIFY
readme.md
readme.md
@@ -85,7 +85,7 @@ var text = ClipboardService.GetText();
## Instance API
-In adition to the above static API, there is an instance API exposed:
+In addition to the above static API, there is an instance API exposed:
<!-- snippet: SetTextInstance -->
<a id='snippet-settextinstance'/></a>
@@ -99,7 +99,7 @@ clipboard.SetText("Text to place in clipboard");
### Dependency Injection
-An instance of `IClipboard` can be injected into `IServiceCollection`:
+An instance of `Clipboard` can be injected into `IServiceCollection`:
<!-- snippet: InjectClipboard -->
<a id='snippet-injectclipboard'/></a>
@@ -109,6 +109,10 @@ serviceCollection.InjectClipboard();
<sup><a href='/src/BlazorSample/Program.cs#L16-L18' title='File snippet `injectclipboard` was extracted from'>snippet source</a> | <a href='#snippet-injectclipboard' title='Navigate to start of snippet `injectclipboard`'>anchor</a></sup>
<!-- endsnippet -->
+The instance should be injected by using `IClipboard`.
+
+There is also a `InjectMockClipboard` that injects an instance of `MockClipboard` with all methods stubbed out.
+
## Supported on

MODIFY
readme.source.md
readme.source.md
@@ -41,17 +41,21 @@ snippet: GetText
## Instance API
-In adition to the above static API, there is an instance API exposed:
+In addition to the above static API, there is an instance API exposed:
snippet: SetTextInstance
### Dependency Injection
-An instance of `IClipboard` can be injected into `IServiceCollection`:
+An instance of `Clipboard` can be injected into `IServiceCollection`:
snippet: InjectClipboard
+The instance should be injected by using `IClipboard`.
+
+There is also a `InjectMockClipboard` that injects an instance of `MockClipboard` with all methods stubbed out.
+
## Supported on

MODIFY
src/TextCopy/BlazorClipboard.cs
src/TextCopy/BlazorClipboard.cs
@@ -6,9 +6,7 @@ using System.Threading.Tasks;
namespace TextCopy
{
- /// <summary>
- /// Provides methods to place text on and retrieve text from the system Clipboard.
- /// </summary>
+ /// <inheritdoc />
public class BlazorClipboard :
IClipboard
{
@@ -34,35 +32,27 @@ namespace TextCopy
invokeAsync = invokeAsync.MakeGenericMethod(typeof(string));
}
- /// <summary>
- /// Retrieves text data from the Clipboard.
- /// </summary>
+ /// <inheritdoc />
public virtual async Task<string?> GetTextAsync(CancellationToken cancellation = default)
{
var parameters = new object[] {"navigator.clipboard.readText", cancellation, Array.Empty<object>()};
return await (ValueTask<string>) invokeAsync.Invoke(jsRuntime, parameters);
}
- /// <summary>
- /// Retrieves text data from the Clipboard.
- /// </summary>
+ /// <inheritdoc />
public virtual string? GetText()
{
return GetTextAsync().GetAwaiter().GetResult();
}
- /// <summary>
- /// Clears the Clipboard and then adds text data to it.
- /// </summary>
+ /// <inheritdoc />
public virtual async Task SetTextAsync(string text, CancellationToken cancellation = default)
{
var parameters = new object[] {"navigator.clipboard.writeText", cancellation, new object[] {text}};
await (ValueTask<string>) invokeAsync.Invoke(jsRuntime, parameters);
}
- /// <summary>
- /// Clears the Clipboard and then adds text data to it.
- /// </summary>
+ /// <inheritdoc />
public virtual void SetText(string text)
{
SetTextAsync(text).GetAwaiter().GetResult();

MODIFY
src/TextCopy/Clipboard.cs
src/TextCopy/Clipboard.cs
@@ -3,39 +3,29 @@ using System.Threading.Tasks;
namespace TextCopy
{
- /// <summary>
- /// Provides methods to place text on and retrieve text from the system Clipboard.
- /// </summary>
+ /// <inheritdoc />
public class Clipboard :
IClipboard
{
- /// <summary>
- /// Retrieves text data from the Clipboard.
- /// </summary>
+ /// <inheritdoc />
public virtual Task<string?> GetTextAsync(CancellationToken cancellation = default)
{
return ClipboardService.GetTextAsync(cancellation);
}
- /// <summary>
- /// Retrieves text data from the Clipboard.
- /// </summary>
+ /// <inheritdoc />
public virtual string? GetText()
{
return ClipboardService.GetText();
}
- /// <summary>
- /// Clears the Clipboard and then adds text data to it.
- /// </summary>
+ /// <inheritdoc />
public virtual Task SetTextAsync(string text, CancellationToken cancellation = default)
{
return ClipboardService.SetTextAsync(text, cancellation);
}
- /// <summary>
- /// Clears the Clipboard and then adds text data to it.
- /// </summary>
+ /// <inheritdoc />
public virtual void SetText(string text)
{
ClipboardService.SetText(text);

MODIFY
src/TextCopy/ServiceExtensions.cs
src/TextCopy/ServiceExtensions.cs
@@ -9,10 +9,22 @@ namespace TextCopy
public static class ServiceExtensions
{
/// <summary>
- /// Retrieves text data from the Clipboard.
+ /// Adds a singleton instance of <see cref="MockClipboard"/> as an
+ /// <see cref="IClipboard"/> to <paramref name="services"/>.
+ /// </summary>
+ public static void InjectMockClipboard(this IServiceCollection services)
+ {
+ Guard.AgainstNull(services, nameof(services));
+ services.AddSingleton<IClipboard>(_ => new MockClipboard());
+ }
+
+ /// <summary>
+ /// Adds a singleton instance of <see cref="Clipboard"/> as an
+ /// <see cref="IClipboard"/> to <paramref name="services"/>.
/// </summary>
public static void InjectClipboard(this IServiceCollection services)
{
+ Guard.AgainstNull(services, nameof(services));
services.AddSingleton<IClipboard>(provider =>
{

Mock clipboard (#202)