-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(container): add test for tracking init in cookie policy modal (#…
…14800) ref: MANAGER-16559 Signed-off-by: Dustin Kroger <[email protected]>
- Loading branch information
Showing
1 changed file
with
92 additions
and
0 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
packages/manager/apps/container/src/cookie-policy/CookiePolicy.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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( | ||
<ApplicationContext.Provider value={{ shell, environment }}> | ||
<CookiePolicy onValidate={onValidate} shell={shell} /> | ||
</ApplicationContext.Provider> | ||
); | ||
}; | ||
|
||
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 }); | ||
}); | ||
}); |