-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathSummaryList.test.tsx
More file actions
187 lines (147 loc) · 5.49 KB
/
SummaryList.test.tsx
File metadata and controls
187 lines (147 loc) · 5.49 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { render } from '@testing-library/react';
import { type ComponentProps, createRef } from 'react';
import { SummaryList } from '..';
describe('SummaryList', () => {
it('matches snapshot', () => {
const { container } = render(<SummaryList />);
expect(container).toMatchSnapshot('SummaryList');
});
it('matches snapshot without border', () => {
const { container } = render(<SummaryList noBorder />);
expect(container).toMatchSnapshot();
});
it('forwards refs', () => {
const ref = createRef<HTMLDListElement>();
const { container } = render(<SummaryList ref={ref} />);
const insetTextEl = container.querySelector('dl');
expect(ref.current).toBe(insetTextEl);
expect(ref.current).toHaveClass('nhsuk-summary-list');
});
it('adds css classes when noBorder prop supplied', () => {
const { container } = render(<SummaryList noBorder />);
expect(container.querySelector('.nhsuk-summary-list--no-border')).toBeTruthy();
});
describe('SummaryList.Row', () => {
it('matches snapshot', () => {
const { container } = render(<SummaryList.Row>Row</SummaryList.Row>);
expect(container).toHaveTextContent('Row');
expect(container).toMatchSnapshot();
});
it('matches snapshot with key and value', () => {
const { container } = render(
<SummaryList.Row>
<SummaryList.Key>Name</SummaryList.Key>
<SummaryList.Value>Karen Francis</SummaryList.Value>
</SummaryList.Row>,
);
expect(container).toMatchSnapshot();
});
it('matches snapshot with key, value and action', () => {
const { container } = render(
<SummaryList.Row>
<SummaryList.Key>Name</SummaryList.Key>
<SummaryList.Value>Karen Francis</SummaryList.Value>
<SummaryList.Action href="#" visuallyHiddenText="name">
Change
</SummaryList.Action>
</SummaryList.Row>,
);
expect(container).toMatchSnapshot();
});
it('matches snapshot with key, value and multiple actions', () => {
const { container } = render(
<SummaryList.Row>
<SummaryList.Key>Name</SummaryList.Key>
<SummaryList.Value>Karen Francis</SummaryList.Value>
<SummaryList.Action href="#" visuallyHiddenText="name">
Change
</SummaryList.Action>
<SummaryList.Action href="#" visuallyHiddenText="name">
Delete
</SummaryList.Action>
</SummaryList.Row>,
);
expect(container).toMatchSnapshot();
});
it('matches snapshot without border', () => {
const { container } = render(<SummaryList.Row noBorder>Row</SummaryList.Row>);
expect(container).toHaveTextContent('Row');
expect(container).toMatchSnapshot();
});
});
describe('SummaryList.Key', () => {
it('matches snapshot', () => {
const { container } = render(<SummaryList.Key>Example key</SummaryList.Key>);
expect(container).toHaveTextContent('Example key');
expect(container).toMatchSnapshot();
});
});
describe('SummaryList.Value', () => {
it('matches snapshot', () => {
const { container } = render(<SummaryList.Value>Example value</SummaryList.Value>);
expect(container).toHaveTextContent('Example value');
expect(container).toMatchSnapshot();
});
});
describe('SummaryList.Action', () => {
it('matches snapshot', () => {
const { container } = render(<SummaryList.Action href="#">Edit</SummaryList.Action>);
expect(container).toHaveTextContent('Edit');
expect(container).toMatchSnapshot();
});
it('matches snapshot with visually hidden text', () => {
const { container } = render(
<SummaryList.Action href="#" visuallyHiddenText="example key">
Edit
</SummaryList.Action>,
);
expect(container).toHaveTextContent('Edit example key');
expect(container).toMatchSnapshot();
});
it('renders with visually hidden HTML', () => {
const { container } = render(
<SummaryList.Action
href="#"
visuallyHiddenText={
<>
example key <em>with HTML</em>
</>
}
>
Edit
</SummaryList.Action>,
);
const visuallyHiddenEl = container.querySelector('.nhsuk-u-visually-hidden');
expect(visuallyHiddenEl).toContainHTML('example key <em>with HTML</em>');
expect(container).toHaveTextContent('Edit example key with HTML');
expect(container).toMatchSnapshot();
});
it('renders as custom element', () => {
function CustomLink({ children, href, ...rest }: ComponentProps<'a'>) {
return (
<a href={href} {...rest} data-custom-link="true">
{children}
</a>
);
}
const { container } = render(
<SummaryList.Action href="#" visuallyHiddenText="example key" asElement={CustomLink}>
Edit
</SummaryList.Action>,
);
const rowActionEl = container.querySelector('a');
expect(rowActionEl?.dataset).toHaveProperty('customLink', 'true');
});
it('forwards refs', () => {
const ref = createRef<HTMLAnchorElement>();
const { container } = render(
<SummaryList.Action href="#" visuallyHiddenText="example key" ref={ref}>
Edit
</SummaryList.Action>,
);
const rowActionEl = container.querySelector('a');
expect(ref.current).toBe(rowActionEl);
expect(ref.current).toHaveAttribute('href', '#');
});
});
});