Skip to content

Commit

Permalink
fix(areComponentsEqual): fix behaviour (#829)
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey authored and gregberge committed Jan 29, 2018
1 parent 6508461 commit d4dcd07
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/react-hot-loader/src/utils.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { setConfig as setProxyConfig } from 'react-stand-in'

setProxyConfig({ logger })

const getProxyOrType = type => {
const proxy = getProxyByType(type)
return proxy ? proxy.get() : type
}

export const areComponentsEqual = (a, b) =>
getProxyByType(a) === getProxyByType(b)
getProxyOrType(a) === getProxyOrType(b)

export const setConfig = config => Object.assign(reactHotLoader.config, config)
66 changes: 66 additions & 0 deletions packages/react-hot-loader/test/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React, { Component } from 'react'
import reactHotLoader from '../src/reactHotLoader'
import { areComponentsEqual } from '../src/utils.dev'

reactHotLoader.patch(React)

describe('utils (dev)', () => {
describe('areComponentsEqual', () => {
const createClasses = () => {
class Component1 extends Component {
render() {
return 42
}
}

class Component2 extends Component {
render() {
return 43
}
}

return { Component1, Component2 }
}

const createStateless = () => {
const Component1 = () => 42
const Component2 = () => 43
return { Component1, Component2 }
}

const testSuite = factory => {
it('should compare non-registred components', () => {
const { Component1, Component2 } = factory()

const element1 = <Component1 />
const element2 = <Component2 />

expect(Component1 === Component2).toBe(false)
expect(Component1 === element1.type).toBe(false)

expect(areComponentsEqual(Component1, element1.type)).toBe(true)
expect(areComponentsEqual(Component1, element2.type)).toBe(false)
})

it('should compare registered components', () => {
const { Component1, Component2 } = factory()

reactHotLoader.register(Component1, 'Class1', 'util.dev')
reactHotLoader.register(Component2, 'Class2', 'util.dev')

const element1 = <Component1 />
const element2 = <Component2 />

expect(Component1 === Component2).toBe(false)
expect(Component1 === element1.type).toBe(false)

expect(areComponentsEqual(Component1, element1.type)).toBe(true)
expect(areComponentsEqual(Component1, element2.type)).toBe(false)
})
}

describe('class based', () => testSuite(createClasses))

describe('function based', () => testSuite(createStateless))
})
})

0 comments on commit d4dcd07

Please sign in to comment.