Skip to content

Commit

Permalink
Fixing a bug with notFound handler hooks. #271
Browse files Browse the repository at this point in the history
  • Loading branch information
Krasimir Tsonev committed Feb 1, 2021
1 parent f781507 commit 2478e63
Show file tree
Hide file tree
Showing 17 changed files with 79 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 8.8.3

Fixing a bug with notFound handler hooks. #271

## 8.8.2

Fixing typing.
Expand Down
3 changes: 2 additions & 1 deletion lib/es/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ export default function Navigo(appRoute, resolveOptions) {
}

function navigate(to, navigateOptions) {
to = clean(root) + "/" + clean(to);
to = clean(root) + "/" + clean(to); // console.log("---->" + to);

var context = {
instance: self,
to: to,
Expand Down
3 changes: 2 additions & 1 deletion lib/es/lifecycles.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import checkForAlreadyHook from "./middlewares/checkForAlreadyHook";
import checkForNotFoundHandler from "./middlewares/checkForNotFoundHandler";
import errorOut from "./middlewares/errorOut";
import flushCurrent from "./middlewares/flushCurrent";
import updateState from "./middlewares/updateState";
export var foundLifecycle = [checkForAlreadyHook, checkForBeforeHook, callHandler, checkForAfterHook];
export var notFoundLifeCycle = [checkForLeaveHook, checkForNotFoundHandler, Q["if"](function (_ref) {
var notFoundHandled = _ref.notFoundHandled;
return notFoundHandled;
}, foundLifecycle, [errorOut]), flushCurrent];
}, foundLifecycle.concat([updateState]), [errorOut, flushCurrent])];
2 changes: 1 addition & 1 deletion lib/es/middlewares/updateState.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { undefinedOrTrue } from "../utils";
export default function callHandler(context, done) {
export default function updateState(context, done) {
if (undefinedOrTrue(context.navigateOptions, "updateState")) {
context.instance._setCurrent(context.matches);
}
Expand Down
11 changes: 7 additions & 4 deletions lib/navigo.amd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/navigo.amd.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/navigo.amd.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/navigo.amd.min.js.map

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions lib/navigo.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/navigo.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/navigo.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/navigo.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "navigo",
"version": "8.8.2",
"version": "8.8.3",
"description": "A simple vanilla JavaScript router",
"main": "lib/navigo.js",
"browser": "lib/navigo.min.js",
Expand Down
43 changes: 41 additions & 2 deletions src/__tests__/issues.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import NavigoRouter from "../../index";
import Navigo from "../index";
import { Match } from "../../index";

describe("Given the Navigo library", () => {
beforeEach(() => {
Expand Down Expand Up @@ -323,12 +324,13 @@ describe("Given the Navigo library", () => {
const r: NavigoRouter = new Navigo("/");

r.on(/xyz\/[0-9a-z]{4,8}$/, h1, {
leave: (done, match) => {
leave: (done, match: Match) => {
expect(match.data).toStrictEqual(["asdf", "sub"]);
done();
},
});

r.on(/xyz\/[0-9a-z]{4,8}\/sub$/, h2, {
r.on(/xyz\/([0-9a-z]{4,8})\/(sub)$/, h2, {
leave: (done, match) => {
done();
},
Expand All @@ -347,8 +349,45 @@ describe("Given the Navigo library", () => {
expect(h2).toBeCalledWith(
expect.objectContaining({
url: "xyz/asdf/sub",
data: ["asdf", "sub"],
})
);
});
});
describe("and the problem described in #271", () => {
it("should call the leave hook of the not found handler", () => {
const router: NavigoRouter = new Navigo("/");
const existing = jest.fn();
const nonExisting = jest.fn();
const leave = jest.fn().mockImplementation((done) => done());
const before = jest.fn().mockImplementation((done) => done());
const after = jest.fn();
const already = jest.fn();

router.on("existing", existing);
router.notFound(nonExisting, {
before,
after,
leave,
already,
});

router.navigate("/non-existent");
router.navigate("/non-existent");

expect(existing).toBeCalledTimes(0);
expect(nonExisting).toBeCalledTimes(1);
expect(leave).toBeCalledTimes(0);
expect(before).toBeCalledTimes(1);
expect(after).toBeCalledTimes(1);

router.navigate("/existing");

expect(existing).toBeCalledTimes(1);
expect(nonExisting).toBeCalledTimes(1);
expect(leave).toBeCalledTimes(1);

router.navigate("/blah");
});
});
});
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export default function Navigo(
}
function navigate(to: string, navigateOptions?: NavigateOptions): void {
to = `${clean(root)}/${clean(to)}`;
// console.log("---->" + to);
const context: QContext = {
instance: self,
to,
Expand Down
10 changes: 6 additions & 4 deletions src/lifecycles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import checkForAlreadyHook from "./middlewares/checkForAlreadyHook";
import checkForNotFoundHandler from "./middlewares/checkForNotFoundHandler";
import errorOut from "./middlewares/errorOut";
import flushCurrent from "./middlewares/flushCurrent";
import updateState from "./middlewares/updateState";

export const foundLifecycle = [
checkForAlreadyHook,
Expand All @@ -19,8 +20,9 @@ export const foundLifecycle = [
export const notFoundLifeCycle = [
checkForLeaveHook,
checkForNotFoundHandler,
Q.if(({ notFoundHandled }: QContext) => notFoundHandled, foundLifecycle, [
errorOut,
]),
flushCurrent,
Q.if(
({ notFoundHandled }: QContext) => notFoundHandled,
foundLifecycle.concat([updateState]),
[errorOut, flushCurrent]
),
];
2 changes: 1 addition & 1 deletion src/middlewares/updateState.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { QContext } from "../../index";
import { undefinedOrTrue } from "../utils";

export default function callHandler(context: QContext, done) {
export default function updateState(context: QContext, done) {
if (undefinedOrTrue(context.navigateOptions, "updateState")) {
context.instance._setCurrent(context.matches);
}
Expand Down

0 comments on commit 2478e63

Please sign in to comment.