Skip to content
This repository was archived by the owner on Jun 27, 2022. It is now read-only.

Commit d1b2343

Browse files
committed
Uses custom test runner for variative testing
1 parent 64f4b03 commit d1b2343

File tree

5 files changed

+264
-149
lines changed

5 files changed

+264
-149
lines changed

src/match.test.ts

Lines changed: 0 additions & 149 deletions
This file was deleted.

test/path.test.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { runner } from './setup/runner'
2+
3+
runner('Path', [
4+
{
5+
given: '/user/:userId',
6+
when: [
7+
{
8+
actual: '/user/abc-123',
9+
it: {
10+
matches: true,
11+
params: { userId: 'abc-123' },
12+
},
13+
},
14+
{
15+
actual: '/user/abc-123/',
16+
it: {
17+
matches: true,
18+
params: { userId: 'abc-123' },
19+
},
20+
},
21+
{
22+
actual: '/user/',
23+
it: {
24+
matches: false,
25+
params: null,
26+
},
27+
},
28+
{
29+
actual: '/user/abc-123/messages',
30+
it: {
31+
matches: false,
32+
params: null,
33+
},
34+
},
35+
{
36+
actual: '/arbitrary/abc-123',
37+
it: {
38+
matches: false,
39+
params: null,
40+
},
41+
},
42+
],
43+
},
44+
{
45+
given: '/user/:userId/messages',
46+
when: [
47+
{
48+
actual: '/user/abc-123/messages',
49+
it: {
50+
matches: true,
51+
params: {
52+
userId: 'abc-123',
53+
},
54+
},
55+
},
56+
{
57+
actual: '/user/abc-123/messages/arbitrary',
58+
it: {
59+
matches: false,
60+
params: null,
61+
},
62+
},
63+
{
64+
actual: '/user/abc-123/',
65+
it: {
66+
matches: false,
67+
params: null,
68+
},
69+
},
70+
],
71+
},
72+
{
73+
given: '/user/*/messages',
74+
when: [
75+
{
76+
actual: '/user/arbitrary/messages',
77+
it: {
78+
matches: true,
79+
params: null,
80+
},
81+
},
82+
{
83+
actual: '/user/arbitrary/messages/more',
84+
it: {
85+
matches: false,
86+
params: null,
87+
},
88+
},
89+
{
90+
actual: '/user/',
91+
it: {
92+
matches: false,
93+
params: null,
94+
},
95+
},
96+
],
97+
},
98+
])

test/regexp.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { runner } from './setup/runner'
2+
3+
runner('RegExp', [
4+
{
5+
given: /\/users\//,
6+
when: [
7+
{
8+
actual: '/users/',
9+
it: {
10+
matches: true,
11+
params: null,
12+
},
13+
},
14+
{
15+
actual: '/users/abcd-1234/',
16+
it: {
17+
matches: true,
18+
params: null,
19+
},
20+
},
21+
{
22+
actual: '/settings',
23+
it: {
24+
matches: false,
25+
params: null,
26+
},
27+
},
28+
],
29+
},
30+
{
31+
given: /\/user\/.+?\//,
32+
when: [
33+
{
34+
actual: '/user/abcd-1234/',
35+
it: {
36+
matches: true,
37+
params: null,
38+
},
39+
},
40+
{
41+
actual: '/user/',
42+
it: {
43+
matches: false,
44+
params: null,
45+
},
46+
},
47+
],
48+
},
49+
])

test/setup/runner.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Path, match } from '../../src/match'
2+
3+
export interface Scenario {
4+
given: Path
5+
when: Array<{
6+
actual: string
7+
it: {
8+
[key: string]: any
9+
} & ReturnType<typeof match>
10+
}>
11+
}
12+
13+
export const runner = (name: string, scenarios: Scenario[]) => {
14+
describe(name, () => {
15+
scenarios.forEach((scenario) => {
16+
describe(`given "${scenario.given}" path`, () => {
17+
scenario.when.forEach((data) => {
18+
describe(`and "${data.actual}" url`, () => {
19+
let result: ReturnType<typeof match>
20+
21+
beforeAll(() => {
22+
result = match(scenario.given, data.actual)
23+
})
24+
25+
Object.entries(data.it).forEach(([key, expectedValue]) => {
26+
it(`"${key}": "${JSON.stringify(expectedValue)}"`, () => {
27+
expect(result).toHaveProperty(key, expectedValue)
28+
})
29+
})
30+
})
31+
})
32+
})
33+
})
34+
})
35+
}

0 commit comments

Comments
 (0)