From 5eec2a08764ac9f6b6a79939fe6b45218a90d1ee Mon Sep 17 00:00:00 2001 From: "A.Ottr" Date: Fri, 10 Jan 2025 10:45:21 +0100 Subject: [PATCH] test(container): add test for tracking init in cookie policy modal (#14800) ref: MANAGER-16559 Signed-off-by: Dustin Kroger --- .../src/cookie-policy/CookiePolicy.spec.tsx | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 packages/manager/apps/container/src/cookie-policy/CookiePolicy.spec.tsx diff --git a/packages/manager/apps/container/src/cookie-policy/CookiePolicy.spec.tsx b/packages/manager/apps/container/src/cookie-policy/CookiePolicy.spec.tsx new file mode 100644 index 000000000000..b6711ab3e49c --- /dev/null +++ b/packages/manager/apps/container/src/cookie-policy/CookiePolicy.spec.tsx @@ -0,0 +1,92 @@ +import { render, waitFor } from '@testing-library/react'; +import { it, vi, describe, expect } from 'vitest'; +import { initShell } from '@ovh-ux/shell'; +import CookiePolicy from './CookiePolicy'; +import { Environment, User } from '@ovh-ux/manager-config'; +import ApplicationContext from '@/context/application.context'; +import { useCookies } from 'react-cookie'; + +const onValidate = vi.fn(); +const trackingInit = vi.fn().mockResolvedValue(undefined); +const trackingSetEnabled = vi.fn().mockResolvedValue(undefined); +const trackingOnConsentModalDisplay = vi.fn().mockResolvedValue(undefined); + +const renderCookiePolicy = async () => { + const shell = await initShell(); + const environment = shell.getPlugin('environment').getEnvironment(); + render( + + + + ); +}; + +const mockedShell = (region: string) => ({ + getPlugin: (plugin: string) => ({ + environment: { + getEnvironment: () => ({ + user: { + ovhSubsidiary: region, + } as User, + getRegion: () => region, + } as Environment), + }, + tracking: { + setRegion: vi.fn(), + init: trackingInit, + setEnabled: trackingSetEnabled, + onConsentModalDisplay: trackingOnConsentModalDisplay, + }, + }[plugin]), +}); + +vi.mock('@ovh-ux/shell'); +vi.mock('react-cookie'); + + +describe('CookiePolicy.component', () => { + + afterEach(() => { + vi.restoreAllMocks(); + }); + + it.each([ + ['EU', 'valid'], + ['US', 'invalid'], + ])('should init tracking if region is %s and cookie is %s', async (region, cookieValidity) => { + + const cookieValue = cookieValidity === 'valid' ? '1' : '0'; + vi.mocked(useCookies).mockReturnValue([{ MANAGER_TRACKING: cookieValue, }, vi.fn(), vi.fn()]); + (await import('@ovh-ux/shell')).initShell = vi.fn().mockResolvedValue(mockedShell(region)); + void renderCookiePolicy(); + await waitFor(() => { + expect(trackingInit).toHaveBeenCalledWith(true); + expect(trackingSetEnabled).not.toHaveBeenCalled(); + expect(trackingOnConsentModalDisplay).not.toHaveBeenCalled(); + }, { timeout: 2000 }); + }); + + it('should show consent modal if cookie is null and region is not US', async () => { + + vi.mocked(useCookies).mockReturnValue([{ MANAGER_TRACKING: null, }, vi.fn(), vi.fn()]); + (await import('@ovh-ux/shell')).initShell = vi.fn().mockResolvedValue(mockedShell('EU')); + void renderCookiePolicy(); + await waitFor(() => { + expect(trackingInit).not.toHaveBeenCalled(); + expect(trackingSetEnabled).not.toHaveBeenCalled(); + expect(trackingOnConsentModalDisplay).toHaveBeenCalled(); + }, { timeout: 2000 }); + }); + + it('should disable tracking plugin if cookie is invalid and region is not US', async () => { + + vi.mocked(useCookies).mockReturnValue([{ MANAGER_TRACKING: '0', }, vi.fn(), vi.fn()]); + (await import('@ovh-ux/shell')).initShell = vi.fn().mockResolvedValue(mockedShell('EU')); + void renderCookiePolicy(); + await waitFor(() => { + expect(trackingInit).not.toHaveBeenCalled(); + expect(trackingSetEnabled).toHaveBeenCalledWith(false); + expect(trackingOnConsentModalDisplay).not.toHaveBeenCalled(); + }, { timeout: 2000 }); + }); +});