Skip to content

Commit de96a33

Browse files
committed
Pass an optional selector to connect
1 parent 48199dc commit de96a33

File tree

3 files changed

+71
-15
lines changed

3 files changed

+71
-15
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
## Unreleased
44

5+
- `connect` now takes a selector as an optional second argument.
6+
7+
You can now pass an optional selector function to `connect`. It will be
8+
passed the `state` and should return whatever the connected component
9+
expects as the first argument.
10+
11+
connect(FooComponent, state => state.foo);
12+
513
- Filter out booleans in the html helper.
614

715
Previously only `null` and `undefined` were filtered out. Now both `true`

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ export function createStore(reducer) {
4444
roots.set(root, component);
4545
render();
4646
},
47-
connect(component) {
47+
connect(component, selector = state => state) {
4848
// Return a decorated component function.
49-
return (...args) => component(state, ...args);
49+
return (...args) => component(selector(state), ...args);
5050
},
5151
dispatch(action, ...args) {
5252
state = reducer(state, action, args);

test/connect_test.js

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,38 @@ require = require("@std/esm")(module, {esm: "js"});
33
const assert = require("assert");
44
const { createStore } = require("../index")
55

6-
function counter(state = 0, action) {
7-
switch (action) {
8-
case "INCREMENT":
9-
return state + 1;
10-
default:
11-
return state;
12-
}
13-
}
14-
156
suite("connect", function() {
167
let store;
178
let root;
189

10+
function counter(state = 0, action) {
11+
switch (action) {
12+
case "INCREMENT":
13+
return state + 1;
14+
default:
15+
return state;
16+
}
17+
}
18+
1919
setup(function() {
2020
store = createStore(counter);
2121
root = document.createElement("div");
2222
});
2323

24-
test("passes the current state as the first argument", function() {
24+
test("pass the current state as the first argument", function() {
2525
const { attach, connect, dispatch } = store;
2626

2727
const TestApp = spyFunction();
2828
const ConnectedTestApp = connect(TestApp);
29+
2930
attach(ConnectedTestApp, root);
30-
dispatch("INCREMENT");
31+
assert.deepEqual(TestApp.args(), [0]);
3132

33+
dispatch("INCREMENT");
3234
assert.deepEqual(TestApp.args(), [1]);
3335
});
3436

35-
test("passes other args after the state", function() {
37+
test("pass other args after the state", function() {
3638
const { attach, connect, dispatch } = store;
3739

3840
const TestComponent = spyFunction();
@@ -43,8 +45,54 @@ suite("connect", function() {
4345
}
4446

4547
attach(TestApp, root);
46-
dispatch("INCREMENT");
48+
assert.deepEqual(TestComponent.args(), [0, "Foo"]);
4749

50+
dispatch("INCREMENT");
4851
assert.deepEqual(TestComponent.args(), [1, "Foo"]);
4952
});
5053
});
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

Comments
 (0)