-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathTrend.test.tsx
More file actions
185 lines (133 loc) · 5.87 KB
/
Copy pathTrend.test.tsx
File metadata and controls
185 lines (133 loc) · 5.87 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
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
import * as React from 'react';
import { render, screen, within } from 'wrappedTestingLibrary';
import Trend from './Trend';
const renderTrend = ({
current = 42,
previous = 42,
trendPreference = 'NEUTRAL',
}: Partial<React.ComponentProps<typeof Trend>> = {}) =>
render(<Trend current={current} previous={previous} trendPreference={trendPreference} />);
const findTrend = async () => {
const trend = await screen.findByTestId('trend-value');
return trend.innerHTML;
};
describe('Trend', () => {
it('shows absolute delta', async () => {
renderTrend({ previous: 23 });
expect(await findTrend()).toMatch(/\+19/);
});
it('shows relative delta as percentage', async () => {
renderTrend({ previous: 23 });
expect(await findTrend()).toMatch(/\+82.6%/);
});
it('shows absolute delta if values are equal', async () => {
renderTrend();
expect(await findTrend()).toMatch(/^0 \//);
});
it('shows relative delta as percentage if values are equal', async () => {
renderTrend();
expect(await findTrend()).toMatch(/0\.0%/);
});
it('shows negative absolute delta', async () => {
renderTrend({ current: 23 });
expect(await findTrend()).toMatch(/-19/);
});
it('shows negative relative delta as percentage', async () => {
renderTrend({ current: 23 });
expect(await findTrend()).toMatch(/-45.2%/);
});
it('shows adequate results if previous value is 0', async () => {
renderTrend({ current: 23, previous: 0 });
expect(await findTrend()).toMatch(/\+23/);
});
it('shows adequate results if previous value is NaN', async () => {
renderTrend({ current: 23, previous: NaN });
expect(await findTrend()).toEqual('-- / --');
});
it('shows adequate results if current value is 0', async () => {
renderTrend({ current: 0, previous: 42 });
expect(await findTrend()).toMatch(/-42 \/ -100\.0%/);
});
it('shows adequate results if current value is NaN', async () => {
renderTrend({ current: NaN, previous: 42 });
expect(await findTrend()).toEqual('-- / --');
});
describe('renders background according to values and trend preference', () => {
it.each`
trendPreference
${'NEUTRAL'}
${'HIGHER'}
${'LOWER'}
`(
'shows neutral background if values are equal and trend preference is $trendPreference',
async ({ trendPreference }: { trendPreference: 'NEUTRAL' | 'LOWER' | 'HIGHER' }) => {
renderTrend({ trendPreference });
const background = await screen.findByTestId('trend-background');
expect(background).toHaveStyleRule('background-color', '#fff!important');
},
);
it('shows good background if current value and preference are higher', async () => {
renderTrend({ current: 43, trendPreference: 'HIGHER' });
const background = await screen.findByTestId('trend-background');
expect(background).toHaveStyleRule('background-color', '#2ECA8F!important');
});
it('shows good background if current value and preference are lower', async () => {
renderTrend({ current: 41, trendPreference: 'LOWER' });
const background = await screen.findByTestId('trend-background');
expect(background).toHaveStyleRule('background-color', '#2ECA8F!important');
});
it('shows bad background if current value is lower but preference is higher', async () => {
renderTrend({ current: 41, trendPreference: 'HIGHER' });
const background = await screen.findByTestId('trend-background');
expect(background).toHaveStyleRule('background-color', '#FE4A49!important');
});
it('shows bad background if current value is higher but preference is lower', async () => {
renderTrend({ current: 43, trendPreference: 'LOWER' });
const background = await screen.findByTestId('trend-background');
expect(background).toHaveStyleRule('background-color', '#FE4A49!important');
});
it('shows neutral background if current value is higher but preference is neutral', async () => {
renderTrend({ current: 43, trendPreference: 'NEUTRAL' });
const background = await screen.findByTestId('trend-background');
expect(background).toHaveStyleRule('background-color', '#fff!important');
});
it('shows neutral background if current value is lower but preference is neutral', async () => {
renderTrend({ current: 41, trendPreference: 'NEUTRAL' });
const background = await screen.findByTestId('trend-background');
expect(background).toHaveStyleRule('background-color', '#fff!important');
});
});
describe('renders icon indicating trend direction', () => {
it('shows circle right if values are equal', async () => {
renderTrend();
const trendIcon = await screen.findByTestId('trend-icon');
within(trendIcon).getByText('arrow_circle_right');
});
it('shows circle down if current values is lower', async () => {
renderTrend({ current: 41 });
const trendIcon = await screen.findByTestId('trend-icon');
within(trendIcon).getByText('arrow_circle_down');
});
it('shows circle up if current values is higher', async () => {
renderTrend({ current: 43 });
const trendIcon = await screen.findByTestId('trend-icon');
within(trendIcon).getByText('arrow_circle_up');
});
});
});