-
Notifications
You must be signed in to change notification settings - Fork 0
/
Store.test.js
64 lines (49 loc) · 1.33 KB
/
Store.test.js
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
/* globals describe, it, expect */
const Store = require('./Store')
describe('Store', () => {
it('can be created', () => {
const s = new Store()
expect(s).toBeTruthy()
})
it('supports transaction semantics', () => {
const s = new Store()
const result = s.transact(state => {
state.x = 100
})
expect(result.then).toBeInstanceOf(Function)
})
it('transactions resolve to returned value', () => {
const s = new Store()
const result = s.transact(state => 10)
expect(result).resolves.toEqual(10)
})
it('successful transactions get retained', async () => {
const s = new Store()
await s.transact(state => {
state.x = 1
})
return s.transact(state => {
expect(state.x).toEqual(1)
})
})
it('failing transactions get rolledback', async () => {
const s = new Store()
expect(s.transact(state => {
state.x = 1
throw new Error('ROLLBACK')
})).rejects.toMatchObject({message: 'ROLLBACK'})
return s.transact(state => {
expect(state.x).toBeUndefined()
})
})
it('works with deleting props', async () => {
const s = new Store()
await s.transact(state => {
state.x = {a: 1}
})
await s.transact(state => {
delete state.x.a
})
expect(await s.transact(state => state.x.a)).toBeUndefined()
})
})