|
7 | 7 |
|
8 | 8 | import React from 'react'; |
9 | 9 | import createReactClass from 'create-react-class'; |
10 | | - |
11 | 10 | import ComponentRegistry from '../src/ComponentRegistry'; |
12 | 11 |
|
| 12 | +const onPageLoadedCallbacks = []; |
| 13 | +const onPageUnloadedCallbacks = []; |
| 14 | + |
| 15 | +jest.mock('../src/pageLifecycle', () => ({ |
| 16 | + onPageLoaded: jest.fn(cb => { |
| 17 | + onPageLoadedCallbacks.push(cb); |
| 18 | + cb(); |
| 19 | + }), |
| 20 | + onPageUnloaded: jest.fn(cb => { |
| 21 | + onPageUnloadedCallbacks.push(cb); |
| 22 | + cb(); |
| 23 | + }) |
| 24 | +})); |
| 25 | + |
| 26 | +jest.mock('../src/context', () => ({ |
| 27 | + getContextAndRailsContext: () => ({ railsContext: { componentRegistryTimeout: 100 } }) |
| 28 | +})); |
| 29 | + |
13 | 30 | describe('ComponentRegistry', () => { |
14 | 31 | beforeEach(() => { |
15 | 32 | ComponentRegistry.clear(); |
| 33 | + onPageLoadedCallbacks.forEach(cb => cb()); |
| 34 | + }); |
| 35 | + |
| 36 | + afterEach(() => { |
| 37 | + onPageUnloadedCallbacks.forEach(cb => cb()); |
16 | 38 | }); |
17 | 39 |
|
18 | 40 | it('registers and retrieves React function components', () => { |
@@ -138,4 +160,27 @@ describe('ComponentRegistry', () => { |
138 | 160 | const C9 = null; |
139 | 161 | expect(() => ComponentRegistry.register({ C9 })).toThrow(/Called register with null component named C9/); |
140 | 162 | }); |
| 163 | + |
| 164 | + it('retrieves component asynchronously when registered later', async () => { |
| 165 | + expect.assertions(1); |
| 166 | + const C1 = () => <div>HELLO</div>; |
| 167 | + const componentPromise = ComponentRegistry.getOrWaitForComponent('C1'); |
| 168 | + ComponentRegistry.register({ C1 }); |
| 169 | + const component = await componentPromise; |
| 170 | + expect(component).toEqual({ |
| 171 | + name: 'C1', |
| 172 | + component: C1, |
| 173 | + renderFunction: false, |
| 174 | + isRenderer: false, |
| 175 | + }); |
| 176 | + }); |
| 177 | + |
| 178 | + it('handles timeout for unregistered components', async () => { |
| 179 | + expect.assertions(1); |
| 180 | + try { |
| 181 | + await ComponentRegistry.getOrWaitForComponent('NonExistent'); |
| 182 | + } catch (error) { |
| 183 | + expect(error.message).toMatch(/Could not find component/); |
| 184 | + } |
| 185 | + }); |
141 | 186 | }); |
0 commit comments