Skip to content

Commit cec3dbc

Browse files
committed
fix tests
1 parent 3104d12 commit cec3dbc

File tree

1 file changed

+63
-3
lines changed

1 file changed

+63
-3
lines changed

src/static/helpers/slasHelper.test.ts

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,55 @@ describe('Guest user flow', () => {
379379
expect(accessToken).toBe(expectedTokenResponse);
380380
});
381381

382+
test('throw warning for invalid params', async () => {
383+
// Spy on console.warn
384+
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation();
385+
386+
const expectedTokenBody = {
387+
body: {
388+
client_id: 'client_id',
389+
channel_id: 'site_id',
390+
code: 'J2lHm0cgXmnXpwDhjhLoyLJBoUAlBfxDY-AhjqGMC-o',
391+
code_verifier: expect.stringMatching(/./) as string,
392+
grant_type: 'authorization_code_pkce',
393+
redirect_uri: 'redirect_uri',
394+
usid: '048adcfb-aa93-4978-be9e-09cb569fdcb9',
395+
dnt: 'false',
396+
},
397+
};
398+
const mockSlasClient = createMockSlasClient();
399+
const {shortCode, organizationId} = mockSlasClient.clientConfig.parameters;
400+
401+
nock(`https://${shortCode}.api.commercecloud.salesforce.com`)
402+
.get(`/shopper/auth/v1/organizations/${organizationId}/oauth2/authorize`)
403+
.query(true)
404+
.reply(303, {response_body: 'response_body'}, {location: url});
405+
406+
const accessToken = await slasHelper.loginGuestUser(mockSlasClient, {
407+
redirectURI: parameters.redirectURI,
408+
dnt: false,
409+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
410+
// @ts-ignore
411+
hello: 'world',
412+
});
413+
414+
// Assert the warning was logged
415+
expect(consoleWarnSpy).toHaveBeenCalledWith(
416+
expect.stringContaining('Invalid Parameter for authorizeCustomer: hello')
417+
);
418+
419+
expect(getAccessTokenMock).toBeCalledWith({
420+
...expectedTokenBody,
421+
parameters: {
422+
hello: 'world',
423+
},
424+
});
425+
expect(accessToken).toBe(expectedTokenResponse);
426+
427+
// Restore the original console.warn
428+
consoleWarnSpy.mockRestore();
429+
});
430+
382431
test('can pass custom params, headers on public guest', async () => {
383432
const expectedTokenBody = {
384433
body: {
@@ -527,7 +576,9 @@ describe('Registered B2C user flow', () => {
527576
expect(getAccessTokenMock).toBeCalledWith(expectedTokenBody);
528577
});
529578

530-
test('can pass custom parameters, headers and body field', async () => {
579+
test('can pass custom parameters, headers and body field, and throw warning for invalid params', async () => {
580+
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation();
581+
531582
// slasClient is copied and tries to make an actual API call
532583
const mockSlasClient = createMockSlasClient();
533584
const {shortCode, organizationId} = mockSlasClient.clientConfig.parameters;
@@ -549,6 +600,9 @@ describe('Registered B2C user flow', () => {
549600
redirectURI: 'redirect_uri',
550601
dnt: false,
551602
c_color: 'red',
603+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
604+
// @ts-ignore intentionally passing invalid param
605+
invalid_param: 'invalid param',
552606
},
553607
{
554608
headers: {c_header: 'test'},
@@ -559,12 +613,18 @@ describe('Registered B2C user flow', () => {
559613
},
560614
}
561615
);
562-
616+
expect(consoleWarnSpy).toHaveBeenCalledWith(
617+
expect.stringContaining(
618+
'Invalid Parameter for authenticateCustomer: invalid_param'
619+
)
620+
);
563621
expect(getAccessTokenMock).toBeCalledWith({
564622
...expectedTokenBody,
565623
headers: {c_header: 'test'},
566-
parameters: {c_color: 'red'},
624+
parameters: {c_color: 'red', invalid_param: 'invalid param'},
567625
});
626+
// Restore the original console.warn
627+
consoleWarnSpy.mockRestore();
568628
});
569629

570630
test('uses code challenge and authorization header to generate auth code with slas private client', async () => {

0 commit comments

Comments
 (0)