|
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} 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 | | - ] |
34 | | -} |
| 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 | +})) |
35 | 20 |
|
36 | | -const TEST_COMPONENTS = { |
37 | | - ['commerce_assets.carousel']: () => <div className="carousel">Carousel</div> |
| 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 |
38 | 26 | } |
39 | 27 |
|
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() |
| 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 | 44 |
|
45 | | - expect(() => { |
46 | | - render(<Component component={SAMPLE_COMPONENT} />) |
47 | | - }).toThrow() |
48 | | - console.error = originalError |
49 | | -}) |
| 45 | + beforeEach(() => { |
| 46 | + jest.clearAllMocks() |
| 47 | + // Suppress console.log during tests |
| 48 | + jest.spyOn(console, 'log').mockImplementation(() => {}) |
| 49 | + }) |
50 | 50 |
|
51 | | -test('Page renders correct component', () => { |
52 | | - const component = <Component component={SAMPLE_COMPONENT} /> |
| 51 | + afterEach(() => { |
| 52 | + jest.restoreAllMocks() |
| 53 | + }) |
53 | 54 |
|
54 | | - const {container} = render(component, { |
55 | | - wrapper: () => ( |
56 | | - <PageContext.Provider value={{components: TEST_COMPONENTS}}> |
57 | | - {component} |
58 | | - </PageContext.Provider> |
| 55 | + test('renders component when DynamicComponent is available', () => { |
| 56 | + const MockDynamicComponent = ({title}: {title: string}) => ( |
| 57 | + <div data-testid="dynamic-component">{title}</div> |
59 | 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() |
60 | 66 | }) |
61 | 67 |
|
62 | | - // Component are in document. |
63 | | - expect(container.querySelectorAll('.component')?.length).toBe(1) |
| 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) |
64 | 73 |
|
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) |
| 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 | + } |
| 82 | + |
| 83 | + expect(mockRegistry.preload).toHaveBeenCalledWith('commerce_assets.banner') |
| 84 | + }) |
| 85 | + |
| 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(<Component component={mockComponent} regionId="test-region" className="custom-class" />) |
| 96 | + |
| 97 | + expect(receivedProps.title).toBe('Test Banner') |
| 98 | + expect(receivedProps.imageUrl).toBe('/test-image.jpg') |
| 99 | + expect(receivedProps.className).toBe('custom-class') |
| 100 | + expect(receivedProps.regionId).toBe('test-region') |
| 101 | + expect(receivedProps.component).toBe(mockComponent) |
| 102 | + expect(receivedProps.regions).toEqual([]) |
| 103 | + expect(receivedProps.designMetadata).toEqual({ |
| 104 | + name: 'Test Component', |
| 105 | + isFragment: false, |
| 106 | + isVisible: true, |
| 107 | + isLocalized: false, |
| 108 | + id: 'test-component-id' |
| 109 | + }) |
| 110 | + }) |
| 111 | + |
| 112 | + test('handles component without designMetadata', () => { |
| 113 | + const componentWithoutDesignMetadata = { |
| 114 | + ...mockComponent, |
| 115 | + designMetadata: undefined |
| 116 | + } as unknown as ComponentProps['component'] |
| 117 | + const receivedProps: Record<string, unknown> = {} |
| 118 | + const MockDynamicComponent = (props: Record<string, unknown>) => { |
| 119 | + Object.assign(receivedProps, props) |
| 120 | + return <div data-testid="dynamic-component">Test</div> |
| 121 | + } |
| 122 | + mockRegistry.getComponent.mockReturnValue(MockDynamicComponent) |
| 123 | + mockRegistry.getFallback.mockReturnValue(null) |
| 124 | + |
| 125 | + render(<Component component={componentWithoutDesignMetadata} regionId="test-region" />) |
| 126 | + |
| 127 | + expect((receivedProps.designMetadata as {name: string | undefined}).name).toBeUndefined() |
| 128 | + }) |
68 | 129 | }) |
0 commit comments