Skip to content

Commit 4bb770d

Browse files
committed
Work in Progress
1 parent 1f5e7ec commit 4bb770d

File tree

7 files changed

+571
-10
lines changed

7 files changed

+571
-10
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as assert from 'uvu/assert';
2+
import {expect} from 'chai';
3+
4+
export default (history, done) => {
5+
let steps = [
6+
({ location }) => {
7+
debugger;
8+
expect(location).to.include({
9+
pathname: '/'
10+
});
11+
12+
history.push('/the/path?the=query#the-hash');
13+
},
14+
({ action, location }) => {
15+
assert.is(action, 'PUSH');
16+
expect(location).to.include({
17+
pathname: '/the/path?the=query#the-hash',
18+
search: '',
19+
hash: ''
20+
});
21+
22+
debugger
23+
/*
24+
let { spy, destroy } = spyOn(console, 'warn');
25+
26+
history.push('../other/path?another=query#another-hash');
27+
28+
expect(spy).not.toHaveBeenCalled();
29+
30+
destroy();
31+
*/
32+
},
33+
({ location }) => {
34+
expect(location).to.include({
35+
pathname: '../other/path?another=query#another-hash',
36+
search: '',
37+
hash: ''
38+
});
39+
}
40+
];
41+
42+
execSteps(steps, history, done);
43+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { suite } from "uvu";
2+
import * as chai from 'chai';
3+
import * as ENV from "./setup/env.js";
4+
import { useHashHistory } from "../index.ts";
5+
6+
import PushRelativePathnameWarning from './TestSequences/PushRelativePathnameWarning.js';
7+
8+
const TestUseHistory = suite("test history hook");
9+
10+
TestUseHistory.before.each(ENV.reset);
11+
12+
TestUseHistory("Runs", async () => {
13+
debugger
14+
const history = useHashHistory({hashRoot:"/"})
15+
chai.expect(() => {
16+
PushRelativePathnameWarning(history, (e) => {
17+
if (e) {
18+
throw(e)
19+
}
20+
})
21+
}).to.not.throw()
22+
});
23+
24+
TestUseHistory.run();
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { parsePath, createMemoryHistory } from "history";
2+
import { parseHTML } from "linkedom";
3+
4+
const MAIN = "main";
5+
const POP = "popstate";
6+
const BODY = `<body><${MAIN}></${MAIN}></body>`;
7+
8+
const parseToHash = (url) => {
9+
const path = parsePath(url);
10+
return path;
11+
};
12+
13+
const proxyGet = (object, key, handler) => {
14+
return new Proxy(object, {
15+
get(target, prop, _) {
16+
if (prop !== key) {
17+
return Reflect.get(target, prop, _);
18+
}
19+
return handler(target);
20+
},
21+
});
22+
};
23+
24+
const proxyApplyUrl = (fn) => {
25+
return new Proxy(fn, {
26+
apply(fnTarget, _this, [state, _title, url]) {
27+
return fnTarget(parseToHash(url), state);
28+
},
29+
});
30+
};
31+
32+
const makeGlobalDocument = () => {
33+
const history = createMemoryHistory();
34+
const defaultView = parseHTML(BODY);
35+
const { document } = defaultView;
36+
const popEvent = document.createEvent("CustomEvent");
37+
popEvent.initCustomEvent(POP, false, false, null);
38+
history.listen(() => document.dispatchEvent(popEvent));
39+
return proxyGet(document, "defaultView", (target) => {
40+
const win = proxyGet(target.defaultView, "history", () => {
41+
return new Proxy(history, {
42+
get(targetHistory, prop, _) {
43+
switch (prop) {
44+
case "pushState":
45+
return proxyApplyUrl(targetHistory.push);
46+
case "replaceState":
47+
return proxyApplyUrl(targetHistory.replace);
48+
default:
49+
return Reflect.get(targetHistory, prop, _);
50+
}
51+
},
52+
});
53+
});
54+
return proxyGet(win, "location", (_) => history.location);
55+
});
56+
};
57+
58+
class ExecSteps {
59+
60+
constructor ({listen, location}) {
61+
this.listen = listen
62+
this.ctx = {
63+
location
64+
}
65+
}
66+
steps (steps, history, done) {
67+
steps.reduce(async (context, step) => {
68+
this.ctx = await context;
69+
return await new Promise((resolve) => {
70+
this.listen((result) => {
71+
resolve({
72+
...this.ctx, ...result
73+
});
74+
});
75+
step(this.ctx)
76+
});
77+
}, this.ctx).then((result) => {
78+
done(result)
79+
}).catch((e) => {
80+
done(e)
81+
});
82+
}
83+
}
84+
85+
export function reset() {
86+
const newGlobal = {
87+
document: makeGlobalDocument()
88+
};
89+
global.document = newGlobal.document;
90+
const window = document.defaultView;
91+
const {listen, location} = window.history;
92+
93+
const exec = new ExecSteps({listen, location})
94+
global.execSteps = exec.steps.bind(exec)
95+
}

packages/use-hash-history/__tests__/TestSequences/PushMissingPathname.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import expect from 'expect';
21

32
import { execSteps } from './utils.js';
43

@@ -14,7 +13,7 @@ export default (history, done) => {
1413
({ action, location }) => {
1514
expect(action).toBe('PUSH');
1615
expect(location).toMatchObject({
17-
pathname: 'home?the=query#the-hash',
16+
pathname: '/home?the=query#the-hash',
1817
search: '',
1918
hash: ''
2019
});
@@ -24,7 +23,7 @@ export default (history, done) => {
2423
({ action, location }) => {
2524
expect(action).toBe('PUSH');
2625
expect(location).toMatchObject({
27-
pathname: 'home?another=query#another-hash',
26+
pathname: '/?another=query#another-hash',
2827
search: '',
2928
hash: ''
3029
});

packages/use-hash-history/__tests__/TestSequences/PushRelativePathnameWarning.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ export default (history, done) => {
2121

2222
let { spy, destroy } = spyOn(console, 'warn');
2323

24+
debugger
2425
history.push('../other/path?another=query#another-hash');
2526

26-
expect(spy).not.toHaveBeenCalledWith(
27-
expect.stringContaining('relative pathnames are not supported')
28-
);
27+
expect(spy).not.toHaveBeenCalled();
2928

3029
destroy();
3130
},

packages/use-hash-history/package.json

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55
"license": "MIT",
66
"author": "John T. Hoffer",
77
"repository": "github:thejohnhoffer/use-hash-history",
8-
"contributors": [{
9-
"name": "React Training",
10-
"email": "[email protected]"
11-
}],
8+
"contributors": [
9+
{
10+
"name": "React Training",
11+
"email": "[email protected]"
12+
}
13+
],
14+
"scripts": {
15+
"debug": "node --inspect-brk node_modules/uvu/bin.js -r tsm -r esm __debug__"
16+
},
17+
"type": "module",
1218
"main": "main.js",
1319
"module": "index.js",
1420
"types": "index.d.ts",
@@ -23,5 +29,13 @@
2329
],
2430
"peerDependencies": {
2531
"history": "5.x"
32+
},
33+
"devDependencies": {
34+
"chai": "^4.3.4",
35+
"esm": "^3.2.25",
36+
"history": "^5.1.0",
37+
"linkedom": "^0.13.0",
38+
"tsm": "^2.2.1",
39+
"uvu": "^0.5.2"
2640
}
2741
}

0 commit comments

Comments
 (0)