|
1 | 1 | /* eslint-env jest */
|
2 |
| - |
3 | 2 | import React from 'react'
|
4 |
| -import '../lib/patch.dev' |
5 |
| -import RHL from '../lib/reactHotLoader' |
6 |
| - |
7 |
| -RHL.disableComponentProxy = true |
8 |
| - |
9 |
| -function A1() {} |
10 |
| -function A2() {} |
11 |
| -function A3() {} |
12 |
| -function B1() {} |
13 |
| -function B2() {} |
14 |
| - |
15 |
| -function runAllTests(useWeakMap) { |
16 |
| - describe('patch', () => { |
17 |
| - beforeEach(() => { |
18 |
| - RHL.reset(useWeakMap) |
19 |
| - }) |
20 |
| - |
21 |
| - it('is identity for unrecognized types', () => { |
22 |
| - expect(<div />.type).toBe('div') |
23 |
| - expect(<A1 />.type).toBe(A1) |
24 |
| - }) |
25 |
| - |
26 |
| - it('report proxy named duplicates', () => { |
27 |
| - const createUniqueComponent = variable => () => <div>123{variable}</div> |
28 |
| - const f1 = createUniqueComponent(1) |
29 |
| - const f2 = createUniqueComponent(2) |
30 |
| - f2.displayName = 'another' |
31 |
| - |
32 |
| - const spy = jest.spyOn(console, 'warn').mockImplementation(() => {}) |
33 |
| - |
34 |
| - try { |
35 |
| - RHL.register(createUniqueComponent, 'f1', '/wow/test.js') |
36 |
| - React.createElement(f1) |
37 |
| - React.createElement(f1) |
38 |
| - expect(console.warn.mock.calls.length).toBe(0) |
39 |
| - RHL.register(createUniqueComponent, 'f1', '/wow/test.js') |
40 |
| - React.createElement(f2) |
41 |
| - expect(console.warn.mock.calls.length).toBe(0) |
42 |
| - } finally { |
43 |
| - spy.mockRestore() |
44 |
| - } |
45 |
| - }) |
46 |
| - |
47 |
| - it('resolves registered types by their last ID', () => { |
48 |
| - RHL.register(A1, 'a', 'test.js') |
49 |
| - const A = <A1 />.type |
50 |
| - expect(A).not.toBe(A1) |
51 |
| - expect(A).toBeInstanceOf(Function) |
52 |
| - expect(<A />.type).toBe(A) |
53 |
| - |
54 |
| - RHL.register(A2, 'a', 'test.js') |
55 |
| - expect(<A1 />.type).toBe(A) |
56 |
| - expect(<A2 />.type).toBe(A) |
57 |
| - expect(<A />.type).toBe(A) |
| 3 | +import reactHotLoader from '../src/reactHotLoader' |
| 4 | +import patchExport from '../src/patch.dev' |
58 | 5 |
|
59 |
| - RHL.register(A3, 'a', 'test.js') |
60 |
| - expect(<A1 />.type).toBe(A) |
61 |
| - expect(<A2 />.type).toBe(A) |
62 |
| - expect(<A3 />.type).toBe(A) |
63 |
| - expect(<A />.type).toBe(A) |
64 |
| - |
65 |
| - RHL.register(B1, 'b', 'test.js') |
66 |
| - const B = <B1 />.type |
67 |
| - expect(<A1 />.type).toBe(A) |
68 |
| - expect(<A2 />.type).toBe(A) |
69 |
| - expect(<A3 />.type).toBe(A) |
70 |
| - expect(<A />.type).toBe(A) |
71 |
| - expect(<B1 />.type).toBe(B) |
72 |
| - expect(<B />.type).toBe(B) |
73 |
| - |
74 |
| - RHL.register(B2, 'b', 'test.js') |
75 |
| - expect(<A1 />.type).toBe(A) |
76 |
| - expect(<A2 />.type).toBe(A) |
77 |
| - expect(<A3 />.type).toBe(A) |
78 |
| - expect(<A />.type).toBe(A) |
79 |
| - expect(<B1 />.type).toBe(B) |
80 |
| - expect(<B2 />.type).toBe(B) |
81 |
| - expect(<B />.type).toBe(B) |
82 |
| - }) |
83 |
| - |
84 |
| - it('works with reexported types', () => { |
85 |
| - RHL.register(A1, 'a', 'test.js') |
86 |
| - RHL.register(A1, 'x', 'test2.js') |
87 |
| - |
88 |
| - const A = <A1 />.type |
89 |
| - expect(A.type).not.toBe(A1) |
90 |
| - expect(A).toBeInstanceOf(Function) |
91 |
| - expect(<A />.type).toBe(A) |
92 |
| - |
93 |
| - RHL.register(A2, 'a', 'test.js') |
94 |
| - RHL.register(A2, 'x', 'test2.js') |
95 |
| - expect(<A1 />.type).toBe(A) |
96 |
| - expect(<A2 />.type).toBe(A) |
97 |
| - expect(<A />.type).toBe(A) |
98 |
| - }) |
99 |
| - |
100 |
| - it('passes props through', () => { |
101 |
| - expect(<div x={42} y="lol" />.props).toEqual({ |
102 |
| - x: 42, |
103 |
| - y: 'lol', |
104 |
| - }) |
105 |
| - expect(<A1 x={42} y="lol" />.props).toEqual({ |
106 |
| - x: 42, |
107 |
| - y: 'lol', |
108 |
| - }) |
109 |
| - |
110 |
| - RHL.register(B1, 'b', 'test.js') |
111 |
| - expect(<B1 x={42} y="lol" />.props).toEqual({ |
112 |
| - x: 42, |
113 |
| - y: 'lol', |
114 |
| - }) |
115 |
| - RHL.register(B2, 'b', 'test.js') |
116 |
| - expect(<B2 x={42} y="lol" />.props).toEqual({ |
117 |
| - x: 42, |
118 |
| - y: 'lol', |
119 |
| - }) |
120 |
| - }) |
121 |
| - |
122 |
| - it('passes children through', () => { |
123 |
| - expect( |
124 |
| - ( |
125 |
| - <div> |
126 |
| - {'Hi'} |
127 |
| - {'Bye'} |
128 |
| - </div> |
129 |
| - ).props.children, |
130 |
| - ).toEqual(['Hi', 'Bye']) |
131 |
| - expect( |
132 |
| - ( |
133 |
| - <A1> |
134 |
| - {'Hi'} |
135 |
| - {'Bye'} |
136 |
| - </A1> |
137 |
| - ).props.children, |
138 |
| - ).toEqual(['Hi', 'Bye']) |
139 |
| - |
140 |
| - RHL.register(B1, 'b', 'test.js') |
141 |
| - expect( |
142 |
| - ( |
143 |
| - <B1> |
144 |
| - {'Hi'} |
145 |
| - {'Bye'} |
146 |
| - </B1> |
147 |
| - ).props.children, |
148 |
| - ).toEqual(['Hi', 'Bye']) |
149 |
| - RHL.register(B2, 'b', 'test.js') |
150 |
| - expect( |
151 |
| - ( |
152 |
| - <B2> |
153 |
| - {'Hi'} |
154 |
| - {'Bye'} |
155 |
| - </B2> |
156 |
| - ).props.children, |
157 |
| - ).toEqual(['Hi', 'Bye']) |
158 |
| - }) |
| 6 | +describe('patch', () => { |
| 7 | + it('should export reactHotLoader', () => { |
| 8 | + expect(patchExport).toBe(reactHotLoader) |
159 | 9 | })
|
160 |
| -} |
161 | 10 |
|
162 |
| -runAllTests(true) |
163 |
| -runAllTests(false) |
| 11 | + it('should patch React methods', () => { |
| 12 | + expect(React.createElement.isPatchedByReactHotLoader).toBe(true) |
| 13 | + expect(React.createFactory.isPatchedByReactHotLoader).toBe(true) |
| 14 | + expect(React.Children.only.isPatchedByReactHotLoader).toBe(true) |
| 15 | + }) |
| 16 | +}) |
0 commit comments