/
index.test.ts
193 lines (156 loc) · 4.84 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
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
import type { Ref } from 'vue-demi'
import { ref } from 'vue-demi'
import { invoke } from '@vueuse/shared'
import type { Equal, Expect } from '@type-challenges/utils'
import { until } from '.'
describe('until', () => {
it('should work', () => {
return new Promise<void>((resolve, reject) => {
const r = ref(0)
invoke(async () => {
expect(r.value).toBe(0)
const x = await until(r).toBe(1)
expect(x).toBe(1)
resolve()
}).catch(reject)
setTimeout(() => {
r.value = 1
}, 100)
})
})
it('should work for changedTimes', () => {
return new Promise<void>((resolve, reject) => {
const r = ref(0)
invoke(async () => {
expect(r.value).toBe(0)
const x = await until(r).changedTimes(3)
expect(x).toBe(3)
resolve()
}).catch(reject)
setTimeout(() => {
r.value = 1
r.value = 2
r.value = 3
}, 100)
})
})
it('should support `not`', () => {
return new Promise<void>((resolve, reject) => {
const r = ref(0)
invoke(async () => {
expect(r.value).toBe(0)
const x = await until(r).not.toBe(0)
expect(x).toBe(1)
resolve()
}).catch(reject)
setTimeout(() => {
r.value = 1
}, 100)
})
})
it('should support `not` as separate instances', () => {
return new Promise<void>((resolve, reject) => {
const r = ref(0)
invoke(async () => {
expect(r.value).toBe(0)
const instance = until(r)
const x = await instance.not.toBe(0)
const y = await instance.not.toBe(2)
expect(x).toBe(1)
expect(y).toBe(1)
resolve()
}).catch(reject)
setTimeout(() => {
r.value = 1
}, 100)
})
})
it('should support toBeNull()', () => {
return new Promise<void>((resolve, reject) => {
const r = ref<number | null>(null)
invoke(async () => {
expect(r.value).toBe(null)
const x = await until(r).not.toBeNull()
expect(x).toBe(1)
resolve()
}).catch(reject)
setTimeout(() => {
r.value = 1
}, 100)
})
})
it('should support array', () => {
return new Promise<void>((resolve, reject) => {
const r = ref<number[]>([1, 2, 3])
invoke(async () => {
expect(r.value).toEqual([1, 2, 3])
const x = await until(r).toContains(4, { deep: true })
expect(x).toEqual([1, 2, 3, 4])
resolve()
}).catch(reject)
setTimeout(() => {
r.value.push(4)
}, 100)
})
})
it('should support array with not', () => {
return new Promise<void>((resolve, reject) => {
const r = ref<number[]>([1, 2, 3])
invoke(async () => {
expect(r.value).toEqual([1, 2, 3])
const x = await until(r).not.toContains(2, { deep: true })
expect(x).toEqual([1])
resolve()
}).catch(reject)
setTimeout(() => {
r.value.pop()
r.value.pop()
}, 100)
})
})
it('should immediately timeout', () => {
return new Promise<void>((resolve, reject) => {
const r = ref(0)
invoke(async () => {
expect(r.value).toBe(0)
const x = await until(r).toBe(1, { timeout: 0 })
expect(x).toBe(0)
resolve()
}).catch(reject)
setTimeout(() => {
r.value = 1
}, 100)
})
})
it('should type check', () => {
async () => {
const x = ref<'x'>()
// type checks are done this way to prevent unused variable warnings
// and duplicate name warnings
'test' as any as Expect<Equal<typeof x, Ref<'x' | undefined>>>
const one = await until(x).toBe(1 as const)
'test' as any as Expect<Equal<typeof one, 1>>
const xTruthy = await until(x).toBeTruthy()
'test' as any as Expect<Equal<typeof xTruthy, 'x'>>
const xFalsy = await until(x).not.toBeTruthy()
'test' as any as Expect<Equal<typeof xFalsy, undefined>>
const xUndef = await until(x).toBeUndefined()
'test' as any as Expect<Equal<typeof xUndef, undefined>>
const xNotUndef = await until(x).not.toBeUndefined()
'test' as any as Expect<Equal<typeof xNotUndef, 'x'>>
const y = ref<'y' | null>(null)
'test' as any as Expect<Equal<typeof y, Ref<'y' | null>>>
const yNull = await until(y).toBeNull()
'test' as any as Expect<Equal<typeof yNull, null>>
const yNotNull = await until(y).not.toBeNull()
'test' as any as Expect<Equal<typeof yNotNull, 'y'>>
const z = ref<1 | 2 | 3>(1)
'test' as any as Expect<Equal<typeof z, Ref<1 | 2 | 3>>>
const is1 = (x: number): x is 1 => x === 1
const z1 = await until(z).toMatch(is1)
'test' as any as Expect<Equal<typeof z1, 1>>
const zNot1 = await until(z).not.toMatch(is1)
'test' as any as Expect<Equal<typeof zNot1, 2 | 3>>
}
})
})