|
1 | | -/* eslint-disable jest/no-disabled-tests */ |
2 | | -/* eslint-disable react/state-in-constructor */ |
3 | | -/* eslint-disable lines-between-class-members */ |
4 | | -/* eslint-disable @typescript-eslint/no-unused-vars */ |
5 | | -/* eslint-disable @typescript-eslint/no-var-requires */ |
6 | | -/* eslint-disable import/order */ |
7 | | -/* eslint-disable import/no-extraneous-dependencies */ |
8 | | -/* eslint-disable react/jsx-filename-extension */ |
9 | | -import { string } from 'prop-types'; |
10 | | -import React, { useState } from 'react'; |
11 | | -import { render } from 'react-dom'; |
12 | | -import linkFiberStart from '../routers/linkFiber'; |
| 1 | +import linkFiberInitialization from '../routers/linkFiber'; |
| 2 | +import { Snapshot, Status, FiberRoot } from '../types/backendTypes'; |
| 3 | +import Tree from '../models/tree'; |
| 4 | +import { DevTools } from '../types/linkFiberTypes'; |
| 5 | +import updateAndSendSnapShotTree from '../routers/snapShot'; |
| 6 | +import throttle from '../controllers/throttle'; |
| 7 | + |
| 8 | +describe('linkFiber', () => { |
| 9 | + let snapShot: Snapshot; |
| 10 | + let mode: Status; |
| 11 | + let linkFiber; |
| 12 | + const mockPostMessage = jest.fn(); |
13 | 13 |
|
14 | | -const puppeteer = require('puppeteer'); |
15 | | -const SERVER = require('../puppeteerServer'); |
16 | | - |
17 | | -// Apple uses port 5000 for Air Play. |
18 | | -const APP = 'http://localhost:5001'; |
19 | | - |
20 | | -let linkFiber; |
21 | | -let mode; |
22 | | -let snapShot; |
23 | | - |
24 | | -let browser; |
25 | | -let page; |
| 14 | + beforeEach(() => { |
| 15 | + // Create snapshot and mode objects |
| 16 | + snapShot = { |
| 17 | + tree: new Tree('root', 'root'), |
| 18 | + }; |
| 19 | + mode = { |
| 20 | + jumping: false, |
| 21 | + paused: false, |
| 22 | + navigating: undefined, |
| 23 | + }; |
26 | 24 |
|
27 | | -interface fooState { |
28 | | - foo: string; |
29 | | - setFoo?: (string) => void; |
30 | | -} |
31 | | -function App(): JSX.Element { |
32 | | - const [fooState, setFooState] = useState({ |
33 | | - foo: 'bar', |
| 25 | + linkFiber = linkFiberInitialization(snapShot, mode); |
| 26 | + // Set up mock postMessage function |
| 27 | + window.postMessage = mockPostMessage; |
| 28 | + |
| 29 | + // Set up mock React DevTools global hook |
| 30 | + (window as any).__REACT_DEVTOOLS_GLOBAL_HOOK__ = { |
| 31 | + renderers: new Map<1, { version: string }>(), |
| 32 | + inject: jest.fn(), |
| 33 | + supportsFiber: true, |
| 34 | + onCommitFiberRoot: jest.fn(), |
| 35 | + onCommitFiberUnmount: jest.fn(), |
| 36 | + rendererInterfaces: {}, |
| 37 | + }; |
34 | 38 | }); |
35 | | - return <div>{fooState}</div>; |
36 | | -} |
37 | | - |
38 | | -describe('unit test for linkFiber', () => { |
39 | | - beforeAll(async () => { |
40 | | - await SERVER; |
41 | | - const args = puppeteer |
42 | | - .defaultArgs() |
43 | | - .filter((arg) => String(arg).toLowerCase() !== '--disable-extensions'); |
44 | | - browser = await puppeteer.launch({ |
45 | | - args: args.concat([ |
46 | | - '--no-sandbox', |
47 | | - '--disable-setuid-sandbox', |
48 | | - '---extensions-on-chrome-urls', |
49 | | - '--whitelisted-extension-id=fmkadmapgofadopljbjfkapdkoienihi', |
50 | | - '--whitelisted-extension-id=hilpbahfbckghckaiafiiinjkeagmfhn', |
51 | | - '--load-extension=/mnt/d/Libraries/Documents/codeRepos/reactime/src/extension/build', |
52 | | - ]), |
53 | | - devtools: true, |
54 | | - ignoreDefaultArgs: true, |
55 | | - }); |
56 | | - |
57 | | - const c = await puppeteer.connect({ |
58 | | - browserWSEndpoint: browser.wsEndpoint(), |
59 | | - ignoreHTTPSErrors: false, |
60 | | - }); |
61 | 39 |
|
62 | | - page = await browser.newPage(); |
| 40 | + afterEach(() => { |
| 41 | + jest.clearAllMocks(); |
| 42 | + delete window.__REACT_DEVTOOLS_GLOBAL_HOOK__; |
63 | 43 | }); |
64 | 44 |
|
65 | | - afterAll(async () => { |
66 | | - await SERVER.close(); |
67 | | - |
68 | | - await browser.close(); |
| 45 | + it('link fiber should return a function', () => { |
| 46 | + expect(typeof linkFiber).toBe('function'); |
69 | 47 | }); |
70 | 48 |
|
71 | | - beforeEach(() => { |
72 | | - snapShot = { tree: null }; |
73 | | - mode = { |
74 | | - jumping: false, |
75 | | - paused: false, |
76 | | - }; |
77 | | - linkFiber = linkFiberStart(snapShot, mode); |
| 49 | + it('returned function should not throw an error', () => { |
| 50 | + expect(() => linkFiber()).not.toThrowError(); |
| 51 | + }); |
78 | 52 |
|
79 | | - page.waitForFunction( |
80 | | - async (lf) => { |
81 | | - const container = document.createElement('div'); |
82 | | - render(<App />, container); |
83 | | - lf(container); |
| 53 | + it('should send message to front end that React DevTools is installed', () => { |
| 54 | + linkFiber(); |
| 55 | + expect(mockPostMessage).toHaveBeenCalled(); |
| 56 | + expect(mockPostMessage).toHaveBeenCalledWith( |
| 57 | + { |
| 58 | + action: 'devToolsInstalled', |
| 59 | + payload: 'devToolsInstalled', |
84 | 60 | }, |
85 | | - {}, |
86 | | - linkFiber, |
| 61 | + '*', |
87 | 62 | ); |
88 | 63 | }); |
89 | 64 |
|
90 | | - test('type of tree should be an object', () => { |
91 | | - expect(typeof snapShot.tree).toBe('object'); |
| 65 | + it('should not do anything if React Devtools is not installed', () => { |
| 66 | + delete window.__REACT_DEVTOOLS_GLOBAL_HOOK__; |
| 67 | + expect(() => linkFiber()).not.toThrowError(); |
| 68 | + expect(mockPostMessage).not.toHaveBeenCalled(); |
92 | 69 | }); |
93 | 70 |
|
94 | | - test.skip('linkFiber should mutate the snapshot tree property', () => { |
95 | | - expect(snapShot.tree.state).toBe('root'); |
96 | | - expect(snapShot.tree.children).toHaveLength(1); |
97 | | - expect(snapShot.tree.children[0].component.state.foo).toBe('bar'); |
98 | | - }); |
| 71 | + xit('should not do anything if the target application is not a React App', () => {}); |
99 | 72 |
|
100 | | - test.skip('linkFiber should modify the setState of the stateful component', () => { |
101 | | - expect(snapShot.tree.children[0].component.setState.linkFiberChanged).toBe(true); |
102 | | - }); |
103 | | -}); |
| 73 | + xit('should send a message to the front end if the target application is a React App', () => {}); |
104 | 74 |
|
105 | | -SERVER.close(); |
| 75 | + xit('should initiate an event listener for visibility change', () => {}); |
| 76 | +}); |
0 commit comments