Skip to content

Commit f4df027

Browse files
authored
V2 Storefront Preview add siteId query parameter to shopper context calls (@W-15976653@) (#1876)
* Add siteId from getConfig * Add getConfig docs & tests * clean up * lint
1 parent 5af2acf commit f4df027

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

packages/pwa-kit-react-sdk/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## v2.8.4 (Apr 09, 2024)
2+
- Updated StorefrontPreview component to make siteId available [#1876](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/1876)
3+
14
## v2.8.3 (Apr 09, 2024)
25
## v2.8.2 (Dec 01, 2023)
36
## v2.8.1 (Nov 08, 2023)

packages/pwa-kit-react-sdk/src/ssr/universal/components/storefront-preview/index.jsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import PropTypes from 'prop-types'
1010
import {Helmet} from 'react-helmet'
1111
import {detectStorefrontPreview, getClientScript} from './utils'
1212
import {useHistory} from 'react-router-dom'
13+
import {getConfig} from 'pwa-kit-runtime/utils/ssr-config'
1314

1415
/**
1516
*
@@ -28,11 +29,18 @@ export const StorefrontPreview = ({
2829
const history = useHistory()
2930
const isHostTrusted = detectStorefrontPreview()
3031

32+
// The result of getConfig is a mutable object. When it is changed, it affects subsequent calls of getConfig.
33+
// Inside the render, we update the configuration object with the current siteId. This change gets serialized
34+
// and placed in the DOM. We should be aware that the mutable behavior could introduce potential bugs.
35+
const {app} = getConfig()
36+
const siteId = app.commerceAPI.parameters.siteId
37+
3138
useEffect(() => {
3239
if (enabled && isHostTrusted) {
3340
window.STOREFRONT_PREVIEW = {
3441
...window.STOREFRONT_PREVIEW,
3542
getToken,
43+
siteId,
3644
experimentalUnsafeNavigate: (path, action = 'push', ...args) => {
3745
history[action](path, ...args)
3846
},
@@ -43,6 +51,7 @@ export const StorefrontPreview = ({
4351
}, [
4452
enabled,
4553
getToken,
54+
siteId,
4655
experimentalUnsafeAdditionalSearchParams,
4756
experimentalUnsafeReloadServerSide
4857
])

packages/pwa-kit-react-sdk/src/ssr/universal/components/storefront-preview/index.test.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {StorefrontPreview} from './index'
1010
import {detectStorefrontPreview} from './utils'
1111
import {Helmet} from 'react-helmet'
1212
import {useHistory} from 'react-router-dom'
13+
import {getConfig} from 'pwa-kit-runtime/utils/ssr-config'
1314

1415
jest.mock('./utils', () => {
1516
const origin = jest.requireActual('./utils')
@@ -30,17 +31,28 @@ jest.mock('react-router-dom', () => {
3031
}
3132
})
3233

34+
jest.mock('pwa-kit-runtime/utils/ssr-config', () => ({
35+
getConfig: jest.fn()
36+
}))
37+
3338
describe('Storefront Preview Component', function () {
3439
const oldWindow = window
3540

3641
beforeEach(() => {
3742
// eslint-disable-next-line
3843
window = {...oldWindow}
44+
45+
getConfig.mockReturnValue({
46+
app: {
47+
commerceAPI: {proxyPath: '/mobify/proxy/api', parameters: {siteId: 'site-id'}}
48+
}
49+
})
3950
})
4051

4152
afterEach(() => {
4253
// eslint-disable-next-line
4354
window = oldWindow
55+
jest.resetAllMocks()
4456
})
4557

4658
test('Renders children when enabled', async () => {
@@ -104,20 +116,26 @@ describe('Storefront Preview Component', function () {
104116

105117
mount(<StorefrontPreview getToken={() => 'my-token'} />)
106118
expect(window.STOREFRONT_PREVIEW.getToken).toBeDefined()
119+
expect(window.STOREFRONT_PREVIEW.siteId).toBeDefined()
107120
expect(window.STOREFRONT_PREVIEW.experimentalUnsafeNavigate).toBeDefined()
108121
expect(window.STOREFRONT_PREVIEW.experimentalUnsafeAdditionalSearchParams).toBeDefined()
109122
expect(window.STOREFRONT_PREVIEW.experimentalUnsafeReloadServerSide).toBeDefined()
110123
})
111124

112125
test('window.STOREFRONT_PREVIEW.experimentalUnsafeNavigate', () => {
113126
detectStorefrontPreview.mockReturnValue(true)
127+
const replace = jest.fn()
128+
const push = jest.fn()
129+
130+
useHistory.mockReturnValue({replace, push})
131+
114132
mount(<StorefrontPreview getToken={() => 'my-token'} />)
115133

116134
window.STOREFRONT_PREVIEW.experimentalUnsafeNavigate('/', 'replace')
117-
expect(useHistory().replace).toHaveBeenCalledWith('/')
135+
expect(replace).toHaveBeenCalledWith('/')
118136

119137
window.STOREFRONT_PREVIEW.experimentalUnsafeNavigate('/')
120-
expect(useHistory().push).toHaveBeenCalledWith('/')
138+
expect(push).toHaveBeenCalledWith('/')
121139
})
122140

123141
test('window.STOREFRONT_PREVIEW.experimentalUnsafeAdditionalSearchParams', async () => {

0 commit comments

Comments
 (0)