From cf4f34ed8897ccefbce592da0d45549bdf7b0a60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Wikstr=C3=B6m?= <> Date: Sat, 24 Feb 2018 22:36:56 +0100 Subject: [PATCH] test --- src/createAppHistory.spec.ts | 16 ++++++++++++++++ src/createSource.ts | 23 ++++++++++++++--------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/createAppHistory.spec.ts b/src/createAppHistory.spec.ts index f61526e..2183be1 100644 --- a/src/createAppHistory.spec.ts +++ b/src/createAppHistory.spec.ts @@ -21,6 +21,22 @@ describe("createAppHistory", async () => { expect(history.cacheLimit).toBe(123); }); + it("cannot be invoked with provider that does not declare a matching factory func", () => { + let error: Error | null = null; + + try { + createAppHistory({ + mode: "memory", + provider: {}, + }); + } catch (caught) { + error = caught; + } + + expect(error).not.toBeNull(); + expect(error.message).toBe("app-history: Provider does not support memory history"); + }); + describe("returns a history extension object that", async () => { it("keeps track of app history depth", async () => { const history = await createAndInitAppHistory({mode: "memory"}); diff --git a/src/createSource.ts b/src/createSource.ts index 954d211..0a33390 100644 --- a/src/createSource.ts +++ b/src/createSource.ts @@ -2,10 +2,21 @@ import { createBrowserHistory, createMemoryHistory } from "history"; import { IHistory, IHistoryProvider, SourceMode, UserConfirmation } from "./api"; +const defaultUserConfirmation: UserConfirmation = (message, callback) => { + callback(window.confirm(message)); +}; + +const noUserConfirmation: UserConfirmation = (_, callback) => { + callback(true); +}; + +const makeDefaultUserConfirmation = (mode: SourceMode) => + mode === "browser" ? defaultUserConfirmation : noUserConfirmation; + export function createSource( onChangeWasBlocked: () => void, mode: SourceMode, - getUserConfirmation?: UserConfirmation, + getUserConfirmation: UserConfirmation = makeDefaultUserConfirmation(mode), provider?: IHistoryProvider, ): IHistory { const getTrackedUserConfirmation: UserConfirmation = (message, callback) => { @@ -19,13 +30,7 @@ export function createSource( } }; - if (getUserConfirmation) { - getUserConfirmation(message, trackingCallback); - } else if (mode === "browser") { - trackingCallback(window.confirm(message)); - } else { - callback(true); - } + getUserConfirmation(message, trackingCallback); }; if (!provider) { @@ -39,7 +44,7 @@ export function createSource( provider.createMemoryHistory : provider.createBrowserHistory; if (!factoryFunc) { - throw new Error("app-history: Missing provider for " + mode + " history"); + throw new Error("app-history: Provider does not support " + mode + " history"); } if (provider) {