-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.test.js
46 lines (37 loc) · 1.09 KB
/
index.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
import { describe, expect, it } from '@jest/globals'
import assert from 'node:assert'
import DefaultStore from './index.js'
describe('DefaultStore', () => {
it('cannot be created without new', () => {
assert.throws(() => DefaultStore(), /TypeError/)
})
it('merges in state using `setState`', () => {
const store = new DefaultStore()
expect(store.getState()).toEqual({})
store.setState({
a: 1,
b: 2,
})
expect(store.getState()).toEqual({ a: 1, b: 2 })
store.setState({ b: 3 })
expect(store.getState()).toEqual({ a: 1, b: 3 })
})
it('notifies subscriptions when state changes', () => {
let expected = []
let calls = 0
function listener (prevState, nextState, patch) {
calls++
expect([prevState, nextState, patch]).toEqual(expected)
}
const store = new DefaultStore()
store.subscribe(listener)
expected = [{}, { a: 1, b: 2 }, { a: 1, b: 2 }]
store.setState({
a: 1,
b: 2,
})
expected = [{ a: 1, b: 2 }, { a: 1, b: 3 }, { b: 3 }]
store.setState({ b: 3 })
expect(calls).toBe(2)
})
})