From 6baf0b6009682b866a183a9499128f1b1e8ec687 Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Wed, 17 Jul 2024 17:34:36 +0200 Subject: [PATCH] add test --- examples/todomvc/tests/new.spec.ts | 49 ++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 examples/todomvc/tests/new.spec.ts diff --git a/examples/todomvc/tests/new.spec.ts b/examples/todomvc/tests/new.spec.ts new file mode 100644 index 0000000000..c660ea3baf --- /dev/null +++ b/examples/todomvc/tests/new.spec.ts @@ -0,0 +1,49 @@ +/* eslint-disable notice/notice */ + +import { test, expect } from '@playwright/test'; +import type { Page } from '@playwright/test'; + +test.describe.configure({ mode: 'parallel' }); + +test.beforeEach(async ({ page }) => { + await page.goto('https://demo.playwright.dev/todomvc'); +}); + +const TODO_ITEMS = [ + 'buy some cheese', + 'feed the cat', + 'book a doctors appointment' +]; + +test.describe('New Todo', () => { + test('should allow me to add todo items', async ({ page }) => { + // create a new todo locator + const newTodo = page.getByPlaceholder('What needs to be done?'); + // Create 1st todo. + await newTodo.fill(TODO_ITEMS[0]); + await newTodo.press('Enter'); + + // Make sure the list only has one todo item. + await expect(page.getByTestId('todo-title')).toHaveText([ + TODO_ITEMS[0] + ]); + + // Create 2nd todo. + await newTodo.fill(TODO_ITEMS[1]); + await newTodo.press('Enter'); + + // Make sure the list now has two todo items. + await expect(page.getByTestId('todo-title')).toHaveText([ + TODO_ITEMS[0], + TODO_ITEMS[1], + ]); + + await checkNumberOfTodosInLocalStorage(page, 2); + }); +}); + +async function checkNumberOfTodosInLocalStorage(page: Page, expected: number) { + return await page.waitForFunction(e => { + return JSON.parse(localStorage['react-todos']).length === e; + }, expected); +}