1+ import React from 'react' ;
2+ import { ReduxFocus } from 'react-redux-focus' ;
3+ import { connect , connectAndCheck } from '../src' ;
4+
5+ import EnzymeReactAdapter from 'enzyme-adapter-react-16' ;
6+ import { mount , configure as configureEnzyme } from 'enzyme' ;
7+
8+ configureEnzyme ( { adapter : new EnzymeReactAdapter ( ) } ) ;
9+
10+
11+ describe ( 'Test' , ( ) => {
12+ it ( 'should secure memoization' , ( ) => {
13+ const cb = jest . fn ( ) ;
14+ const rb = jest . fn ( ) ;
15+ const mapStateToProps = state => {
16+ cb ( ) ;
17+ return { number : state . key }
18+ } ;
19+
20+ const Component = ( { number} ) => {
21+ rb ( ) ;
22+ return < div > { number } </ div >
23+ } ;
24+ const ConnectedComponent = connect ( mapStateToProps ) ( Component ) ;
25+
26+ const wrapper = mount (
27+ < ReduxFocus focus = { ( state , props ) => props . state } state = { { key : 'key1-value' , key2 : 2 } } >
28+ < ConnectedComponent />
29+ </ ReduxFocus >
30+ ) ;
31+ expect ( wrapper . html ( ) ) . toMatch ( / k e y 1 - v a l u e / ) ;
32+ expect ( cb ) . toHaveBeenCalledTimes ( 1 ) ;
33+ expect ( rb ) . toHaveBeenCalledTimes ( 1 ) ;
34+
35+ wrapper . setProps ( { state : { key : 'key1-value' , key2 : 3 } } ) ;
36+
37+ expect ( wrapper . html ( ) ) . toMatch ( / k e y 1 - v a l u e / ) ;
38+ expect ( cb ) . toHaveBeenCalledTimes ( 1 ) ;
39+ expect ( rb ) . toHaveBeenCalledTimes ( 1 ) ;
40+
41+ wrapper . setProps ( { state : { key : 'key2-value' , key2 : 3 } } ) ;
42+
43+ expect ( wrapper . html ( ) ) . toMatch ( / k e y 2 - v a l u e / ) ;
44+ expect ( cb ) . toHaveBeenCalledTimes ( 2 ) ;
45+ expect ( rb ) . toHaveBeenCalledTimes ( 2 ) ;
46+
47+ expect ( ConnectedComponent . __trackingPaths ) . toEqual ( [ 'key' ] ) ;
48+ } ) ;
49+
50+ it ( 'should maintain per-instance memoization' , ( ) => {
51+ const cb = jest . fn ( ) ;
52+ const mapStateToProps = state => {
53+ cb ( ) ;
54+ return { number : state . key }
55+ } ;
56+
57+ const Component = ( { number} ) => < div > { number } </ div > ;
58+ const ConnectedComponent = connect ( mapStateToProps ) ( Component ) ;
59+
60+ const wrapper = mount (
61+ < ReduxFocus focus = { ( state , props ) => props . state } state = { { key1 : 'key1-value' , key2 : 'key2-value' } } >
62+ < ReduxFocus focus = { ( state ) => ( { key : state . key1 } ) } >
63+ < ConnectedComponent />
64+ </ ReduxFocus >
65+ < ReduxFocus focus = { ( state ) => ( { key : state . key2 } ) } >
66+ < ConnectedComponent />
67+ </ ReduxFocus >
68+ </ ReduxFocus >
69+ ) ;
70+ expect ( cb ) . toHaveBeenCalledTimes ( 2 ) ;
71+
72+ wrapper . setProps ( { state : { key1 : 'key1-value' , key2 : 'key2-value' , key3 : 'key3' } } ) ;
73+ expect ( cb ) . toHaveBeenCalledTimes ( 2 ) ;
74+
75+ wrapper . setProps ( { state : { key1 : 'key1-1value' , key2 : 'key2-value' } } ) ;
76+ expect ( cb ) . toHaveBeenCalledTimes ( 3 ) ;
77+
78+ wrapper . setProps ( { state : { key1 : 'key1-1value' , key2 : 'key2-1value' } } ) ;
79+ expect ( cb ) . toHaveBeenCalledTimes ( 4 ) ;
80+
81+ wrapper . setProps ( { state : { key1 : 'key1-value' , key2 : 'key2-value' } } ) ;
82+ expect ( cb ) . toHaveBeenCalledTimes ( 6 ) ;
83+
84+ expect ( ConnectedComponent . __trackingPaths ) . toEqual ( [ 'key' ] ) ;
85+ } ) ;
86+
87+ it ( 'should maintain per-instance memoization' , ( ) => {
88+ const cb = jest . fn ( ) ;
89+ const mapStateToProps = ( state , props ) => {
90+ cb ( ) ;
91+ return { number : state [ props . keyName ] , test : state . flag ? state . secret : 0 }
92+ } ;
93+
94+ const Component = ( { number} ) => < div > { number } </ div > ;
95+ const ConnectedComponent = connect ( mapStateToProps ) ( Component ) ;
96+
97+ const wrapper = mount (
98+ < ReduxFocus focus = { ( state , props ) => props . state } state = { { key1 : 'key1-value' , key2 : 'key2-value' } } >
99+ < ConnectedComponent keyName = "key1" />
100+ < ConnectedComponent keyName = "key2" />
101+ </ ReduxFocus >
102+ ) ;
103+ expect ( cb ) . toHaveBeenCalledTimes ( 2 ) ;
104+
105+ wrapper . setProps ( { state : { key1 : 'key1-value' , key2 : 'key2-value' , key3 : 'key3' } } ) ;
106+ expect ( cb ) . toHaveBeenCalledTimes ( 2 ) ;
107+
108+ expect ( ConnectedComponent . __trackingPaths ) . toEqual ( [ 'key1' , 'flag' , 'key2' ] ) ;
109+
110+ wrapper . setProps ( { state : { key1 : 'key1-1value' , key2 : 'key2-value' , flag : true } } ) ;
111+ expect ( cb ) . toHaveBeenCalledTimes ( 4 ) ;
112+ expect ( ConnectedComponent . __trackingPaths ) . toEqual ( [ 'key1' , 'flag' , 'key2' , 'secret' ] ) ;
113+ } ) ;
114+
115+ it ( 'should check the purity of a function' , ( ) => {
116+
117+ const mapStateToProps = ( state , props ) => ( {
118+ number : state [ props . keyName ] ,
119+ test : state . flag ? state . secret : 0 ,
120+ subObjec : { }
121+ } ) ;
122+
123+ const Component = ( { number} ) => < div > { number } </ div > ;
124+
125+ const spy = jest . spyOn ( global . console , 'error' )
126+
127+ const ConnectedComponent = connectAndCheck ( mapStateToProps ) ( Component ) ;
128+ const wrapper = mount (
129+ < ReduxFocus focus = { ( state , props ) => props . state } state = { { key1 : 'key1-value' } } >
130+ < ConnectedComponent keyName = "key1" />
131+ </ ReduxFocus >
132+ ) ;
133+ wrapper . setProps ( { state : { key1 : 'key1-value' , key2 :2 } } ) ;
134+
135+ expect ( spy ) . toHaveBeenCalledWith ( "shouldBePure" , expect . anything ( ) , "`s result is not equal at [subObjec], while should be equal" ) ;
136+
137+ spy . mockRestore ( ) ;
138+ } ) ;
139+
140+
141+ } ) ;
0 commit comments