Skip to content

Commit

Permalink
fix(react-hot-loader): fix missing export
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Dec 29, 2017
1 parent bed0bb2 commit 239ca5d
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 158 deletions.
2 changes: 2 additions & 0 deletions packages/react-hot-loader/src/patch.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ import React from 'react'
import reactHotLoader from './reactHotLoader'

reactHotLoader.patch(React)

export default reactHotLoader

This comment has been minimized.

Copy link
@theKashey

theKashey Dec 29, 2017

Collaborator

Ouch

169 changes: 11 additions & 158 deletions packages/react-hot-loader/test/patch.dev.test.js
Original file line number Diff line number Diff line change
@@ -1,163 +1,16 @@
/* eslint-env jest */

import React from 'react'
import '../lib/patch.dev'
import RHL from '../lib/reactHotLoader'

RHL.disableComponentProxy = true

function A1() {}
function A2() {}
function A3() {}
function B1() {}
function B2() {}

function runAllTests(useWeakMap) {
describe('patch', () => {
beforeEach(() => {
RHL.reset(useWeakMap)
})

it('is identity for unrecognized types', () => {
expect(<div />.type).toBe('div')
expect(<A1 />.type).toBe(A1)
})

it('report proxy named duplicates', () => {
const createUniqueComponent = variable => () => <div>123{variable}</div>
const f1 = createUniqueComponent(1)
const f2 = createUniqueComponent(2)
f2.displayName = 'another'

const spy = jest.spyOn(console, 'warn').mockImplementation(() => {})

try {
RHL.register(createUniqueComponent, 'f1', '/wow/test.js')
React.createElement(f1)
React.createElement(f1)
expect(console.warn.mock.calls.length).toBe(0)
RHL.register(createUniqueComponent, 'f1', '/wow/test.js')
React.createElement(f2)
expect(console.warn.mock.calls.length).toBe(0)
} finally {
spy.mockRestore()
}
})

it('resolves registered types by their last ID', () => {
RHL.register(A1, 'a', 'test.js')
const A = <A1 />.type
expect(A).not.toBe(A1)
expect(A).toBeInstanceOf(Function)
expect(<A />.type).toBe(A)

RHL.register(A2, 'a', 'test.js')
expect(<A1 />.type).toBe(A)
expect(<A2 />.type).toBe(A)
expect(<A />.type).toBe(A)
import reactHotLoader from '../src/reactHotLoader'
import patchExport from '../src/patch.dev'

RHL.register(A3, 'a', 'test.js')
expect(<A1 />.type).toBe(A)
expect(<A2 />.type).toBe(A)
expect(<A3 />.type).toBe(A)
expect(<A />.type).toBe(A)

RHL.register(B1, 'b', 'test.js')
const B = <B1 />.type
expect(<A1 />.type).toBe(A)
expect(<A2 />.type).toBe(A)
expect(<A3 />.type).toBe(A)
expect(<A />.type).toBe(A)
expect(<B1 />.type).toBe(B)
expect(<B />.type).toBe(B)

RHL.register(B2, 'b', 'test.js')
expect(<A1 />.type).toBe(A)
expect(<A2 />.type).toBe(A)
expect(<A3 />.type).toBe(A)
expect(<A />.type).toBe(A)
expect(<B1 />.type).toBe(B)
expect(<B2 />.type).toBe(B)
expect(<B />.type).toBe(B)
})

it('works with reexported types', () => {
RHL.register(A1, 'a', 'test.js')
RHL.register(A1, 'x', 'test2.js')

const A = <A1 />.type
expect(A.type).not.toBe(A1)
expect(A).toBeInstanceOf(Function)
expect(<A />.type).toBe(A)

RHL.register(A2, 'a', 'test.js')
RHL.register(A2, 'x', 'test2.js')
expect(<A1 />.type).toBe(A)
expect(<A2 />.type).toBe(A)
expect(<A />.type).toBe(A)
})

it('passes props through', () => {
expect(<div x={42} y="lol" />.props).toEqual({
x: 42,
y: 'lol',
})
expect(<A1 x={42} y="lol" />.props).toEqual({
x: 42,
y: 'lol',
})

RHL.register(B1, 'b', 'test.js')
expect(<B1 x={42} y="lol" />.props).toEqual({
x: 42,
y: 'lol',
})
RHL.register(B2, 'b', 'test.js')
expect(<B2 x={42} y="lol" />.props).toEqual({
x: 42,
y: 'lol',
})
})

it('passes children through', () => {
expect(
(
<div>
{'Hi'}
{'Bye'}
</div>
).props.children,
).toEqual(['Hi', 'Bye'])
expect(
(
<A1>
{'Hi'}
{'Bye'}
</A1>
).props.children,
).toEqual(['Hi', 'Bye'])

RHL.register(B1, 'b', 'test.js')
expect(
(
<B1>
{'Hi'}
{'Bye'}
</B1>
).props.children,
).toEqual(['Hi', 'Bye'])
RHL.register(B2, 'b', 'test.js')
expect(
(
<B2>
{'Hi'}
{'Bye'}
</B2>
).props.children,
).toEqual(['Hi', 'Bye'])
})
describe('patch', () => {
it('should export reactHotLoader', () => {
expect(patchExport).toBe(reactHotLoader)
})
}

runAllTests(true)
runAllTests(false)
it('should patch React methods', () => {
expect(React.createElement.isPatchedByReactHotLoader).toBe(true)
expect(React.createFactory.isPatchedByReactHotLoader).toBe(true)
expect(React.Children.only.isPatchedByReactHotLoader).toBe(true)
})
})
156 changes: 156 additions & 0 deletions packages/react-hot-loader/test/reactHotLoader.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/* eslint-env jest */

import React from 'react'
import '../lib/patch.dev'
import RHL from '../lib/reactHotLoader'

RHL.disableComponentProxy = true

function A1() {}
function A2() {}
function A3() {}
function B1() {}
function B2() {}

describe('reactHotLoader', () => {
beforeEach(() => RHL.reset())

it('is identity for unrecognized types', () => {
expect(<div />.type).toBe('div')
expect(<A1 />.type).toBe(A1)
})

it('report proxy named duplicates', () => {
const createUniqueComponent = variable => () => <div>123{variable}</div>
const f1 = createUniqueComponent(1)
const f2 = createUniqueComponent(2)
f2.displayName = 'another'

const spy = jest.spyOn(console, 'warn').mockImplementation(() => {})

try {
RHL.register(createUniqueComponent, 'f1', '/wow/test.js')
React.createElement(f1)
React.createElement(f1)
expect(console.warn.mock.calls.length).toBe(0)
RHL.register(createUniqueComponent, 'f1', '/wow/test.js')
React.createElement(f2)
expect(console.warn.mock.calls.length).toBe(0)
} finally {
spy.mockRestore()
}
})

it('resolves registered types by their last ID', () => {
RHL.register(A1, 'a', 'test.js')
const A = <A1 />.type
expect(A).not.toBe(A1)
expect(A).toBeInstanceOf(Function)
expect(<A />.type).toBe(A)

RHL.register(A2, 'a', 'test.js')
expect(<A1 />.type).toBe(A)
expect(<A2 />.type).toBe(A)
expect(<A />.type).toBe(A)

RHL.register(A3, 'a', 'test.js')
expect(<A1 />.type).toBe(A)
expect(<A2 />.type).toBe(A)
expect(<A3 />.type).toBe(A)
expect(<A />.type).toBe(A)

RHL.register(B1, 'b', 'test.js')
const B = <B1 />.type
expect(<A1 />.type).toBe(A)
expect(<A2 />.type).toBe(A)
expect(<A3 />.type).toBe(A)
expect(<A />.type).toBe(A)
expect(<B1 />.type).toBe(B)
expect(<B />.type).toBe(B)

RHL.register(B2, 'b', 'test.js')
expect(<A1 />.type).toBe(A)
expect(<A2 />.type).toBe(A)
expect(<A3 />.type).toBe(A)
expect(<A />.type).toBe(A)
expect(<B1 />.type).toBe(B)
expect(<B2 />.type).toBe(B)
expect(<B />.type).toBe(B)
})

it('works with reexported types', () => {
RHL.register(A1, 'a', 'test.js')
RHL.register(A1, 'x', 'test2.js')

const A = <A1 />.type
expect(A.type).not.toBe(A1)
expect(A).toBeInstanceOf(Function)
expect(<A />.type).toBe(A)

RHL.register(A2, 'a', 'test.js')
RHL.register(A2, 'x', 'test2.js')
expect(<A1 />.type).toBe(A)
expect(<A2 />.type).toBe(A)
expect(<A />.type).toBe(A)
})

it('passes props through', () => {
expect(<div x={42} y="lol" />.props).toEqual({
x: 42,
y: 'lol',
})
expect(<A1 x={42} y="lol" />.props).toEqual({
x: 42,
y: 'lol',
})

RHL.register(B1, 'b', 'test.js')
expect(<B1 x={42} y="lol" />.props).toEqual({
x: 42,
y: 'lol',
})
RHL.register(B2, 'b', 'test.js')
expect(<B2 x={42} y="lol" />.props).toEqual({
x: 42,
y: 'lol',
})
})

it('passes children through', () => {
expect(
(
<div>
{'Hi'}
{'Bye'}
</div>
).props.children,
).toEqual(['Hi', 'Bye'])
expect(
(
<A1>
{'Hi'}
{'Bye'}
</A1>
).props.children,
).toEqual(['Hi', 'Bye'])

RHL.register(B1, 'b', 'test.js')
expect(
(
<B1>
{'Hi'}
{'Bye'}
</B1>
).props.children,
).toEqual(['Hi', 'Bye'])
RHL.register(B2, 'b', 'test.js')
expect(
(
<B2>
{'Hi'}
{'Bye'}
</B2>
).props.children,
).toEqual(['Hi', 'Bye'])
})
})

0 comments on commit 239ca5d

Please sign in to comment.