Skip to content

Commit bd72ebb

Browse files
Merge pull request #16 from oslabs-beta/testing
Testing
2 parents 080bce5 + bb60f71 commit bd72ebb

File tree

13 files changed

+2845
-1205
lines changed

13 files changed

+2845
-1205
lines changed

demo-app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"@types/express": "^4.17.13",
1515
"@types/node": "^17.0.8",
1616
"@types/react": "^17.0.38",
17-
"@types/react-dom": "^17.0.11",
17+
"@types/react-dom": "^17.0.19",
1818
"babel-loader": "^8.2.3",
1919
"copy-webpack-plugin": "^10.2.0",
2020
"css-loader": "^6.5.1",

demo-app/src/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module 'react-dom/client' {
2+
const ReactDOMClient: { createRoot: any };
3+
export = ReactDOMClient;
4+
}

demo-nextjs

Lines changed: 0 additions & 1 deletion
This file was deleted.

package.json

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"buildlegacy": "webpack --mode production",
2525
"devlegacy": "webpack --mode development --watch",
2626
"test": "jest --verbose --coverage",
27+
"test-backend": "jest --verbose --coverage src/backend",
2728
"test-on": "./node_modules/.bin/jest $1",
2829
"docker-test-lint": "eslint --ext .js --ext .jsx src",
2930
"docs": "typedoc --json docs --inputFiles src/app --inputFiles src/backend --readme docs/readme.md",
@@ -109,10 +110,11 @@
109110
"@types/chrome": "^0.0.119",
110111
"@types/d3": "^7.4.0",
111112
"@types/d3-scale-chromatic": "^2.0.0",
112-
"@types/jest": "^26.0.4",
113+
"@types/jest": "^29.5.0",
113114
"@types/lodash.isequal": "^4.5.5",
114115
"@types/node": "^12.19.6",
115116
"@types/react": "^17.0.43",
117+
"@types/react-router": "^5.1.20",
116118
"@typescript-eslint/eslint-plugin": "^3.6.1",
117119
"@typescript-eslint/parser": "^3.6.1",
118120
"babel-loader": "^8.1.0",
@@ -121,31 +123,31 @@
121123
"css-loader": "^3.6.0",
122124
"enzyme": "^3.11.0",
123125
"enzyme-adapter-react-16": "^1.15.6",
124-
"eslint": "^6.8.0",
126+
"eslint": "^8.36.0",
125127
"eslint-config-airbnb": "^18.2.0",
126128
"eslint-plugin-import": "^2.22.0",
127129
"eslint-plugin-jest": "^22.21.0",
128130
"eslint-plugin-jsx-a11y": "^6.3.1",
129131
"eslint-plugin-react": "^7.20.3",
130132
"eslint-plugin-react-hooks": "^1.7.0",
131133
"express": "^4.17.1",
132-
"jest": "^26.1.0",
134+
"jest": "^29.5.0",
133135
"jest-cli": "^26.1.0",
134136
"jest-diff": "^26.1.0",
135-
"jest-runner-eslint": "^0.7.7",
137+
"jest-runner-eslint": "^2.0.0",
136138
"jscharting-react": "^1.2.1",
137139
"prettier": "2.8.4",
138140
"puppeteer": "^14.3.0",
139141
"sass": "^1.26.10",
140142
"sass-loader": "^7.3.1",
141143
"sinon-chrome": "^3.0.1",
142144
"style-loader": "^0.23.1",
143-
"ts-jest": "^26.1.2",
144-
"ts-loader": "^7.0.5",
145+
"ts-jest": "^29.0.5",
146+
"ts-loader": "^8.0.0",
145147
"ts-node": "^9.1.1",
146-
"typedoc": "^0.17.8",
148+
"typedoc": "^0.5.0",
147149
"typedoc-webpack-plugin": "^1.1.4",
148-
"typescript": "^3.9.6",
150+
"typescript": "^4.9.0",
149151
"webpack": "^4.43.0",
150152
"webpack-chrome-extension-reloader": "^1.3.0",
151153
"webpack-cli": "^3.3.12"
@@ -158,8 +160,7 @@
158160
"@fortawesome/free-regular-svg-icons": "^5.15.1",
159161
"@fortawesome/free-solid-svg-icons": "^5.15.1",
160162
"@fortawesome/react-fontawesome": "^0.1.12",
161-
"@material-ui/core": "^4.11.2",
162-
"@mui/material": "^5.8.2",
163+
"@material-ui/core": "^4.12.4",
163164
"@types/react-dom": "^17.0.14",
164165
"@types/react-router-dom": "^5.3.3",
165166
"@visx/axis": "^1.0.0",
@@ -169,13 +170,13 @@
169170
"@visx/glyph": "^1.0.0",
170171
"@visx/gradient": "^1.0.0",
171172
"@visx/grid": "^1.0.0",
172-
"@visx/group": "^1.0.0",
173+
"@visx/group": "^3.0.0",
173174
"@visx/hierarchy": "^1.0.0",
174175
"@visx/legend": "^1.0.0",
175176
"@visx/responsive": "^1.0.0",
176177
"@visx/scale": "^1.0.0",
177-
"@visx/shape": "^1.0.0",
178-
"@visx/text": "^1.0.0",
178+
"@visx/shape": "^3.0.0",
179+
"@visx/text": "^3.0.0",
179180
"@visx/tooltip": "^1.0.0",
180181
"@visx/zoom": "^1.0.0",
181182
"acorn": "^7.3.1",
@@ -191,14 +192,15 @@
191192
"intro.js-react": "^0.6.0",
192193
"jest-runner": "^26.1.0",
193194
"jscharting": "^3.0.2",
195+
"jsdom": "^21.1.1",
194196
"jsondiffpatch": "^0.3.11",
195197
"lodash": "^4.17.21",
196198
"prop-types": "^15.7.2",
197199
"rc-slider": "^8.7.1",
198200
"rc-tooltip": "^3.7.3",
199-
"react": "^16.14.0",
201+
"react": "^18.0.0",
200202
"react-apexcharts": "^1.3.7",
201-
"react-dom": "^16.13.1",
203+
"react-dom": "^18.2.0",
202204
"react-hover": "^2.0.0",
203205
"react-html-parser": "^2.0.2",
204206
"react-json-tree": "^0.11.2",

src/backend/__tests__/linkFiber.test.tsx

Lines changed: 78 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,35 @@ import Tree from '../models/tree';
44
import { DevTools } from '../types/linkFiberTypes';
55
import updateAndSendSnapShotTree from '../routers/snapShot';
66
import throttle from '../controllers/throttle';
7+
import createTree from '../controllers/createTree/createTree';
8+
import routes from '../models/routes';
9+
import { JSDOM } from 'jsdom';
710

811
describe('linkFiber', () => {
912
let snapShot: Snapshot;
1013
let mode: Status;
1114
let linkFiber;
1215
let fiberRoot: FiberRoot;
1316
const mockPostMessage = jest.fn();
17+
let dom: JSDOM;
18+
beforeAll(() => {
19+
// Set up a fake DOM environment with JSDOM
20+
dom = new JSDOM('<!DOCTYPE html><html><body></body></html>', { url: 'http://localhost' });
21+
global.window = dom.window;
22+
global.document = dom.window._document;
23+
});
24+
25+
afterAll(() => {
26+
// Clean up the fake DOM environment
27+
global.window = undefined;
28+
global.document = undefined;
29+
dom.window.close();
30+
});
31+
beforeEach(() => {
32+
routes = new Routes();
33+
window.history.replaceState = jest.fn();
34+
window.history.pushState = jest.fn();
35+
});
1436

1537
beforeEach(() => {
1638
// Create snapshot and mode objects
@@ -49,9 +71,7 @@ describe('linkFiber', () => {
4971
renderers: new Map<1, { version: string }>([[1, { version: '16' }]]),
5072
inject: jest.fn(),
5173
supportsFiber: true,
52-
onCommitFiberRoot: jest.fn((...args) => {
53-
console.log(...args);
54-
}),
74+
onCommitFiberRoot: () => console.log('test'),
5575
onCommitFiberUnmount: jest.fn(),
5676
rendererInterfaces: {},
5777
getFiberRoots: jest.fn(() => [{ current: { tag: 3 } }]),
@@ -74,7 +94,13 @@ describe('linkFiber', () => {
7494
});
7595

7696
describe('React dev tools and react app check', () => {
77-
it('should send message to front end that React DevTools is installed', () => {
97+
it('should not do anything if React Devtools is not installed', () => {
98+
delete window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
99+
expect(() => linkFiber()).not.toThrowError();
100+
expect(mockPostMessage).not.toHaveBeenCalled();
101+
});
102+
103+
it('should post a message to front end that React DevTools is installed', () => {
78104
linkFiber();
79105
expect(mockPostMessage).toHaveBeenCalled();
80106
expect(mockPostMessage).toHaveBeenCalledWith(
@@ -86,15 +112,9 @@ describe('linkFiber', () => {
86112
);
87113
});
88114

89-
it('should not do anything if React Devtools is not installed', () => {
90-
delete window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
91-
expect(() => linkFiber()).not.toThrowError();
92-
expect(mockPostMessage).not.toHaveBeenCalled();
93-
});
94-
95-
it('should send a message to the front end if the target application is a React App', () => {
115+
it('should post a message to the front end if the target application is a React App', () => {
96116
linkFiber();
97-
// the third call is from the onCommitFiberRoot() function
117+
// the third call is from the onCommitFiberRoot() function to send snapshot to front end
98118
expect(mockPostMessage).toHaveBeenCalledTimes(3);
99119
expect(mockPostMessage).toHaveBeenCalledWith(
100120
{
@@ -115,6 +135,7 @@ describe('linkFiber', () => {
115135
it('should not do anything if the target application is not a React App', () => {
116136
(window as any).__REACT_DEVTOOLS_GLOBAL_HOOK__.renderers = new Map();
117137
linkFiber();
138+
expect(mockPostMessage).toHaveBeenCalledTimes(1);
118139
expect(mockPostMessage).not.toHaveBeenCalledTimes(3);
119140
});
120141
});
@@ -127,56 +148,56 @@ describe('linkFiber', () => {
127148
});
128149
});
129150

130-
describe('throttledUpdateSnapshot', () => {
131-
const mockUpdateAndSendSnapShotTree = jest.fn();
132-
133-
beforeEach(() => {
134-
jest.useFakeTimers();
135-
mockUpdateAndSendSnapShotTree.mockClear();
136-
});
137-
138-
afterEach(() => {
139-
jest.runOnlyPendingTimers();
140-
jest.useRealTimers();
141-
});
142-
143-
it('throttled function should be called with the correct arguments', () => {
144-
const throttledUpdateSnapshot = throttle(mockUpdateAndSendSnapShotTree, 1000);
145-
const onCoolDown = true;
146-
const isCallQueued = true;
147-
throttledUpdateSnapshot(onCoolDown, isCallQueued);
148-
149-
expect(mockUpdateAndSendSnapShotTree).toHaveBeenCalledWith(onCoolDown, isCallQueued);
150-
});
151-
152-
it('should call updateAndSendSnapShotTree only once per 100ms', () => {
153-
const throttledUpdateSnapshot = throttle(mockUpdateAndSendSnapShotTree, 100);
154-
throttledUpdateSnapshot();
155-
expect(mockUpdateAndSendSnapShotTree).toHaveBeenCalledTimes(1);
156-
throttledUpdateSnapshot();
157-
expect(mockUpdateAndSendSnapShotTree).toHaveBeenCalledTimes(1);
158-
jest.advanceTimersByTime(100);
159-
expect(mockUpdateAndSendSnapShotTree).toHaveBeenCalledTimes(2);
151+
describe('addOneMoreStep', () => {
152+
it('should monkey patch on onCommitFiberRoot', () => {
153+
const mockOnCommitFiberRoot =
154+
window.__REACT_DEVTOOLS_GLOBAL_HOOK__?.onCommitFiberRoot.toString();
155+
linkFiber();
156+
const onCommitFiberRoot = window.__REACT_DEVTOOLS_GLOBAL_HOOK__?.onCommitFiberRoot.toString();
157+
expect(mockOnCommitFiberRoot).not.toEqual(onCommitFiberRoot);
160158
});
161159

162-
it('should call throttle after visibility change', () => {
163-
const throttledUpdateSnapshot = throttle(mockUpdateAndSendSnapShotTree, 100);
164-
// Simulate visibility change
165-
const visibilityChangeEvent = new Event('visibilitychange');
166-
document.dispatchEvent(visibilityChangeEvent);
167-
expect(throttle).toHaveBeenCalled();
160+
it('should send a snapshot when new fiberRoot is committed', () => {
161+
linkFiber();
162+
const payload = createTree(fiberRoot.current);
163+
payload.route = routes.addRoute(window.location.href);
164+
expect(mockPostMessage).toHaveBeenCalled();
165+
expect(mockPostMessage).toHaveBeenCalledTimes(3);
166+
expect(mockPostMessage).toHaveBeenCalledWith(
167+
{
168+
action: 'recordSnap',
169+
payload,
170+
},
171+
'*',
172+
);
168173
});
169174
});
170175

171-
describe('addOneMoreStep', () => {
172-
it('should add a new step to the current path in the snapshot tree', () => {});
173-
});
174-
175-
describe('onCommitFiberRoot', () => {
176-
it('should call throttledUpdateSnapshot', () => {
177-
linkFiber();
178-
const onCommitFiberRoot = window.__REACT_DEVTOOLS_GLOBAL_HOOK__?.onCommitFiberRoot;
179-
expect(onCommitFiberRoot).toHaveBeenCalled();
176+
describe('mode unit tests', () => {
177+
it('should update react fiber tree based on the payload from frontend when mode is navigating', () => {
178+
const newRoot: FiberRoot = {
179+
current: {
180+
tag: 1,
181+
key: null,
182+
elementType: 'div',
183+
type: 'div',
184+
stateNode: {
185+
state: { count: 2 },
186+
setState: function (newState) {
187+
this.state = newState;
188+
},
189+
},
190+
child: null,
191+
sibling: null,
192+
index: 0,
193+
memoizedState: null,
194+
memoizedProps: {},
195+
dependencies: null,
196+
_debugHookTypes: [],
197+
},
198+
};
199+
const payload = createTree(newRoot.current);
200+
payload.route = routes.addRoute(window.location.href);
180201
});
181202
});
182203
});

src/backend/__tests__/routes.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
1+
import { JSDOM } from 'jsdom';
2+
13
import { Routes, Route } from '../models/routes';
24

35
describe('Route class testing', () => {
46
let routes: Routes;
7+
let dom: JSDOM;
8+
beforeAll(() => {
9+
// Set up a fake DOM environment with JSDOM
10+
dom = new JSDOM('<!DOCTYPE html><html><body></body></html>', { url: 'http://localhost' });
11+
global.window = dom.window;
12+
global.document = dom.window.document;
13+
});
514

15+
afterAll(() => {
16+
// Clean up the fake DOM environment
17+
global.window = undefined;
18+
global.document = undefined;
19+
dom.window.close();
20+
});
621
beforeEach(() => {
722
routes = new Routes();
823
window.history.replaceState = jest.fn();

src/backend/__tests__/throttle.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('throttle unit tests', () => {
1414
});
1515

1616
it('should return a function', () => {
17-
const result = throttle(() => {}, 1000);
17+
const result = throttle(mockCallback, 1000);
1818
expect(typeof result).toBe('function');
1919
});
2020

@@ -56,7 +56,9 @@ describe('throttle unit tests', () => {
5656
expect(mockCallback).toHaveBeenCalledTimes(2);
5757

5858
throttledFunc();
59-
jest.advanceTimersByTime(1500);
59+
jest.advanceTimersByTime(100);
60+
throttledFunc();
61+
jest.advanceTimersByTime(901);
6062
expect(mockCallback).toHaveBeenCalledTimes(3);
6163
});
6264
});

src/backend/__tests__/tree.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ xdescribe('Tree unit test', () => {
7373
});
7474

7575
xdescribe('Adding children', () => {
76-
const returnChild = newTree.addChild('stateful', 'child', {}, null);
76+
const returnChild: Tree = newTree.addChild('stateful', 'child', {}, null);
7777

7878
it("should have the child be in the children's array property", () => {
7979
// check if returnChild is in the children array property of tree that invoked addChild

src/backend/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import 'regenerator-runtime/runtime';
1212
import linkFiberInitialization from './routers/linkFiber';
1313
import timeJumpInitialization from './controllers/timeJump';
1414
import { Snapshot, Status, MsgData } from './types/backendTypes';
15-
import componentActionsRecord from './models/masterState';
1615
import routes from './models/routes';
1716

1817
// * State snapshot object initialized here

0 commit comments

Comments
 (0)