Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mårten Wikström committed Feb 24, 2018
1 parent 38eceff commit cf4f34e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
16 changes: 16 additions & 0 deletions src/createAppHistory.spec.ts
Expand Up @@ -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"});
Expand Down
23 changes: 14 additions & 9 deletions src/createSource.ts
Expand Up @@ -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) => {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit cf4f34e

Please sign in to comment.