|
| 1 | +import { delay, mount } from '@hypothesis/frontend-testing'; |
| 2 | + |
| 3 | +import { lazy } from '../lazy'; |
| 4 | + |
| 5 | +describe('lazy', () => { |
| 6 | + let fakeComponent; |
| 7 | + let fakeLoader; |
| 8 | + let LazyComponent; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + fakeComponent = ({ text }) => ( |
| 12 | + <div data-testid="loaded-component">{text}</div> |
| 13 | + ); |
| 14 | + fakeLoader = sinon.stub(); |
| 15 | + LazyComponent = lazy('TestComponent', fakeLoader, { |
| 16 | + fallback: ({ text }) => <div data-testid="fallback">{text}</div>, |
| 17 | + errorFallback: ({ text }, error) => ( |
| 18 | + <div data-testid="error-fallback"> |
| 19 | + {text} - Error: {error.message} |
| 20 | + </div> |
| 21 | + ), |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + sinon.restore(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('renders fallback initially', () => { |
| 30 | + fakeLoader.returns(Promise.resolve(fakeComponent)); |
| 31 | + const wrapper = mount(<LazyComponent text="test" />); |
| 32 | + |
| 33 | + assert.isTrue(wrapper.exists('[data-testid="fallback"]')); |
| 34 | + assert.equal(wrapper.find('[data-testid="fallback"]').text(), 'test'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('renders loaded component after loading completes', async () => { |
| 38 | + fakeLoader.returns(Promise.resolve(fakeComponent)); |
| 39 | + const wrapper = mount(<LazyComponent text="test" />); |
| 40 | + |
| 41 | + // Initially shows fallback |
| 42 | + assert.isTrue(wrapper.exists('[data-testid="fallback"]')); |
| 43 | + assert.isFalse(wrapper.exists('[data-testid="loaded-component"]')); |
| 44 | + |
| 45 | + // Wait for component to load |
| 46 | + await delay(0); |
| 47 | + wrapper.update(); |
| 48 | + |
| 49 | + // Now shows loaded component |
| 50 | + assert.isFalse(wrapper.exists('[data-testid="fallback"]')); |
| 51 | + assert.isTrue(wrapper.exists('[data-testid="loaded-component"]')); |
| 52 | + assert.equal( |
| 53 | + wrapper.find('[data-testid="loaded-component"]').text(), |
| 54 | + 'test', |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it('passes props to loaded component', async () => { |
| 59 | + fakeLoader.returns(Promise.resolve(fakeComponent)); |
| 60 | + const wrapper = mount(<LazyComponent text="test" customProp="value" />); |
| 61 | + |
| 62 | + await delay(0); |
| 63 | + wrapper.update(); |
| 64 | + |
| 65 | + const loadedComponent = wrapper.find('[data-testid="loaded-component"]'); |
| 66 | + assert.equal(loadedComponent.text(), 'test'); |
| 67 | + // The component should receive all props passed to the lazy wrapper |
| 68 | + assert.equal(loadedComponent.parent().prop('customProp'), 'value'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('renders error fallback when loading fails', async () => { |
| 72 | + const error = new Error('Loading failed'); |
| 73 | + fakeLoader.returns(Promise.reject(error)); |
| 74 | + const wrapper = mount(<LazyComponent text="test" />); |
| 75 | + |
| 76 | + // Initially shows fallback |
| 77 | + assert.isTrue(wrapper.exists('[data-testid="fallback"]')); |
| 78 | + |
| 79 | + // Wait for loading to fail |
| 80 | + await delay(0); |
| 81 | + wrapper.update(); |
| 82 | + |
| 83 | + // Now shows error fallback |
| 84 | + assert.isFalse(wrapper.exists('[data-testid="fallback"]')); |
| 85 | + assert.isTrue(wrapper.exists('[data-testid="error-fallback"]')); |
| 86 | + assert.equal( |
| 87 | + wrapper.find('[data-testid="error-fallback"]').text(), |
| 88 | + 'test - Error: Loading failed', |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + it('renders default error fallback when `errorFallback` is not provided', async () => { |
| 93 | + const error = new Error('Loading failed'); |
| 94 | + fakeLoader.returns(Promise.reject(error)); |
| 95 | + |
| 96 | + const LazyComponentWithoutErrorFallback = lazy( |
| 97 | + 'TestComponent', |
| 98 | + fakeLoader, |
| 99 | + { |
| 100 | + fallback: ({ text }) => <div data-testid="fallback">{text}</div>, |
| 101 | + }, |
| 102 | + ); |
| 103 | + |
| 104 | + const wrapper = mount(<LazyComponentWithoutErrorFallback text="test" />); |
| 105 | + |
| 106 | + // Wait for loading to fail |
| 107 | + await delay(0); |
| 108 | + wrapper.update(); |
| 109 | + |
| 110 | + assert.isTrue( |
| 111 | + wrapper.text().includes('There was a problem loading this content'), |
| 112 | + ); |
| 113 | + assert.isTrue(wrapper.text().includes('Loading failed')); |
| 114 | + }); |
| 115 | + |
| 116 | + it('does not call loader again if re-rendered while loading', async () => { |
| 117 | + fakeLoader.returns(Promise.resolve(fakeComponent)); |
| 118 | + const wrapper = mount(<LazyComponent text="test" />); |
| 119 | + |
| 120 | + // Re-render component before loader result has been processed. |
| 121 | + wrapper.setProps({ text: 'updated' }); |
| 122 | + |
| 123 | + assert.calledOnce(fakeLoader); |
| 124 | + }); |
| 125 | + |
| 126 | + it('sets displayName on the returned component', () => { |
| 127 | + assert.equal(LazyComponent.displayName, 'Lazy(TestComponent)'); |
| 128 | + }); |
| 129 | +}); |
0 commit comments