/
Panel.tsx
293 lines (276 loc) · 9.1 KB
/
Panel.tsx
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import React, { Fragment } from 'react';
import { styled, themes, convert } from '@storybook/theming';
import { ScrollArea, TabsState, Link, Placeholder } from '@storybook/components';
import { SizeMe } from 'react-sizeme';
import Result from './Result';
import provideJestResult, { Test } from '../hoc/provideJestResult';
const StatusTypes = {
PASSED_TYPE: 'passed',
FAILED_TYPE: 'failed',
PENDING_TYPE: 'pending',
TODO_TYPE: 'todo',
};
const List = styled.ul({
listStyle: 'none',
fontSize: 14,
padding: 0,
margin: 0,
});
const Item = styled.li({
display: 'block',
padding: 0,
});
const ProgressWrapper = styled.div({
position: 'relative',
height: 10,
width: 30,
display: 'flex',
top: -2,
});
const SuiteHead = styled.div({
display: 'flex',
alignItems: 'baseline',
position: 'absolute',
zIndex: 2,
right: 20,
marginTop: 15,
});
const SuiteTotals = styled(({ result, className, width }) => (
<div className={className}>
<Fragment>
{width > 325 && result.assertionResults ? (
<div>
{result.assertionResults.length} {result.assertionResults.length > 1 ? `tests` : `test`}
</div>
) : null}
{width > 280 && result.endTime && result.startTime ? (
<div>
{result.endTime - result.startTime}
ms
</div>
) : null}
</Fragment>
</div>
))(({ theme }) => ({
display: 'flex',
alignItems: 'center',
color: theme.color.dark,
fontSize: '14px',
marginTop: -5,
'& > *': {
marginRight: 10,
},
}));
const SuiteProgressPortion = styled.div<{ color: any; progressPercent: number }>(
({ theme, color, progressPercent }) => ({
height: 6,
top: 3,
width: `${progressPercent}%`,
backgroundColor: color,
})
);
interface ContentProps {
tests: Test[];
className?: string;
}
const getTestsByTypeMap = (result: any) => {
const testsByType: Map<string, any> = new Map();
result.assertionResults.forEach((assertion: any) => {
testsByType.set(
assertion.status,
testsByType.get(assertion.status)
? testsByType.get(assertion.status).concat(assertion)
: [assertion]
);
});
return testsByType;
};
const getColorByType = (type: string) => {
// using switch to allow for new types to be added
switch (type) {
case StatusTypes.PASSED_TYPE:
return convert(themes.light).color.positive;
case StatusTypes.FAILED_TYPE:
return convert(themes.light).color.negative;
case StatusTypes.PENDING_TYPE:
return convert(themes.light).color.warning;
case StatusTypes.TODO_TYPE:
return convert(themes.light).color.purple;
default:
return null;
}
};
const Content = styled(({ tests, className }: ContentProps) => (
<div className={className}>
{tests.map(({ name, result }) => {
if (!result || !result.assertionResults) {
return (
<Placeholder key={name}>
This story has tests configured, but no file was found
</Placeholder>
);
}
const testsByType: Map<string, any> = getTestsByTypeMap(result);
const entries: any = testsByType.entries();
const sortedTestsByCount = [...entries].sort((a, b) => a[1].length - b[1].length);
return (
<SizeMe refreshMode="debounce" key={name}>
{({ size }: { size: any }) => {
const { width } = size;
return (
<section>
<SuiteHead>
<SuiteTotals {...{ result, width }} />
{width > 240 ? (
<ProgressWrapper>
{sortedTestsByCount.map((entry: any) => {
return (
<SuiteProgressPortion
key={`progress-portion-${entry[0]}`}
color={getColorByType(entry[0])}
progressPercent={
entry[1]
? (entry[1].length / result.assertionResults.length) * 100
: 0
}
/>
);
})}
</ProgressWrapper>
) : null}
</SuiteHead>
<TabsState
initial="failing-tests"
backgroundColor={convert(themes.light).background.hoverable}
>
<div
id="failing-tests"
title={`${
testsByType.get(StatusTypes.FAILED_TYPE)
? testsByType.get(StatusTypes.FAILED_TYPE).length
: 0
} Failed`}
color={getColorByType(StatusTypes.FAILED_TYPE)}
>
<List>
{testsByType.get(StatusTypes.FAILED_TYPE) ? (
testsByType.get(StatusTypes.FAILED_TYPE).map((res: any) => (
<Item key={res.fullName || res.title}>
<Result {...res} />
</Item>
))
) : (
<Placeholder key={`no-tests-${StatusTypes.FAILED_TYPE}`}>
This story has no failing tests.
</Placeholder>
)}
</List>
</div>
<div
id="passing-tests"
title={`${
testsByType.get(StatusTypes.PASSED_TYPE)
? testsByType.get(StatusTypes.PASSED_TYPE).length
: 0
} Passed`}
color={getColorByType(StatusTypes.PASSED_TYPE)}
>
<List>
{testsByType.get(StatusTypes.PASSED_TYPE) ? (
testsByType.get(StatusTypes.PASSED_TYPE).map((res: any) => (
<Item key={res.fullName || res.title}>
<Result {...res} />
</Item>
))
) : (
<Placeholder key={`no-tests-${StatusTypes.PASSED_TYPE}`}>
This story has no passing tests.
</Placeholder>
)}
</List>
</div>
<div
id="pending-tests"
title={`${
testsByType.get(StatusTypes.PENDING_TYPE)
? testsByType.get(StatusTypes.PENDING_TYPE).length
: 0
} Pending`}
color={getColorByType(StatusTypes.PENDING_TYPE)}
>
<List>
{testsByType.get(StatusTypes.PENDING_TYPE) ? (
testsByType.get(StatusTypes.PENDING_TYPE).map((res: any) => (
<Item key={res.fullName || res.title}>
<Result {...res} />
</Item>
))
) : (
<Placeholder key={`no-tests-${StatusTypes.PENDING_TYPE}`}>
This story has no pending tests.
</Placeholder>
)}
</List>
</div>
<div
id="todo-tests"
title={`${
testsByType.get(StatusTypes.TODO_TYPE)
? testsByType.get(StatusTypes.TODO_TYPE).length
: 0
} Todo`}
color={getColorByType(StatusTypes.TODO_TYPE)}
>
<List>
{testsByType.get(StatusTypes.TODO_TYPE) ? (
testsByType.get(StatusTypes.TODO_TYPE).map((res: any) => (
<Item key={res.fullName || res.title}>
<Result {...res} />
</Item>
))
) : (
<Placeholder key={`no-tests-${StatusTypes.TODO_TYPE}`}>
This story has no tests todo.
</Placeholder>
)}
</List>
</div>
</TabsState>
</section>
);
}}
</SizeMe>
);
})}
</div>
))({
flex: '1 1 0%',
});
interface PanelProps {
tests: null | Test[];
}
const Panel = ({ tests }: PanelProps) => (
<ScrollArea vertical>
{tests ? (
<Content tests={tests} />
) : (
<Placeholder>
<Fragment>No tests found</Fragment>
<Fragment>
Learn how to
<Link
href="https://github.com/storybookjs/storybook/tree/master/addons/jest"
target="_blank"
withArrow
>
add Jest test results to your story
</Link>
</Fragment>
</Placeholder>
)}
</ScrollArea>
);
Panel.defaultProps = {
tests: null,
};
export default provideJestResult(Panel);