@@ -3,36 +3,38 @@ require = require("@std/esm")(module, {esm: "js"});
3
3
const assert = require ( "assert" ) ;
4
4
const { createStore } = require ( "../index" )
5
5
6
- function counter ( state = 0 , action ) {
7
- switch ( action ) {
8
- case "INCREMENT" :
9
- return state + 1 ;
10
- default :
11
- return state ;
12
- }
13
- }
14
-
15
6
suite ( "connect" , function ( ) {
16
7
let store ;
17
8
let root ;
18
9
10
+ function counter ( state = 0 , action ) {
11
+ switch ( action ) {
12
+ case "INCREMENT" :
13
+ return state + 1 ;
14
+ default :
15
+ return state ;
16
+ }
17
+ }
18
+
19
19
setup ( function ( ) {
20
20
store = createStore ( counter ) ;
21
21
root = document . createElement ( "div" ) ;
22
22
} ) ;
23
23
24
- test ( "passes the current state as the first argument" , function ( ) {
24
+ test ( "pass the current state as the first argument" , function ( ) {
25
25
const { attach, connect, dispatch } = store ;
26
26
27
27
const TestApp = spyFunction ( ) ;
28
28
const ConnectedTestApp = connect ( TestApp ) ;
29
+
29
30
attach ( ConnectedTestApp , root ) ;
30
- dispatch ( "INCREMENT" ) ;
31
+ assert . deepEqual ( TestApp . args ( ) , [ 0 ] ) ;
31
32
33
+ dispatch ( "INCREMENT" ) ;
32
34
assert . deepEqual ( TestApp . args ( ) , [ 1 ] ) ;
33
35
} ) ;
34
36
35
- test ( "passes other args after the state" , function ( ) {
37
+ test ( "pass other args after the state" , function ( ) {
36
38
const { attach, connect, dispatch } = store ;
37
39
38
40
const TestComponent = spyFunction ( ) ;
@@ -43,8 +45,54 @@ suite("connect", function() {
43
45
}
44
46
45
47
attach ( TestApp , root ) ;
46
- dispatch ( "INCREMENT" ) ;
48
+ assert . deepEqual ( TestComponent . args ( ) , [ 0 , "Foo" ] ) ;
47
49
50
+ dispatch ( "INCREMENT" ) ;
48
51
assert . deepEqual ( TestComponent . args ( ) , [ 1 , "Foo" ] ) ;
49
52
} ) ;
50
53
} ) ;
54
+
55
+ suite ( "connect with a selector" , function ( ) {
56
+ let store ;
57
+ let root ;
58
+
59
+ function counter ( state = { current : 0 } , action ) {
60
+ switch ( action ) {
61
+ case "INCREMENT" :
62
+ return { current : state . current + 1 } ;
63
+ default :
64
+ return state ;
65
+ }
66
+ }
67
+
68
+ setup ( function ( ) {
69
+ store = createStore ( counter ) ;
70
+ root = document . createElement ( "div" ) ;
71
+ } ) ;
72
+
73
+ test ( "use the identity selector by default" , function ( ) {
74
+ const { attach, connect, dispatch } = store ;
75
+
76
+ const TestApp = spyFunction ( ) ;
77
+ const ConnectedTestApp = connect ( TestApp ) ;
78
+
79
+ attach ( ConnectedTestApp , root ) ;
80
+ assert . deepEqual ( TestApp . args ( ) , [ { current : 0 } ] ) ;
81
+
82
+ dispatch ( "INCREMENT" ) ;
83
+ assert . deepEqual ( TestApp . args ( ) , [ { current : 1 } ] ) ;
84
+ } ) ;
85
+
86
+ test ( "apply the selector to the state" , function ( ) {
87
+ const { attach, connect, dispatch } = store ;
88
+
89
+ const TestApp = spyFunction ( ) ;
90
+ const ConnectedTestApp = connect ( TestApp , state => state . current ) ;
91
+
92
+ attach ( ConnectedTestApp , root ) ;
93
+ assert . deepEqual ( TestApp . args ( ) , [ 0 ] ) ;
94
+
95
+ dispatch ( "INCREMENT" ) ;
96
+ assert . deepEqual ( TestApp . args ( ) , [ 1 ] ) ;
97
+ } ) ;
98
+ } ) ;
0 commit comments