You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.1 KiB
49 lines
1.1 KiB
|
2 years ago
|
import { describe, expect, it } from '@jest/globals'
|
||
|
|
import DefaultStore from './index.js'
|
||
|
|
|
||
|
|
describe('DefaultStore', () => {
|
||
|
|
it('can be created with or without new', () => {
|
||
|
|
let store = DefaultStore()
|
||
|
|
expect(typeof store).toBe('object')
|
||
|
|
store = new DefaultStore()
|
||
|
|
expect(typeof store).toBe('object')
|
||
|
|
})
|
||
|
|
|
||
|
|
it('merges in state using `setState`', () => {
|
||
|
|
const store = 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 = 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)
|
||
|
|
})
|
||
|
|
})
|