Skip to content

Commit

Permalink
Added end-to-end tests
Browse files Browse the repository at this point in the history
  • Loading branch information
felixgirault committed Oct 10, 2024
1 parent 9e6e012 commit f44a41c
Showing 1 changed file with 161 additions and 0 deletions.
161 changes: 161 additions & 0 deletions e2e/orejime.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import {test, expect} from '@playwright/test';

test.describe('Orejime', () => {
test.beforeEach(async ({page}) => {
await page.goto('/');
});

test('should show a notice', async ({page}) => {
const notice = await page.locator('.orejime-Notice');
await expect(notice).toBeVisible();
});

test('should navigate to the notice first', async ({page}) => {
await page.keyboard.press('Tab');
const firstButton = await page.locator('.orejime-Notice-saveButton');
await expect(firstButton).toBeVisible();
});

test('should accept all cookies from the notice', async ({page, context}) => {
await page.locator('.orejime-Notice-saveButton').click();
const notice = await page.locator('.orejime-Notice');
await expect(notice).not.toBeVisible();

const cookies = await context.cookies();
const orejimeCookie = cookies.find(({name}) => name === 'orejime');
const consents = JSON.parse(orejimeCookie!.value);

expect(consents).toEqual(expect.objectContaining({
'inline-tracker': true,
'external-tracker': true,
'disabled-by-default': true,
'always-on': true
}));
});

test('should reject all cookies from the notice', async ({page, context}) => {
await page.locator('.orejime-Notice-declineButton').click();
const notice = await page.locator('.orejime-Notice');
await expect(notice).not.toBeVisible();

const cookies = await context.cookies();
const orejimeCookie = cookies.find(({name}) => name === 'orejime');
const consents = JSON.parse(orejimeCookie!.value);

expect(consents).toEqual(expect.objectContaining({
'inline-tracker': false,
'external-tracker': false,
'disabled-by-default': false,
'always-on': true
}));
});

test('should open a modal', async ({page}) => {
await page.locator('.orejime-Notice-learnMoreButton').click();
const notice = await page.locator('.orejime-Notice');
await expect(notice).toBeVisible();

const modal = await page.locator('.orejime-Modal');
await expect(modal).toBeVisible();
await expect(notice).toBeVisible();
});

test('should close the modal via the close button', async ({page}) => {
await page.locator('.orejime-Notice-learnMoreButton').click();
const modal = await page.locator('.orejime-Modal');
await expect(modal).toBeVisible();

await page.locator('.orejime-Modal-closeButton').click();
await expect(modal).not.toBeVisible();

const notice = await page.locator('.orejime-Notice');
await expect(notice).toBeVisible();
});

test('should close the modal via the overlay', async ({page}) => {
await page.locator('.orejime-Notice-learnMoreButton').click();
const modal = await page.locator('.orejime-Modal');
await expect(modal).toBeVisible();

await page.locator('.orejime-ModalOverlay').click();
await expect(modal).not.toBeVisible();

const notice = await page.locator('.orejime-Notice');
await expect(notice).toBeVisible();
});

test('should close the modal via `Escape` key', async ({page}) => {
await page.locator('.orejime-Notice-learnMoreButton').click();
const modal = await page.locator('.orejime-Modal');
await expect(modal).toBeVisible();

await page.keyboard.press('Escape');
await expect(modal).not.toBeVisible();

const notice = await page.locator('.orejime-Notice');
await expect(notice).toBeVisible();
});

test('should move focus after closing the modal', async ({page}) => {
const openModalButton = await page.locator('.orejime-Notice-learnMoreButton');
openModalButton.click();

const modal = await page.locator('.orejime-Modal');
await expect(modal).toBeVisible();

await page.keyboard.press('Escape');
await expect(openModalButton).toBeFocused();
});

test('should accept all cookies from the modal', async ({page, context}) => {
await page.locator('.orejime-Notice-learnMoreButton').click();
await page.locator('.orejime-AppToggles-enableAll').click();

const checkbox = await page.locator('#orejime-app-item-disabled-by-default');
await expect(checkbox).toBeChecked();

const mandatoryCheckbox = await page.locator('#orejime-app-item-always-on');
await expect(mandatoryCheckbox).toBeChecked();

await page.locator('.orejime-Modal-saveButton').click();
const cookies = await context.cookies();
const orejimeCookie = cookies.find(({name}) => name === 'orejime');
const consents = JSON.parse(orejimeCookie!.value);

expect(consents).toEqual(expect.objectContaining({
'inline-tracker': true,
'external-tracker': true,
'disabled-by-default': true,
'always-on': true
}));

const notice = await page.locator('.orejime-Notice');
await expect(notice).not.toBeVisible();
});

test('should reject all cookies from the modal', async ({page, context}) => {
await page.locator('.orejime-Notice-learnMoreButton').click();
await page.locator('.orejime-AppToggles-disableAll').click();

const checkbox = await page.locator('#orejime-app-item-disabled-by-default');
await expect(checkbox).not.toBeChecked();

const mandatoryCheckbox = await page.locator('#orejime-app-item-always-on');
await expect(mandatoryCheckbox).toBeChecked();

await page.locator('.orejime-Modal-saveButton').click();
const cookies = await context.cookies();
const orejimeCookie = cookies.find(({name}) => name === 'orejime');
const consents = JSON.parse(orejimeCookie!.value);

expect(consents).toEqual(expect.objectContaining({
'inline-tracker': false,
'external-tracker': false,
'disabled-by-default': false,
'always-on': true
}));

const notice = await page.locator('.orejime-Notice');
await expect(notice).not.toBeVisible();
});
});

0 comments on commit f44a41c

Please sign in to comment.