Skip to content

Commit 4d5aeb7

Browse files
authored
test: add e2e tests for timezone feature (#23)
* test: add e2e tests for timezone feature * test(e2e): trim playwright config * docs: add note about tests * test: no default timeout * ci: fix artifact name
1 parent ec4183a commit 4d5aeb7

12 files changed

+1551
-6
lines changed

.github/workflows/test-and-release.yaml

+13-1
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,22 @@ jobs:
3939
PUBLIC_VERSION: ${{ github.sha }}
4040
run: npm run build
4141

42+
- name: Install playwright
43+
run: npx playwright install
44+
45+
- name: Start server
46+
run: npx node-static build > server.log 2>&1 &
47+
48+
- name: Wait for server to be up
49+
run: until nc -w 10 127.0.0.1 8080; do sleep 1; done
50+
51+
- name: Run end-to-end tests
52+
run: npm run test:e2e
53+
4254
- uses: actions/upload-artifact@v3
4355
if: always()
4456
with:
45-
name: ${GITHUB_REPOSITORY}-${{ github.sha }}
57+
name: ${{ env.GITHUB_REPOSITORY }}-${{ github.sha }}
4658
path: build
4759

4860
- name: Semantic release

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
node_modules/
66
npm-debug.log
77
build/
8+
test-results/
9+
playwright-report/
10+
e2e-tests-out

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ conferences!
2020

2121
npm start
2222

23+
### Run the tests
24+
25+
Unit tests can be run using
26+
27+
npm test
28+
29+
End-to-end tests can be run using
30+
31+
npm run test-e2e
32+
2333
## Architecture decision records (ADRs)
2434

2535
see [./adr](./adr).

e2e-tests/.eslintrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"no-restricted-imports": "off"
4+
}
5+
}

e2e-tests/example.spec.ts

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { BrowserContext, chromium, expect, test } from '@playwright/test'
2+
import path from 'path'
3+
import sinon from 'sinon'
4+
5+
test.describe('Adding an item', () => {
6+
let context: BrowserContext
7+
test.beforeAll(async () => {
8+
const browser = await chromium.launch() // Or 'firefox' or 'webkit'.
9+
context = await browser.newContext({
10+
locale: 'no-NO',
11+
timezoneId: 'Europe/Berlin',
12+
})
13+
// See https://github.com/microsoft/playwright/issues/6347#issuecomment-965887758
14+
await context.addInitScript({
15+
path: path.join(process.cwd(), 'node_modules/sinon/pkg/sinon.js'),
16+
})
17+
// Auto-enable sinon right away
18+
// and enforce our "current" date
19+
await context.addInitScript(() => {
20+
const clock = sinon.useFakeTimers()
21+
clock.setSystemTime(new Date('2022-03-11T12:00:00Z'))
22+
;(window as any).__clock = clock
23+
})
24+
})
25+
26+
test('should allow me to add todo items', async () => {
27+
const page = await context.newPage()
28+
await page.goto('http://localhost:8080/')
29+
30+
// Create a new entry
31+
await page.locator('button[name="edit-schedule"]').click()
32+
await page.locator('input[name="conference-day"]').fill('2022-03-12')
33+
await page
34+
.locator('select[name="conference-timezone"]')
35+
.selectOption('Asia/Tokyo')
36+
await page.locator('input[name="session-hour"]').fill('9')
37+
await page.locator('input[name="session-minute"]').fill('45')
38+
39+
const sessionName = 'Breakfast for Champions'
40+
const sessionNameInput = page.locator('input[name="session-name"]')
41+
await sessionNameInput.fill(sessionName)
42+
await sessionNameInput.press('Enter')
43+
44+
await page.locator('button[name="save-schedule"]').click()
45+
46+
// Table should have the new entry
47+
await expect(page.locator('table tbody')).toContainText(sessionName)
48+
49+
// Find the row of the new entry
50+
const tr = page
51+
.locator(`table tbody td:has-text("${sessionName}")`) // td
52+
.locator(`xpath=ancestor::tr`)
53+
const conferenceTime = tr.locator('td:first-child')
54+
await expect(conferenceTime).toContainText('09:45')
55+
56+
// Time Zone difference between Berlin and Tokyo on 2022-03-12 is 8 hours
57+
58+
const localTime = tr.locator('td:nth-child(2)')
59+
await expect(localTime).toContainText('01:45')
60+
})
61+
62+
test('should display the users time zone', async () => {
63+
const page = await context.newPage()
64+
await page.goto('http://localhost:8080/')
65+
66+
await expect(
67+
page.locator('table thead th:nth-child(2) small'),
68+
).toContainText('Berlin')
69+
})
70+
})

e2e-tests/tsconfig.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"module": "esnext",
5+
"moduleResolution": "Node",
6+
"sourceMap": true,
7+
"outDir": "../e2e-tests-out",
8+
"esModuleInterop": true,
9+
"baseUrl": "../src",
10+
"strict": true
11+
},
12+
"include": ["../playwright.config.ts", "../e2e-tests"]
13+
}

0 commit comments

Comments
 (0)