@samitouri / QOS-React / commits / 81183b1492

Named snapshots. Tests use act() abstraction.

Brian Vaughn committed Apr 11, 2019 at 10:19 UTC 81183b1492bd3bc0f0b98fbb3b5ca9706be0892b
2 files changed +69 -45
src/__tests__/__snapshots__/store-test.js.snap
+39 -26
@@ -1,6 +1,41 @@
1 // Jest Snapshot v1, https://goo.gl/fbAQLP
2
3 -exports[`Store should support mount and update operations 1`] = `
3 +exports[`Store should support mount and update operations for multiple roots: mount 1`] = `
4 +[root]
5 + ▾ <Parent key="A">
6 + <Child key="0">
7 + <Child key="1">
8 + <Child key="2">
9 +[root]
10 + ▾ <Parent key="B">
11 + <Child key="0">
12 + <Child key="1">
13 +`;
14 +
15 +exports[`Store should support mount and update operations for multiple roots: unmount A 1`] = ``;
16 +
17 +exports[`Store should support mount and update operations for multiple roots: unmount B 1`] = `
18 +[root]
19 + ▾ <Parent key="A">
20 + <Child key="0">
21 + <Child key="1">
22 + <Child key="2">
23 + <Child key="3">
24 +`;
25 +
26 +exports[`Store should support mount and update operations for multiple roots: update 1`] = `
27 +[root]
28 + ▾ <Parent key="A">
29 + <Child key="0">
30 + <Child key="1">
31 + <Child key="2">
32 + <Child key="3">
33 +[root]
34 + ▾ <Parent key="B">
35 + <Child key="0">
36 +`;
37 +
38 +exports[`Store should support mount and update operations: mount 1`] = `
39 [root]
40 ▾ <Grandparent>
41 ▾ <Parent>
@@ -15,7 +50,9 @@ exports[`Store should support mount and update operations 1`] = `
50 <Child key="3">
51 `;
52
18 -exports[`Store should support mount and update operations 2`] = `
53 +exports[`Store should support mount and update operations: unmount 1`] = ``;
54 +
55 +exports[`Store should support mount and update operations: update 1`] = `
56 [root]
57 ▾ <Grandparent>
58 ▾ <Parent>
@@ -25,27 +62,3 @@ exports[`Store should support mount and update operations 2`] = `
62 <Child key="0">
63 <Child key="1">
64 `;
28 -
29 -exports[`Store should support mount and update operations for multiple roots 1`] = `
30 -[root]
31 - ▾ <Parent>
32 - <Child key="0">
33 - <Child key="1">
34 - <Child key="2">
35 -[root]
36 - ▾ <Parent>
37 - <Child key="0">
38 - <Child key="1">
39 -`;
40 -
41 -exports[`Store should support mount and update operations for multiple roots 2`] = `
42 -[root]
43 - ▾ <Parent>
44 - <Child key="0">
45 - <Child key="1">
46 - <Child key="2">
47 - <Child key="3">
48 -[root]
49 - ▾ <Parent>
50 - <Child key="0">
51 -`;
src/__tests__/store-test.js
+30 -19
@@ -3,13 +3,23 @@
3 describe('Store', () => {
4 let React;
5 let ReactDOM;
6 + let TestUtils;
7 let store;
8
9 + const act = (callback: Function) => {
10 + TestUtils.act(() => {
11 + callback();
12 + });
13 +
14 + jest.runAllTimers(); // Flush Bridge operations
15 + };
16 +
17 beforeEach(() => {
18 store = global.store;
19
20 React = require('react');
21 ReactDOM = require('react-dom');
22 + TestUtils = require('react-dom/test-utils');
23 });
24
25 it('should support mount and update operations', () => {
@@ -25,17 +35,14 @@ describe('Store', () => {
35
36 const container = document.createElement('div');
37
28 - ReactDOM.render(<Grandparent count={4} />, container);
29 -
30 - jest.runAllTimers(); // Flush Bridge operations
31 -
32 - expect(store).toMatchSnapshot();
38 + act(() => ReactDOM.render(<Grandparent count={4} />, container));
39 + expect(store).toMatchSnapshot('mount');
40
34 - ReactDOM.render(<Grandparent count={2} />, container);
41 + act(() => ReactDOM.render(<Grandparent count={2} />, container));
42 + expect(store).toMatchSnapshot('update');
43
36 - jest.runAllTimers(); // Flush Bridge operations
37 -
38 - expect(store).toMatchSnapshot();
44 + act(() => ReactDOM.unmountComponentAtNode(container));
45 + expect(store).toMatchSnapshot('unmount');
46 });
47
48 it('should support mount and update operations for multiple roots', () => {
@@ -46,18 +53,22 @@ describe('Store', () => {
53 const containerA = document.createElement('div');
54 const containerB = document.createElement('div');
55
49 - ReactDOM.render(<Parent count={3} />, containerA);
50 - ReactDOM.render(<Parent count={2} />, containerB);
51 -
52 - jest.runAllTimers(); // Flush Bridge operations
53 -
54 - expect(store).toMatchSnapshot();
56 + act(() => {
57 + ReactDOM.render(<Parent key="A" count={3} />, containerA);
58 + ReactDOM.render(<Parent key="B" count={2} />, containerB);
59 + });
60 + expect(store).toMatchSnapshot('mount');
61
56 - ReactDOM.render(<Parent count={4} />, containerA);
57 - ReactDOM.render(<Parent count={1} />, containerB);
62 + act(() => {
63 + ReactDOM.render(<Parent key="A" count={4} />, containerA);
64 + ReactDOM.render(<Parent key="B" count={1} />, containerB);
65 + });
66 + expect(store).toMatchSnapshot('update');
67
59 - jest.runAllTimers(); // Flush Bridge operations
68 + act(() => ReactDOM.unmountComponentAtNode(containerB));
69 + expect(store).toMatchSnapshot('unmount B');
70
61 - expect(store).toMatchSnapshot();
71 + act(() => ReactDOM.unmountComponentAtNode(containerA));
72 + expect(store).toMatchSnapshot('unmount A');
73 });
74 });