/
reactHotLoader.test.js
176 lines (152 loc) · 6.04 KB
/
reactHotLoader.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import React from 'react'
import { mount } from 'enzyme'
import { PROXY_KEY, UNWRAP_PROXY } from '../src/proxy/constants'
import { get as getGeneration } from '../src/global/generation'
import reactHotLoader from '../src/reactHotLoader'
import { internalConfiguration } from '../src/configuration'
describe('reactHotLoader', () => {
let Div
let Span
beforeEach(() => {
Div = () => <div />
Span = () => <span />
reactHotLoader.patch(React)
reactHotLoader.reset()
})
describe('#patch', () => {
let OriginalReactMock
let ReactMock
beforeEach(() => {
OriginalReactMock = {
createElement: jest.fn(),
cloneElement: jest.fn(),
createFactory: jest.fn(),
Children: {
only: jest.fn(x => x),
},
}
ReactMock = { ...OriginalReactMock }
})
it('should patch all methods', () => {
reactHotLoader.patch(ReactMock)
expect(ReactMock.createElement.isPatchedByReactHotLoader).toBe(true)
expect(ReactMock.cloneElement.isPatchedByReactHotLoader).toBe(true)
expect(ReactMock.createFactory.isPatchedByReactHotLoader).toBe(true)
expect(ReactMock.Children.only.isPatchedByReactHotLoader).toBe(true)
})
describe('#createElement', () => {
it('should create a proxy and call original method with it', () => {
reactHotLoader.patch(ReactMock)
ReactMock.createElement(Div, { foo: 'bar' })
const DivProxy = OriginalReactMock.createElement.mock.calls[0][0]
expect(DivProxy[PROXY_KEY]).toBeDefined()
})
})
describe('#createFactory', () => {
it('should create a factory that returns proxy', () => {
reactHotLoader.patch(ReactMock)
const dummyFactory = ReactMock.createFactory(Div)
dummyFactory({ foo: 'bar' })
const DivProxy = OriginalReactMock.createElement.mock.calls[0][0]
expect(DivProxy[PROXY_KEY]).toBeDefined()
})
})
describe('#Children.only', () => {
it('should returns a proxy', () => {
reactHotLoader.patch(ReactMock)
const children = { type: Div, props: { foo: 'bar' } }
const result = ReactMock.Children.only(children)
const DivProxy = result.type
expect(DivProxy[PROXY_KEY]).toBeDefined()
})
})
})
describe('#reset', () => {
it('should reset all proxies', () => {
const proxyElement = React.createElement(Div, { foo: 'bar' })
const secondProxyElement = React.createElement(Div, { foo: 'bar' })
expect(proxyElement.type[PROXY_KEY]).toBe(
secondProxyElement.type[PROXY_KEY],
)
// After that, a new proxy key should be generated
// meaning that a new proxy has been created
reactHotLoader.reset()
const thirdProxyElement = React.createElement(Div, { foo: 'bar' })
expect(proxyElement.type[PROXY_KEY]).not.toBe(
thirdProxyElement.type[PROXY_KEY],
)
})
})
describe('#disableProxyCreation', () => {
afterEach(() => {
internalConfiguration.disableProxyCreation = false
})
it('should disable the creation of proxy', () => {
internalConfiguration.disableProxyCreation = true
const proxyElement = React.createElement(Div, { foo: 'bar' })
expect(proxyElement.type[PROXY_KEY]).not.toBeDefined()
})
it('should still be possible to get existing proxies', () => {
React.createElement(Div, { foo: 'bar' })
internalConfiguration.disableProxyCreation = true
const proxyElement = React.createElement(Div, { foo: 'bar' })
expect(proxyElement.type[PROXY_KEY]).toBeDefined()
})
})
describe('#register', () => {
it('should increment update counter', () => {
const oldGeneration = getGeneration()
reactHotLoader.register(Div, 'Div', 'reactHotLoader.test.js')
// new thing, no change
expect(getGeneration()).toBe(oldGeneration + 1)
reactHotLoader.register(Div, 'Div', 'reactHotLoader.test.js')
// no replacement
expect(getGeneration()).toBe(oldGeneration + 2)
const NewDiv = () => <div />
reactHotLoader.register(NewDiv, 'Div', 'reactHotLoader.test.js')
// replacement!
expect(getGeneration()).toBe(oldGeneration + 3)
})
it('should ignore dom elements and incomplete signature', () => {
reactHotLoader.register(Div, 'Div', 'reactHotLoader.test.js')
reactHotLoader.register('div', 'Div', 'reactHotLoader.test.js')
reactHotLoader.register(Span, 'Div')
reactHotLoader.register(Span, '')
reactHotLoader.register(Span, '', '')
const proxyElement = React.createElement(Div)
expect(proxyElement.type[UNWRAP_PROXY]()).toBe(Div)
})
it('should update proxy', () => {
reactHotLoader.register(Div, 'Div', 'reactHotLoader.test.js')
const proxyElement = React.createElement(Div)
expect(proxyElement.type[UNWRAP_PROXY]()).toBe(Div)
reactHotLoader.register(Span, 'Div', 'reactHotLoader.test.js')
expect(proxyElement.type[UNWRAP_PROXY]()).toBe(Span)
})
it('should not double-proxy', () => {
const Component1 = () => <div>42</div>
const Element1 = <Component1 />
const Type1 = Element1.type
const Element2 = <Type1 />
const Element3 = React.Children.only(Element1)
const Element4 = React.cloneElement(Element1)
expect(Element1.type).toBe(Element2.type)
expect(Element1.type).toBe(Element3.type)
expect(Element1.type).toBe(Element4.type)
})
it('should result into shadowing the original component', () => {
// Registering Div
reactHotLoader.register(Div, 'Div', 'reactHotLoader.test.js')
// Creating a Div element, proxy of Div is used
let proxyElement = React.createElement(Div)
let wrapper = mount(proxyElement)
expect(wrapper.html()).toBe('<div></div>')
// Register the same component but with a Span
reactHotLoader.register(Span, 'Div', 'reactHotLoader.test.js')
// Creating a Div element, proxy of Span is used
proxyElement = React.createElement(Div)
wrapper = mount(proxyElement)
expect(wrapper.html()).toBe('<span></span>')
})
})
})