From cef2b764eb553cf17d8e1ec628a28fc98681bd51 Mon Sep 17 00:00:00 2001 From: Laura Martinez Garcia Date: Mon, 29 Jan 2024 10:06:28 +0100 Subject: [PATCH] fix: mocked self object in tests --- .../tagging/store/__tests__/actions.spec.ts | 17 ++++---- .../__tests__/session.service.spec.ts | 40 +++++++++++-------- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/packages/x-components/src/x-modules/tagging/store/__tests__/actions.spec.ts b/packages/x-components/src/x-modules/tagging/store/__tests__/actions.spec.ts index f93e41888a..80f65c95b6 100644 --- a/packages/x-components/src/x-modules/tagging/store/__tests__/actions.spec.ts +++ b/packages/x-components/src/x-modules/tagging/store/__tests__/actions.spec.ts @@ -1,8 +1,6 @@ import { createLocalVue } from '@vue/test-utils'; import Vuex, { Store } from 'vuex'; import { TaggingRequest } from '@empathyco/x-types'; -import { BrowserStorageService } from '@empathyco/x-storage-service'; -import { DefaultSessionService } from '@empathyco/x-utils/src/index'; import { getTaggingResponseStub } from '../../../../__stubs__/tagging-response-stubs.factory'; import { XComponentsAdapterDummy } from '../../../../__tests__/adapter.dummy'; import { installNewXPlugin } from '../../../../__tests__/utils'; @@ -18,6 +16,16 @@ describe('testing tagging module actions', () => { localVue.config.productionTip = false; // Silent production console messages. localVue.use(Vuex); + const mockedSessionId = 'beabb84c-c0aa-4d3a-911b-54779f7f4a8f'; + const selfSpy = jest.spyOn(self, 'self', 'get'); + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + selfSpy.mockImplementation(() => ({ + crypto: { + randomUUID: () => mockedSessionId + } + })); + const store: SafeStore = new Store(taggingXStoreModule as any); installNewXPlugin({ adapter, store }, localVue); @@ -51,11 +59,6 @@ describe('testing tagging module actions', () => { it('should track with a session id if the consent is true', async () => { resetTaggingStateWith(store, { consent: true }); - const storageKey = DefaultSessionService.SESSION_ID_KEY; - const sessionStorage = new BrowserStorageService(localStorage); - - const session1Id = 'beabb84c-c0aa-4d3a-911b-54779f7f4a8f'; - sessionStorage.setItem(storageKey, session1Id, 1000); await store.dispatch('track', queryTagging); expect(adapter.tagging).toHaveBeenCalled(); diff --git a/packages/x-utils/src/services/__tests__/session.service.spec.ts b/packages/x-utils/src/services/__tests__/session.service.spec.ts index 5b49e8db81..b9fa8bfc23 100644 --- a/packages/x-utils/src/services/__tests__/session.service.spec.ts +++ b/packages/x-utils/src/services/__tests__/session.service.spec.ts @@ -5,6 +5,15 @@ describe('testing session id service', () => { const mockedStorageService = new InMemoryStorageService(); const sessionService = new DefaultSessionService(mockedStorageService, 1); const storageKey = DefaultSessionService.SESSION_ID_KEY; + const mockedSessionId = 'beabb84c-c0aa-4d3a-911b-54779f7f4a8f'; + const selfSpy = jest.spyOn(self, 'self', 'get'); + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + selfSpy.mockImplementation(() => ({ + crypto: { + randomUUID: () => mockedSessionId + } + })); const getItemSpy = jest.spyOn(mockedStorageService, 'getItem'); const setItemSpy = jest.spyOn(mockedStorageService, 'setItem'); @@ -17,12 +26,10 @@ describe('testing session id service', () => { }); it('creates a new session id when calling getSessionId and a session does not exist', () => { - const sessionId = 'beabb84c-c0aa-4d3a-911b-54779f7f4a8f'; - const session = mockedStorageService.getItem(storageKey) ?? sessionId; - mockedStorageService.setItem(storageKey, sessionId); + const session = sessionService.getSessionId(); expect(getItemSpy).toHaveBeenCalledTimes(1); expect(setItemSpy).toHaveBeenCalledTimes(1); - expect(setItemSpy).toHaveBeenCalledWith(storageKey, expect.any(String)); + expect(setItemSpy).toHaveBeenCalledWith(storageKey, expect.any(String), expect.any(Number)); expect(session).toMatch(/^[A-Za-z0-9_-]+$/); }); @@ -33,25 +40,26 @@ describe('testing session id service', () => { }); it('returns an existing session id', () => { - const session1Id = 'beabb84c-c0aa-4d3a-911b-54779f7f4a8f'; - mockedStorageService.setItem(storageKey, session1Id); + const session1 = sessionService.getSessionId(); const session2 = sessionService.getSessionId(); - expect(session1Id).toEqual(session2); + expect(session1).toEqual(session2); }); it('generates a new session after ttl runs out', () => { - const sessionStorage = new BrowserStorageService(localStorage); - const session1Id = 'beabb84c-c0aa-4d3a-911b-54779f7f4a8f'; - const session2Id = '350b1b9a-7bd2-499a-9506-e904c4121226'; - const session1 = sessionStorage.getItem(storageKey) ?? session1Id; - sessionStorage.setItem(storageKey, session1, 1000); + const sessionService = new DefaultSessionService(new BrowserStorageService(localStorage), 1000); + const session1 = sessionService.getSessionId(); jest.advanceTimersByTime(999); - const session2 = sessionStorage.getItem(storageKey); - sessionStorage.setItem(storageKey, session1, 1000); + const session2 = sessionService.getSessionId(); expect(session1).toEqual(session2); jest.advanceTimersByTime(1001); - const session3 = sessionStorage.getItem(storageKey) ?? session2Id; - sessionStorage.setItem(storageKey, session1, 1000); + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + selfSpy.mockImplementation(() => ({ + crypto: { + randomUUID: () => 'beabb84c-c0aa-4d3a-552n-54779f7f4a8f' + } + })); + const session3 = sessionService.getSessionId(); expect(session1).not.toEqual(session3); }); });