-
Notifications
You must be signed in to change notification settings - Fork 173
/
test-item.tsx
143 lines (126 loc) · 3.42 KB
/
test-item.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
import React, { Fragment } from "react";
import styled from "styled-components";
import { TestFileItem } from "./transformer";
import { TestFileResult } from "../../server/api/workspace/test-result/file-result";
import TestIndicator from "./test-indicator";
import { color, space } from "styled-system";
import * as Convert from "ansi-to-html";
import OPEN_FAILURE from "./open-failure.gql";
import { useMutation } from "react-apollo-hooks";
const convert = new Convert({
colors: {
1: "#FF4F56",
2: "#19E28D"
}
});
function getResults(item: TestFileItem, testResult: TestFileResult) {
if (!testResult || !testResult.testResults) {
return null;
}
return testResult.testResults.find(result => result.title === item.name);
}
const Container = styled.div`
${color};
${space};
padding-left: 25px;
`;
const Label = styled.div`
display: flex;
align-items: center;
font-weight: 600;
font-size: 13px;
span {
margin-left: 5px;
}
`;
const Content = styled.div<any>`
padding: 5px;
display: flex;
flex-direction: column;
background-color: #262529;
border-radius: 4px;
margin-bottom: 10px;
border: 1px solid ${props => (props.only ? "#9d8301" : "#333437")};
`;
const FailureMessage = styled.div`
padding-left: 20px;
pre {
overflow: auto;
}
`;
const Duration = styled.span`
font-weight: 400;
font-size: 12px;
color: #fcd101;
`;
function escapeHtml(unsafe: string) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
interface Props {
item: TestFileItem;
result: TestFileResult | null;
}
export default function Test({
item: { name, only, children },
item,
result
}: Props) {
const testResult = getResults(item, result as any);
const isDurationAvailable = testResult && testResult.duration !== undefined;
const haveFailure = testResult && testResult.failureMessages.length > 0;
const allChildrenPassing = (children || []).every(child => {
if (child.type === "it") {
const childResult = getResults(child, result as any);
return childResult && childResult.status === "passed";
}
return true;
});
if (children && children.length > 0) {
}
const openFailure = useMutation(OPEN_FAILURE, {
variables: {
failure: testResult && testResult.failureMessages ? testResult.failureMessages[0] : ''
}
});
return (
<Container>
<Content only={only} onClick={ () => openFailure()}>
<Label>
<TestIndicator
status={
item.type === "describe" && allChildrenPassing
? "passed"
: testResult && testResult.status
}
describe={item.type === "describe"}
todo={item.type === "todo"}
/>
<span>{name}</span>
{isDurationAvailable && (
<Duration>{testResult && testResult.duration} ms</Duration>
)}
</Label>
{testResult && haveFailure && (
<FailureMessage>
<pre
dangerouslySetInnerHTML={{
__html: convert.toHtml(
escapeHtml(testResult.failureMessages.join(","))
)
}}
/>
</FailureMessage>
)}
</Content>
{children &&
children.map(child => (
<Test key={child.id} item={child} result={result} />
))}
</Container>
);
}