-
Notifications
You must be signed in to change notification settings - Fork 799
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(react-hot-loader): fix missing export
- Loading branch information
Showing
3 changed files
with
169 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']) | ||
}) | ||
}) |
Ouch