Skip to content

Commit

Permalink
refactor: simplify mocking by removing reliance on HAR files
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalinDe committed Dec 14, 2023
1 parent be17e64 commit 9b36614
Show file tree
Hide file tree
Showing 12 changed files with 14 additions and 270 deletions.
7 changes: 1 addition & 6 deletions web/frontend/tests/footer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { expect, test } from '@playwright/test';
import { default as i18n } from 'i18next';
import { initI18n, setUp } from './shared';
import { UPDATE, mockPersonalInfo } from './mocks';
import { mockPersonalInfo } from './mocks';

initI18n();

test.beforeEach(async ({ page }) => {
if (UPDATE === true) {
return;
}
await mockPersonalInfo(page);
await setUp(page, '/about');
});

test('Assert copyright notice is visible', async ({ page }) => {
test.skip(UPDATE === true, 'Do not run regular tests when updating HAR files');
const footerCopyright = await page.getByTestId('footerCopyright');
await expect(footerCopyright).toBeVisible();
await expect(footerCopyright).toHaveText(
Expand All @@ -23,7 +19,6 @@ test('Assert copyright notice is visible', async ({ page }) => {
});

test('Assert version information is visible', async ({ page }) => {
test.skip(UPDATE === true, 'Do not run regular tests when updating HAR files');
const footerVersion = await page.getByTestId('footerVersion');
await expect(footerVersion).toBeVisible();
await expect(footerVersion).toHaveText(
Expand Down
10 changes: 1 addition & 9 deletions web/frontend/tests/formIndex.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { expect, test } from '@playwright/test';
import { default as i18n } from 'i18next';
import { assertHasFooter, assertHasNavBar, initI18n, setUp } from './shared';
import { UPDATE, mockPersonalInfo, mockEvoting } from './mocks';
import { mockPersonalInfo, mockEvoting } from './mocks';

Check failure on line 4 in web/frontend/tests/formIndex.spec.ts

View workflow job for this annotation

GitHub Actions / Web frontend Lint

Member 'mockEvoting' of the import declaration should be sorted alphabetically

initI18n();

test.beforeEach(async ({ page }) => {
if (UPDATE === true) {
return;
}
// mock empty list per default
await mockEvoting(page);
await mockPersonalInfo(page);
Expand All @@ -18,34 +15,29 @@ test.beforeEach(async ({ page }) => {
// main elements

test('Assert navigation bar is present', async ({ page }) => {
test.skip(UPDATE === true, 'Do not run regular tests when updating HAR files');
await assertHasNavBar(page);
});

test('Assert footer is present', async ({ page }) => {
test.skip(UPDATE === true, 'Do not run regular tests when updating HAR files');
await assertHasFooter(page);
});

// pagination bar

test('Assert pagination bar is present', async ({ page }) => {
test.skip(UPDATE === true, 'Do not run regular tests when updating HAR files');
await expect(page.getByTestId('navPagination')).toBeVisible();
await expect(page.getByRole('button', { name: i18n.t('previous') })).toBeVisible();
await expect(page.getByRole('button', { name: i18n.t('next') })).toBeVisible();
});

test('Assert pagination works correctly for empty list', async ({ page }) => {
test.skip(UPDATE === true, 'Do not run regular tests when updating HAR files');
await expect(page.getByTestId('navPaginationMessage')).toHaveText(i18n.t('showingNOverMOfXResults', { n: 1, m: 1, x: 0 }));

Check failure on line 34 in web/frontend/tests/formIndex.spec.ts

View workflow job for this annotation

GitHub Actions / Web frontend Lint

Replace `i18n.t('showingNOverMOfXResults',·{·n:·1,·m:·1,·x:·0·})` with `⏎····i18n.t('showingNOverMOfXResults',·{·n:·1,·m:·1,·x:·0·})⏎··`
for (let key of ['next', 'previous']) {
await expect(page.getByRole('button', { name: i18n.t(key) })).toBeDisabled();
}
});

test('Assert pagination works correctly for non-empty list', async ({ page }) => {
test.skip(UPDATE === true, 'Do not run regular tests when updating HAR files');
// mock non-empty list w/ 11 elements i.e. 2 pages
await mockEvoting(page, false);
await page.reload();
Expand Down
14 changes: 0 additions & 14 deletions web/frontend/tests/hars/123456/get_dev_login.har

This file was deleted.

61 changes: 0 additions & 61 deletions web/frontend/tests/hars/123456/personal_info.har

This file was deleted.

14 changes: 0 additions & 14 deletions web/frontend/tests/hars/789012/get_dev_login.har

This file was deleted.

61 changes: 0 additions & 61 deletions web/frontend/tests/hars/789012/personal_info.har

This file was deleted.

This file was deleted.

60 changes: 0 additions & 60 deletions web/frontend/tests/hars/anonymous/personal_info.har

This file was deleted.

21 changes: 11 additions & 10 deletions web/frontend/tests/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
export const SCIPER_ADMIN = '123456';
export const SCIPER_USER = '789012';
export const UPDATE = false;

export async function mockPersonalInfo(page: any, sciper?: string) {
// clear current mock
await page.unroute('/api/personal_info');
await page.routeFromHAR(`./tests/hars/${sciper ?? 'anonymous'}/personal_info.har`, {
url: '/api/personal_info',
update: UPDATE,
await page.route('/api/personal_info', async (route) => {
if (sciper) {
route.fulfill({ path: `./tests/json/personal_info/${sciper}.json` });
}

Check failure on line 10 in web/frontend/tests/mocks.ts

View workflow job for this annotation

GitHub Actions / Web frontend Lint

Delete `⏎···`
else {
route.fulfill({ status: 401, contentType: 'text/html', body: 'Unauthenticated'})

Check failure on line 12 in web/frontend/tests/mocks.ts

View workflow job for this annotation

GitHub Actions / Web frontend Lint

Replace `})` with `·});`
}
});
}

export async function mockGetDevLogin(page: any) {
await page.routeFromHAR(`./tests/hars/${SCIPER_ADMIN}/get_dev_login.har`, {
url: `/api/get_dev_login/${SCIPER_ADMIN}`,
update: UPDATE,
await page.route(`/api/get_dev_login/${SCIPER_ADMIN}`, async (route) => {
await route.fulfill({});
});
await page.routeFromHAR(`./tests/hars/${SCIPER_USER}/get_dev_login.har`, {
url: `/api/get_dev_login/${SCIPER_USER}`,
update: UPDATE,
await page.route(`/api/get_dev_login/${SCIPER_USER}`, async (route) => {
await route.fulfill({});
});
if (
process.env.REACT_APP_SCIPER_ADMIN !== undefined &&
Expand Down
Loading

0 comments on commit 9b36614

Please sign in to comment.