>
110 | `)
111 | })
112 | })
113 |
--------------------------------------------------------------------------------
/src/__tests__/to-have-accessible-description.js:
--------------------------------------------------------------------------------
1 | import {render} from './helpers/test-utils'
2 |
3 | describe('.toHaveAccessibleDescription', () => {
4 | it('works with the link title attribute', () => {
5 | const {queryByTestId} = render(`
6 |
10 | `)
11 |
12 | const link = queryByTestId('link')
13 | expect(link).toHaveAccessibleDescription()
14 | expect(link).toHaveAccessibleDescription('A link to start over')
15 | expect(link).not.toHaveAccessibleDescription('Home page')
16 | expect(() => {
17 | expect(link).toHaveAccessibleDescription('Invalid description')
18 | }).toThrow(/expected element to have accessible description/i)
19 | expect(() => {
20 | expect(link).not.toHaveAccessibleDescription()
21 | }).toThrow(/expected element not to have accessible description/i)
22 |
23 | const extraLink = queryByTestId('extra-link')
24 | expect(extraLink).not.toHaveAccessibleDescription()
25 | expect(() => {
26 | expect(extraLink).toHaveAccessibleDescription()
27 | }).toThrow(/expected element to have accessible description/i)
28 | })
29 |
30 | it('works with aria-describedby attributes', () => {
31 | const {queryByTestId} = render(`
32 |
33 |
34 |
35 |
The logo of Our Company
36 |
37 | `)
38 |
39 | const avatar = queryByTestId('avatar')
40 | expect(avatar).not.toHaveAccessibleDescription()
41 | expect(() => {
42 | expect(avatar).toHaveAccessibleDescription('User profile pic')
43 | }).toThrow(/expected element to have accessible description/i)
44 |
45 | const logo = queryByTestId('logo')
46 | expect(logo).not.toHaveAccessibleDescription('Company logo')
47 | expect(logo).toHaveAccessibleDescription('The logo of Our Company')
48 | expect(logo).toHaveAccessibleDescription(/logo of our company/i)
49 | expect(logo).toHaveAccessibleDescription(
50 | expect.stringContaining('logo of Our Company'),
51 | )
52 | expect(() => {
53 | expect(logo).toHaveAccessibleDescription("Our company's logo")
54 | }).toThrow(/expected element to have accessible description/i)
55 | expect(() => {
56 | expect(logo).not.toHaveAccessibleDescription('The logo of Our Company')
57 | }).toThrow(/expected element not to have accessible description/i)
58 | })
59 |
60 | it('works with aria-description attribute', () => {
61 | const {queryByTestId} = render(`
62 |
63 | `)
64 |
65 | const logo = queryByTestId('logo')
66 | expect(logo).not.toHaveAccessibleDescription('Company logo')
67 | expect(logo).toHaveAccessibleDescription('The logo of Our Company')
68 | expect(logo).toHaveAccessibleDescription(/logo of our company/i)
69 | expect(logo).toHaveAccessibleDescription(
70 | expect.stringContaining('logo of Our Company'),
71 | )
72 | expect(() => {
73 | expect(logo).toHaveAccessibleDescription("Our company's logo")
74 | }).toThrow(/expected element to have accessible description/i)
75 | expect(() => {
76 | expect(logo).not.toHaveAccessibleDescription('The logo of Our Company')
77 | }).toThrow(/expected element not to have accessible description/i)
78 | })
79 |
80 | it('handles multiple ids', () => {
81 | const {queryByTestId} = render(`
82 |
83 |
First description
84 |
Second description
85 |
Third description
86 |
87 |
88 |
89 | `)
90 |
91 | expect(queryByTestId('multiple')).toHaveAccessibleDescription(
92 | 'First description Second description Third description',
93 | )
94 | expect(queryByTestId('multiple')).toHaveAccessibleDescription(
95 | /Second description Third/,
96 | )
97 | expect(queryByTestId('multiple')).toHaveAccessibleDescription(
98 | expect.stringContaining('Second description Third'),
99 | )
100 | expect(queryByTestId('multiple')).toHaveAccessibleDescription(
101 | expect.stringMatching(/Second description Third/),
102 | )
103 | expect(queryByTestId('multiple')).not.toHaveAccessibleDescription(
104 | 'Something else',
105 | )
106 | expect(queryByTestId('multiple')).not.toHaveAccessibleDescription('First')
107 | })
108 |
109 | it('normalizes whitespace', () => {
110 | const {queryByTestId} = render(`
111 |
112 | Step
113 | 1
114 | of
115 | 4
116 |
117 |
118 | And
119 | extra
120 | description
121 |
122 |
123 | `)
124 |
125 | expect(queryByTestId('target')).toHaveAccessibleDescription(
126 | 'Step 1 of 4 And extra description',
127 | )
128 | })
129 | })
130 |
--------------------------------------------------------------------------------
/src/__tests__/to-have-attribute.js:
--------------------------------------------------------------------------------
1 | import {render} from './helpers/test-utils'
2 |
3 | test('.toHaveAttribute', () => {
4 | const {queryByTestId} = render(`
5 |
6 | OK
7 |
8 |
9 | `)
10 |
11 | expect(queryByTestId('ok-button')).toHaveAttribute('disabled')
12 | expect(queryByTestId('ok-button')).toHaveAttribute('type')
13 | expect(queryByTestId('ok-button')).not.toHaveAttribute('class')
14 | expect(queryByTestId('ok-button')).toHaveAttribute('type', 'submit')
15 | expect(queryByTestId('ok-button')).not.toHaveAttribute('type', 'button')
16 | expect(queryByTestId('svg-element')).toHaveAttribute('width')
17 | expect(queryByTestId('svg-element')).toHaveAttribute('width', '12')
18 | expect(queryByTestId('ok-button')).not.toHaveAttribute('height')
19 |
20 | expect(() =>
21 | expect(queryByTestId('ok-button')).not.toHaveAttribute('disabled'),
22 | ).toThrowError()
23 | expect(() =>
24 | expect(queryByTestId('ok-button')).not.toHaveAttribute('type'),
25 | ).toThrowError()
26 | expect(() =>
27 | expect(queryByTestId('ok-button')).toHaveAttribute('class'),
28 | ).toThrowError()
29 | expect(() =>
30 | expect(queryByTestId('ok-button')).not.toHaveAttribute('type', 'submit'),
31 | ).toThrowError()
32 | expect(() =>
33 | expect(queryByTestId('ok-button')).toHaveAttribute('type', 'button'),
34 | ).toThrowError()
35 | expect(() =>
36 | expect(queryByTestId('svg-element')).not.toHaveAttribute('width'),
37 | ).toThrowError()
38 | expect(() =>
39 | expect(queryByTestId('svg-element')).not.toHaveAttribute('width', '12'),
40 | ).toThrowError()
41 | expect(() =>
42 | expect({thisIsNot: 'an html element'}).not.toHaveAttribute(),
43 | ).toThrowError()
44 |
45 | // Asymmetric matchers
46 | expect(queryByTestId('ok-button')).toHaveAttribute(
47 | 'type',
48 | expect.stringContaining('sub'),
49 | )
50 | expect(queryByTestId('ok-button')).toHaveAttribute(
51 | 'type',
52 | expect.stringMatching(/sub*/),
53 | )
54 | expect(queryByTestId('ok-button')).toHaveAttribute('type', expect.anything())
55 |
56 | expect(() =>
57 | expect(queryByTestId('ok-button')).toHaveAttribute(
58 | 'type',
59 | expect.not.stringContaining('sub'),
60 | ),
61 | ).toThrowError()
62 | })
63 |
--------------------------------------------------------------------------------
/src/__tests__/to-have-description.js:
--------------------------------------------------------------------------------
1 | import {render} from './helpers/test-utils'
2 |
3 | describe('.toHaveDescription', () => {
4 | let spy
5 | beforeAll(() => {
6 | // @deprecated intentionally hiding warnings for test clarity
7 | spy = jest.spyOn(console, 'warn').mockImplementation(() => {})
8 | })
9 |
10 | afterAll(() => {
11 | spy.mockRestore()
12 | })
13 |
14 | test('handles positive test cases', () => {
15 | const {queryByTestId} = render(`
16 | The description
17 |
18 |
19 |
20 |
21 | `)
22 |
23 | expect(queryByTestId('single')).toHaveDescription('The description')
24 | expect(queryByTestId('single')).toHaveDescription(
25 | expect.stringContaining('The'),
26 | )
27 | expect(queryByTestId('single')).toHaveDescription(/The/)
28 | expect(queryByTestId('single')).toHaveDescription(
29 | expect.stringMatching(/The/),
30 | )
31 | expect(queryByTestId('single')).toHaveDescription(/description/)
32 | expect(queryByTestId('single')).not.toHaveDescription('Something else')
33 | expect(queryByTestId('single')).not.toHaveDescription('The')
34 |
35 | expect(queryByTestId('invalid_id')).not.toHaveDescription()
36 | expect(queryByTestId('invalid_id')).toHaveDescription('')
37 |
38 | expect(queryByTestId('without')).not.toHaveDescription()
39 | expect(queryByTestId('without')).toHaveDescription('')
40 | })
41 |
42 | test('handles multiple ids', () => {
43 | const {queryByTestId} = render(`
44 | First description
45 | Second description
46 | Third description
47 |
48 |
49 | `)
50 |
51 | expect(queryByTestId('multiple')).toHaveDescription(
52 | 'First description Second description Third description',
53 | )
54 | expect(queryByTestId('multiple')).toHaveDescription(
55 | /Second description Third/,
56 | )
57 | expect(queryByTestId('multiple')).toHaveDescription(
58 | expect.stringContaining('Second description Third'),
59 | )
60 | expect(queryByTestId('multiple')).toHaveDescription(
61 | expect.stringMatching(/Second description Third/),
62 | )
63 | expect(queryByTestId('multiple')).not.toHaveDescription('Something else')
64 | expect(queryByTestId('multiple')).not.toHaveDescription('First')
65 | })
66 |
67 | test('handles negative test cases', () => {
68 | const {queryByTestId} = render(`
69 | The description
70 |
71 | `)
72 |
73 | expect(() =>
74 | expect(queryByTestId('other')).toHaveDescription('The description'),
75 | ).toThrowError()
76 |
77 | expect(() =>
78 | expect(queryByTestId('target')).toHaveDescription('Something else'),
79 | ).toThrowError()
80 |
81 | expect(() =>
82 | expect(queryByTestId('target')).not.toHaveDescription('The description'),
83 | ).toThrowError()
84 | })
85 |
86 | test('normalizes whitespace', () => {
87 | const {queryByTestId} = render(`
88 |
89 | Step
90 | 1
91 | of
92 | 4
93 |
94 |
95 | And
96 | extra
97 | description
98 |
99 |
100 | `)
101 |
102 | expect(queryByTestId('target')).toHaveDescription(
103 | 'Step 1 of 4 And extra description',
104 | )
105 | })
106 |
107 | test('can handle multiple levels with content spread across decendants', () => {
108 | const {queryByTestId} = render(`
109 |
110 | Step
111 | 1
112 | of
113 |
114 |
115 | 4
116 |