Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

areComponentEqual to work as expected #829

Merged
merged 1 commit into from
Jan 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
})
})