-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.ts
136 lines (118 loc) · 2.87 KB
/
index.test.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
import test from 'ava'
import expect from 'expect'
import DynamicEnvironment, { demand } from './index'
const sample = <T>(array: T[]): T => array[Math.floor(Math.random() * array.length)]!
test('detects context while giving FIFO preference', (t) => {
let a = true
let b = true
let c = true
const env = new DynamicEnvironment({
A: () => a,
B: () => b,
C: () => c,
})
t.is(env.revealContext(), 'A')
t.is(env.pick({ A: 1, B: 2, C: 3 }), 1)
a = false
t.is(env.revealContext(), 'B')
t.is(env.pick({ A: 1, B: 2, C: 3 }), 2)
a = false
b = false
t.is(env.revealContext(), 'C')
t.is(env.pick({ A: 1, B: 2, C: 3 }), 3)
a = false
b = false
c = false
t.is(env.revealContext(), null)
t.throws(() => env.pick({ A: 1, B: 2, C: 3 }), {
message: `No compatible context has been detected`,
})
})
test('throws error when values are not found', (t) => {
t.throws(
() =>
new DynamicEnvironment({
A: () => false,
B: () => true,
}).pick({ A: 1 }),
{ message: `No respective value found for active context` }
)
})
test('lists available contexts', (t) => {
t.deepEqual(
new DynamicEnvironment({
A: () => {},
B: () => {},
}).contexts,
{ A: 'A', B: 'B' }
)
})
test('returns demanded values when possible', (t) => {
t.deepEqual(demand(1), 1)
t.deepEqual(demand(2, 'MY_VAR'), 2)
t.deepEqual(demand(false, 'MY_VAR'), false)
t.deepEqual(demand('undefined', 'MY_VAR'), 'undefined')
})
test('throws error when demanded values are not found', (t) => {
let a: string
t.throws(() => demand(a), { message: `required variable has not been defined` })
t.throws(() => demand(undefined, 'MY_VAR'), {
message: `required variable 'MY_VAR' has not been defined`,
})
})
test('builds a static tree with no magic', (t) => {
const env = new DynamicEnvironment({
WRONG: () => false,
RIGHT: () => true,
})
let best = 'nextjs'
let worst = () => sample(['angularjs', 'angular'])
t.deepEqual(
{
framework: {
best: env.pick({
RIGHT: demand(best),
WRONG: 'meteor',
}),
worst: env.pick({
WRONG: 'emberjs',
RIGHT: worst,
}),
},
},
{
framework: {
best: 'nextjs',
worst,
},
}
)
})
test('builds a static tree executing the functions', (t) => {
const env = new DynamicEnvironment({
WRONG: () => false,
RIGHT: () => true,
})
let best = 'nextjs'
let worst = () => sample(['angularjs', 'angular'])
expect(
env.tree({
framework: {
best: env.pick({
RIGHT: demand(best),
WRONG: 'meteor',
}),
worst: env.pick({
WRONG: 'emberjs',
RIGHT: worst,
}),
},
})
).toEqual({
framework: {
best: 'nextjs',
worst: expect.stringMatching(/angular(js)?/),
},
})
t.pass()
})