Skip to content

Commit c6a867a

Browse files
author
george treviranus
committed
add unit tests
1 parent 5798f28 commit c6a867a

File tree

3 files changed

+75
-28
lines changed

3 files changed

+75
-28
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ Once a store is created, you'll be able to add subscriptions with `subscribe` an
8686

8787
Here's a breakdown of each binding needed when initializing a new store:
8888

89-
#### `reducer(type, state, payload = {})`
89+
#### `reducer(state, { type, payload } = action)`
9090

91-
> `type (string)`: The action dispatched.<br/>`state (object)`: A copy of the current state object.<br/>`payload (object={})`: The payload given during dispatch.
91+
> `state (object)`: A copy of the current state object.<br/>`action ({ type, payload })`: The dispatched action type and its payload.
9292
9393
Creates a new version of state and returns it, based on the `type` and `payload`. If the return value is falsy, nothing happens.
9494

__tests__/core-flux.spec.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { createStore } from "../core-flux"
2+
3+
const testBindState = jest.fn()
4+
const testBindSubscriber = jest.fn()
5+
const testReducer = jest.fn()
6+
7+
describe("createStore", () => {
8+
let store,
9+
data = "data",
10+
subscriber = {},
11+
type = "TEST_TYPE",
12+
payload = {}
13+
14+
beforeAll(() => {
15+
store = createStore({}, testReducer, testBindSubscriber, testBindState)
16+
})
17+
18+
it("creates dispatch and subscribe helper functions", () => {
19+
expect(store).toEqual(expect.any(Object))
20+
expect(store.dispatch).toEqual(expect.any(Function))
21+
expect(store.subscribe).toEqual(expect.any(Function))
22+
})
23+
24+
it("calls subscriber binding when subscription is added", () => {
25+
store.subscribe(subscriber, data)
26+
27+
expect(testBindSubscriber).toBeCalledWith(
28+
expect.arrayContaining([subscriber, data]),
29+
expect.any(Object)
30+
)
31+
})
32+
33+
it("calls reducer when dispatch is made", () => {
34+
store.dispatch(type, payload)
35+
36+
expect(testReducer).toBeCalledWith(
37+
expect.any(Object),
38+
expect.objectContaining({ type, payload })
39+
)
40+
})
41+
42+
it("calls state binding when dispatch is made", () => {
43+
store.dispatch(type, payload)
44+
45+
expect(testBindState).toBeCalledWith(
46+
expect.arrayContaining([expect.arrayContaining([subscriber, data])]),
47+
undefined,
48+
expect.any(Function)
49+
)
50+
})
51+
})

core-flux.js

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,40 +29,30 @@ function clone(obj) {
2929
const isArray = Array.isArray(obj)
3030

3131
let value,
32-
newObj = isArray ? [] : {}
32+
copyObj = isArray ? [] : {}
3333

3434
if (isArray) {
3535
let length = obj.length,
3636
idx = -1
3737
while (++idx < length) {
38-
newObj[idx] = obj[idx]
38+
copyObj[idx] = obj[idx]
3939
}
4040
} else {
4141
for (const key in obj) {
4242
value = obj[key]
43-
newObj[key] = newObj[key] =
43+
copyObj[key] = copyObj[key] =
4444
value === null ? null : typeof value === "object" ? clone(value) : value
4545
}
4646
}
4747

48-
return newObj
48+
return copyObj
4949
}
5050

51-
/**
52-
* Append a new state object to a Store.
53-
* @param {string} id
54-
* @param {{}} initialState
55-
*/
56-
function initState(id, initialState = {}) {
57-
Stores[id].state = initialState
58-
}
59-
60-
/**
61-
* Append a new subscriber array to a Store.
62-
* @param {string} id
63-
*/
64-
function initSubscriptions(id) {
65-
Stores[id].subscriptions = []
51+
function setInitialStore(id, initialState) {
52+
Stores[id] = {
53+
state: initialState,
54+
subscriptions: [],
55+
}
6656
}
6757

6858
/**
@@ -88,21 +78,26 @@ function setState(id, nextState = {}) {
8878
* @param {Function} updater
8979
* @returns {{dispatch: Function, subscribe: Function}}
9080
*/
91-
export function createStore(initialState, reducer, bindSubscriber, bindState) {
81+
export function createStore(
82+
initialState = {},
83+
reducer,
84+
bindSubscriber,
85+
bindState
86+
) {
9287
const id = createId()
93-
94-
Stores[id] = {}
95-
initState(id, initialState)
96-
initSubscriptions(id)
88+
setInitialStore(id, initialState)
9789

9890
return {
9991
dispatch(type, payload) {
10092
const state = getState(id)
101-
const nextState = reducer(type, state, payload || {})
93+
const reducedState = reducer(state, {
94+
type,
95+
payload: payload || {},
96+
})
10297

10398
return bindState(
10499
Stores[id].subscriptions,
105-
nextState,
100+
reducedState,
106101
function (rawNextState) {
107102
return setState(id, rawNextState)
108103
}
@@ -119,5 +114,6 @@ export function createStore(initialState, reducer, bindSubscriber, bindState) {
119114
const length = subscriptions.length
120115
bindSubscriber(subscriptions[length - 1], state)
121116
},
117+
// data: Stores[id],
122118
}
123119
}

0 commit comments

Comments
 (0)