-
Notifications
You must be signed in to change notification settings - Fork 884
Expand file tree
/
Copy pathavatar.test.tsx
More file actions
126 lines (102 loc) · 4.26 KB
/
Copy pathavatar.test.tsx
File metadata and controls
126 lines (102 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
Copyright (c) Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import * as React from 'react';
import { render, fireEvent, getByText } from '@testing-library/react';
import { Avatar } from '..';
// manually trigger src load error. jsdom will not fire a load event
// https://github.com/jsdom/jsdom/issues/1816#issuecomment-310106280
// @ts-ignore
function triggerLoadError(container) {
const img = container.querySelector('img');
if (img) fireEvent.error(img);
}
// @ts-ignore
function triggerLoad(container) {
const img = container.querySelector('img');
if (img) fireEvent.load(img);
}
describe('Avatar', () => {
it('applies expected accessibility attributes to img element', () => {
const name = 'user name';
const { container } = render(<Avatar name={name} src="valid-img-src.png" />);
const image = container.querySelector('img')?.getAttribute('alt');
expect(image).toBe(name);
});
it('does not render img element if image fails to load', () => {
const { container } = render(<Avatar name="user name" src="invalid-img-src.png" />);
const before = container.querySelector('img');
expect(before).not.toBeNull();
triggerLoadError(container);
const after = container.querySelector('img')?.getAttribute('test-style');
const style = JSON.parse(after || '');
expect(style.display).toBe('none');
});
it('applies expected accessibility attributes to root when loaded', () => {
const name = 'user name';
const { container } = render(<Avatar name={name} src="invalid-img-src.png" />);
triggerLoad(container);
const root = container.querySelector('div');
expect(root?.getAttribute('aria-label')).toBeNull();
expect(root?.getAttribute('role')).toBeNull();
});
it('applies expected accessibility attributes to root if image fails to load', () => {
const name = 'user name';
const { container } = render(<Avatar name={name} src="invalid-img-src.png" />);
triggerLoadError(container);
const root = container.querySelector('div');
expect(root?.getAttribute('aria-label')).toBe(name);
expect(root?.getAttribute('role')).toBe('img');
});
it('renders user first 2 initials when image fails to load', () => {
const name = 'user name';
const { container } = render(<Avatar name={name} src="invalid-img-src.png" />);
triggerLoadError(container);
getByText(container, 'UN');
});
it('only renders 2 initials if more names exist when image fails to load', () => {
const name = 'user name surname';
const { container } = render(<Avatar name={name} src="invalid-img-src.png" />);
triggerLoadError(container);
getByText(container, 'UN');
});
it('only renders 1 initial if one name exists when image fails to load', () => {
const name = 'user';
const { container } = render(<Avatar name={name} src="invalid-img-src.png" />);
triggerLoadError(container);
getByText(container, 'U');
});
it('only renders initial from name if src is not provided', () => {
const name = 'user';
const { container } = render(<Avatar name={name} />);
getByText(container, 'U');
});
it('renders explicit initials prop instead of deriving from name', () => {
const { container } = render(<Avatar name="user name" initials="XY" />);
getByText(container, 'XY');
});
it('resets noImageAvailable flag when src is updated', () => {
function TestCase() {
const [showImg, setShowImg] = React.useState(false);
return (
<div>
<Avatar name="user name" src={showImg ? 'valid-img-src.png' : undefined} />
<button onClick={() => setShowImg(true)}>toggle</button>
</div>
);
}
const { container } = render(<TestCase />);
triggerLoad(container);
const before = container.querySelector('img')?.getAttribute('test-style');
const beforeStyle = JSON.parse(before || '');
expect(beforeStyle.display).toBe('none');
const button = container.querySelector('button');
if (button) fireEvent.click(button);
triggerLoad(container);
const after = container.querySelector('img')?.getAttribute('test-style');
const afterStyle = JSON.parse(after || '');
expect(afterStyle.display).toBe('block');
});
});