mirror of
https://github.com/bitwarden/browser.git
synced 2024-12-11 14:48:46 +01:00
06affa9654
* Remove derived state from state classes * Create provider for derived state Derived state is automatically stored to memory storage, but can be derived from any observable. * Fixup state provider method definitions * Test `DefaultDerivedState` * remove implementation notes * Write docs for derived state * fixup derived state provider types * Implement buffered delayUntil operator * Move state types to a common module * Move mock ports to centra location * Alias DerivedStateDependency type * Add dependencies to browser * Prefer internal rxjs operators for ref counting * WIP * Ensure complete on subjects * Foreground/background messaging for browser Defers work for browser to the background * Test foreground port behaviors * Inject foreground and background derived state services * remove unnecessary class field * Adhere to required options * Add dderived state to CLI * Prefer type definition in type parameters to options * Prefer instance method * Implements factory methods for common uses * Remove nothing test * Remove share subject reference Share manages connector subjects internally and will reuse them until refcount is 0 and the cleanup time has passed. Saving our own reference just risks memory leaks without real testability benefits. * Fix interaction state
30 lines
1.1 KiB
TypeScript
30 lines
1.1 KiB
TypeScript
import { mockDeep } from "jest-mock-extended";
|
|
|
|
/**
|
|
* Mocks a chrome.runtime.Port set up to send messages through `postMessage` to `onMessage.addListener` callbacks.
|
|
* @param name - The name of the port.
|
|
* @param immediateOnConnectExecution - Whether to immediately execute the onConnect callbacks against the new port.
|
|
* Defaults to false. If true, the creator of the port will not have had a chance to set up listeners yet.
|
|
* @returns a mock chrome.runtime.Port
|
|
*/
|
|
export function mockPorts() {
|
|
// notify listeners of a new port
|
|
(chrome.runtime.connect as jest.Mock).mockImplementation((portInfo) => {
|
|
const port = mockDeep<chrome.runtime.Port>();
|
|
port.name = portInfo.name;
|
|
|
|
// set message broadcast
|
|
(port.postMessage as jest.Mock).mockImplementation((message) => {
|
|
(port.onMessage.addListener as jest.Mock).mock.calls.forEach(([callbackFn]) => {
|
|
callbackFn(message, port);
|
|
});
|
|
});
|
|
|
|
(chrome.runtime.onConnect.addListener as jest.Mock).mock.calls.forEach(([callbackFn]) => {
|
|
callbackFn(port);
|
|
});
|
|
|
|
return port;
|
|
});
|
|
}
|