Skip to content

Commit 5fcbd85

Browse files
Add async component retrieval and timeout handling to ComponentRegistry
1 parent 033ce1f commit 5fcbd85

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

node_package/tests/ComponentRegistry.test.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,34 @@
77

88
import React from 'react';
99
import createReactClass from 'create-react-class';
10-
1110
import ComponentRegistry from '../src/ComponentRegistry';
1211

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+
1330
describe('ComponentRegistry', () => {
1431
beforeEach(() => {
1532
ComponentRegistry.clear();
33+
onPageLoadedCallbacks.forEach(cb => cb());
34+
});
35+
36+
afterEach(() => {
37+
onPageUnloadedCallbacks.forEach(cb => cb());
1638
});
1739

1840
it('registers and retrieves React function components', () => {
@@ -138,4 +160,27 @@ describe('ComponentRegistry', () => {
138160
const C9 = null;
139161
expect(() => ComponentRegistry.register({ C9 })).toThrow(/Called register with null component named C9/);
140162
});
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+
});
141186
});

0 commit comments

Comments
 (0)