-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdisplay.js
195 lines (162 loc) · 4.71 KB
/
display.js
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
import hljs from 'highlight.js/lib/core';
import hljsJavascript from 'highlight.js/lib/languages/javascript';
import hljsDefineGraphQL from 'highlightjs-graphql';
import hljsCss from 'highlight.js/styles/googlecode.css';
import styles from './display.css';
hljs.registerLanguage('javascript', hljsJavascript);
hljsDefineGraphQL(hljs);
// Display items with these titles as inline
const SHORT = ['status', 'statusText', 'duration', 'url'];
/**
* Prepares the main cypress test visualization container and returns it.
*/
export const getContainer = styles => {
const doc = cy.state('document');
Object.entries(styles || {}).forEach(([ name, contents ]) => {
const className = `style-${name}`;
if (!doc.querySelector(`.${className}`)) {
const style = doc.createElement('style');
style.classList.add(className);
style.textContent = contents.toString();
doc.body.appendChild(style);
}
});
let container = doc.querySelector('.container');
if (!container) {
container = doc.createElement('div');
container.classList.add('container');
doc.body.appendChild(container);
}
// Clear the container from cypress's default stuff.
container.innerHTML = '';
return container;
};
/**
* Creates an element within the iframe's document and returns it.
*/
export const createElement = ({ children, classNames, element, parentNode, textContent }) => {
const doc = cy.state('document');
const node = doc.createElement(element || 'div');
(classNames || []).forEach(className => {
if (className) {
node.classList.add(className);
}
});
if (parentNode) {
parentNode.appendChild(node);
}
if (textContent) {
node.textContent = textContent;
}
(children || []).forEach(child => {
node.appendChild(createElement(child));
});
return node;
};
export const getHelpers = () => {
/**
* Creates a wrapper elemement
*/
const wrapper = createElement({ classNames: [ 'cypress-rest-graphql' ] });
/**
* Renders an item for display
*
* @param {Object} item single item, in the rest-graphql format
*/
const displayItem = item => {
const container = createElement({
classNames: [
'item',
SHORT.includes(item[0]) && 'short',
],
parentNode: wrapper,
});
const title = createElement({
classNames: [ 'title' ],
parentNode: container,
textContent: item[0],
});
const pre = createElement({
element: 'pre',
parentNode: container,
});
if (item[0] === 'query') {
const code = createElement({
classNames: [ 'graphQL' ],
element: 'code',
parentNode: pre,
textContent: item[1],
});
hljs.highlightBlock(code);
}
else if (!item[1]) {
createElement({
element: 'code',
parentNode: pre,
textContent: '---',
});
}
else if (typeof item[1] === 'object') {
const code = createElement({
classNames: [ 'json' ],
element: 'code',
parentNode: pre,
textContent: JSON.stringify(item[1], null, 2),
});
hljs.highlightBlock(code);
}
else {
pre.textContent = item[1];
}
};
return { displayItem, wrapper };
}
/**
* Pushes current request data to the Cypress UI pane, for easy review
* @param {Object} resp - Object containing response data from the query
* @param {String} type - String to display at the top
*/
export const display = (resp, type) => {
const container = getContainer({
main: styles,
hljs: hljsCss,
});
const {displayItem, wrapper} = getHelpers();
const { url, title, duration, statusText, status, ...data } = resp;
// Setting graphQL header
createElement({
classNames: [ 'header' ],
parentNode: wrapper,
children: [
{ element: 'h1', textContent: type },
{ element: 'h2', textContent: title },
{
classNames: [ 'duration-response' ],
children: [
{
classNames: [ 'duration' ],
element: 'span',
textContent: `${duration} ms`,
},
{
classNames: [ 'statusText', statusText ],
element: 'span',
textContent: statusText,
},
]
},
],
});
// Setting graphQL specific values at the top
displayItem([ 'statusText', statusText ]);
displayItem([ 'duration', duration + ' ms' ]);
if (url) displayItem([ 'url', url ]);
displayItem([ 'status', status ]);
// Processing additional entries
Object.entries(data).forEach((item) => {
displayItem(item);
});
container.appendChild(wrapper);
};
export const displayRest = (resp) => display(resp, 'REST');
export const displayGraphQL = (resp) => display(resp, 'GraphQL');