@@ -4,8 +4,8 @@ const mockBindState = jest.fn()
44const mockBindSubscriber = jest . fn ( )
55const mockReducer = jest . fn ( )
66
7- const dataObj = "variable test data"
8- const subscriberObj = { }
7+ const testSubscriberData = "variable test data"
8+ const testSubscriber = { }
99
1010function testReducer ( state , action ) {
1111 if ( action . type === "TEST_TYPE" ) {
@@ -26,80 +26,78 @@ describe("createStore", () => {
2626 describe ( "artifacts" , ( ) => {
2727 it ( "returns dispatch and subscribe helper functions" , ( ) => {
2828 // Given
29- const store = getMockStore ( )
29+ const Store = getMockStore ( )
3030
3131 // Then
32- expect ( store ) . toEqual ( expect . any ( Object ) )
33- expect ( store . dispatch ) . toEqual ( expect . any ( Function ) )
34- expect ( store . subscribe ) . toEqual ( expect . any ( Function ) )
32+ expect ( Store ) . toEqual ( expect . any ( Object ) )
33+ expect ( Store . dispatch ) . toEqual ( expect . any ( Function ) )
34+ expect ( Store . subscribe ) . toEqual ( expect . any ( Function ) )
3535 } )
3636
3737 it ( "returns live data pointer" , ( ) => {
3838 // Given
39- const store = getMockStore ( )
39+ const Store = getMockStore ( )
4040
4141 // Then
42- expect ( store . __data ) . toEqual (
42+ expect ( Store . __data ) . toEqual (
4343 expect . objectContaining ( { state : { } , subscriptions : [ ] } )
4444 )
4545 } )
4646 } )
4747
4848 describe ( "state bindings" , ( ) => {
4949 const TEST_TYPE = "TEST_TYPE"
50- const payload = { foo : "bar" }
50+ const testPayload = { foo : "bar" }
5151
5252 it ( "calls reducer on dispatch" , ( ) => {
5353 // Given
54- const store = getMockStore ( )
54+ const Store = getMockStore ( )
5555
5656 // When
57- store . dispatch ( TEST_TYPE , payload )
57+ Store . dispatch ( TEST_TYPE , testPayload )
5858
5959 // Then
6060 expect ( mockReducer ) . toBeCalledWith (
61- expect . any ( Object ) ,
62- expect . objectContaining ( { payload, type : TEST_TYPE } )
61+ Store . __data . state ,
62+ expect . objectContaining ( { payload : testPayload , type : TEST_TYPE } )
6363 )
6464 } )
6565
6666 it ( "calls state binding on dispatch" , ( ) => {
6767 // Given
68- const store = createStore (
68+ const Store = createStore (
6969 { } ,
7070 testReducer ,
7171 mockBindSubscriber ,
7272 mockBindState
7373 )
7474
7575 // When
76- store . subscribe ( subscriberObj , dataObj )
77- store . dispatch ( TEST_TYPE , payload )
76+ Store . subscribe ( testSubscriber , testSubscriberData )
77+ Store . dispatch ( TEST_TYPE , testPayload )
7878
7979 // Then
8080 expect ( mockBindState ) . toBeCalledWith (
81- expect . arrayContaining ( [
82- expect . arrayContaining ( [ subscriberObj , dataObj ] ) ,
83- ] ) ,
84- { foo : "bar" } ,
81+ Store . __data . subscriptions ,
82+ expect . objectContaining ( { foo : "bar" } ) ,
8583 expect . any ( Function )
8684 )
8785 } )
8886
89- it ( "sets reduced state back to store using setState helper" , ( ) => {
87+ it ( "sets reduced state back to store.state using setState helper" , ( ) => {
9088 // Given
91- const store = createStore (
89+ const Store = createStore (
9290 { } ,
9391 testReducer ,
9492 mockBindSubscriber ,
9593 testBindState
9694 )
9795
9896 // When
99- store . dispatch ( TEST_TYPE , payload )
97+ Store . dispatch ( TEST_TYPE , testPayload )
10098
10199 // Then
102- expect ( store . __data . state ) . toEqual (
100+ expect ( Store . __data . state ) . toEqual (
103101 expect . objectContaining ( { foo : "bar" } )
104102 )
105103 } )
@@ -108,15 +106,30 @@ describe("createStore", () => {
108106 describe ( "subscriber bindings" , ( ) => {
109107 it ( "calls subscriber binding when subscribe is called" , ( ) => {
110108 // Given
111- const store = getMockStore ( )
109+ const Store = getMockStore ( )
112110
113111 // When
114- store . subscribe ( subscriberObj , dataObj )
112+ Store . subscribe ( testSubscriber , testSubscriberData )
115113
116114 // Then
117115 expect ( mockBindSubscriber ) . toBeCalledWith (
118- expect . arrayContaining ( [ subscriberObj , dataObj ] ) ,
119- expect . any ( Object )
116+ Store . __data . subscriptions [ 0 ] ,
117+ Store . __data . state
118+ )
119+ } )
120+
121+ it ( "adds subscription to store.subscriptions" , ( ) => {
122+ // Given
123+ const Store = getMockStore ( )
124+
125+ // When
126+ Store . subscribe ( testSubscriber , testSubscriberData )
127+
128+ // Then
129+ expect ( Store . __data . subscriptions ) . toEqual (
130+ expect . arrayContaining ( [
131+ expect . arrayContaining ( [ testSubscriber , testSubscriberData ] ) ,
132+ ] )
120133 )
121134 } )
122135 } )
0 commit comments