Skip to content

Commit 55a1ce5

Browse files
Josep Martinsjosepmartins
andauthored
refactor(Avatar): move files to folder, add e2e tests and stories (#2904)
* refactor(Avatar): move files to folder and add stories * Update generated/components.json * Adjust source and data in docs * Add e2e tests * test(vrt): update snapshots * Opt the component into storybook tests * Add e2e test * test(vrt): update snapshots --------- Co-authored-by: josepmartins <josepmartins@users.noreply.github.com>
1 parent 6db8c8a commit 55a1ce5

38 files changed

+250
-38
lines changed
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading

docs/content/Avatar.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
title: Avatar
33
status: Alpha
44
componentId: avatar
5-
source: https://github.com/primer/react/blob/main/src/Avatar.tsx
5+
source: https://github.com/primer/react/blob/main/src/Avatar
66
---
77

8-
import data from '../../src/Avatar.docs.json'
8+
import data from '../../src/Avatar/Avatar.docs.json'
99

1010
import {Avatar, Box} from '@primer/react'
1111

docs/content/Flash.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
componentId: flash
33
title: Flash
44
status: Alpha
5-
source: https://github.com/primer/react/blob/main/src/Flash.tsx
5+
source: https://github.com/primer/react/blob/main/src/Flash
66
---
77

88
import data from '../../src/Flash/Flash.docs.json'

e2e/components/Avatar.test.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import {test, expect} from '@playwright/test'
2+
import {visit} from '../test-helpers/storybook'
3+
import {themes} from '../test-helpers/themes'
4+
5+
test.describe('Avatar', () => {
6+
test.describe('Default', () => {
7+
for (const theme of themes) {
8+
test.describe(theme, () => {
9+
test('default @vrt', async ({page}) => {
10+
await visit(page, {
11+
id: 'components-avatar--default',
12+
globals: {
13+
colorScheme: theme,
14+
},
15+
})
16+
17+
// Default state
18+
expect(await page.screenshot()).toMatchSnapshot(`Avatar.Default.${theme}.png`)
19+
})
20+
21+
test('axe @aat', async ({page}) => {
22+
await visit(page, {
23+
id: 'components-avatar--default',
24+
globals: {
25+
colorScheme: theme,
26+
},
27+
})
28+
await expect(page).toHaveNoViolations({
29+
rules: {
30+
'color-contrast': {
31+
enabled: theme !== 'dark_dimmed',
32+
},
33+
},
34+
})
35+
})
36+
})
37+
}
38+
})
39+
40+
test.describe('Size', () => {
41+
for (const theme of themes) {
42+
test.describe(theme, () => {
43+
test('default @vrt', async ({page}) => {
44+
await visit(page, {
45+
id: 'components-avatar-features--size',
46+
globals: {
47+
colorScheme: theme,
48+
},
49+
})
50+
51+
// Default state
52+
expect(await page.screenshot()).toMatchSnapshot(`Avatar.Size.${theme}.png`)
53+
})
54+
55+
test('axe @aat', async ({page}) => {
56+
await visit(page, {
57+
id: 'components-avatar-features--size',
58+
globals: {
59+
colorScheme: theme,
60+
},
61+
})
62+
await expect(page).toHaveNoViolations({
63+
rules: {
64+
'color-contrast': {
65+
enabled: theme !== 'dark_dimmed',
66+
},
67+
},
68+
})
69+
})
70+
})
71+
}
72+
})
73+
74+
test.describe('Square', () => {
75+
for (const theme of themes) {
76+
test.describe(theme, () => {
77+
test('default @vrt', async ({page}) => {
78+
await visit(page, {
79+
id: 'components-avatar-features--square',
80+
globals: {
81+
colorScheme: theme,
82+
},
83+
})
84+
85+
// Default state
86+
expect(await page.screenshot()).toMatchSnapshot(`Avatar.Square.${theme}.png`)
87+
})
88+
89+
test('axe @aat', async ({page}) => {
90+
await visit(page, {
91+
id: 'components-avatar-features--square',
92+
globals: {
93+
colorScheme: theme,
94+
},
95+
})
96+
await expect(page).toHaveNoViolations({
97+
rules: {
98+
'color-contrast': {
99+
enabled: theme !== 'dark_dimmed',
100+
},
101+
},
102+
})
103+
})
104+
})
105+
}
106+
})
107+
})

generated/components.json

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -87,38 +87,6 @@
8787
}
8888
]
8989
},
90-
"avatar": {
91-
"id": "avatar",
92-
"name": "Avatar",
93-
"status": "alpha",
94-
"a11yReviewed": false,
95-
"stories": [],
96-
"props": [
97-
{
98-
"name": "alt",
99-
"type": "string",
100-
"defaultValue": "''",
101-
"description": "Provide alt text when the avatar is used without a name next to it."
102-
},
103-
{
104-
"name": "size",
105-
"type": "number",
106-
"defaultValue": "20",
107-
"description": "The size of the avatar in pixels."
108-
},
109-
{
110-
"name": "square",
111-
"type": "boolean",
112-
"defaultValue": "false",
113-
"description": "If true, the avatar will be square instead of circular."
114-
},
115-
{
116-
"name": "sx",
117-
"type": "SystemStyleObject"
118-
}
119-
],
120-
"subcomponents": []
121-
},
12290
"avatar_pair": {
12391
"id": "avatar_pair",
12492
"name": "AvatarPair",
@@ -2220,6 +2188,38 @@
22202188
}
22212189
]
22222190
},
2191+
"avatar": {
2192+
"id": "avatar",
2193+
"name": "Avatar",
2194+
"status": "alpha",
2195+
"a11yReviewed": false,
2196+
"stories": [],
2197+
"props": [
2198+
{
2199+
"name": "alt",
2200+
"type": "string",
2201+
"defaultValue": "''",
2202+
"description": "Provide alt text when the avatar is used without a name next to it."
2203+
},
2204+
{
2205+
"name": "size",
2206+
"type": "number",
2207+
"defaultValue": "20",
2208+
"description": "The size of the avatar in pixels."
2209+
},
2210+
{
2211+
"name": "square",
2212+
"type": "boolean",
2213+
"defaultValue": "false",
2214+
"description": "If true, the avatar will be square instead of circular."
2215+
},
2216+
{
2217+
"name": "sx",
2218+
"type": "SystemStyleObject"
2219+
}
2220+
],
2221+
"subcomponents": []
2222+
},
22232223
"branch_name": {
22242224
"id": "branch_name",
22252225
"name": "BranchName",

script/generate-e2e-tests.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,25 @@ const components = new Map([
7171
],
7272
},
7373
],
74+
[
75+
'Avatar',
76+
{
77+
stories: [
78+
{
79+
id: 'components-avatar--default',
80+
name: 'Default',
81+
},
82+
{
83+
id: 'components-avatar-features--size',
84+
name: 'Size',
85+
},
86+
{
87+
id: 'components-avatar-features--square',
88+
name: 'Square',
89+
},
90+
],
91+
},
92+
],
7493
[
7594
'BranchName',
7695
{
File renamed without changes.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React from 'react'
2+
import {ComponentMeta} from '@storybook/react'
3+
import Avatar from './Avatar'
4+
5+
export default {
6+
title: 'Components/Avatar/Features',
7+
component: Avatar,
8+
} as ComponentMeta<typeof Avatar>
9+
10+
export const Square = () => <Avatar square src="https://avatars.githubusercontent.com/primer" />
11+
12+
export const Size = () => (
13+
<div>
14+
<Avatar size={4} src="https://avatars.githubusercontent.com/u/92997159?v=4" />
15+
<Avatar size={8} src="https://avatars.githubusercontent.com/u/92997159?v=4" />
16+
<Avatar size={12} src="https://avatars.githubusercontent.com/u/92997159?v=4" />
17+
<Avatar size={16} src="https://avatars.githubusercontent.com/u/92997159?v=4" />
18+
<Avatar size={20} src="https://avatars.githubusercontent.com/u/92997159?v=4" />
19+
<Avatar size={24} src="https://avatars.githubusercontent.com/u/92997159?v=4" />
20+
<Avatar size={28} src="https://avatars.githubusercontent.com/u/92997159?v=4" />
21+
<Avatar size={32} src="https://avatars.githubusercontent.com/u/92997159?v=4" />
22+
<Avatar size={40} src="https://avatars.githubusercontent.com/u/92997159?v=4" />
23+
<Avatar size={48} src="https://avatars.githubusercontent.com/u/92997159?v=4" />
24+
<Avatar size={56} src="https://avatars.githubusercontent.com/u/92997159?v=4" />
25+
<Avatar size={64} src="https://avatars.githubusercontent.com/u/92997159?v=4" />
26+
</div>
27+
)

src/Avatar/Avatar.stories.tsx

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import React from 'react'
2+
import {ComponentMeta, ComponentStory} from '@storybook/react'
3+
import Avatar from './Avatar'
4+
5+
export default {
6+
title: 'Components/Avatar',
7+
component: Avatar,
8+
} as ComponentMeta<typeof Avatar>
9+
10+
export const Default = () => <Avatar src="https://avatars.githubusercontent.com/u/92997159?v=4" />
11+
12+
export const Playground: ComponentStory<typeof Avatar> = args => (
13+
<Avatar src="https://avatars.githubusercontent.com/u/92997159?v=4" {...args} />
14+
)
15+
16+
Playground.args = {
17+
size: '20',
18+
}
19+
20+
Playground.argTypes = {
21+
alt: {
22+
controls: false,
23+
table: {
24+
disable: true,
25+
},
26+
},
27+
ref: {
28+
controls: false,
29+
table: {
30+
disable: true,
31+
},
32+
},
33+
forwardedAs: {
34+
controls: false,
35+
table: {
36+
disable: true,
37+
},
38+
},
39+
sx: {
40+
controls: false,
41+
table: {
42+
disable: true,
43+
},
44+
},
45+
as: {
46+
controls: false,
47+
table: {
48+
disable: true,
49+
},
50+
},
51+
theme: {
52+
controls: false,
53+
table: {
54+
disable: true,
55+
},
56+
},
57+
}

src/Avatar.tsx renamed to src/Avatar/Avatar.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import styled from 'styled-components'
2-
import {get} from './constants'
3-
import sx, {SxProp} from './sx'
4-
import {ComponentProps} from './utils/types'
2+
import {get} from '../constants'
3+
import sx, {SxProp} from '../sx'
4+
import {ComponentProps} from '../utils/types'
55

66
type StyledAvatarProps = {
77
/** Sets the width and height of the avatar. */

src/Avatar/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default, AvatarProps} from './Avatar'

src/__tests__/storybook.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const ROOT_DIRECTORY = path.resolve(__dirname, '..', '..')
88
// TODO: Remove this allowlist when all components use the new story format
99
const allowlist = [
1010
'ActionList',
11+
'Avatar',
1112
'Breadcrumbs',
1213
'BranchName',
1314
'Button',

0 commit comments

Comments
 (0)