|
5 | 5 | * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
6 | 6 | */ |
7 | 7 | import React from 'react' |
8 | | -import {render, screen} from '@testing-library/react' |
9 | | -import {Component, ComponentProps} from './index' |
10 | | -import {registry} from '../registry' |
11 | | - |
12 | | -// Mock the registry |
13 | | -jest.mock('../registry', () => ({ |
14 | | - registry: { |
15 | | - getComponent: jest.fn(), |
16 | | - getFallback: jest.fn(), |
17 | | - preload: jest.fn() |
18 | | - } |
19 | | -})) |
20 | | - |
21 | | -// Type the mock registry with flexible return types for testing |
22 | | -const mockRegistry = registry as unknown as { |
23 | | - getComponent: jest.Mock |
24 | | - getFallback: jest.Mock |
25 | | - preload: jest.Mock |
| 8 | +import {render} from '@testing-library/react' |
| 9 | +import Component from './index' |
| 10 | +import {PageContext} from '../Page' |
| 11 | + |
| 12 | +const SAMPLE_COMPONENT = { |
| 13 | + id: 'rfdvj4ojtltljw3', |
| 14 | + typeId: 'commerce_assets.carousel', |
| 15 | + data: { |
| 16 | + title: 'Topseller', |
| 17 | + category: 'topseller' |
| 18 | + }, |
| 19 | + regions: [ |
| 20 | + { |
| 21 | + id: 'regionB1', |
| 22 | + components: [ |
| 23 | + { |
| 24 | + id: 'rfdvj4ojtltljw3', |
| 25 | + typeId: 'commerce_assets.carousel', |
| 26 | + data: { |
| 27 | + title: 'Topseller', |
| 28 | + category: 'topseller' |
| 29 | + } |
| 30 | + } |
| 31 | + ] |
| 32 | + } |
| 33 | + ] |
26 | 34 | } |
27 | 35 |
|
28 | | -describe('Component', () => { |
29 | | - // Create mock component data - cast to ComponentProps['component'] for test flexibility |
30 | | - const mockComponent = { |
31 | | - id: 'test-component-id', |
32 | | - typeId: 'commerce_assets.banner', |
33 | | - data: { |
34 | | - title: 'Test Banner', |
35 | | - imageUrl: '/test-image.jpg' |
36 | | - }, |
37 | | - visible: true, |
38 | | - localized: false, |
39 | | - designMetadata: { |
40 | | - name: 'Test Component' |
41 | | - }, |
42 | | - regions: [] |
43 | | - } as unknown as ComponentProps['component'] |
44 | | - |
45 | | - beforeEach(() => { |
46 | | - jest.clearAllMocks() |
47 | | - // Suppress console.log during tests |
48 | | - jest.spyOn(console, 'log').mockImplementation(() => {}) |
49 | | - }) |
50 | | - |
51 | | - afterEach(() => { |
52 | | - jest.restoreAllMocks() |
53 | | - }) |
54 | | - |
55 | | - test('renders component when DynamicComponent is available', () => { |
56 | | - const MockDynamicComponent = ({title}: {title: string}) => ( |
57 | | - <div data-testid="dynamic-component">{title}</div> |
58 | | - ) |
59 | | - mockRegistry.getComponent.mockReturnValue(MockDynamicComponent) |
60 | | - mockRegistry.getFallback.mockReturnValue(null) |
61 | | - |
62 | | - render(<Component component={mockComponent} regionId="test-region" />) |
63 | | - |
64 | | - expect(screen.getByTestId('dynamic-component')).toBeInTheDocument() |
65 | | - expect(screen.getByText('Test Banner')).toBeInTheDocument() |
66 | | - }) |
| 36 | +const TEST_COMPONENTS = { |
| 37 | + ['commerce_assets.carousel']: () => <div className="carousel">Carousel</div> |
| 38 | +} |
67 | 39 |
|
68 | | - test('calls preload when DynamicComponent is not available', () => { |
69 | | - const preloadPromise = Promise.resolve() |
70 | | - mockRegistry.getComponent.mockReturnValue(undefined) |
71 | | - mockRegistry.getFallback.mockReturnValue(null) |
72 | | - mockRegistry.preload.mockReturnValue(preloadPromise) |
| 40 | +test('Page throws if used outside of a Page component', () => { |
| 41 | + // Mock console.error to suppress React error boundary warnings |
| 42 | + const originalError = console.error |
| 43 | + console.error = jest.fn() |
73 | 44 |
|
74 | | - // Component throws the preload promise for Suspense to catch |
75 | | - // We can't test the throw directly because React catches it internally |
76 | | - // Instead we verify preload is called with the correct typeId |
77 | | - try { |
78 | | - render(<Component component={mockComponent} regionId="test-region" />) |
79 | | - } catch (e) { |
80 | | - // Expected - Suspense boundary catches this |
81 | | - } |
| 45 | + expect(() => { |
| 46 | + render(<Component component={SAMPLE_COMPONENT} />) |
| 47 | + }).toThrow() |
| 48 | + console.error = originalError |
| 49 | +}) |
82 | 50 |
|
83 | | - expect(mockRegistry.preload).toHaveBeenCalledWith('commerce_assets.banner') |
84 | | - }) |
| 51 | +test('Page renders correct component', () => { |
| 52 | + const component = <Component component={SAMPLE_COMPONENT} /> |
85 | 53 |
|
86 | | - test('passes correct props to DynamicComponent', () => { |
87 | | - const receivedProps: Record<string, unknown> = {} |
88 | | - const MockDynamicComponent = (props: Record<string, unknown>) => { |
89 | | - Object.assign(receivedProps, props) |
90 | | - return <div data-testid="dynamic-component">Test</div> |
91 | | - } |
92 | | - mockRegistry.getComponent.mockReturnValue(MockDynamicComponent) |
93 | | - mockRegistry.getFallback.mockReturnValue(null) |
94 | | - |
95 | | - render( |
96 | | - <Component component={mockComponent} regionId="test-region" className="custom-class" /> |
| 54 | + const {container} = render(component, { |
| 55 | + wrapper: () => ( |
| 56 | + <PageContext.Provider value={{components: TEST_COMPONENTS}}> |
| 57 | + {component} |
| 58 | + </PageContext.Provider> |
97 | 59 | ) |
98 | | - |
99 | | - expect(receivedProps.title).toBe('Test Banner') |
100 | | - expect(receivedProps.imageUrl).toBe('/test-image.jpg') |
101 | | - expect(receivedProps.className).toBe('custom-class') |
102 | | - expect(receivedProps.regionId).toBe('test-region') |
103 | | - expect(receivedProps.component).toBe(mockComponent) |
104 | | - expect(receivedProps.regions).toEqual([]) |
105 | | - expect(receivedProps.designMetadata).toEqual({ |
106 | | - name: 'Test Component', |
107 | | - isFragment: false, |
108 | | - isVisible: true, |
109 | | - isLocalized: false, |
110 | | - id: 'test-component-id' |
111 | | - }) |
112 | 60 | }) |
113 | 61 |
|
114 | | - test('handles component without designMetadata', () => { |
115 | | - const componentWithoutDesignMetadata = { |
116 | | - ...mockComponent, |
117 | | - designMetadata: undefined |
118 | | - } as unknown as ComponentProps['component'] |
119 | | - const receivedProps: Record<string, unknown> = {} |
120 | | - const MockDynamicComponent = (props: Record<string, unknown>) => { |
121 | | - Object.assign(receivedProps, props) |
122 | | - return <div data-testid="dynamic-component">Test</div> |
123 | | - } |
124 | | - mockRegistry.getComponent.mockReturnValue(MockDynamicComponent) |
125 | | - mockRegistry.getFallback.mockReturnValue(null) |
| 62 | + // Component are in document. |
| 63 | + expect(container.querySelectorAll('.component')?.length).toBe(1) |
126 | 64 |
|
127 | | - render(<Component component={componentWithoutDesignMetadata} regionId="test-region" />) |
128 | | - |
129 | | - expect((receivedProps.designMetadata as {name: string | undefined}).name).toBeUndefined() |
130 | | - }) |
| 65 | + // Provided components are in document. (Note: Sub-regions/components aren't rendered because that is |
| 66 | + // the responsibility of the component definition.) |
| 67 | + expect(container.querySelectorAll('.carousel')?.length).toBe(1) |
131 | 68 | }) |
0 commit comments