all repos — h3 @ 68daa6a4f7e8660bb651e52e6d2a171984f568ad

A tiny, extremely minimalist JavaScript microframework.

__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
 38
 39
 40
 41
 42
const mod = require("../h3.js");
const h3 = mod.h3;
const h = mod.h;

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: { "/": () => h("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");
    });
});