Skip to content

Commit 590a81b

Browse files
committed
added tests for linkFiber and index
1 parent 952fe1d commit 590a81b

File tree

5 files changed

+127
-40
lines changed

5 files changed

+127
-40
lines changed

package/__tests__/index.test.js

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,69 @@
1-
import { shallow } from 'enzyme';
2-
31
const index = require('../index');
42

53
jest.mock('../timeJump');
64

5+
describe('unit testing for index.js', () => {
6+
test('index.js should be exporting the linkFiber, timeJump, getTree methods', () => {
7+
expect(typeof index.linkFiber).toBe('function');
8+
expect(typeof index.timeJump).toBe('function');
9+
expect(typeof index.getTree).toBe('function');
10+
});
711

8-
// describe('unit testing for index.js', () => {
9-
// test('index.js should be exporting the linkState and timeJump methods', () => {
10-
// expect(typeof index.linkState).toBe('function');
11-
// expect(typeof index.timeJump).toBe('function');
12-
// });
13-
14-
// test('index.js should be listening to the window for the jumpToSnap message', (done) => {
15-
// const calls = 10;
16-
// let count = 0;
17-
// global.addEventListener('message', ({ data: { action } }) => {
18-
// if (action === 'jumpToSnap') {
19-
// count += 1;
20-
// // timeJump should be called everytime a message is received
21-
// expect(index.timeJump.mock.calls.length).toBe(count);
22-
23-
// // test is done once all messages have been received
24-
// if (count === calls) done();
25-
// }
26-
// });
27-
28-
// // iterate ${calls} amount of times
29-
// [...Array(calls).keys()].forEach(() => global.postMessage({ action: 'jumpToSnap', payload: ['test'] }, '*'));
30-
// });
12+
test('index.js should be listening to the window for the jumpToSnap message', (done) => {
13+
const calls = 10;
14+
let count = 0;
15+
global.addEventListener('message', ({ data: { action } }) => {
16+
switch (action) {
17+
case 'jumpToSnap':
18+
count += 1;
19+
expect(index.timeJump.mock.calls.length).toBe(count);
20+
if (count === calls) done();
21+
break;
22+
default:
23+
break;
24+
}
25+
});
26+
[...Array(calls).keys()].forEach(() => global.postMessage({ action: 'jumpToSnap', payload: ['test'] }, '*'));
27+
});
28+
test('setLock message should change the mode object to the given payload', (done) => {
29+
const mode = { paused: false };
30+
const action = 'setLock';
31+
const payloads = [true, false, true, false, true, false, false];
32+
let count = 0;
33+
const calls = payloads.length;
34+
global.addEventListener('message', ({ data: { action: actionReceived, payload } }) => {
35+
switch (actionReceived) {
36+
case 'setLock':
37+
mode.locked = payload;
38+
expect(mode.locked).toBe(payloads[count]);
39+
count += 1;
40+
if (calls === count) done();
41+
break;
42+
default:
43+
break;
44+
}
45+
});
46+
payloads.forEach(payload => global.postMessage({ action, payload }, '*'));
47+
});
3148

32-
// });
49+
test('setPause message should change the mode object to the given payload', (done) => {
50+
const mode = { paused: false };
51+
const action = 'setPause';
52+
const payloads = [true, false, true, false, true, false, false];
53+
let count = 0;
54+
const calls = payloads.length;
55+
global.addEventListener('message', ({ data: { action: actionReceived, payload } }) => {
56+
switch (actionReceived) {
57+
case 'setPause':
58+
mode.locked = payload;
59+
expect(mode.locked).toBe(payloads[count]);
60+
count += 1;
61+
if (calls === count) done();
62+
break;
63+
default:
64+
break;
65+
}
66+
});
67+
payloads.forEach(payload => global.postMessage({ action, payload }, '*'));
68+
});
69+
});

package/__tests__/linkFiber.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// const Tree = require('./tree');
2+
let linkFiber;
3+
let ReactDOM;
4+
let React;
5+
let mode;
6+
let snapShot;
7+
8+
describe('unit test for linkFiber', () => {
9+
beforeEach(() => {
10+
snapShot = { tree: null };
11+
mode = {
12+
jumping: false,
13+
paused: false,
14+
locked: false,
15+
};
16+
React = require('react');
17+
ReactDOM = require('react-dom');
18+
linkFiber = require('../linkFiber')(snapShot, mode);
19+
20+
class App extends React.Component {
21+
constructor(props) {
22+
super(props);
23+
this.state = { foo: 'bar' };
24+
}
25+
26+
render() {
27+
return <div>{this.state.foo}</div>;
28+
}
29+
}
30+
31+
const container = document.createElement('div');
32+
ReactDOM.render(<App />, container);
33+
linkFiber(container);
34+
});
35+
36+
test('linkFiber should mutate the snapshot tree property', () => {
37+
// linkFiber mutates the snapshot
38+
expect(typeof snapShot.tree).toBe('object');
39+
expect(snapShot.tree.component.state).toBe('root');
40+
expect(snapShot.tree.children).toHaveLength(1);
41+
expect(snapShot.tree.children[0].component.state.foo).toBe('bar');
42+
});
43+
44+
test('linkFiber should modify the setState of the stateful component', () => {
45+
console.log(snapShot.tree.children[0].component.setState);
46+
// snapShot.tree.children[0].component.setState({ foo: 'josh' });
47+
// expect(snapShot.tree.children[0].component.setState).toBeInstanceOf(newSetState);
48+
// expect(snapShot.tree.children[0].component.state.foo).toBe('josh');
49+
});
50+
});

package/__tests__/timeJump.test.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import TestRenderer from 'react-test-renderer';
21
const timeJumpExport = require('../timeJump');
3-
import { shallow } from 'enzyme';
42

53
describe('unit testing for timeJump.js', () => {
64
let timeJump;
@@ -15,14 +13,10 @@ describe('unit testing for timeJump.js', () => {
1513
mockFuncs = [];
1614
timeJump = timeJumpExport(snapShot, mode);
1715

18-
const createSetStateAsync = (i) => {
19-
return (state) => {
20-
return new Promise((resolve) => {
21-
mockFuncs[i](state);
22-
resolve();
23-
});
24-
};
25-
};
16+
const createSetStateAsync = i => state => new Promise((resolve) => {
17+
mockFuncs[i](state);
18+
resolve();
19+
});
2620

2721
for (let i = 0; i < count; i += 1) {
2822
mockFuncs.push(jest.fn());
@@ -56,5 +50,4 @@ describe('unit testing for timeJump.js', () => {
5650
// timeJump(newSnapShot);
5751
// mockFuncs.forEach((mockFunc, i) => expect(mockFunc.mock.calls[1][0]).toEqual({ testkey: `testval${i}` }));
5852
// });
59-
6053
});

package/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const snapShot = { tree: null };
22

33
const mode = {
4-
jumping: false, paused: false, locked: false,
4+
jumping: false,
5+
paused: false,
6+
locked: false,
57
};
68

79
const linkFiber = require('./linkFiber')(snapShot, mode);
@@ -21,9 +23,11 @@ window.addEventListener('message', ({ data: { action, payload } }) => {
2123
mode.paused = payload;
2224
break;
2325
default:
26+
break;
2427
}
2528
});
2629

30+
// window.postMessage({ data: { action: 'foo', payload: 'bar' } });
2731
module.exports = {
2832
timeJump,
2933
linkFiber,

package/linkFiber.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = (snap, mode) => {
2222
// make a copy of setState
2323
const oldSetState = component.setState.bind(component);
2424

25-
function newSetState(state, callback = () => { }) {
25+
function newSetState(state, callback = () => {}) {
2626
// dont do anything if state is locked
2727
// UNLESS we are currently jumping through time
2828
if (mode.locked && !mode.jumping) return;
@@ -64,8 +64,11 @@ module.exports = (snap, mode) => {
6464
const { current } = fiberRoot;
6565
snap.tree = createTree(current);
6666
}
67+
6768
return (container) => {
68-
const { _reactRootContainer: { _internalRoot } } = container;
69+
const {
70+
_reactRootContainer: { _internalRoot },
71+
} = container;
6972
fiberRoot = _internalRoot;
7073
updateSnapShotTree();
7174

0 commit comments

Comments
 (0)