Files
privacy.sexy/tests/unit/presentation/components/Shared/Hooks/Clipboard/BrowserClipboard.spec.ts
undergroundwires 7770a9b521 Refactor DI for simplicity and type safety
This commit improves the dependency injection mechanism by introducing a
custom `injectKey` function.

Key improvements are:

- Enforced type consistency during dependency registration and
  instantiation.
- Simplified injection process, abstracting away the complexity with a
  uniform API, regardless of the dependency's lifetime.
- Eliminated the possibility of `undefined` returns during dependency
  injection, promoting fail-fast behavior.
- Removed the necessity for type casting to `symbol` for injection keys
  in unit tests by using existing types.
- Consalidated imports, combining keys and injection functions in one
  `import` statement.
2023-11-09 13:17:38 +01:00

74 lines
2.3 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { BrowserClipboard, NavigatorClipboard } from '@/presentation/components/Shared/Hooks/Clipboard/BrowserClipboard';
import { StubWithObservableMethodCalls } from '@tests/unit/shared/Stubs/StubWithObservableMethodCalls';
import { expectThrowsAsync } from '@tests/shared/Assertions/ExpectThrowsAsync';
describe('BrowserClipboard', () => {
describe('writeText', () => {
it('calls navigator clipboard with the correct text', async () => {
// arrange
const expectedText = 'test text';
const navigatorClipboard = new NavigatorClipboardStub();
const clipboard = new BrowserClipboard(navigatorClipboard);
// act
await clipboard.copyText(expectedText);
// assert
const calls = navigatorClipboard.callHistory;
expect(calls).to.have.lengthOf(1);
const call = calls.find((c) => c.methodName === 'writeText');
expect(call).toBeDefined();
const [actualText] = call.args;
expect(actualText).to.equal(expectedText);
});
it('throws when navigator clipboard fails', async () => {
// arrange
const expectedError = 'internalError';
const navigatorClipboard = new NavigatorClipboardStub();
navigatorClipboard.writeText = () => {
throw new Error(expectedError);
};
const clipboard = new BrowserClipboard(navigatorClipboard);
// act
const act = () => clipboard.copyText('unimportant-text');
// assert
await expectThrowsAsync(act, expectedError);
});
});
});
class NavigatorClipboardStub
extends StubWithObservableMethodCalls<NavigatorClipboard>
implements NavigatorClipboard {
writeText(data: string): Promise<void> {
this.registerMethodCall({
methodName: 'writeText',
args: [data],
});
return Promise.resolve();
}
read(): Promise<ClipboardItems> {
throw new Error('Method not implemented.');
}
readText(): Promise<string> {
throw new Error('Method not implemented.');
}
write(): Promise<void> {
throw new Error('Method not implemented.');
}
addEventListener(): void {
throw new Error('Method not implemented.');
}
dispatchEvent(): boolean {
throw new Error('Method not implemented.');
}
removeEventListener(): void {
throw new Error('Method not implemented.');
}
}