forked from hplush/slowreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.test.ts
59 lines (52 loc) · 1.3 KB
/
environment.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
import { atom } from 'nanostores'
import { deepStrictEqual, equal, throws } from 'node:assert'
import { test } from 'node:test'
import {
getEnvironment,
getTestEnvironment,
onEnvironment,
setupEnvironment
} from '../index.ts'
test('throws on current environment if it is not set', () => {
throws(() => {
getEnvironment()
}, 'Error: No Slow Reader environment')
})
test('runs callback when environment will be set', () => {
let calls: string[] = []
let cleans = ''
onEnvironment(env => {
calls.push(env.locale.get())
return () => {
cleans += '1'
}
})
onEnvironment(() => {
return [
() => {
cleans += '2'
},
() => {
cleans += '3'
}
]
})
deepStrictEqual(calls, [])
setupEnvironment({ ...getTestEnvironment(), locale: atom('fr') })
deepStrictEqual(calls, ['fr'])
equal(cleans, '')
setupEnvironment({ ...getTestEnvironment(), locale: atom('ru') })
deepStrictEqual(calls, ['fr', 'ru'])
equal(cleans, '123')
let after: string[] = []
onEnvironment(env => {
after.push(env.locale.get())
})
deepStrictEqual(after, ['ru'])
equal(cleans, '123')
})
test('returns current environment', () => {
let locale = atom('fr')
setupEnvironment({ ...getTestEnvironment(), locale })
equal(getEnvironment().locale, locale)
})