-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.spec.ts
165 lines (136 loc) · 4.93 KB
/
main.spec.ts
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
import { _def, match } from "./main"
describe("Helper", () => {
describe("match", () => {
it("throws if matcher is not an object or an array", () => {
const disallowedMatchers = [
new Function(),
class {},
"Str",
42,
true,
null,
undefined,
]
disallowedMatchers.forEach((matcher) => {
expect(() => match("")(matcher)).toThrow(TypeError)
expect(() => match("")(matcher)).toThrow(
/`matcher` has to be either an Object or an Array/,
)
})
})
it("throws if no arms present when matcher is an object", () => {
const matcher = {}
expect(() => match("")(matcher)).toThrow()
expect(() => match("")(matcher)).toThrow(
/`matcher` has to contain at least one arm/,
)
})
it("throws if no arms present when matcher is an array", () => {
const matcher = [[]]
expect(() => match("")(...matcher)).toThrow()
expect(() => match("")(...matcher)).toThrow(
/`matcher` has to contain at least one arm/,
)
})
it("throws an error if `_def` is not present on the matcher object", () => {
const config = {
explicitMatch: Symbol("ExplicitMatch"),
matchedValue: "Matched Value",
}
const matcher = {
[config.explicitMatch]: config.matchedValue,
}
expect(() => match(config.explicitMatch)(matcher)).toThrow(ReferenceError)
expect(() => match(config.explicitMatch)(matcher)).toThrow(
/`matcher` needs to contain a default `_def` arm/,
)
})
// it('has `_def` property available', () => {
// expect((match as any)._def).toEqual(_def)
// })
describe("basic", () => {
it("correctly matches against a value, returns correct result", () => {
const config = {
explicitMatch: Symbol("ExplicitMatch"),
matchedValue: "Matched Value",
defaultValue: "Default Value",
}
const matcher = {
[config.explicitMatch]: config.matchedValue,
[_def]: config.defaultValue,
}
expect(match(config.explicitMatch)(matcher)).toEqual(config.matchedValue)
})
it("correctly matches against a value, calls correct result", () => {
const config = {
explicitMatch: Symbol("ExplicitMatch"),
matchedValue: jest.fn(),
defaultValue: "Default Value",
}
const matcher = {
[config.explicitMatch]: config.matchedValue,
[_def]: config.defaultValue,
}
match(config.explicitMatch)(matcher)
expect(config.matchedValue).toHaveBeenCalledWith(config.explicitMatch)
})
})
describe("advanced", () => {
it("correctly matches against a value, returns correct result", () => {
const config = {
explicitMatch: Symbol("ExplicitMatch"),
matchedValue: "Matched Value",
defaultValue: "Default Value",
}
const matcher = [
[config.explicitMatch, config.matchedValue],
[_def, config.defaultValue],
]
expect(match(config.explicitMatch)(...matcher)).toEqual(config.matchedValue)
})
it("correctly matches against a value, calls correct result", () => {
const config = {
explicitMatch: Symbol("ExplicitMatch"),
matchedValue: jest.fn(),
defaultValue: "Default Value",
}
const matcher = [
[config.explicitMatch, config.matchedValue],
[_def, config.defaultValue],
]
match(config.explicitMatch)(...matcher)
expect(config.matchedValue).toHaveBeenCalledWith(config.explicitMatch)
})
it("correctly matches against an assertion, returns correct result", () => {
const explicitMatch = "Hello, World!"
const config = {
truthyAssertionMatch: (val: string) => val === explicitMatch,
falsyAssertionMatch: (val: string) => val.length === explicitMatch.length + 1,
matchedValue: "Matched Value",
unmatchedValue: "Unmatched Value",
defaultValue: "Default Value",
}
const matcher = [
[config.truthyAssertionMatch, config.matchedValue],
[config.falsyAssertionMatch, config.unmatchedValue],
[_def, config.defaultValue],
]
expect(match(explicitMatch)(...matcher)).toEqual(config.matchedValue)
})
it("correctly matches against `_def`, returns correct result", () => {
const config = {
falsyAssertionMatch: () => false,
nonMatch: Symbol("Something"),
unmatchedValue: "Unmatched Value",
defaultValue: "Default Value",
}
const matcher = [
[config.falsyAssertionMatch, config.unmatchedValue],
[config.nonMatch, config.unmatchedValue],
[_def, config.defaultValue],
]
expect(match("")(...matcher)).toEqual(config.defaultValue)
})
})
})
})