Skip to content

Commit c0b691d

Browse files
committed
Add tests
1 parent acb49ce commit c0b691d

File tree

6 files changed

+197
-6
lines changed

6 files changed

+197
-6
lines changed

packages/pwa-kit-react-sdk/src/ssr/universal/utils.client.test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77
import * as utils from './utils'
8+
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
9+
import {getEnvBasePath} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'
10+
11+
jest.mock('@salesforce/pwa-kit-runtime/utils/ssr-config')
12+
jest.mock('@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths')
813

914
describe('getProxyConfigs (client-side)', () => {
1015
const configs = [{foo: 'bar'}]
@@ -33,3 +38,51 @@ describe('getAssetUrl (client-side)', () => {
3338
expect(utils.getAssetUrl('/path')).toBe('test.com/path')
3439
})
3540
})
41+
42+
describe('getBasename (client-side)', () => {
43+
beforeEach(() => {
44+
jest.clearAllMocks()
45+
})
46+
47+
test('should return basename when showBasename is true', () => {
48+
const mockBasename = '/test-base'
49+
getEnvBasePath.mockReturnValue(mockBasename)
50+
getConfig.mockReturnValue({
51+
app: {
52+
url: {
53+
showBasename: true
54+
}
55+
}
56+
})
57+
58+
expect(utils.getBasename()).toBe(mockBasename)
59+
})
60+
61+
test('should return empty string when showBasename is undefined', () => {
62+
getConfig.mockReturnValue({
63+
app: {
64+
url: {}
65+
}
66+
})
67+
68+
expect(utils.getBasename()).toBe('')
69+
})
70+
71+
test('should return empty string when showBasename is false', () => {
72+
getConfig.mockReturnValue({
73+
app: {
74+
url: {
75+
showBasename: false
76+
}
77+
}
78+
})
79+
80+
expect(utils.getBasename()).toBe('')
81+
})
82+
83+
test('should return empty string when app config is missing', () => {
84+
getConfig.mockReturnValue({})
85+
86+
expect(utils.getBasename()).toBe('')
87+
})
88+
})

packages/pwa-kit-react-sdk/src/ssr/universal/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,6 @@ export const getProxyConfigs = () => {
6464
*/
6565
export const getBasename = () => {
6666
const config = getConfig()
67-
const showBasename = config?.app?.url?.showBasename !== false
67+
const showBasename = config?.app?.url?.showBasename === true
6868
return showBasename ? getEnvBasePath() : ''
6969
}

packages/pwa-kit-react-sdk/src/ssr/universal/utils.server.test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,62 @@
1313

1414
import * as utils from './utils'
1515
import {proxyConfigs} from '@salesforce/pwa-kit-runtime/utils/ssr-shared'
16+
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
17+
import {getEnvBasePath} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'
18+
19+
jest.mock('@salesforce/pwa-kit-runtime/utils/ssr-config')
20+
jest.mock('@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths')
1621

1722
describe('getProxyConfigs (server-side)', () => {
1823
test('should return the currently used proxy configs', () => {
1924
expect(utils.getProxyConfigs()).toEqual(proxyConfigs)
2025
})
2126
})
27+
28+
describe('getBasename (server-side)', () => {
29+
beforeEach(() => {
30+
jest.clearAllMocks()
31+
})
32+
33+
test('should return basename when showBasename is true', () => {
34+
const mockBasename = '/test-base'
35+
getEnvBasePath.mockReturnValue(mockBasename)
36+
getConfig.mockReturnValue({
37+
app: {
38+
url: {
39+
showBasename: true
40+
}
41+
}
42+
})
43+
44+
expect(utils.getBasename()).toBe(mockBasename)
45+
})
46+
47+
test('should return empty string when showBasename is undefined', () => {
48+
getConfig.mockReturnValue({
49+
app: {
50+
url: {}
51+
}
52+
})
53+
54+
expect(utils.getBasename()).toBe('')
55+
})
56+
57+
test('should return empty string when showBasename is false', () => {
58+
getConfig.mockReturnValue({
59+
app: {
60+
url: {
61+
showBasename: false
62+
}
63+
}
64+
})
65+
66+
expect(utils.getBasename()).toBe('')
67+
})
68+
69+
test('should return empty string when app config is missing', () => {
70+
getConfig.mockReturnValue({})
71+
72+
expect(utils.getBasename()).toBe('')
73+
})
74+
})

packages/template-retail-react-app/app/utils/site-utils.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
resolveSiteFromUrl
1212
} from '@salesforce/retail-react-app/app/utils/site-utils'
1313
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
14+
import {getBasename} from '@salesforce/pwa-kit-react-sdk/ssr/universal/utils'
1415

1516
import mockConfig from '@salesforce/retail-react-app/config/mocks/default'
1617
import {
@@ -25,8 +26,18 @@ jest.mock('@salesforce/pwa-kit-runtime/utils/ssr-config', () => {
2526
}
2627
})
2728

29+
jest.mock('@salesforce/pwa-kit-react-sdk/ssr/universal/utils', () => {
30+
const original = jest.requireActual('@salesforce/pwa-kit-react-sdk/ssr/universal/utils')
31+
return {
32+
...original,
33+
getBasename: jest.fn(() => '')
34+
}
35+
})
36+
2837
beforeEach(() => {
2938
jest.resetModules()
39+
// Reset the mock after resetModules
40+
getBasename.mockReturnValue('')
3041
})
3142

3243
afterEach(() => {
@@ -310,6 +321,42 @@ describe('getParamsFromPath', function () {
310321
expect(getParamsFromPath(path)).toEqual(expectedRes)
311322
})
312323
})
324+
325+
describe('getParamsFromPath with basename', () => {
326+
test('should remove basename from path when showBasename is true', () => {
327+
const basename = '/test-base'
328+
// Re-require modules to get fresh imports with mocks
329+
// This is because these modules are first imported at module load time before mocks were set
330+
// and have references to the original functions.
331+
/* eslint-disable @typescript-eslint/no-var-requires */
332+
const {
333+
getParamsFromPath: getParamsFromPathFresh
334+
} = require('@salesforce/retail-react-app/app/utils/site-utils')
335+
const {
336+
getConfig: getConfigFresh
337+
} = require('@salesforce/pwa-kit-runtime/utils/ssr-config')
338+
const {
339+
getBasename: getBasenameFresh
340+
} = require('@salesforce/pwa-kit-react-sdk/ssr/universal/utils')
341+
/* eslint-enable @typescript-eslint/no-var-requires */
342+
343+
getBasenameFresh.mockReturnValue(basename)
344+
getConfigFresh.mockImplementation(() => ({
345+
...mockConfig,
346+
app: {
347+
...mockConfig.app,
348+
url: {
349+
...mockConfig.app.url,
350+
showBasename: true
351+
}
352+
}
353+
}))
354+
355+
const path = `${basename}/us/en-US/category/womens`
356+
const result = getParamsFromPathFresh(path)
357+
expect(result).toEqual({siteRef: 'us', localeRef: 'en-US'})
358+
})
359+
})
313360
})
314361

315362
describe('resolveLocaleFromUrl', function () {

packages/template-retail-react-app/app/utils/url.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,10 @@ export const getPathWithLocale = (shortCode, buildUrl, opts = {}) => {
142142
let {pathname, search} = location
143143

144144
// Remove basename from pathname if present before processing
145-
const envBasePath = getEnvBasePath()
146-
if (envBasePath && pathname.startsWith(envBasePath)) {
147-
pathname = pathname.substring(envBasePath.length)
145+
// Respect showBasename config setting
146+
const basenameToRemove = getBasename()
147+
if (basenameToRemove && pathname.startsWith(basenameToRemove)) {
148+
pathname = pathname.substring(basenameToRemove.length)
148149
}
149150

150151
// sanitize the site from current url if existing
@@ -189,8 +190,8 @@ export const getPathWithLocale = (shortCode, buildUrl, opts = {}) => {
189190
// Add basename for locale selection URLs since they bypass React Router
190191
// (React Router handles basename for navigation via basename prop)
191192
// Respect showBasename config setting
192-
const basename = getBasename()
193-
return basename ? `${basename}${newUrl}` : newUrl
193+
const basenameToAdd = getBasename()
194+
return basenameToAdd ? `${basenameToAdd}${newUrl}` : newUrl
194195
}
195196

196197
/**

packages/template-retail-react-app/app/utils/url.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import {
2020
} from '@salesforce/retail-react-app/app/utils/url'
2121
import {getUrlConfig} from '@salesforce/retail-react-app/app/utils/site-utils'
2222
import mockConfig from '@salesforce/retail-react-app/config/mocks/default'
23+
import {getBasename} from '@salesforce/pwa-kit-react-sdk/ssr/universal/utils'
24+
import {getConfig} from '@salesforce/retail-react-app/../../app/utils/utils'
2325

2426
afterEach(() => {
2527
jest.clearAllMocks()
@@ -48,6 +50,14 @@ jest.mock('./site-utils', () => {
4850
}
4951
})
5052

53+
jest.mock('@salesforce/pwa-kit-react-sdk/ssr/universal/utils', () => {
54+
const original = jest.requireActual('@salesforce/pwa-kit-react-sdk/ssr/universal/utils')
55+
return {
56+
...original,
57+
getBasename: jest.fn(() => '')
58+
}
59+
})
60+
5161
describe('buildUrlSet returns the expected set of urls', () => {
5262
test('when no values are passed in', () => {
5363
const set = buildUrlSet()
@@ -187,6 +197,33 @@ describe('getPathWithLocale', () => {
187197
const relativeUrl = getPathWithLocale('en-GB', buildUrl, {location})
188198
expect(relativeUrl).toBe(`/`)
189199
})
200+
201+
describe('getPathWithLocale with basename and showBasename', () => {
202+
test('should include basename when showBasename is true', () => {
203+
const basename = '/test-base'
204+
205+
getBasename.mockReturnValue(basename)
206+
getConfig.mockReturnValue({
207+
...mockConfig,
208+
app: {
209+
...mockConfig.app,
210+
url: {
211+
...mockConfig.app.url,
212+
showBasename: true
213+
}
214+
}
215+
})
216+
217+
// Location pathname should have a basename when showBasename is true
218+
const location = new URL(
219+
`http://localhost:3000${basename}/uk/it-IT/category/newarrivals-womens`
220+
)
221+
const buildUrl = createUrlTemplate(mockConfig.app, 'uk', 'it-IT')
222+
223+
const relativeUrl = getPathWithLocale('fr-FR', buildUrl, {location})
224+
expect(relativeUrl).toBe(`${basename}/uk/fr/category/newarrivals-womens`)
225+
})
226+
})
190227
})
191228

192229
describe('createUrlTemplate tests', () => {

0 commit comments

Comments
 (0)