Skip to content

Commit d4dcd07

Browse files
theKasheygregberge
authored andcommitted
fix(areComponentsEqual): fix behaviour (#829)
1 parent 6508461 commit d4dcd07

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

Diff for: packages/react-hot-loader/src/utils.dev.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import { setConfig as setProxyConfig } from 'react-stand-in'
55

66
setProxyConfig({ logger })
77

8+
const getProxyOrType = type => {
9+
const proxy = getProxyByType(type)
10+
return proxy ? proxy.get() : type
11+
}
12+
813
export const areComponentsEqual = (a, b) =>
9-
getProxyByType(a) === getProxyByType(b)
14+
getProxyOrType(a) === getProxyOrType(b)
1015

1116
export const setConfig = config => Object.assign(reactHotLoader.config, config)

Diff for: packages/react-hot-loader/test/utils.test.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import React, { Component } from 'react'
2+
import reactHotLoader from '../src/reactHotLoader'
3+
import { areComponentsEqual } from '../src/utils.dev'
4+
5+
reactHotLoader.patch(React)
6+
7+
describe('utils (dev)', () => {
8+
describe('areComponentsEqual', () => {
9+
const createClasses = () => {
10+
class Component1 extends Component {
11+
render() {
12+
return 42
13+
}
14+
}
15+
16+
class Component2 extends Component {
17+
render() {
18+
return 43
19+
}
20+
}
21+
22+
return { Component1, Component2 }
23+
}
24+
25+
const createStateless = () => {
26+
const Component1 = () => 42
27+
const Component2 = () => 43
28+
return { Component1, Component2 }
29+
}
30+
31+
const testSuite = factory => {
32+
it('should compare non-registred components', () => {
33+
const { Component1, Component2 } = factory()
34+
35+
const element1 = <Component1 />
36+
const element2 = <Component2 />
37+
38+
expect(Component1 === Component2).toBe(false)
39+
expect(Component1 === element1.type).toBe(false)
40+
41+
expect(areComponentsEqual(Component1, element1.type)).toBe(true)
42+
expect(areComponentsEqual(Component1, element2.type)).toBe(false)
43+
})
44+
45+
it('should compare registered components', () => {
46+
const { Component1, Component2 } = factory()
47+
48+
reactHotLoader.register(Component1, 'Class1', 'util.dev')
49+
reactHotLoader.register(Component2, 'Class2', 'util.dev')
50+
51+
const element1 = <Component1 />
52+
const element2 = <Component2 />
53+
54+
expect(Component1 === Component2).toBe(false)
55+
expect(Component1 === element1.type).toBe(false)
56+
57+
expect(areComponentsEqual(Component1, element1.type)).toBe(true)
58+
expect(areComponentsEqual(Component1, element2.type)).toBe(false)
59+
})
60+
}
61+
62+
describe('class based', () => testSuite(createClasses))
63+
64+
describe('function based', () => testSuite(createStateless))
65+
})
66+
})

0 commit comments

Comments
 (0)