Skip to content

Commit effaaa3

Browse files
committed
test(avatar): refactor to use the util fn in pw tests
1 parent 89f017b commit effaaa3

File tree

1 file changed

+13
-48
lines changed

1 file changed

+13
-48
lines changed
Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,56 @@
1-
import AxeBuilder from '@axe-core/playwright'
21
import { expect, test } from '@playwright/test'
3-
import { gotoStory } from '../utils'
2+
import { gotoStory, testA11yWithAttachedResults } from '../utils'
43

54
test.describe('basic variant', () => {
6-
test('has avatar image', async ({ page }) => {
5+
test.beforeEach(async ({ page }) => {
76
await gotoStory('avatar', 'basic', page)
8-
97
await page.getByAltText('avatar').waitFor()
10-
8+
})
9+
test('has avatar image', async ({ page }) => {
1110
expect(page.getByAltText('avatar')).toHaveAttribute(
1211
'src',
1312
'https://avatars.githubusercontent.com/u/1846056?v=4',
1413
)
15-
1614
expect(page).toHaveScreenshot()
1715
})
1816
test('has no a11y violations', async ({ page }, testInfo) => {
19-
await gotoStory('avatar', 'basic', page)
20-
21-
await page.getByAltText('avatar').waitFor()
22-
23-
const accessibilityScanResults = await new AxeBuilder({ page })
24-
.include('[data-scope="avatar"][data-part="root"]')
25-
.analyze()
26-
27-
await testInfo.attach('accessibility-scan-results', {
28-
body: JSON.stringify(accessibilityScanResults, null, 2),
29-
contentType: 'application/json',
30-
})
31-
17+
const accessibilityScanResults = await testA11yWithAttachedResults(page, testInfo, 'avatar')
3218
expect(accessibilityScanResults.violations).toEqual([])
3319
})
3420
})
3521

3622
test.describe('closed variant', () => {
37-
test('displays avatar with name', async ({ page }) => {
23+
test.beforeEach(async ({ page }) => {
3824
await gotoStory('avatar', 'closed', page)
39-
4025
await page.getByAltText('Christian').waitFor()
26+
})
27+
test('displays avatar with name', async ({ page }) => {
4128
expect(page.getByAltText('Christian')).toHaveAttribute(
4229
'src',
4330
'https://avatars.githubusercontent.com/u/1846056?v=4',
4431
)
4532
expect(page).toHaveScreenshot()
4633
})
4734
test('has no a11y violations', async ({ page }, testInfo) => {
48-
await gotoStory('avatar', 'closed', page)
49-
await page.getByAltText('Christian').waitFor()
50-
51-
const accessibilityScanResults = await new AxeBuilder({ page })
52-
.include('[data-scope="avatar"][data-part="root"]')
53-
.analyze()
54-
55-
await testInfo.attach('accessibility-scan-results', {
56-
body: JSON.stringify(accessibilityScanResults, null, 2),
57-
contentType: 'application/json',
58-
})
59-
35+
const accessibilityScanResults = await testA11yWithAttachedResults(page, testInfo, 'avatar')
6036
expect(accessibilityScanResults.violations).toEqual([])
6137
})
6238
})
6339

6440
test.describe('root-provided variant', () => {
65-
test('has avatar image', async ({ page }) => {
41+
test.beforeEach(async ({ page }) => {
6642
await gotoStory('avatar', 'root-provider', page)
67-
6843
await page.getByAltText('avatar').waitFor()
44+
})
45+
test('has avatar image', async ({ page }) => {
6946
expect(page.getByAltText('avatar')).toHaveAttribute(
7047
'src',
7148
'https://avatars.githubusercontent.com/u/1846056?v=4',
7249
)
7350
expect(page).toHaveScreenshot()
7451
})
7552
test('has no a11y violations', async ({ page }, testInfo) => {
76-
await gotoStory('avatar', 'root-provider', page)
77-
78-
await page.getByAltText('avatar').waitFor()
79-
80-
const accessibilityScanResults = await new AxeBuilder({ page })
81-
.include('[data-scope="avatar"][data-part="root"]')
82-
.analyze()
83-
84-
await testInfo.attach('accessibility-scan-results', {
85-
body: JSON.stringify(accessibilityScanResults, null, 2),
86-
contentType: 'application/json',
87-
})
88-
53+
const accessibilityScanResults = await testA11yWithAttachedResults(page, testInfo, 'avatar')
8954
expect(accessibilityScanResults.violations).toEqual([])
9055
})
9156
})

0 commit comments

Comments
 (0)