@depository/preact 0.54.0 → 0.58.0

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@depository/preact",
4
- "version": "0.54.0",
4
+ "version": "0.58.0",
5
5
  "description": "Preact bindings for depository",
6
6
  "module": "./src/index.js",
7
7
  "exports": {
@@ -27,16 +27,14 @@
27
27
  "access": "public"
28
28
  },
29
29
  "devDependencies": {
30
- "@depository/function-middleware": "^0.54.0",
31
- "@depository/store": "^0.54.0",
30
+ "@depository/store": "^0.58.0",
32
31
  "htm": "^3.0.4",
33
32
  "jsdom": "^16.4.0",
34
33
  "mocha": "8.3.2",
35
34
  "mocha-dominate": "^2.0.0",
36
35
  "preact": "^10.5.7",
37
- "serve": "^11.3.2",
38
- "simulate-events": "^1.0.0",
39
- "unexpected": "12.0.0",
36
+ "serve": "^12.0.1",
37
+ "unexpected": "12.0.3",
40
38
  "unexpected-dom": "^4.18.0"
41
39
  },
42
40
  "peerDependencies": {
@@ -45,5 +43,5 @@
45
43
  "engines": {
46
44
  "node": ">=12"
47
45
  },
48
- "gitHead": "ea8c27f90f70f8307139c669aee402299efce95d"
46
+ "gitHead": "0f1070ade2785e4c20b10e58cfba56fa12106733"
49
47
  }
@@ -1,18 +1,19 @@
1
1
  export const createBinding = ({ h, Component, StoreContext }) => {
2
- const connect = (functionbindings, ChildComponent) => {
2
+ const connect = (bindingOrFunction, ChildComponent) => {
3
3
  if (!ChildComponent) {
4
- ChildComponent = functionbindings;
5
- functionbindings = null;
4
+ ChildComponent = bindingOrFunction;
5
+ bindingOrFunction = null;
6
6
  }
7
7
 
8
+ const isFunctionBindings = typeof bindingOrFunction === "function";
9
+
8
10
  class Connected extends Component {
9
11
  constructor(props) {
10
12
  super(props);
11
13
 
12
- const bindings =
13
- typeof functionbindings === "function"
14
- ? functionbindings(props)
15
- : functionbindings;
14
+ const bindings = isFunctionBindings
15
+ ? bindingOrFunction(props)
16
+ : bindingOrFunction;
16
17
 
17
18
  const store = props.store;
18
19
 
@@ -20,21 +21,38 @@ export const createBinding = ({ h, Component, StoreContext }) => {
20
21
  return store.dispatch(action);
21
22
  };
22
23
 
23
- this.observer = bindings && store.observe(bindings);
24
+ this._observable = bindings && store.observe(bindings);
24
25
 
25
- this.state = this.observer ? this.observer.value : {};
26
+ this.state = this._observable ? this._observable.value : {};
26
27
  }
27
28
 
28
- componentDidMount() {
29
- if (this.observer) {
30
- this.subscription = this.observer.subscribe((value) => {
31
- this.setState(value);
29
+ _subscribe() {
30
+ this._subscription =
31
+ this._observable &&
32
+ this._observable.subscribe((data) => {
33
+ this.setState(data);
32
34
  });
33
- }
35
+ }
36
+
37
+ componentDidMount() {
38
+ this._subscribe();
34
39
  }
35
40
 
36
41
  componentWillUnmount() {
37
- this.subscription && this.subscription.unsubscribe();
42
+ this._subscription && this._subscription.unsubscribe();
43
+ }
44
+
45
+ componentDidUpdate() {
46
+ if (this._observable && isFunctionBindings) {
47
+ const bindings = bindingOrFunction(this.props);
48
+ const _observable = this.props.store.observe(bindings);
49
+ if (this._observable !== _observable) {
50
+ this._subscription.unsubscribe();
51
+ this._observable = _observable;
52
+ this._subscribe();
53
+ this.setState(this._observable.value);
54
+ }
55
+ }
38
56
  }
39
57
 
40
58
  render({ children, store, ...other }, state) {
@@ -1,14 +1,10 @@
1
1
  import unexpected from "unexpected";
2
2
  import unexpectedDom from "unexpected-dom";
3
- import simulateEvents from "simulate-events";
4
3
  import { Store } from "@depository/store";
5
- import { functionMiddleware } from "@depository/function-middleware";
6
4
  import { StoreProvider, useData, useDispatch } from "./index.js";
7
5
  import { h, render } from "preact";
8
6
  import htm from "htm";
9
7
 
10
- const simulate = simulateEvents.default;
11
-
12
8
  const delay = (timeout = 0) =>
13
9
  new Promise((resolve) => {
14
10
  setTimeout(resolve, timeout);
@@ -60,8 +56,6 @@ describe("useData", () => {
60
56
  b: 10,
61
57
  });
62
58
 
63
- store.useMiddleware(functionMiddleware());
64
-
65
59
  render(
66
60
  html`<${StoreProvider} value=${store}><${Calculator} /><//>`,
67
61
  container
@@ -73,7 +67,8 @@ describe("useData", () => {
73
67
  });
74
68
 
75
69
  it("handles dispatching actions", async () => {
76
- simulate(container.querySelector("[data-test-id=increment]"), "click");
70
+ const incrementButton = container.querySelector("[data-test-id=increment]");
71
+ incrementButton.dispatchEvent(new CustomEvent("click"));
77
72
 
78
73
  await delay();
79
74
 
package/src/index.spec.js CHANGED
@@ -1,14 +1,10 @@
1
1
  import unexpected from "unexpected";
2
2
  import unexpectedDom from "unexpected-dom";
3
- import simulateEvents from "simulate-events";
4
3
  import { Store } from "@depository/store";
5
- import { functionMiddleware } from "@depository/function-middleware";
6
4
  import { StoreProvider, connect } from "./index.js";
7
5
  import { h, render } from "preact";
8
6
  import htm from "htm";
9
7
 
10
- const simulate = simulateEvents.default;
11
-
12
8
  const delay = (timeout = 0) =>
13
9
  new Promise((resolve) => {
14
10
  setTimeout(resolve, timeout);
@@ -49,34 +45,75 @@ const Calculator = connect(
49
45
  }
50
46
  );
51
47
 
48
+ const Quote = connect(
49
+ ({ id }) => ({ quote: `quote.${id}` }),
50
+ ({ quote }) => quote
51
+ );
52
+
53
+ const Quotes = connect(
54
+ { quoteId: "selectedQuote" },
55
+ ({ quoteId }) => html`<${Quote} id=${quoteId} />`
56
+ );
57
+
52
58
  describe("preact", () => {
53
59
  let container, store;
54
60
 
55
61
  beforeEach(() => {
56
62
  container = document.createElement("div");
63
+ });
57
64
 
58
- store = new Store({
59
- a: 10,
60
- b: 10,
61
- });
62
-
63
- store.useMiddleware(functionMiddleware());
65
+ const renderCalculator = () => {
66
+ store = new Store({ a: 10, b: 10 });
64
67
 
65
68
  render(
66
69
  html`<${StoreProvider} value=${store}><${Calculator} /><//>`,
67
70
  container
68
71
  );
69
- });
72
+ };
70
73
 
71
74
  it("handles rendering paths and computeds", () => {
75
+ renderCalculator();
72
76
  expect(container, "queried for test id", "sum", "to have text", "10+10=20");
73
77
  });
74
78
 
75
79
  it("handles dispatching actions", async () => {
76
- simulate(container.querySelector("[data-test-id=increment]"), "click");
80
+ renderCalculator();
81
+
82
+ const incrementButton = container.querySelector("[data-test-id=increment]");
83
+ incrementButton.dispatchEvent(new CustomEvent("click"));
77
84
 
78
85
  await delay();
79
86
 
80
87
  expect(container, "queried for test id", "sum", "to have text", "11+11=22");
81
88
  });
89
+
90
+ const renderQuotes = () => {
91
+ store = new Store({
92
+ selectedQuote: 0,
93
+ quote: {
94
+ 0: "The greatest glory in living lies not in never falling, but in rising every time we fall. -Nelson Mandela",
95
+ 1: "The way to get started is to quit talking and begin doing. -Walt Disney",
96
+ },
97
+ });
98
+
99
+ render(html`<${StoreProvider} value=${store}><${Quotes} /><//>`, container);
100
+ };
101
+
102
+ it("handles function bindings with changing props", async () => {
103
+ renderQuotes();
104
+
105
+ expect(
106
+ container,
107
+ "to have text",
108
+ "The greatest glory in living lies not in never falling, but in rising every time we fall. -Nelson Mandela"
109
+ );
110
+
111
+ await store.dispatch({ payload: { selectedQuote: 1 } });
112
+
113
+ expect(
114
+ container,
115
+ "to have text",
116
+ "The way to get started is to quit talking and begin doing. -Walt Disney"
117
+ );
118
+ });
82
119
  });