From 4ff14c8731746c3badd1e372d56daadc6d4fe03a Mon Sep 17 00:00:00 2001 From: Syed Sajjad Hussain Shah <52817156+syedsajjadkazmii@users.noreply.github.com> Date: Thu, 16 Nov 2023 10:44:29 +0500 Subject: [PATCH] fix: fix duplicate mfe context calls and cookie set exc (#1103) --- src/common-components/data/reducers.js | 6 +-- src/data/tests/cookies.test.js | 52 ++++++++++++++++++++++++++ src/data/utils/cookies.js | 12 +++--- 3 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 src/data/tests/cookies.test.js diff --git a/src/common-components/data/reducers.js b/src/common-components/data/reducers.js index 841ab966..c2150cda 100644 --- a/src/common-components/data/reducers.js +++ b/src/common-components/data/reducers.js @@ -2,9 +2,7 @@ import { THIRD_PARTY_AUTH_CONTEXT, THIRD_PARTY_AUTH_CONTEXT_CLEAR_ERROR_MSG } fr import { COMPLETE_STATE, FAILURE_STATE, PENDING_STATE } from '../../data/constants'; export const defaultState = { - fieldDescriptions: { - fields: {}, - }, + fieldDescriptions: {}, optionalFields: { fields: {}, extended_profile: [], @@ -33,7 +31,7 @@ const reducer = (state = defaultState, action = {}) => { case THIRD_PARTY_AUTH_CONTEXT.SUCCESS: { return { ...state, - fieldDescriptions: action.payload.fieldDescriptions.fields, + fieldDescriptions: action.payload.fieldDescriptions?.fields, optionalFields: action.payload.optionalFields, thirdPartyAuthContext: action.payload.thirdPartyAuthContext, thirdPartyAuthApiStatus: COMPLETE_STATE, diff --git a/src/data/tests/cookies.test.js b/src/data/tests/cookies.test.js new file mode 100644 index 00000000..fd448dd2 --- /dev/null +++ b/src/data/tests/cookies.test.js @@ -0,0 +1,52 @@ +import { getConfig } from '@edx/frontend-platform'; +import Cookies from 'universal-cookie'; + +import { setCookie } from '../utils'; + +// Mock getConfig function +jest.mock('@edx/frontend-platform', () => ({ + getConfig: jest.fn(), +})); + +// Mock Cookies class +jest.mock('universal-cookie'); + +describe('setCookie function', () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should set a cookie with default options', () => { + getConfig.mockReturnValue({ SESSION_COOKIE_DOMAIN: 'example.com' }); + + setCookie('testCookie', 'testValue'); + + expect(Cookies).toHaveBeenCalled(); + expect(Cookies).toHaveBeenCalledWith(); + expect(Cookies.prototype.set).toHaveBeenCalledWith('testCookie', 'testValue', { + domain: 'example.com', + path: '/', + }); + }); + + it('should set a cookie with specified expiry', () => { + getConfig.mockReturnValue({ SESSION_COOKIE_DOMAIN: 'example.com' }); + + const expiry = new Date('2023-12-31'); + setCookie('testCookie', 'testValue', expiry); + + expect(Cookies).toHaveBeenCalled(); + expect(Cookies).toHaveBeenCalledWith(); + expect(Cookies.prototype.set).toHaveBeenCalledWith('testCookie', 'testValue', { + domain: 'example.com', + path: '/', + expires: expiry, + }); + }); + + it('should not set a cookie if cookieName is undefined', () => { + setCookie(undefined, 'testValue'); + + expect(Cookies).not.toHaveBeenCalled(); + }); +}); diff --git a/src/data/utils/cookies.js b/src/data/utils/cookies.js index 6290e373..1aad2858 100644 --- a/src/data/utils/cookies.js +++ b/src/data/utils/cookies.js @@ -2,10 +2,12 @@ import { getConfig } from '@edx/frontend-platform'; import Cookies from 'universal-cookie'; export default function setCookie(cookieName, cookieValue, cookieExpiry) { - const cookies = new Cookies(); - const options = { domain: getConfig().SESSION_COOKIE_DOMAIN, path: '/' }; - if (cookieExpiry) { - options.expires = cookieExpiry; + if (cookieName) { // To avoid setting getting exception when setting cookie with undefined names. + const cookies = new Cookies(); + const options = { domain: getConfig().SESSION_COOKIE_DOMAIN, path: '/' }; + if (cookieExpiry) { + options.expires = cookieExpiry; + } + cookies.set(cookieName, cookieValue, options); } - cookies.set(cookieName, cookieValue, options); }