Skip to content

Commit

Permalink
Add tests w/ Playwright (#1)
Browse files Browse the repository at this point in the history
* Add tests w/ Playwright

* ..
  • Loading branch information
mikker authored Dec 29, 2023
1 parent 983a5ab commit 9f7b8b2
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 3 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Playwright Tests
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- name: Install dependencies
run: npm install
- name: Install Playwright Browsers
run: npx playwright install --with-deps
- name: Run Playwright tests
run: npx playwright test
- uses: actions/upload-artifact@v3
if: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 30
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
package-lock.json
.vercel
node_modules/
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
"src"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 0",
"dev": "five-server --open=false"
"dev": "five-server --open=false",
"test": "playwright test",
"test:serve": "serve -l 3030"
},
"keywords": [],
"author": "Mikkel Malmberg (@mikker)",
"license": "MIT",
"devDependencies": {
"five-server": "^0.3.1"
"@playwright/test": "^1.40.1",
"five-server": "^0.3.1",
"serve": "^14.2.1"
},
"repository": {
"type": "git",
Expand Down
78 changes: 78 additions & 0 deletions playwright.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// @ts-check
const { defineConfig, devices } = require("@playwright/test");

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();

/**
* @see https://playwright.dev/docs/test-configuration
*/
module.exports = defineConfig({
testDir: "./tests",
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: process.env.CI ? "github" : "list",
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
baseURL: "http://localhost:3030/tests",

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: "on-first-retry",
},

/* Configure projects for major browsers */
projects: [
{
name: "chromium",
use: { ...devices["Desktop Chrome"] },
},

// {
// name: "firefox",
// use: { ...devices["Desktop Firefox"] },
// },

{
name: "webkit",
use: { ...devices["Desktop Safari"] },
},

/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },
// {
// name: 'Mobile Safari',
// use: { ...devices['iPhone 12'] },
// },

/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],

/* Run your local dev server before starting the tests */
webServer: {
command: "npm run test:serve",
url: "http://localhost:3030/tests",
reuseExistingServer: !process.env.CI,
},
});
32 changes: 32 additions & 0 deletions tests/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Sourdough Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body {
background: #eee;
}
</style>
<link rel="stylesheet" href="../src/sourdough-toast.css" />
<script type="module">
import { Sourdough, toast } from "../src/sourdough-toast.js";

const sourdough = new Sourdough();

window.addEventListener("DOMContentLoaded", () => {
sourdough.boot();
});

window.toast = toast;
</script>
</head>
<body>
<button onclick="toast('Basic toast')">Basic</button>
<button onclick="toast.success('Success toast')">Success</button>
<button onclick="toast.info('Info toast')">Info</button>
<button onclick="toast.warning('Warning toast')">Warning</button>
<button onclick="toast.error('Error toast')">Error</button>
</body>
</html>
51 changes: 51 additions & 0 deletions tests/sourdough-toast.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// @ts-check
const { test, expect } = require("@playwright/test");

test.beforeEach(async ({ page }) => {
await page.goto("/tests");
});

test("has title", async ({ page }) => {
await expect(page).toHaveTitle("Sourdough Test");
});

test("a basic toast can render and then disappear after duration", async ({
page,
}) => {
await page.getByRole("button", { name: "Basic" }).click();

await expect(page.locator("[data-sourdough-toast]")).toHaveCount(1);
await expect(page.locator("[data-sourdough-toast]")).toHaveCount(0);
});

test("only renders max toasts", async ({ page }) => {
await page.getByRole("button", { name: "Basic" }).click();
await page.getByRole("button", { name: "Basic" }).click();
await page.getByRole("button", { name: "Basic" }).click();
await page.getByRole("button", { name: "Basic" }).click();

await expect(page.locator("[data-sourdough-toast]")).toHaveCount(3);
});

test("toast have types", async ({ page }) => {
await page.getByRole("button", { name: "Success" }).click();
await expect(page.getByText("Success toast", { exact: true })).toHaveCount(1);

await page.getByRole("button", { name: "Error" }).click();
await expect(page.getByText("Error toast", { exact: true })).toHaveCount(1);

await page.getByRole("button", { name: "Warning" }).click();
await expect(page.getByText("Warning toast", { exact: true })).toHaveCount(1);

await page.getByRole("button", { name: "Info" }).click();
await expect(page.getByText("Info toast", { exact: true })).toHaveCount(1);
});

test("toasts don't dismiss when hovered", async ({ page }) => {
await page.getByRole("button", { name: "Basic" }).click();

await page.hover("[data-sourdough-toast]");
await new Promise((resolve) => setTimeout(resolve, 5000));

await expect(page.locator("[data-sourdough-toast]")).toHaveCount(1);
});

0 comments on commit 9f7b8b2

Please sign in to comment.