Skip to content

Commit 3eb5f49

Browse files
committed
feat: pd separate bundle
1 parent 1cadf01 commit 3eb5f49

File tree

69 files changed

+1411
-1965
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1411
-1965
lines changed

package-lock.json

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/commerce-sdk-react/package-lock.json

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/commerce-sdk-react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"files": [
1818
"CHANGELOG.md",
1919
"LICENSE",
20-
"+(auth|components|hooks|scripts)/**/!(*.test*).{ts,js}",
20+
"+(auth|components|hooks|scripts|page-designer)/**/!(*.test*).{ts,js}",
2121
"*.{js,d.ts}",
2222
"!*.test*.{js,d.ts}",
2323
"!test*.*",

packages/commerce-sdk-react/src/components/ShopperExperience/Component/index.test.tsx

Lines changed: 50 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -5,127 +5,64 @@
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77
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+
]
2634
}
2735

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+
}
6739

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()
7344

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+
})
8250

83-
expect(mockRegistry.preload).toHaveBeenCalledWith('commerce_assets.banner')
84-
})
51+
test('Page renders correct component', () => {
52+
const component = <Component component={SAMPLE_COMPONENT} />
8553

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>
9759
)
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-
})
11260
})
11361

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)
12664

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)
13168
})

0 commit comments

Comments
 (0)