Skip to content

Commit

Permalink
test(e2e): fix mock of get list of todos
Browse files Browse the repository at this point in the history
  • Loading branch information
leosuncin committed Mar 27, 2022
1 parent d1ed850 commit 5dd9d44
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 27 deletions.
24 changes: 11 additions & 13 deletions e2e/todo/create.spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
import { faker } from '@faker-js/faker';
import { expect, test } from '@playwright/test';

import type { Todo } from '../../src/features/todo/todoSlice';

const todos: Todo[] = Array.from({ length: 9 }, () => ({
id: faker.datatype.uuid(),
task: faker.lorem.sentence(),
completed: faker.datatype.boolean(),
}));
import db from '../../db.json';

test.describe('create a todo', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/');
await page.route('**/api/todos?**', (route) => {
const url = new URL(route.request().url());
const page = +url.searchParams.get('_page');
const limit = 10;

await page.route('**/api/todos?**', (route) =>
route.fulfill({
return route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify(todos),
}),
);
body: JSON.stringify(db.todos.slice(limit * (page - 1), limit * page)),
});
});

await page.route('**/api/todos', (route) =>
route.fulfill({
Expand All @@ -28,6 +24,8 @@ test.describe('create a todo', () => {
body: route.request().postData(),
}),
);

await page.goto('/');
});

test('submit the form', async ({ page }) => {
Expand Down
4 changes: 3 additions & 1 deletion e2e/todo/filter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ const completedCount = todos.length - activeCount;

test.describe('Filter todos by', () => {
test.beforeEach(async ({ page }) => {
await page.route('**/api/todos**', (route) =>
await page.route('**/api/todos?**', (route) =>
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify(todos),
}),
);

await page.route('**/api/todos/**', (route) =>
route.fulfill({
status: 200,
contentType: 'application/json',
body: '{}',
}),
);

await page.goto('/', { waitUntil: 'networkidle' });
});

Expand Down
10 changes: 3 additions & 7 deletions e2e/todo/list.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { faker } from '@faker-js/faker';
import { expect, test } from '@playwright/test';

import type { Todo } from '../../src/features/todo/todoSlice';
import db from '../../db.json';

test.describe('list and paginate', () => {
let countTotal: number;

test.beforeEach(async ({ page }) => {
await page.route('**/api/todos**', async (route) => {
const todos: Todo[] = Array.from({ length: 10 }, (_, index) => ({
id: faker.datatype.uuid(),
task: `${String(index + 1).padStart(2, '0')} ${faker.lorem.sentence()}`,
completed: faker.datatype.boolean(),
}));
await page.route('**/api/todos?**', async (route) => {
const todos = faker.random.arrayElements(db.todos, 10);
countTotal = faker.datatype.number({ min: 42, max: 104 });

return route.fulfill({
Expand Down
12 changes: 6 additions & 6 deletions e2e/todo/update.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { faker } from '@faker-js/faker';
import { expect, test } from '@playwright/test';

import type { Todo } from '../../src/features/todo/todoSlice';
let list: Todo[] = Array.from({ length: 7 }, () => ({
id: faker.datatype.uuid(),
task: faker.lorem.sentence(),
completed: false,
}));
import db from '../../db.json';

let list = faker.random.arrayElements(
db.todos.filter(({ completed }) => !completed),
7,
);
let todo = faker.random.arrayElement(list);

test.describe('update a todo', () => {
Expand Down

0 comments on commit 5dd9d44

Please sign in to comment.