diff --git a/test/features.test.js b/test/features.test.js new file mode 100644 index 0000000..2fc5fe6 --- /dev/null +++ b/test/features.test.js @@ -0,0 +1,35 @@ +import assert from "assert"; +import Core from "../lib"; + +describe("features", function () { + let core; + it("a feature's schema is loaded", async function () { + const cards = { + schema: ` + create table if not exists cards( + id INTEGER PRIMARY KEY + );`, + }; + core = await Core.init({ features: [cards] }, { internals: true }); + + const tables = core._internals.listTables; + assert.ok(tables.includes("cards")); + }); + it("a feature's handlers can be declared", async function () { + const cards = { + schema: ` + create table if not exists cards( + id INTEGER PRIMARY KEY + );`, + handlers: { + myHandler: (handlerArg) => { + assert.ok(handlerArg); + return "myHandler"; + }, + }, + }; + core = await Core.init({ features: { cards } }); + const result = core.cards.myhandler(); + assert.strictEqual(result, "myHandler"); + }); +}); diff --git a/test/features.test.ts b/test/features.test.ts deleted file mode 100644 index 69cb437..0000000 --- a/test/features.test.ts +++ /dev/null @@ -1,18 +0,0 @@ -import * as assert from "assert"; -import Core, { App } from "../lib"; - -describe("init", function() { - let core: App; - it("each feature context is loaded", async function(){ - const cards = { - schema: ` - create table if not exists cards( - id INTEGER PRIMARY KEY - );` - } - core = await Core.init({features: [cards]}, {internals: true}); - - const tables = core._internals.listTables - assert.ok(tables.includes('cards')) - }) -}); diff --git a/test/helpers.ts b/test/helpers.js similarity index 100% rename from test/helpers.ts rename to test/helpers.js diff --git a/test/init.test.js b/test/init.test.js new file mode 100644 index 0000000..0c8e006 --- /dev/null +++ b/test/init.test.js @@ -0,0 +1,17 @@ +import * as assert from "assert"; +import Core, { App } from "../lib"; + +describe("init", function () { + let core: App; + it("before initialization, no other keys are set", async function () { + assert.ok("init" in Core); + assert.rejects("state" in Core); + core = await Core.init(); + assert.ok("state" in core); + assert.ok("init" in core); + }); + it("adds access to _internals when internals option is true", async function () { + core = await Core.init({}, { internals: true }); + assert.ok("_internals" in core); + }); +}); diff --git a/test/init.test.ts b/test/init.test.ts deleted file mode 100644 index dec1868..0000000 --- a/test/init.test.ts +++ /dev/null @@ -1,17 +0,0 @@ -import * as assert from "assert"; -import Core, { App } from "../lib"; - -describe("init", function() { - let core: App; - it("core contains critical keys", async function() { - core = await Core.init(); - assert.ok('init' in core); - assert.ok('state' in core); - assert.ok('pushState' in core); - assert.ok('setState' in core); - }); - it("core adds an _internals key when internals option is true", async function() { - core = await Core.init({}, {internals: true}); - assert.ok('_internals' in core); - }); -}); diff --git a/test/pushState.test.ts b/test/pushState.test.ts deleted file mode 100644 index 23bd99e..0000000 --- a/test/pushState.test.ts +++ /dev/null @@ -1,23 +0,0 @@ -import * as assert from "assert"; -import Core, { App } from "../lib"; - -describe("state", function() { - let core: App; - it("calls pushState on init", async function() { - var called = false; - core = await Core.init({ - refreshCards: () => [], - pushState: () => (called = true) - }); - assert.equal(called, true); - }); - it("calls pushState on handler call", async function() { - let callCount = 0; - core = await Core.init({ - refreshCards: () => [], - pushState: () => (callCount += 1) - }); - core.setState.resetViewer(); - assert.equal(callCount, 2); - }); -}); diff --git a/test/state.test.js b/test/state.test.js new file mode 100644 index 0000000..8ccc294 --- /dev/null +++ b/test/state.test.js @@ -0,0 +1,30 @@ +import assert from "assert"; +import Core from "../lib"; + +describe("state", function () { + let core; + it("core.state is empty object by default", async function () { + core = await Core.init(); + assert.strictEqual(core.state, {}); + }); + it("core.state returns declared state columns", async function () { + core = await Core.init({ + stateColumns: ["my_column integer"], + }); + assert.notStrictEqual(core.state, { my_column: null }); + }); + + it("core.state calls buildState", async function () { + let calls = 0; + core = await Core.init({ + buildState: (partial, core) => { + calls++; + assert.strictEqual(partial, {}); + assert.ok(core); //gets a truthy value + return { my_state: true }; + }, + }); + assert.strictEqual(calls, 1); + assert.strictEqual(core.state, { my_column: null }); + }); +}); diff --git a/test/state.test.ts b/test/state.test.ts deleted file mode 100644 index af22119..0000000 --- a/test/state.test.ts +++ /dev/null @@ -1,13 +0,0 @@ -import * as assert from "assert"; -import Core, { App } from "../lib"; - -describe("state", function() { - let core: App; - it("core.state returns current client state", async function() { - core = await Core.init({ refreshCards: () => [] }); - assert.notStrictEqual(core.state, { - viewingId: null, - viewing: null - }); - }); -});