From 2cd6be2d0fb16d606211f2cbffe68aa973395c35 Mon Sep 17 00:00:00 2001 From: John Knox Date: Wed, 23 Oct 2019 10:30:47 -0700 Subject: [PATCH] Move tests to internal repo Summary: Some of the tests need to mock behavior of fb-internal dependencies. These fb deps get swapped out with a babel transform and it's become hard to maintain now that we're mocking them as well. For simplicity of mocking, moving them out of the public repo, they'll get run on PRs. Reviewed By: priteshrnandgaonkar Differential Revision: D18086247 fbshipit-source-id: 001e258e00da67a112cb754e851253e5480e578a --- src/__tests__/server.device.tsx | 84 ------- src/__tests__/server.electron.tsx | 68 ----- src/chrome/__tests__/PluginInstaller.node.tsx | 59 ----- .../PluginInstaller.node.tsx.snap | 238 ------------------ 4 files changed, 449 deletions(-) delete mode 100644 src/__tests__/server.device.tsx delete mode 100644 src/__tests__/server.electron.tsx delete mode 100644 src/chrome/__tests__/PluginInstaller.node.tsx delete mode 100644 src/chrome/__tests__/__snapshots__/PluginInstaller.node.tsx.snap diff --git a/src/__tests__/server.device.tsx b/src/__tests__/server.device.tsx deleted file mode 100644 index 5ae7d73a1..000000000 --- a/src/__tests__/server.device.tsx +++ /dev/null @@ -1,84 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - */ - -jest.mock('../fb-stubs/Logger'); -try { - jest.mock('../fb/Logger'); -} catch { - // Allowed to fail when fb modules are not present. -} - -import Server from '../server'; -import {init as initLogger} from '../fb-stubs/Logger'; -import reducers, {Store} from '../reducers/index'; -import {createStore} from 'redux'; -import path from 'path'; -import os from 'os'; -import fs from 'fs'; -import androidDevice from '../dispatcher/androidDevice'; -import iosDevice from '../dispatcher/iOSDevice'; -import Client from '../Client'; -import {BaseDevice} from 'flipper'; - -let server: Server; -let androidCleanup: () => Promise; -const store: Store = createStore(reducers); - -beforeAll(() => { - // create config directory, which is usually created by static/index.js - const flipperDir = path.join(os.homedir(), '.flipper'); - if (!fs.existsSync(flipperDir)) { - fs.mkdirSync(flipperDir); - } - - const logger = initLogger(store); - - androidCleanup = androidDevice(store, logger); - iosDevice(store, logger); - - server = new Server(logger, store); - return server.init(); -}); - -test('Device can connect successfully', done => { - let testFinished = false; - let disconnectedTooEarly = false; - const registeredClients: Client[] = []; - server.addListener('new-client', (client: Client) => { - // Check there is a connected device that has the same device_id as the new client - const deviceId = client.query.device_id; - expect(deviceId).toBeTruthy(); - const devices = store.getState().connections.devices; - expect(devices.map((device: BaseDevice) => device.serial)).toContain( - deviceId, - ); - - // Make sure it only connects once - registeredClients.push(client); - expect(registeredClients).toHaveLength(1); - - // Make sure client stays connected for some time before passing test - setTimeout(() => { - testFinished = true; - expect(disconnectedTooEarly).toBe(false); - done(); - }, 5000); - }); - server.addListener('removed-client', (_id: string) => { - if (!testFinished) { - disconnectedTooEarly = true; - } - }); -}, 20000); - -afterAll(() => - androidCleanup().then(() => { - server.close(); - }), -); diff --git a/src/__tests__/server.electron.tsx b/src/__tests__/server.electron.tsx deleted file mode 100644 index ddfc0a8d3..000000000 --- a/src/__tests__/server.electron.tsx +++ /dev/null @@ -1,68 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - */ - -jest.mock('../fb/Logger'); -try { - jest.mock('../fb/Logger'); -} catch { - // Allowed to fail when fb modules are not present. -} - -import {init as initLogger} from '../fb-stubs/Logger'; -import Server from '../server'; -import reducers, {Store} from '../reducers/index'; -import configureStore from 'redux-mock-store'; -import path from 'path'; -import os from 'os'; -import fs from 'fs'; - -let server: Server | null = null; -const mockStore: Store = configureStore([])( - reducers(undefined, {type: 'INIT'}), -) as Store; - -beforeAll(() => { - // create config directory, which is usually created by static/index.js - const flipperDir = path.join(os.homedir(), '.flipper'); - if (!fs.existsSync(flipperDir)) { - fs.mkdirSync(flipperDir); - } - - const logger = initLogger(mockStore); - server = new Server(logger, mockStore); -}); - -test('servers starting at ports', done => { - const ports = mockStore.getState().application.serverPorts; - const serversToBeStarted = new Set([ports.secure, ports.insecure]); - - // Resolve promise when we get a listen event for each port - const listenerPromise = new Promise((resolve, reject) => { - server!.addListener('listening', port => { - if (!serversToBeStarted.has(port)) { - throw Error(`unknown server started at port ${port}`); - } else { - serversToBeStarted.delete(port); - } - if (serversToBeStarted.size === 0) { - done(); - resolve(); - } - }); - }); - - // Initialise server after the listeners have been setup - server!.init(); - - return listenerPromise; -}); - -afterAll(() => { - return server!.close(); -}); diff --git a/src/chrome/__tests__/PluginInstaller.node.tsx b/src/chrome/__tests__/PluginInstaller.node.tsx deleted file mode 100644 index 67e9d72ed..000000000 --- a/src/chrome/__tests__/PluginInstaller.node.tsx +++ /dev/null @@ -1,59 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - */ - -jest.mock('../../fb-stubs/Logger'); -try { - jest.mock('../../fb/Logger'); -} catch { - // Allowed to fail when fb modules are not present. -} - -import {default as PluginInstaller, PluginDefinition} from '../PluginInstaller'; - -import React from 'react'; -import {render, waitForElement} from '@testing-library/react'; -import configureStore from 'redux-mock-store'; - -const mockStore = configureStore([])({application: {sessionId: 'mysession'}}); -import {Provider} from 'react-redux'; - -const SEARCH_RESULTS = ({ - hits: [ - {name: 'flipper-plugin-hello', version: '0.1.0', description: 'World?'}, - {name: 'flipper-plugin-world', version: '0.2.0', description: 'Hello?'}, - ], -} as unknown) as algoliasearch.Response; - -// *Very* incomplete mock, but that's all we use. -const indexMock: algoliasearch.Index = ({ - search: jest.fn(), -} as unknown) as algoliasearch.Index; - -beforeEach(() => { - indexMock.search = jest.fn(async () => SEARCH_RESULTS); -}); - -test('load PluginInstaller list', async () => { - const component = ( - - new Map()} - searchIndexFactory={() => indexMock} - // Bit ugly to have this as an effectively test-only option, but - // without, we rely on height information from Electron which we don't - // have, causing no items to be rendered. - autoHeight={true} - /> - - ); - const {container, getByText} = render(component); - await waitForElement(() => getByText('flipper-plugin-hello')); - expect((indexMock.search as jest.Mock).mock.calls.length).toBe(2); - expect(container).toMatchSnapshot(); -}); diff --git a/src/chrome/__tests__/__snapshots__/PluginInstaller.node.tsx.snap b/src/chrome/__tests__/__snapshots__/PluginInstaller.node.tsx.snap deleted file mode 100644 index 4a023b677..000000000 --- a/src/chrome/__tests__/__snapshots__/PluginInstaller.node.tsx.snap +++ /dev/null @@ -1,238 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`load PluginInstaller list 1`] = ` -
-
-
-
- -
-
-
-
-
-
-
-
- Name -
-
-
-
-
-
- Version -
-
-
-
-
-
- Description -
-
-
-
-
-
- -
-
-
-
-
-
-
-
- - flipper-plugin-hello - -
-
- - 0.1.0 - -
-
-
- - World? - -
- -
- -
-
-
-
- Install -
-
-
-
-
- - flipper-plugin-world - -
-
- - 0.2.0 - -
-
-
- - Hello? - -
- -
- -
-
-
-
- Install -
-
-
-
-
-
-
-`;