|
7 | 7 | import Auth, {AuthData} from './' |
8 | 8 | import {waitFor} from '@testing-library/react' |
9 | 9 | import jwt from 'jsonwebtoken' |
10 | | -import {helpers, ShopperCustomersTypes} from 'commerce-sdk-isomorphic' |
| 10 | +import {helpers, ShopperCustomersTypes, ShopperLogin} from 'commerce-sdk-isomorphic' |
11 | 11 | import * as utils from '../utils' |
12 | 12 | import {SLAS_SECRET_PLACEHOLDER} from '../constant' |
13 | 13 | import {ShopperLoginTypes} from 'commerce-sdk-isomorphic' |
14 | 14 | import { |
15 | 15 | DEFAULT_SLAS_REFRESH_TOKEN_REGISTERED_TTL, |
16 | 16 | DEFAULT_SLAS_REFRESH_TOKEN_GUEST_TTL |
17 | 17 | } from './index' |
18 | | -import {RequireKeys} from '../hooks/types' |
| 18 | +import {ApiClientConfigParams, RequireKeys} from '../hooks/types' |
19 | 19 |
|
20 | 20 | const baseCustomer: RequireKeys<ShopperCustomersTypes.Customer, 'login'> = { |
21 | 21 | customerId: 'customerId', |
@@ -803,3 +803,87 @@ describe('Auth', () => { |
803 | 803 | }) |
804 | 804 | }) |
805 | 805 | }) |
| 806 | + |
| 807 | +describe('Auth service sends credentials fetch option to the ShopperLogin API', () => { |
| 808 | + beforeEach(() => { |
| 809 | + jest.clearAllMocks() |
| 810 | + }) |
| 811 | + |
| 812 | + test('Adds fetch options with credentials when not defined in config', async () => { |
| 813 | + const auth = new Auth(config) |
| 814 | + await auth.loginGuestUser() |
| 815 | + |
| 816 | + // Ensure the helper method was called |
| 817 | + expect(helpers.loginGuestUser).toHaveBeenCalled() |
| 818 | + expect(helpers.loginGuestUser).toHaveBeenCalledTimes(1) |
| 819 | + |
| 820 | + // Check that the correct parameters were passed to the helper |
| 821 | + const callArguments = (helpers.loginGuestUser as jest.Mock).mock.calls[0] |
| 822 | + expect(callArguments).toBeDefined() |
| 823 | + expect(callArguments.length).toBeGreaterThan(0) |
| 824 | + |
| 825 | + const shopperLogin: ShopperLogin<ApiClientConfigParams> = callArguments[0] |
| 826 | + expect(shopperLogin).toBeDefined() |
| 827 | + expect(shopperLogin.clientConfig).toBeDefined() |
| 828 | + expect(shopperLogin.clientConfig.fetchOptions).toBeDefined() |
| 829 | + |
| 830 | + // Ensure fetch options include the expected credentials |
| 831 | + expect(shopperLogin.clientConfig.fetchOptions.credentials).toBe('same-origin') |
| 832 | + }) |
| 833 | + |
| 834 | + test('Does not override the credentials in fetch options if already exists', async () => { |
| 835 | + const configWithFetchOptions = { |
| 836 | + ...config, |
| 837 | + fetchOptions: { |
| 838 | + credentials: 'include' |
| 839 | + } |
| 840 | + } |
| 841 | + const auth = new Auth(configWithFetchOptions) |
| 842 | + await auth.loginGuestUser() |
| 843 | + |
| 844 | + // Ensure the helper method was called |
| 845 | + expect(helpers.loginGuestUser).toHaveBeenCalled() |
| 846 | + expect(helpers.loginGuestUser).toHaveBeenCalledTimes(1) |
| 847 | + |
| 848 | + // Check that the correct parameters were passed to the helper |
| 849 | + const callArguments = (helpers.loginGuestUser as jest.Mock).mock.calls[0] |
| 850 | + expect(callArguments).toBeDefined() |
| 851 | + expect(callArguments.length).toBeGreaterThan(0) |
| 852 | + |
| 853 | + const shopperLogin: ShopperLogin<ApiClientConfigParams> = callArguments[0] |
| 854 | + expect(shopperLogin).toBeDefined() |
| 855 | + expect(shopperLogin.clientConfig).toBeDefined() |
| 856 | + expect(shopperLogin.clientConfig.fetchOptions).toBeDefined() |
| 857 | + |
| 858 | + // Ensure fetch options include the expected credentials |
| 859 | + expect(shopperLogin.clientConfig.fetchOptions.credentials).toBe('include') |
| 860 | + }) |
| 861 | + |
| 862 | + test('Adds credentials to the fetch options if it is missing', async () => { |
| 863 | + const configWithFetchOptions = { |
| 864 | + ...config, |
| 865 | + fetchOptions: { |
| 866 | + cache: 'no-cache' |
| 867 | + } |
| 868 | + } |
| 869 | + const auth = new Auth(configWithFetchOptions) |
| 870 | + await auth.loginGuestUser() |
| 871 | + |
| 872 | + // Ensure the helper method was called |
| 873 | + expect(helpers.loginGuestUser).toHaveBeenCalled() |
| 874 | + expect(helpers.loginGuestUser).toHaveBeenCalledTimes(1) |
| 875 | + |
| 876 | + // Check that the correct parameters were passed to the helper |
| 877 | + const callArguments = (helpers.loginGuestUser as jest.Mock).mock.calls[0] |
| 878 | + expect(callArguments).toBeDefined() |
| 879 | + expect(callArguments.length).toBeGreaterThan(0) |
| 880 | + |
| 881 | + const shopperLogin: ShopperLogin<ApiClientConfigParams> = callArguments[0] |
| 882 | + expect(shopperLogin).toBeDefined() |
| 883 | + expect(shopperLogin.clientConfig).toBeDefined() |
| 884 | + expect(shopperLogin.clientConfig.fetchOptions).toBeDefined() |
| 885 | + |
| 886 | + // Ensure fetch options include the expected credentials |
| 887 | + expect(shopperLogin.clientConfig.fetchOptions.credentials).toBe('same-origin') |
| 888 | + }) |
| 889 | +}) |
0 commit comments