浏览代码

Unit tests for store

Kirk Trombley 4 年之前
父节点
当前提交
835eba1aa8
共有 3 个文件被更改,包括 133 次插入0 次删除
  1. 6 0
      client/package.json
  2. 53 0
      client/src/tests/__snapshots__/store.test.js.snap
  3. 74 0
      client/src/tests/store.test.js

+ 6 - 0
client/package.json

@@ -34,6 +34,12 @@
   "prettier": {
     "arrowParens": "avoid"
   },
+  "jest": {
+    "coveragePathIgnorePatterns" : [
+      "src/serviceWorker.js",
+      "src/index.js"
+    ]
+  },
   "browserslist": {
     "production": [
       ">0.2%",

+ 53 - 0
client/src/tests/__snapshots__/store.test.js.snap

@@ -0,0 +1,53 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`Store Library creates a store 1`] = `
+Array [
+  "counter",
+  "increment",
+  "changeCounter",
+  "doInvalid",
+  "getReflectedKeys",
+]
+`;
+
+exports[`Store Library creates a store 2`] = `
+Array [
+  Array [
+    "counter",
+    1,
+    0,
+  ],
+  Array [
+    "counter",
+    3,
+    1,
+  ],
+  Array [
+    "counter",
+    10,
+    3,
+  ],
+]
+`;
+
+exports[`Store Library creates a store 3`] = `
+Array [
+  Array [
+    "increment",
+  ],
+  Array [
+    "changeCounter",
+    [Function],
+  ],
+  Array [
+    "changeCounter",
+    10,
+  ],
+  Array [
+    "getReflectedKeys",
+  ],
+  Array [
+    "doInvalid",
+  ],
+]
+`;

+ 74 - 0
client/src/tests/store.test.js

@@ -0,0 +1,74 @@
+import create from "../store";
+
+jest.mock("react");
+
+import { useEffect, useState } from "react";
+
+describe("Store Library", () => {
+  it("creates a store", () => {
+    let v;
+    useState.mockImplementation(fn => {
+      v = fn();
+      return [
+        v,
+        x => {
+          v = x;
+        },
+      ];
+    });
+
+    let unsub = null;
+    useEffect.mockImplementation(fn => {
+      if (unsub) {
+        unsub();
+      }
+      unsub = fn();
+    });
+
+    const [hooks, actions, watch] = create(store => ({
+      counter: 0,
+      increment: () => {
+        store.counter++;
+      },
+      changeCounter: counter => store({ counter }),
+      doInvalid: () => {
+        store.invalidKey = 7;
+      },
+
+      getReflectedKeys: () => Reflect.ownKeys(store),
+    }));
+
+    const changes = [];
+    watch.onChange((...args) => {
+      changes.push(args);
+    });
+    const calls = [];
+    watch.onCall((...args) => {
+      calls.push(args);
+    });
+
+    expect(hooks.useCounter).toBeInstanceOf(Function);
+    expect(actions.increment).toBeInstanceOf(Function);
+    expect(actions.changeCounter).toBeInstanceOf(Function);
+    expect(watch.onChange).toBeInstanceOf(Function);
+    expect(watch.onCall).toBeInstanceOf(Function);
+    expect(watch.setGlobal).toBeInstanceOf(Function);
+
+    expect(hooks.useCounter(() => true)).toBe(0);
+    actions.increment();
+    expect(hooks.useCounter()).toBe(1);
+    actions.changeCounter(x => x * 3);
+    expect(hooks.useCounter()).toBe(3);
+    actions.changeCounter(10);
+    expect(hooks.useCounter()).toBe(10);
+
+    expect(actions.getReflectedKeys()).toMatchSnapshot();
+
+    expect(actions.doInvalid).toThrow();
+
+    expect(changes).toMatchSnapshot();
+    expect(calls).toMatchSnapshot();
+    watch.setGlobal("testing-store");
+    expect(window["testing-store"]).toBeDefined();
+  });
+});