1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
import React, { Component } from 'react';
import reactHotLoader from '../src/reactHotLoader';
import { areComponentsEqual, cold } from '../src/utils.dev';
import configuration from '../src/configuration';
import logger from '../src/logger';
reactHotLoader.patch(React);
jest.mock('../src/logger');
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));
});
describe('cold', () => {
it('should disable RHL for "cold" Components', () => {
const Component1 = () => <div>42</div>;
const Component2 = () => <div>42</div>;
reactHotLoader.register(Component1, 'Class1', 'util.dev');
reactHotLoader.register(Component2, 'Class2', 'util.dev');
cold(Component1);
const element1 = <Component1 />;
const element2 = <Component2 />;
expect(Component1 === element1.type).toBe(true);
expect(Component2 === element2.type).toBe(false);
expect(areComponentsEqual(Component1, element1.type)).toBe(true);
expect(areComponentsEqual(Component2, element2.type)).toBe(true);
});
it('should components by fileName', () => {
const Component1 = () => <div>42</div>;
const Component2 = () => <div>42</div>;
configuration.onComponentRegister = (type, name, file) => {
if (file === 'oneFile') cold(type);
};
reactHotLoader.register(Component1, 'Class1', 'oneFile');
reactHotLoader.register(Component2, 'Class2', 'anotherFile');
const element1 = <Component1 />;
const element2 = <Component2 />;
expect(Component1 === element1.type).toBe(true);
expect(Component2 === element2.type).toBe(false);
expect(areComponentsEqual(Component1, element1.type)).toBe(true);
expect(areComponentsEqual(Component2, element2.type)).toBe(true);
});
it('should report on cold update', () => {
const Component1 = () => <div>42</div>;
const Component2 = () => <div>42</div>;
cold(Component1);
// cold(Component2)
reactHotLoader.register(Component1, 'Cold', 'Winter');
reactHotLoader.register(Component1, 'Cold', 'Winter');
// first registration is expected
expect(logger.error).not.toHaveBeenCalled();
reactHotLoader.register(Component2, 'Cold', 'Winter');
expect(logger.error).toHaveBeenCalledWith(
`React-hot-loader: Cold component`,
'Cold',
'at',
'Winter',
'has been updated',
);
});
});
});
|