next-use-contextual-routing 2.1.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/package.json +4 -4
- package/src/__tests__/index.test-d.ts +10 -0
- package/src/__tests__/index.test.js +131 -0
- package/CHANGELOG.md +0 -24
package/README.md
CHANGED
|
@@ -130,8 +130,8 @@ When using **Contextual routing**, default browsers scroll behaviour on page ref
|
|
|
130
130
|
history.scrollRestoration = 'manual';
|
|
131
131
|
```
|
|
132
132
|
|
|
133
|
-
[ci]: https://
|
|
134
|
-
[ci
|
|
133
|
+
[ci-badge]: https://github.com/toomuchdesign/next-use-contextual-routing/actions/workflows/ci.yml/badge.svg
|
|
134
|
+
[ci]: https://github.com/toomuchdesign/next-use-contextual-routing/actions/workflows/ci.yml
|
|
135
135
|
[npm]: https://www.npmjs.com/package/next-use-contextual-routing
|
|
136
136
|
[npm-badge]: https://img.shields.io/npm/v/next-use-contextual-routing.svg
|
|
137
137
|
[coveralls-badge]: https://coveralls.io/repos/github/toomuchdesign/next-use-contextual-routing/badge.svg?branch=master
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "next-use-contextual-routing",
|
|
3
3
|
"description": "Generate Next.js contextual routing paths",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.3.0",
|
|
5
5
|
"main": "dist/index.cjs.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
7
7
|
"types": "src/index.d.ts",
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"husky": "^4.0.0",
|
|
57
57
|
"jest": "^26.4.2",
|
|
58
58
|
"lint-staged": "^10.0.0",
|
|
59
|
-
"next": "^
|
|
59
|
+
"next": "^12.1.0",
|
|
60
60
|
"prettier": "^2.0.0",
|
|
61
61
|
"react": "^17.0.2",
|
|
62
62
|
"react-dom": "^17.0.2",
|
|
@@ -66,8 +66,8 @@
|
|
|
66
66
|
"tsd": "^0.13.1"
|
|
67
67
|
},
|
|
68
68
|
"peerDependencies": {
|
|
69
|
-
"next": "^9.0.0 || ^10.0.0 || ^11.0.0",
|
|
70
|
-
"react": "
|
|
69
|
+
"next": "^9.0.0 || ^10.0.0 || ^11.0.0 || ^12.0.0",
|
|
70
|
+
"react": ">=16.8.0"
|
|
71
71
|
},
|
|
72
72
|
"jest": {
|
|
73
73
|
"testPathIgnorePatterns": [
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { expectType, expectAssignable, expectError } from 'tsd';
|
|
2
|
+
import { useContextualRouting } from '../';
|
|
3
|
+
|
|
4
|
+
const { returnHref, makeContextualHref } = useContextualRouting();
|
|
5
|
+
|
|
6
|
+
expectType<string>(returnHref);
|
|
7
|
+
expectAssignable<Function>(makeContextualHref);
|
|
8
|
+
|
|
9
|
+
expectError(makeContextualHref('s'));
|
|
10
|
+
expectType<string>(makeContextualHref({ foo: 'bar' }));
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { RouterContext } from 'next/dist/shared/lib/router-context';
|
|
3
|
+
import { renderHook as TLRenderHook } from '@testing-library/react-hooks';
|
|
4
|
+
import stringify from 'qs-stringify';
|
|
5
|
+
import { useContextualRouting, RETURN_HREF_QUERY_PARAM } from '../index';
|
|
6
|
+
|
|
7
|
+
const mockRouter = {
|
|
8
|
+
basePath: '',
|
|
9
|
+
pathname: '/',
|
|
10
|
+
route: '/',
|
|
11
|
+
asPath: '/',
|
|
12
|
+
query: {},
|
|
13
|
+
push: jest.fn(),
|
|
14
|
+
replace: jest.fn(),
|
|
15
|
+
reload: jest.fn(),
|
|
16
|
+
back: jest.fn(),
|
|
17
|
+
prefetch: jest.fn(),
|
|
18
|
+
beforePopState: jest.fn(),
|
|
19
|
+
events: {
|
|
20
|
+
on: jest.fn(),
|
|
21
|
+
off: jest.fn(),
|
|
22
|
+
emit: jest.fn(),
|
|
23
|
+
},
|
|
24
|
+
isFallback: false,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
function contextProvider({ router, children }) {
|
|
28
|
+
return (
|
|
29
|
+
<RouterContext.Provider value={{ ...mockRouter, ...router }}>
|
|
30
|
+
{children}
|
|
31
|
+
</RouterContext.Provider>
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function renderHook({ router }) {
|
|
36
|
+
return TLRenderHook(() => useContextualRouting(), {
|
|
37
|
+
wrapper: ({ children }) => contextProvider({ router, children }),
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
describe('useContextualRouting', () => {
|
|
42
|
+
const initialRouter = {
|
|
43
|
+
asPath: '/startpage/55?page=2#anchor',
|
|
44
|
+
pathname: '/startpage/[id]',
|
|
45
|
+
query: {
|
|
46
|
+
id: 55,
|
|
47
|
+
page: 2,
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
it('returns hrefs relative to the route where contextual routing starts from', () => {
|
|
52
|
+
const { result } = renderHook({ router: initialRouter });
|
|
53
|
+
expect(result.current.returnHref).toBe('/startpage/55?page=2#anchor');
|
|
54
|
+
expect(result.current.makeContextualHref({ extraParam: 'foo' })).toBe(
|
|
55
|
+
'/startpage/[id]?' +
|
|
56
|
+
stringify({
|
|
57
|
+
id: 55,
|
|
58
|
+
page: 2,
|
|
59
|
+
extraParam: 'foo',
|
|
60
|
+
[RETURN_HREF_QUERY_PARAM]: '/startpage/55?page=2#anchor',
|
|
61
|
+
})
|
|
62
|
+
);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('returns expected hrefs when query object is empty', () => {
|
|
66
|
+
const router = {
|
|
67
|
+
...initialRouter,
|
|
68
|
+
asPath: '/startpage',
|
|
69
|
+
query: {},
|
|
70
|
+
};
|
|
71
|
+
const { result } = renderHook({ router });
|
|
72
|
+
expect(result.current.returnHref).toBe('/startpage');
|
|
73
|
+
expect(result.current.makeContextualHref()).toBe(
|
|
74
|
+
'/startpage/[id]?' +
|
|
75
|
+
stringify({
|
|
76
|
+
[RETURN_HREF_QUERY_PARAM]: '/startpage',
|
|
77
|
+
})
|
|
78
|
+
);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe('Contextual routing starts', () => {
|
|
82
|
+
it('preserves hrefs pointing to the page where contextual routing started from', () => {
|
|
83
|
+
const router = { ...initialRouter };
|
|
84
|
+
const { result, rerender } = renderHook({ router });
|
|
85
|
+
|
|
86
|
+
// Simulate route change happening when contextual routing starts
|
|
87
|
+
// @NOTE It seems that rerender accepts only newProp (new initial props) so here we mutate the original router object :(
|
|
88
|
+
// https://github.com/testing-library/react-hooks-testing-library/issues/228
|
|
89
|
+
router.asPath = '/post/5';
|
|
90
|
+
router.query[RETURN_HREF_QUERY_PARAM] = result.current.returnHref;
|
|
91
|
+
rerender();
|
|
92
|
+
|
|
93
|
+
expect(result.current.returnHref).toBe('/startpage/55?page=2#anchor');
|
|
94
|
+
expect(result.current.makeContextualHref()).toBe(
|
|
95
|
+
'/startpage/[id]?' +
|
|
96
|
+
stringify({
|
|
97
|
+
id: 55,
|
|
98
|
+
page: 2,
|
|
99
|
+
[RETURN_HREF_QUERY_PARAM]: '/startpage/55?page=2#anchor',
|
|
100
|
+
})
|
|
101
|
+
);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
describe('Route change before contextual routing has started', () => {
|
|
106
|
+
it('updates returned values to reflect query updates', () => {
|
|
107
|
+
const router = { ...initialRouter };
|
|
108
|
+
const { result, rerender } = renderHook({ router });
|
|
109
|
+
|
|
110
|
+
// Route change 2: back to startpage with different query
|
|
111
|
+
router.asPath = '/startpage/55?page=3#anchor';
|
|
112
|
+
router.pathname = '/startpage/[id]';
|
|
113
|
+
router.query = {
|
|
114
|
+
id: 55,
|
|
115
|
+
page: 3,
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
rerender();
|
|
119
|
+
|
|
120
|
+
expect(result.current.returnHref).toBe('/startpage/55?page=3#anchor');
|
|
121
|
+
expect(result.current.makeContextualHref()).toBe(
|
|
122
|
+
'/startpage/[id]?' +
|
|
123
|
+
stringify({
|
|
124
|
+
id: 55,
|
|
125
|
+
page: 3,
|
|
126
|
+
[RETURN_HREF_QUERY_PARAM]: '/startpage/55?page=3#anchor',
|
|
127
|
+
})
|
|
128
|
+
);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
});
|
package/CHANGELOG.md
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
# 2.1.0
|
|
2
|
-
|
|
3
|
-
### New Features
|
|
4
|
-
|
|
5
|
-
- Widen peerDependencies to include Next.js v10 & v11
|
|
6
|
-
|
|
7
|
-
# 2.0.0
|
|
8
|
-
|
|
9
|
-
### Breaking Changes
|
|
10
|
-
|
|
11
|
-
- `contextualHref` replaced by `makeContextualHref` function
|
|
12
|
-
|
|
13
|
-
### New Features
|
|
14
|
-
|
|
15
|
-
- Support navigation with `history.back` and browser's back button
|
|
16
|
-
- Enable providing extra router params to contextual navigation `href`'s
|
|
17
|
-
|
|
18
|
-
### Minor Changes
|
|
19
|
-
|
|
20
|
-
- Replace `query-string` with `qs-stringify` (-1.7 kb gzipped)
|
|
21
|
-
|
|
22
|
-
# 1.0.0
|
|
23
|
-
|
|
24
|
-
Initial release
|