UNPKG

7.15 kBJavaScriptView Raw
1import React from 'react';
2import '@testing-library/jest-dom';
3import { render, screen } from '@testing-library/react';
4import {
5 testForCustomTag,
6 testForDefaultClass,
7 testForDefaultTag,
8} from '../testUtils';
9import { Progress } from '..';
10
11describe('Progress', () => {
12 it('should render with "div" tag by default', () => {
13 testForDefaultTag(Progress, 'div');
14 });
15
16 it('should render with "progress" class', () => {
17 testForDefaultClass(Progress, 'progress');
18 });
19
20 it('should render with "value" 0 by default', () => {
21 render(<Progress />);
22
23 expect(screen.getByRole('progressbar')).toHaveAttribute(
24 'aria-valuenow',
25 '0',
26 );
27 });
28
29 it('should render with "max" 100 by default', () => {
30 render(<Progress />);
31
32 expect(screen.getByRole('progressbar')).toHaveAttribute(
33 'aria-valuemax',
34 '100',
35 );
36 });
37
38 it('should render with "style" on the parent element', () => {
39 render(<Progress style={{ height: '20px' }} data-testid="sandman" />);
40 expect(
41 getComputedStyle(screen.getByTestId('sandman')).getPropertyValue(
42 'height',
43 ),
44 ).toBe('20px');
45 });
46
47 it('should render with "style" on the progress bar element if bar=true', () => {
48 render(<Progress bar style={{ height: '20px' }} />);
49
50 expect(
51 getComputedStyle(screen.getByRole('progressbar')).getPropertyValue(
52 'height',
53 ),
54 ).toBe('20px');
55 });
56
57 it('should render "barStyle" on the progress bar element', () => {
58 render(
59 <Progress style={{ height: '20px' }} barStyle={{ height: '10px' }} />,
60 );
61
62 expect(
63 getComputedStyle(screen.getByRole('progressbar')).getPropertyValue(
64 'height',
65 ),
66 ).toBe('10px');
67 });
68
69 it('should render with the given "value" when passed as string prop', () => {
70 render(<Progress value="10" />);
71
72 expect(screen.getByRole('progressbar')).toHaveAttribute(
73 'aria-valuenow',
74 '10',
75 );
76 });
77
78 it('should render with the given "value" when passed as number prop', () => {
79 render(<Progress value={10} />);
80
81 expect(screen.getByRole('progressbar')).toHaveAttribute(
82 'aria-valuenow',
83 '10',
84 );
85 });
86
87 it('should render with the given "max" when passed as string prop', () => {
88 render(<Progress max="10" />);
89
90 expect(screen.getByRole('progressbar')).toHaveAttribute(
91 'aria-valuemax',
92 '10',
93 );
94 });
95
96 it('should render with the given "max" when passed as number prop', () => {
97 render(<Progress max={10} />);
98
99 expect(screen.getByRole('progressbar')).toHaveAttribute(
100 'aria-valuemax',
101 '10',
102 );
103 });
104
105 it('should render with "progress-bar-striped" class when striped prop is truthy', () => {
106 render(<Progress striped />);
107
108 expect(screen.getByRole('progressbar')).toHaveClass('progress-bar-striped');
109 });
110
111 it('should render with "progress-bar-striped" and "progress-bar-animated" classes when animated prop is truthy', () => {
112 render(<Progress animated />);
113
114 expect(screen.getByRole('progressbar')).toHaveClass('progress-bar-striped');
115 expect(screen.getByRole('progressbar')).toHaveClass(
116 'progress-bar-animated',
117 );
118 });
119
120 it('should render with "bg-${color}" class when color prop is defined', () => {
121 render(<Progress color="yo" />);
122
123 expect(screen.getByRole('progressbar')).toHaveClass('bg-yo');
124 });
125
126 it('should render additional classes', () => {
127 render(<Progress className="other" data-testid="sandman" />);
128 expect(screen.getByTestId('sandman')).toHaveClass('other');
129 expect(screen.getByTestId('sandman')).toHaveClass('progress');
130 });
131
132 it('should render additional classes on the inner progress bar', () => {
133 render(<Progress barClassName="other" data-testid="sandman" />);
134 expect(screen.getByTestId('sandman')).toHaveClass('progress');
135 expect(screen.getByTestId('sandman')).not.toHaveClass('other');
136 expect(screen.getByRole('progressbar')).toHaveClass('other');
137 });
138
139 it('should render custom tag', () => {
140 testForCustomTag(Progress);
141 });
142
143 it('should render only the .progress when "multi" is passed', () => {
144 render(<Progress multi data-testid="sandman" />);
145
146 expect(screen.getByTestId('sandman')).toHaveClass('progress');
147 expect(screen.getByTestId('sandman')).not.toHaveClass('progress-bar');
148 });
149
150 it('should render only the .progress-bar when "bar" is passed', () => {
151 render(<Progress bar />);
152
153 expect(screen.getByRole('progressbar')).toHaveClass('progress-bar');
154 expect(screen.getByRole('progressbar')).not.toHaveClass('progress');
155 });
156
157 it('should render additional classes', () => {
158 render(<Progress bar className="yo" data-testid="sandman" />);
159
160 expect(screen.getByTestId('sandman')).toHaveClass('progress-bar');
161 expect(screen.getByTestId('sandman')).toHaveClass('yo');
162 expect(screen.getByTestId('sandman')).not.toHaveClass('progress');
163 });
164
165 it('should render additional classes using the barClassName', () => {
166 render(<Progress bar barClassName="yo" data-testid="sandman" />);
167
168 expect(screen.getByTestId('sandman')).toHaveClass('progress-bar');
169 expect(screen.getByTestId('sandman')).toHaveClass('yo');
170 expect(screen.getByTestId('sandman')).not.toHaveClass('progress');
171 });
172
173 it('should render the children (label)', () => {
174 render(<Progress>0%</Progress>);
175
176 expect(screen.getByText('0%')).toBeInTheDocument();
177 // expect(wrapper.text()).toBe('0%');
178 });
179
180 it('should render the children (label) (multi)', () => {
181 render(
182 <Progress multi>
183 <Progress bar value="15">
184 15%
185 </Progress>
186 <Progress bar color="success" value="30">
187 30%
188 </Progress>
189 <Progress bar color="info" value="25">
190 25%
191 </Progress>
192 <Progress bar color="warning" value="20">
193 20%
194 </Progress>
195 <Progress bar color="danger" value="5">
196 5%
197 </Progress>
198 </Progress>,
199 );
200
201 expect(screen.getByText('15%')).toBeInTheDocument();
202 expect(screen.getByText('30%')).toBeInTheDocument();
203 expect(screen.getByText('25%')).toBeInTheDocument();
204 expect(screen.getByText('20%')).toBeInTheDocument();
205 expect(screen.getByText('5%')).toBeInTheDocument();
206 });
207
208 it('should render nested progress bars', () => {
209 render(
210 <Progress multi data-testid="sandman">
211 <Progress bar value="15" />
212 <Progress bar color="success" value="30" />
213 <Progress bar color="info" value="25" />
214 <Progress bar color="warning" value="20" />
215 <Progress bar color="danger" value="5" />
216 </Progress>,
217 );
218
219 expect(screen.getByTestId('sandman')).toHaveClass('progress');
220 expect(screen.getAllByRole('progressbar').length).toBe(5);
221 });
222
223 it('should render nested progress bars and id attribute', () => {
224 render(
225 <Progress multi data-testid="sandman">
226 <Progress bar id="ruh-roh" />
227 </Progress>,
228 );
229
230 expect(screen.getByRole('progressbar')).toHaveAttribute('id', 'ruh-roh');
231 expect(screen.getByTestId('sandman')).toHaveClass('progress');
232 });
233});