File size: 3,237 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @jest-environment jsdom
 */

import '@testing-library/jest-dom';
import { render, screen } from '@testing-library/react';
import ProgressBar from '../';

describe( 'ProgressBar', () => {
	test( 'should show the title', () => {
		render( <ProgressBar value={ 20 } title="foo" /> );

		const progressBar = screen.getByRole( 'progressbar', { name: 'progress bar' } );

		expect( progressBar ).toBeVisible();
		expect( progressBar ).toHaveClass( 'progress-bar__progress' );
		expect( progressBar ).toHaveTextContent( 'foo' );
	} );

	test( 'should add is-pulsing class when isPulsing property is true', () => {
		const { container } = render( <ProgressBar value={ 20 } isPulsing /> );

		expect( container.firstChild ).toHaveClass( 'is-pulsing' );
	} );

	test( 'should not add is-pulsing class when isPulsing property is false', () => {
		const { container } = render( <ProgressBar value={ 20 } isPulsing={ false } /> );

		expect( container.firstChild ).not.toHaveClass( 'is-pulsing' );
	} );

	test( 'should add is-compact class when compact property is true', () => {
		const { container } = render( <ProgressBar value={ 20 } compact /> );

		expect( container.firstChild ).toHaveClass( 'is-compact' );
	} );

	test( 'should not add is-compact class when compact property is false', () => {
		const { container } = render( <ProgressBar value={ 20 } compact={ false } /> );

		expect( container.firstChild ).not.toHaveClass( 'is-compact' );
	} );

	test( 'should properly calculate the width percentage', () => {
		render( <ProgressBar value={ 20 } total={ 40 } /> );

		expect( screen.getByRole( 'progressbar', { name: 'progress bar' } ) ).toHaveStyle(
			'width: 50%'
		);
	} );

	test( 'should have correct aria values', () => {
		render( <ProgressBar value={ 20 } total={ 40 } /> );

		const progressBar = screen.getByRole( 'progressbar', { name: 'progress bar' } );

		expect( progressBar ).toHaveAttribute( 'aria-valuenow', '20' );
		expect( progressBar ).toHaveAttribute( 'aria-valuemax', '40' );
	} );

	test( 'should have the color provided by the color property', () => {
		render( <ProgressBar value={ 20 } color="red" /> );

		expect( screen.getByRole( 'progressbar', { name: 'progress bar' } ) ).toHaveStyle(
			'background-color: red'
		);
	} );

	test( 'should not be able to be more than 100% complete', () => {
		render( <ProgressBar value={ 240 } /> );
		expect( screen.getByRole( 'progressbar', { name: 'progress bar' } ) ).toHaveStyle(
			'width: 100%'
		);
	} );

	test( 'should never jump back', () => {
		const { container, rerender } = render( <ProgressBar value={ 10 } /> );

		const progressBar = screen.getByRole( 'progressbar', { name: 'progress bar' } );

		expect( progressBar ).toHaveStyle( 'width: 10%' );

		rerender( <ProgressBar value={ 20 } /> );

		expect( progressBar ).toHaveStyle( 'width: 20%' );

		rerender( <ProgressBar value={ 15 } /> );

		expect( progressBar ).toHaveStyle( 'width: 20%' );

		rerender( <ProgressBar value={ 30 } /> );

		expect( progressBar ).toHaveStyle( 'width: 30%' );

		expect( container.firstChild ).not.toHaveClass( 'is-pulsing' );

		rerender( <ProgressBar value={ 30 } isPulsing /> );

		expect( container.firstChild ).toHaveClass( 'is-pulsing' );
	} );
} );