|
| 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