Skip to content

Commit

Permalink
fix: replace logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Mårten Wikström committed Feb 15, 2018
1 parent 977dc9c commit 475f2a7
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
58 changes: 56 additions & 2 deletions src/nextMetaState.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createMemoryHistory } from "history";

import { POP, PUSH, REPLACE } from "./api";
import { IMetaState, POP, PUSH, REPLACE } from "./api";
import { initialMetaState } from "./initialMetaState";
import { isWrappedLocation } from "./isWrappedLocation";
import { nextMetaState } from "./nextMetaState";
Expand Down Expand Up @@ -58,6 +58,24 @@ describe("nextMetaState", () => {
});
});

it("can push with cut", () => {
const source = createMemoryHistory();
const initialState: IMetaState = {
cache: ["/foo", "/bar"],
cut: "here",
depth: 123,
};

source.replace("baz", wrapState(null, initialState));
const nextState = nextMetaState(source, PUSH, -1);

expect(nextState).toMatchObject({
cache: ["/foo", "/bar", "/baz"],
cut: "before",
depth: 124,
});
});

it("can replace", () => {
const source = createMemoryHistory();
const initialState = {
Expand All @@ -69,7 +87,43 @@ describe("nextMetaState", () => {
const nextState = nextMetaState(source, REPLACE, -1);

expect(nextState).toMatchObject({
cache: ["/foo", "/baz"],
cache: ["/foo", "/bar"],
depth: 123,
});
});

it("can replace with cut here", () => {
const source = createMemoryHistory();
const initialState: IMetaState = {
cache: ["/foo", "/bar"],
cut: "here",
depth: 123,
};

source.replace("baz", wrapState(null, initialState));
const nextState = nextMetaState(source, REPLACE, -1);

expect(nextState).toMatchObject({
cache: ["/foo", "/bar"],
cut: "here",
depth: 123,
});
});

it("can replace with cut before", () => {
const source = createMemoryHistory();
const initialState: IMetaState = {
cache: ["/foo", "/bar"],
cut: "before",
depth: 123,
};

source.replace("baz", wrapState(null, initialState));
const nextState = nextMetaState(source, REPLACE, -1);

expect(nextState).toMatchObject({
cache: ["/foo", "/bar"],
cut: "before",
depth: 123,
});
});
Expand Down
25 changes: 14 additions & 11 deletions src/nextMetaState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function nextMetaCore(
switch (action) {
case PUSH: return afterPush(meta, path);
case POP: return afterPop(meta);
default: return afterReplace(meta, path);
default: return afterReplace(meta);
}
}

Expand All @@ -35,10 +35,16 @@ function applyCacheLimit(meta: IMetaState, limit: number) {
}

function afterPush(meta: IMetaState, path: string): IMetaState {
return {
const next: IMetaState = {
cache: meta.cache.concat(path),
depth: meta.depth + 1,
};

if (meta.cut === "here") {
next.cut = "before";
}

return next;
}

function afterPop(meta: IMetaState): IMetaState {
Expand All @@ -48,13 +54,10 @@ function afterPop(meta: IMetaState): IMetaState {
};
}

function afterReplace(meta: IMetaState, path: string): IMetaState {
if (meta.depth > 0) {
return {
cache: meta.cache.slice(0, -1).concat(path),
depth: meta.depth,
};
} else {
return meta;
}
function afterReplace(meta: IMetaState): IMetaState {
return {
cache: meta.cache.slice(),
cut: meta.cut,
depth: meta.depth,
};
}

0 comments on commit 475f2a7

Please sign in to comment.