forked from uber/baseweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
70 lines (58 loc) · 1.75 KB
/
tests.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
/*
Copyright (c) Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
/* eslint-disable no-console */
/* eslint-disable react/prop-types */
/* eslint-disable flowtype/require-valid-file-annotation */
/* eslint-env browser */
import React from 'react';
import scenarios from '../src/**/*.scenario.js';
const nameCounts = scenarios.reduce((map, s) => {
if (!map[s.fileName]) {
map[s.fileName] = 1;
} else {
map[s.fileName]++;
}
return map;
}, {});
const collisions = Object.entries(nameCounts).filter(([_, count]) => count > 1);
if (collisions.length >= 1) {
console.error(`Found colliding scenario name(s): ${collisions
.map(([name]) => name)
.join(', ')}. Rename the conflicting .scenario.js file with a unique label.
`);
}
const A11yFail = (props) => (
<div style={{ backgroundColor: 'red', color: 'white' }} role={props.message}>
{props.message}
</div>
);
export default function showTestcase() {
// needs polyfill for IE
const urlParams = new URLSearchParams(window.location.search);
const name = urlParams.get('name');
if (!name) {
const message = 'No scenario name provided.';
console.error(message);
return <A11yFail message={message} />;
}
const scenario = scenarios.find((s) => {
if (!s) return false;
const fileName = s.fileName.split('/').pop();
const [scenarioName] = fileName.split('.scenario.js');
return scenarioName === name;
});
if (!scenario) {
const message = `No scenario found with the name: ${name}`;
console.error(message);
return <A11yFail message={message} />;
}
const Component = scenario.result.Scenario;
return (
<div>
<Component />
</div>
);
}