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 () => {
|
test('Plugin container can render plugin and receive updates', async () => {
|
||||||
await renderMockFlipperWithPlugin(
|
const {renderer, sendMessage, act} = await renderMockFlipperWithPlugin(
|
||||||
TestPlugin,
|
TestPlugin,
|
||||||
async ({renderer, sendMessage, act}) => {
|
);
|
||||||
expect(renderer.baseElement).toMatchInlineSnapshot(`
|
expect(renderer.baseElement).toMatchInlineSnapshot(`
|
||||||
<body>
|
<body>
|
||||||
<div>
|
<div>
|
||||||
@@ -78,6 +78,4 @@ test('Plugin container can render plugin and receive updates', async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
expect((await renderer.findByTestId('counter')).textContent).toBe('2');
|
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 () => {
|
test('can create a Fake flipper', async () => {
|
||||||
await createMockFlipperWithPlugin(
|
const {
|
||||||
TestPlugin,
|
client,
|
||||||
async ({client, device, store, sendMessage}) => {
|
device,
|
||||||
|
store,
|
||||||
|
sendMessage,
|
||||||
|
} = await createMockFlipperWithPlugin(TestPlugin);
|
||||||
expect(client).toBeTruthy();
|
expect(client).toBeTruthy();
|
||||||
expect(device).toBeTruthy();
|
expect(device).toBeTruthy();
|
||||||
expect(store).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;
|
client: Client;
|
||||||
device: BaseDevice;
|
device: BaseDevice;
|
||||||
store: Store;
|
store: Store;
|
||||||
@@ -53,8 +53,7 @@ type MockFlipperCallbackArgs = {
|
|||||||
|
|
||||||
export async function createMockFlipperWithPlugin(
|
export async function createMockFlipperWithPlugin(
|
||||||
pluginClazz: typeof FlipperPlugin,
|
pluginClazz: typeof FlipperPlugin,
|
||||||
callback: (args: MockFlipperCallbackArgs) => Promise<void>,
|
): Promise<MockFlipperResult> {
|
||||||
) {
|
|
||||||
const store = createStore(reducers);
|
const store = createStore(reducers);
|
||||||
const logger = createStubLogger();
|
const logger = createStubLogger();
|
||||||
store.dispatch(registerPlugins([pluginClazz]));
|
store.dispatch(registerPlugins([pluginClazz]));
|
||||||
@@ -134,7 +133,7 @@ export async function createMockFlipperWithPlugin(
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
await callback({
|
return {
|
||||||
client,
|
client,
|
||||||
device: device as any,
|
device: device as any,
|
||||||
store,
|
store,
|
||||||
@@ -153,21 +152,21 @@ export async function createMockFlipperWithPlugin(
|
|||||||
createDevice,
|
createDevice,
|
||||||
createClient,
|
createClient,
|
||||||
logger,
|
logger,
|
||||||
});
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
type Renderer = RenderResult<typeof import('testing-library__dom/queries')>;
|
type Renderer = RenderResult<typeof import('testing-library__dom/queries')>;
|
||||||
|
|
||||||
export async function renderMockFlipperWithPlugin(
|
export async function renderMockFlipperWithPlugin(
|
||||||
pluginClazz: typeof FlipperPlugin,
|
pluginClazz: typeof FlipperPlugin,
|
||||||
callback: (
|
): Promise<
|
||||||
args: MockFlipperCallbackArgs & {
|
MockFlipperResult & {
|
||||||
renderer: Renderer;
|
renderer: Renderer;
|
||||||
act: (cb: () => void) => void;
|
act: (cb: () => void) => void;
|
||||||
},
|
}
|
||||||
) => Promise<void>,
|
> {
|
||||||
) {
|
const args = await createMockFlipperWithPlugin(pluginClazz);
|
||||||
return createMockFlipperWithPlugin(pluginClazz, async (args) => {
|
|
||||||
function selectTestPlugin(store: Store, client: Client) {
|
function selectTestPlugin(store: Store, client: Client) {
|
||||||
store.dispatch(
|
store.dispatch(
|
||||||
selectPlugin({
|
selectPlugin({
|
||||||
@@ -187,15 +186,12 @@ export async function renderMockFlipperWithPlugin(
|
|||||||
</Provider>,
|
</Provider>,
|
||||||
);
|
);
|
||||||
|
|
||||||
await callback({
|
return {
|
||||||
...args,
|
...args,
|
||||||
renderer: renderer as any,
|
renderer: renderer as any,
|
||||||
act(cb) {
|
act(cb) {
|
||||||
testingLibAct(cb);
|
testingLibAct(cb);
|
||||||
args.client.flushMessageBuffer();
|
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 () => {
|
test('queue - events are processed immediately if plugin is selected', async () => {
|
||||||
await createMockFlipperWithPlugin(
|
const {store, client, sendMessage} = await createMockFlipperWithPlugin(
|
||||||
TestPlugin,
|
TestPlugin,
|
||||||
async ({store, client, sendMessage}) => {
|
);
|
||||||
expect(store.getState().connections.selectedPlugin).toBe('TestPlugin');
|
expect(store.getState().connections.selectedPlugin).toBe('TestPlugin');
|
||||||
sendMessage('noop', {});
|
sendMessage('noop', {});
|
||||||
sendMessage('noop', {});
|
sendMessage('noop', {});
|
||||||
@@ -106,18 +106,17 @@ test('queue - events are processed immediately if plugin is selected', async ()
|
|||||||
expect(store.getState().pluginMessageQueue).toMatchInlineSnapshot(
|
expect(store.getState().pluginMessageQueue).toMatchInlineSnapshot(
|
||||||
`Object {}`,
|
`Object {}`,
|
||||||
);
|
);
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('queue - events are NOT processed immediately if plugin is NOT selected (but enabled)', async () => {
|
test('queue - events are NOT processed immediately if plugin is NOT selected (but enabled)', async () => {
|
||||||
await createMockFlipperWithPlugin(
|
const {
|
||||||
TestPlugin,
|
store,
|
||||||
async ({client, device, store, sendMessage}) => {
|
client,
|
||||||
|
sendMessage,
|
||||||
|
device,
|
||||||
|
} = await createMockFlipperWithPlugin(TestPlugin);
|
||||||
selectDeviceLogs(store);
|
selectDeviceLogs(store);
|
||||||
expect(store.getState().connections.selectedPlugin).not.toBe(
|
expect(store.getState().connections.selectedPlugin).not.toBe('TestPlugin');
|
||||||
'TestPlugin',
|
|
||||||
);
|
|
||||||
|
|
||||||
sendMessage('inc', {});
|
sendMessage('inc', {});
|
||||||
sendMessage('inc', {delta: 2});
|
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({
|
expect(store.getState().pluginMessageQueue).toEqual({
|
||||||
[pluginKey]: [{api: 'TestPlugin', method: 'inc', params: {delta: 5}}],
|
[pluginKey]: [{api: 'TestPlugin', method: 'inc', params: {delta: 5}}],
|
||||||
});
|
});
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('queue - events are queued for plugins that are favorite when app is not selected', async () => {
|
test('queue - events are queued for plugins that are favorite when app is not selected', async () => {
|
||||||
await createMockFlipperWithPlugin(
|
const {
|
||||||
TestPlugin,
|
device,
|
||||||
async ({device, store, sendMessage, createClient}) => {
|
store,
|
||||||
|
sendMessage,
|
||||||
|
createClient,
|
||||||
|
} = await createMockFlipperWithPlugin(TestPlugin);
|
||||||
selectDeviceLogs(store);
|
selectDeviceLogs(store);
|
||||||
expect(store.getState().connections.selectedPlugin).not.toBe(
|
expect(store.getState().connections.selectedPlugin).not.toBe('TestPlugin');
|
||||||
'TestPlugin',
|
|
||||||
);
|
|
||||||
|
|
||||||
const client2 = createClient(device, 'TestApp2');
|
const client2 = createClient(device, 'TestApp2');
|
||||||
store.dispatch(selectClient(client2.id));
|
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 () => {
|
test('queue - events are queued for plugins that are favorite when app is selected on different device', async () => {
|
||||||
await createMockFlipperWithPlugin(
|
const {
|
||||||
TestPlugin,
|
client,
|
||||||
async ({client, store, sendMessage, createDevice, createClient}) => {
|
store,
|
||||||
|
sendMessage,
|
||||||
|
createDevice,
|
||||||
|
createClient,
|
||||||
|
} = await createMockFlipperWithPlugin(TestPlugin);
|
||||||
selectDeviceLogs(store);
|
selectDeviceLogs(store);
|
||||||
expect(store.getState().connections.selectedPlugin).not.toBe(
|
expect(store.getState().connections.selectedPlugin).not.toBe('TestPlugin');
|
||||||
'TestPlugin',
|
|
||||||
);
|
|
||||||
|
|
||||||
const device2 = createDevice('serial2');
|
const device2 = createDevice('serial2');
|
||||||
const client2 = createClient(device2, client.query.app); // same app id
|
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 () => {
|
test('queue - events processing will be paused', async () => {
|
||||||
await createMockFlipperWithPlugin(
|
const {
|
||||||
TestPlugin,
|
client,
|
||||||
async ({client, device, store, sendMessage}) => {
|
device,
|
||||||
|
store,
|
||||||
|
sendMessage,
|
||||||
|
} = await createMockFlipperWithPlugin(TestPlugin);
|
||||||
selectDeviceLogs(store);
|
selectDeviceLogs(store);
|
||||||
|
|
||||||
sendMessage('inc', {});
|
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
|
// controlled idler will signal and and off that idling is needed
|
||||||
const idler = new TestIdler();
|
const idler = new TestIdler();
|
||||||
|
|
||||||
const p = processMessageQueue(
|
const p = processMessageQueue(TestPlugin, pluginKey, store, undefined, idler);
|
||||||
TestPlugin,
|
|
||||||
pluginKey,
|
|
||||||
store,
|
|
||||||
undefined,
|
|
||||||
idler,
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(store.getState().pluginStates).toEqual({
|
expect(store.getState().pluginStates).toEqual({
|
||||||
[pluginKey]: {
|
[pluginKey]: {
|
||||||
@@ -324,14 +317,15 @@ test('queue - events processing will be paused', async () => {
|
|||||||
// don't idle anymore
|
// don't idle anymore
|
||||||
idler.run();
|
idler.run();
|
||||||
await p;
|
await p;
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('queue - messages that arrive during processing will be queued', async () => {
|
test('queue - messages that arrive during processing will be queued', async () => {
|
||||||
await createMockFlipperWithPlugin(
|
const {
|
||||||
TestPlugin,
|
client,
|
||||||
async ({client, device, store, sendMessage}) => {
|
device,
|
||||||
|
store,
|
||||||
|
sendMessage,
|
||||||
|
} = await createMockFlipperWithPlugin(TestPlugin);
|
||||||
selectDeviceLogs(store);
|
selectDeviceLogs(store);
|
||||||
|
|
||||||
sendMessage('inc', {});
|
sendMessage('inc', {});
|
||||||
@@ -344,13 +338,7 @@ test('queue - messages that arrive during processing will be queued', async () =
|
|||||||
|
|
||||||
const idler = new TestIdler();
|
const idler = new TestIdler();
|
||||||
|
|
||||||
const p = processMessageQueue(
|
const p = processMessageQueue(TestPlugin, pluginKey, store, undefined, idler);
|
||||||
TestPlugin,
|
|
||||||
pluginKey,
|
|
||||||
store,
|
|
||||||
undefined,
|
|
||||||
idler,
|
|
||||||
);
|
|
||||||
|
|
||||||
// first message is consumed
|
// first message is consumed
|
||||||
expect(store.getState().pluginMessageQueue[pluginKey].length).toBe(1);
|
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();
|
idler.run();
|
||||||
await p;
|
await p;
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('queue - processing can be cancelled', async () => {
|
test('queue - processing can be cancelled', async () => {
|
||||||
await createMockFlipperWithPlugin(
|
const {
|
||||||
TestPlugin,
|
client,
|
||||||
async ({client, device, store, sendMessage}) => {
|
device,
|
||||||
|
store,
|
||||||
|
sendMessage,
|
||||||
|
} = await createMockFlipperWithPlugin(TestPlugin);
|
||||||
selectDeviceLogs(store);
|
selectDeviceLogs(store);
|
||||||
|
|
||||||
sendMessage('inc', {});
|
sendMessage('inc', {});
|
||||||
@@ -401,13 +390,7 @@ test('queue - processing can be cancelled', async () => {
|
|||||||
|
|
||||||
const idler = new TestIdler();
|
const idler = new TestIdler();
|
||||||
|
|
||||||
const p = processMessageQueue(
|
const p = processMessageQueue(TestPlugin, pluginKey, store, undefined, idler);
|
||||||
TestPlugin,
|
|
||||||
pluginKey,
|
|
||||||
store,
|
|
||||||
undefined,
|
|
||||||
idler,
|
|
||||||
);
|
|
||||||
|
|
||||||
// first message is consumed
|
// first message is consumed
|
||||||
await idler.next();
|
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().pluginMessageQueue[pluginKey].length).toBe(1);
|
||||||
expect(store.getState().pluginStates[pluginKey].count).toBe(10);
|
expect(store.getState().pluginStates[pluginKey].count).toBe(10);
|
||||||
await p;
|
await p;
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('queue - make sure resetting plugin state clears the message queue', async () => {
|
test('queue - make sure resetting plugin state clears the message queue', async () => {
|
||||||
await createMockFlipperWithPlugin(
|
const {
|
||||||
TestPlugin,
|
client,
|
||||||
async ({client, device, store, sendMessage}) => {
|
device,
|
||||||
|
store,
|
||||||
|
sendMessage,
|
||||||
|
} = await createMockFlipperWithPlugin(TestPlugin);
|
||||||
selectDeviceLogs(store);
|
selectDeviceLogs(store);
|
||||||
|
|
||||||
sendMessage('inc', {});
|
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);
|
expect(store.getState().pluginMessageQueue[pluginKey]).toBe(undefined);
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('queue will be cleaned up when it exceeds maximum size', () => {
|
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();
|
static persistedStateReducer = jest.fn();
|
||||||
}
|
}
|
||||||
|
|
||||||
await createMockFlipperWithPlugin(
|
const {
|
||||||
TestPlugin,
|
client,
|
||||||
async ({client, store, device, sendMessage}) => {
|
store,
|
||||||
|
device,
|
||||||
|
sendMessage,
|
||||||
|
} = await createMockFlipperWithPlugin(TestPlugin);
|
||||||
selectDeviceLogs(store);
|
selectDeviceLogs(store);
|
||||||
|
|
||||||
store.dispatch(registerPlugins([StubDeviceLogs]));
|
store.dispatch(registerPlugins([StubDeviceLogs]));
|
||||||
@@ -601,9 +586,9 @@ test('client - incoming messages are buffered and flushed together', async () =>
|
|||||||
}
|
}
|
||||||
`);
|
`);
|
||||||
expect(client.messageBuffer).toMatchInlineSnapshot(`Object {}`);
|
expect(client.messageBuffer).toMatchInlineSnapshot(`Object {}`);
|
||||||
expect(
|
expect(StubDeviceLogs.persistedStateReducer.mock.calls).toMatchInlineSnapshot(
|
||||||
StubDeviceLogs.persistedStateReducer.mock.calls,
|
`Array []`,
|
||||||
).toMatchInlineSnapshot(`Array []`);
|
);
|
||||||
|
|
||||||
// tigger processing the queue
|
// tigger processing the queue
|
||||||
const pluginKey = getPluginKey(client.id, device, StubDeviceLogs.id);
|
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