cleaner test API
Summary: The test mock utilities now return a promise, rather than taking a callback, which makes tests slightly nicer to read (similar to react-testing-library). No semantic changes. Reviewed By: jknoxville Differential Revision: D22186278 fbshipit-source-id: ec5b9f4e6bfeee9160e331f8c20a1d4fdcbfeede
This commit is contained in:
committed by
Facebook GitHub Bot
parent
12ac29685d
commit
83e6968fa1
@@ -46,9 +46,9 @@ class TestPlugin extends FlipperPlugin<any, any, any> {
|
||||
}
|
||||
|
||||
test('Plugin container can render plugin and receive updates', async () => {
|
||||
await renderMockFlipperWithPlugin(
|
||||
const {renderer, sendMessage, act} = await renderMockFlipperWithPlugin(
|
||||
TestPlugin,
|
||||
async ({renderer, sendMessage, act}) => {
|
||||
);
|
||||
expect(renderer.baseElement).toMatchInlineSnapshot(`
|
||||
<body>
|
||||
<div>
|
||||
@@ -78,6 +78,4 @@ test('Plugin container can render plugin and receive updates', async () => {
|
||||
});
|
||||
|
||||
expect((await renderer.findByTestId('counter')).textContent).toBe('2');
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
@@ -40,9 +40,12 @@ class TestPlugin extends FlipperPlugin<any, any, any> {
|
||||
}
|
||||
|
||||
test('can create a Fake flipper', async () => {
|
||||
await createMockFlipperWithPlugin(
|
||||
TestPlugin,
|
||||
async ({client, device, store, sendMessage}) => {
|
||||
const {
|
||||
client,
|
||||
device,
|
||||
store,
|
||||
sendMessage,
|
||||
} = await createMockFlipperWithPlugin(TestPlugin);
|
||||
expect(client).toBeTruthy();
|
||||
expect(device).toBeTruthy();
|
||||
expect(store).toBeTruthy();
|
||||
@@ -58,6 +61,4 @@ test('can create a Fake flipper', async () => {
|
||||
},
|
||||
}
|
||||
`);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
@@ -41,7 +41,7 @@ export function createStubLogger(): Logger {
|
||||
};
|
||||
}
|
||||
|
||||
type MockFlipperCallbackArgs = {
|
||||
type MockFlipperResult = {
|
||||
client: Client;
|
||||
device: BaseDevice;
|
||||
store: Store;
|
||||
@@ -53,8 +53,7 @@ type MockFlipperCallbackArgs = {
|
||||
|
||||
export async function createMockFlipperWithPlugin(
|
||||
pluginClazz: typeof FlipperPlugin,
|
||||
callback: (args: MockFlipperCallbackArgs) => Promise<void>,
|
||||
) {
|
||||
): Promise<MockFlipperResult> {
|
||||
const store = createStore(reducers);
|
||||
const logger = createStubLogger();
|
||||
store.dispatch(registerPlugins([pluginClazz]));
|
||||
@@ -134,7 +133,7 @@ export async function createMockFlipperWithPlugin(
|
||||
}),
|
||||
);
|
||||
|
||||
await callback({
|
||||
return {
|
||||
client,
|
||||
device: device as any,
|
||||
store,
|
||||
@@ -153,21 +152,21 @@ export async function createMockFlipperWithPlugin(
|
||||
createDevice,
|
||||
createClient,
|
||||
logger,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
type Renderer = RenderResult<typeof import('testing-library__dom/queries')>;
|
||||
|
||||
export async function renderMockFlipperWithPlugin(
|
||||
pluginClazz: typeof FlipperPlugin,
|
||||
callback: (
|
||||
args: MockFlipperCallbackArgs & {
|
||||
): Promise<
|
||||
MockFlipperResult & {
|
||||
renderer: Renderer;
|
||||
act: (cb: () => void) => void;
|
||||
},
|
||||
) => Promise<void>,
|
||||
) {
|
||||
return createMockFlipperWithPlugin(pluginClazz, async (args) => {
|
||||
}
|
||||
> {
|
||||
const args = await createMockFlipperWithPlugin(pluginClazz);
|
||||
|
||||
function selectTestPlugin(store: Store, client: Client) {
|
||||
store.dispatch(
|
||||
selectPlugin({
|
||||
@@ -187,15 +186,12 @@ export async function renderMockFlipperWithPlugin(
|
||||
</Provider>,
|
||||
);
|
||||
|
||||
await callback({
|
||||
return {
|
||||
...args,
|
||||
renderer: renderer as any,
|
||||
act(cb) {
|
||||
testingLibAct(cb);
|
||||
args.client.flushMessageBuffer();
|
||||
},
|
||||
});
|
||||
|
||||
renderer.unmount();
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
@@ -86,9 +86,9 @@ function selectTestPlugin(store: Store, client: Client) {
|
||||
}
|
||||
|
||||
test('queue - events are processed immediately if plugin is selected', async () => {
|
||||
await createMockFlipperWithPlugin(
|
||||
const {store, client, sendMessage} = await createMockFlipperWithPlugin(
|
||||
TestPlugin,
|
||||
async ({store, client, sendMessage}) => {
|
||||
);
|
||||
expect(store.getState().connections.selectedPlugin).toBe('TestPlugin');
|
||||
sendMessage('noop', {});
|
||||
sendMessage('noop', {});
|
||||
@@ -106,18 +106,17 @@ test('queue - events are processed immediately if plugin is selected', async ()
|
||||
expect(store.getState().pluginMessageQueue).toMatchInlineSnapshot(
|
||||
`Object {}`,
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test('queue - events are NOT processed immediately if plugin is NOT selected (but enabled)', async () => {
|
||||
await createMockFlipperWithPlugin(
|
||||
TestPlugin,
|
||||
async ({client, device, store, sendMessage}) => {
|
||||
const {
|
||||
store,
|
||||
client,
|
||||
sendMessage,
|
||||
device,
|
||||
} = await createMockFlipperWithPlugin(TestPlugin);
|
||||
selectDeviceLogs(store);
|
||||
expect(store.getState().connections.selectedPlugin).not.toBe(
|
||||
'TestPlugin',
|
||||
);
|
||||
expect(store.getState().connections.selectedPlugin).not.toBe('TestPlugin');
|
||||
|
||||
sendMessage('inc', {});
|
||||
sendMessage('inc', {delta: 2});
|
||||
@@ -203,18 +202,17 @@ test('queue - events are NOT processed immediately if plugin is NOT selected (bu
|
||||
expect(store.getState().pluginMessageQueue).toEqual({
|
||||
[pluginKey]: [{api: 'TestPlugin', method: 'inc', params: {delta: 5}}],
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test('queue - events are queued for plugins that are favorite when app is not selected', async () => {
|
||||
await createMockFlipperWithPlugin(
|
||||
TestPlugin,
|
||||
async ({device, store, sendMessage, createClient}) => {
|
||||
const {
|
||||
device,
|
||||
store,
|
||||
sendMessage,
|
||||
createClient,
|
||||
} = await createMockFlipperWithPlugin(TestPlugin);
|
||||
selectDeviceLogs(store);
|
||||
expect(store.getState().connections.selectedPlugin).not.toBe(
|
||||
'TestPlugin',
|
||||
);
|
||||
expect(store.getState().connections.selectedPlugin).not.toBe('TestPlugin');
|
||||
|
||||
const client2 = createClient(device, 'TestApp2');
|
||||
store.dispatch(selectClient(client2.id));
|
||||
@@ -236,18 +234,18 @@ test('queue - events are queued for plugins that are favorite when app is not se
|
||||
],
|
||||
}
|
||||
`);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test('queue - events are queued for plugins that are favorite when app is selected on different device', async () => {
|
||||
await createMockFlipperWithPlugin(
|
||||
TestPlugin,
|
||||
async ({client, store, sendMessage, createDevice, createClient}) => {
|
||||
const {
|
||||
client,
|
||||
store,
|
||||
sendMessage,
|
||||
createDevice,
|
||||
createClient,
|
||||
} = await createMockFlipperWithPlugin(TestPlugin);
|
||||
selectDeviceLogs(store);
|
||||
expect(store.getState().connections.selectedPlugin).not.toBe(
|
||||
'TestPlugin',
|
||||
);
|
||||
expect(store.getState().connections.selectedPlugin).not.toBe('TestPlugin');
|
||||
|
||||
const device2 = createDevice('serial2');
|
||||
const client2 = createClient(device2, client.query.app); // same app id
|
||||
@@ -271,14 +269,15 @@ test('queue - events are queued for plugins that are favorite when app is select
|
||||
],
|
||||
}
|
||||
`);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test('queue - events processing will be paused', async () => {
|
||||
await createMockFlipperWithPlugin(
|
||||
TestPlugin,
|
||||
async ({client, device, store, sendMessage}) => {
|
||||
const {
|
||||
client,
|
||||
device,
|
||||
store,
|
||||
sendMessage,
|
||||
} = await createMockFlipperWithPlugin(TestPlugin);
|
||||
selectDeviceLogs(store);
|
||||
|
||||
sendMessage('inc', {});
|
||||
@@ -292,13 +291,7 @@ test('queue - events processing will be paused', async () => {
|
||||
// controlled idler will signal and and off that idling is needed
|
||||
const idler = new TestIdler();
|
||||
|
||||
const p = processMessageQueue(
|
||||
TestPlugin,
|
||||
pluginKey,
|
||||
store,
|
||||
undefined,
|
||||
idler,
|
||||
);
|
||||
const p = processMessageQueue(TestPlugin, pluginKey, store, undefined, idler);
|
||||
|
||||
expect(store.getState().pluginStates).toEqual({
|
||||
[pluginKey]: {
|
||||
@@ -324,14 +317,15 @@ test('queue - events processing will be paused', async () => {
|
||||
// don't idle anymore
|
||||
idler.run();
|
||||
await p;
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test('queue - messages that arrive during processing will be queued', async () => {
|
||||
await createMockFlipperWithPlugin(
|
||||
TestPlugin,
|
||||
async ({client, device, store, sendMessage}) => {
|
||||
const {
|
||||
client,
|
||||
device,
|
||||
store,
|
||||
sendMessage,
|
||||
} = await createMockFlipperWithPlugin(TestPlugin);
|
||||
selectDeviceLogs(store);
|
||||
|
||||
sendMessage('inc', {});
|
||||
@@ -344,13 +338,7 @@ test('queue - messages that arrive during processing will be queued', async () =
|
||||
|
||||
const idler = new TestIdler();
|
||||
|
||||
const p = processMessageQueue(
|
||||
TestPlugin,
|
||||
pluginKey,
|
||||
store,
|
||||
undefined,
|
||||
idler,
|
||||
);
|
||||
const p = processMessageQueue(TestPlugin, pluginKey, store, undefined, idler);
|
||||
|
||||
// first message is consumed
|
||||
expect(store.getState().pluginMessageQueue[pluginKey].length).toBe(1);
|
||||
@@ -379,14 +367,15 @@ test('queue - messages that arrive during processing will be queued', async () =
|
||||
|
||||
idler.run();
|
||||
await p;
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test('queue - processing can be cancelled', async () => {
|
||||
await createMockFlipperWithPlugin(
|
||||
TestPlugin,
|
||||
async ({client, device, store, sendMessage}) => {
|
||||
const {
|
||||
client,
|
||||
device,
|
||||
store,
|
||||
sendMessage,
|
||||
} = await createMockFlipperWithPlugin(TestPlugin);
|
||||
selectDeviceLogs(store);
|
||||
|
||||
sendMessage('inc', {});
|
||||
@@ -401,13 +390,7 @@ test('queue - processing can be cancelled', async () => {
|
||||
|
||||
const idler = new TestIdler();
|
||||
|
||||
const p = processMessageQueue(
|
||||
TestPlugin,
|
||||
pluginKey,
|
||||
store,
|
||||
undefined,
|
||||
idler,
|
||||
);
|
||||
const p = processMessageQueue(TestPlugin, pluginKey, store, undefined, idler);
|
||||
|
||||
// first message is consumed
|
||||
await idler.next();
|
||||
@@ -419,14 +402,15 @@ test('queue - processing can be cancelled', async () => {
|
||||
expect(store.getState().pluginMessageQueue[pluginKey].length).toBe(1);
|
||||
expect(store.getState().pluginStates[pluginKey].count).toBe(10);
|
||||
await p;
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test('queue - make sure resetting plugin state clears the message queue', async () => {
|
||||
await createMockFlipperWithPlugin(
|
||||
TestPlugin,
|
||||
async ({client, device, store, sendMessage}) => {
|
||||
const {
|
||||
client,
|
||||
device,
|
||||
store,
|
||||
sendMessage,
|
||||
} = await createMockFlipperWithPlugin(TestPlugin);
|
||||
selectDeviceLogs(store);
|
||||
|
||||
sendMessage('inc', {});
|
||||
@@ -443,8 +427,6 @@ test('queue - make sure resetting plugin state clears the message queue', async
|
||||
});
|
||||
|
||||
expect(store.getState().pluginMessageQueue[pluginKey]).toBe(undefined);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test('queue will be cleaned up when it exceeds maximum size', () => {
|
||||
@@ -494,9 +476,12 @@ test('client - incoming messages are buffered and flushed together', async () =>
|
||||
static persistedStateReducer = jest.fn();
|
||||
}
|
||||
|
||||
await createMockFlipperWithPlugin(
|
||||
TestPlugin,
|
||||
async ({client, store, device, sendMessage}) => {
|
||||
const {
|
||||
client,
|
||||
store,
|
||||
device,
|
||||
sendMessage,
|
||||
} = await createMockFlipperWithPlugin(TestPlugin);
|
||||
selectDeviceLogs(store);
|
||||
|
||||
store.dispatch(registerPlugins([StubDeviceLogs]));
|
||||
@@ -601,9 +586,9 @@ test('client - incoming messages are buffered and flushed together', async () =>
|
||||
}
|
||||
`);
|
||||
expect(client.messageBuffer).toMatchInlineSnapshot(`Object {}`);
|
||||
expect(
|
||||
StubDeviceLogs.persistedStateReducer.mock.calls,
|
||||
).toMatchInlineSnapshot(`Array []`);
|
||||
expect(StubDeviceLogs.persistedStateReducer.mock.calls).toMatchInlineSnapshot(
|
||||
`Array []`,
|
||||
);
|
||||
|
||||
// tigger processing the queue
|
||||
const pluginKey = getPluginKey(client.id, device, StubDeviceLogs.id);
|
||||
@@ -648,6 +633,4 @@ test('client - incoming messages are buffered and flushed together', async () =>
|
||||
],
|
||||
}
|
||||
`);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user