__tests__/store.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
const h3 = require("../h3.js").default; describe("h3 (Store)", () => { beforeEach(async () => { const test = () => { h3.on("$init", () => ({ online: true })); h3.on("$stop", () => ({ online: false })); h3.on("online/set", (state, data) => ({ online: data })); }; return await h3.init({ modules: [test], routes: { "/": () => h3("div") } }); }); afterEach(() => { h3.dispatch("$stop"); }); it("should expose a method to retrieve the application state", () => { expect(h3.state.online).toEqual(true); }); it("should expose a method to dispatch messages", () => { expect(h3.state.online).toEqual(true); h3.dispatch("online/set", "YEAH!"); expect(h3.state.online).toEqual("YEAH!"); }); it("should expose a method to subscribe to messages (and also cancel subscriptions)", () => { const sub = h3.on("online/clear", () => ({ online: undefined })); h3.dispatch("online/clear"); expect(h3.state.online).toEqual(undefined); h3.dispatch("online/set", "reset"); expect(h3.state.online).toEqual("reset"); sub(); h3.dispatch("online/clear"); expect(h3.state.online).toEqual("reset"); }); }); |