Skip to content
This repository was archived by the owner on May 1, 2021. It is now read-only.

Commit e1a913b

Browse files
committed
Update href in location
1 parent 3240a3f commit e1a913b

File tree

11 files changed

+4257
-5339
lines changed

11 files changed

+4257
-5339
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ logs
44
npm-debug.log*
55
yarn-debug.log*
66
yarn-error.log*
7-
yarn.lock
87

98
# Diagnostic reports (https://nodejs.org/api/report.html)
109
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: node_js
22
node_js:
3-
- "10"
3+
- "12"
44
cache: npm
55

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ module.exports = {
156156
// testRunner: "jasmine2",
157157

158158
// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
159-
// testURL: "http://localhost",
159+
testURL: 'http://localhost:3000',
160160

161161
// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
162162
// timers: "real",

package-lock.json

Lines changed: 0 additions & 5316 deletions
This file was deleted.

package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,17 @@
3939
"author": "Peter Beshai <peter.beshai@gmail.com>",
4040
"license": "ISC",
4141
"devDependencies": {
42-
"@babel/core": "^7.3.4",
43-
"@babel/preset-env": "^7.3.4",
44-
"@babel/preset-typescript": "^7.3.3",
42+
"@babel/core": "^7.9.0",
43+
"@babel/preset-env": "^7.9.5",
44+
"@babel/preset-typescript": "^7.9.0",
4545
"@types/jest": "^24.0.11",
46-
"babel-jest": "^24.5.0",
47-
"jest": "^24.5.0",
48-
"rimraf": "^2.6.3",
49-
"typescript": "^3.3.3333"
46+
"@types/node": "12.7.4",
47+
"babel-jest": "^25.3.0",
48+
"jest": "^25.3.0",
49+
"rimraf": "^3.0.2",
50+
"typescript": "^3.8.3"
5051
},
5152
"dependencies": {
52-
"query-string": "^5.0.0"
53+
"query-string": "5.1.1"
5354
}
5455
}

src/__tests__/helpers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import { stringify, ParsedQuery } from 'query-string';
22

33
export function makeMockLocation(query: ParsedQuery): Location {
44
const queryStr = stringify(query);
5+
const search = queryStr.length ? `?${queryStr}` : '';
56
return {
67
protocol: 'http:',
7-
host: 'localhost',
8+
host: 'localhost:3000',
89
pathname: '/',
9-
search: queryStr.length ? `?${queryStr}` : '',
10+
search,
11+
href: 'http://localhost:3000/' + search,
1012
key: `mock-${Date.now()}`,
1113
} as Location & { key: string };
1214
}

src/__tests__/updateLocation-test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('updateLocation', () => {
99
expect(newLocation.search).toBe('?foo=xxx');
1010
expect(newLocation.key).toBeDefined();
1111
expect(newLocation.key).not.toBe((location as any).key);
12+
expect(newLocation.href).toBe('http://localhost:3000/?foo=xxx');
1213

1314
// check multiple params
1415
expect(
@@ -21,6 +22,7 @@ describe('updateLocation', () => {
2122
const location = makeMockLocation({ foo: 'abc', bar: '555' });
2223
const newLocation = updateLocation({}, location);
2324
expect(newLocation.search).toBe('');
25+
expect(newLocation.href).toBe('http://localhost:3000/');
2426

2527
// check updating from no params
2628
expect(updateLocation({ foo: 'xxx' }, makeMockLocation({})).search).toBe(

src/decodeQueryParams.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function decodeQueryParams<QPCMap extends QueryParamConfigMap>(
1717
for (const paramName of paramNames) {
1818
const encodedValue = encodedQuery[paramName];
1919
if (encodedValue == null) {
20-
decodedQuery[paramName] = undefined;
20+
decodedQuery[paramName as keyof QPCMap] = undefined;
2121
continue;
2222
}
2323

@@ -32,9 +32,9 @@ export function decodeQueryParams<QPCMap extends QueryParamConfigMap>(
3232
// it default to be a string type.
3333
(decodedQuery as any)[paramName] = encodedValue;
3434
} else {
35-
decodedQuery[paramName] = paramConfigMap[paramName].decode(
36-
encodedValue as string | string[]
37-
);
35+
decodedQuery[paramName as keyof QPCMap] = paramConfigMap[
36+
paramName
37+
].decode(encodedValue as string | string[]);
3838
}
3939
}
4040

src/encodeQueryParams.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function encodeQueryParams<QPCMap extends QueryParamConfigMap>(
1717
for (const paramName of paramNames) {
1818
const decodedValue = query[paramName];
1919
if (decodedValue == null) {
20-
encodedQuery[paramName] = undefined;
20+
encodedQuery[paramName as keyof QPCMap] = undefined;
2121
continue;
2222
}
2323

@@ -31,9 +31,9 @@ export function encodeQueryParams<QPCMap extends QueryParamConfigMap>(
3131
// it be included by default as a string type.
3232
(encodedQuery as any)[paramName] = String(decodedValue);
3333
} else {
34-
encodedQuery[paramName] = paramConfigMap[paramName].encode(
35-
query[paramName]
36-
);
34+
encodedQuery[paramName as keyof QPCMap] = paramConfigMap[
35+
paramName
36+
].encode(query[paramName]);
3737
}
3838
}
3939

src/updateLocation.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { stringify, parse as parseQueryString } from 'query-string';
1+
import { stringify, parse as parseQueryString, parseUrl } from 'query-string';
22
import { EncodedQuery, EncodedQueryWithNulls } from './types';
33

44
/**
@@ -32,13 +32,17 @@ export function updateLocation(
3232
location: Location
3333
): Location {
3434
const encodedSearchString = stringify(filterNully(encodedQuery));
35+
const search = encodedSearchString.length ? `?${encodedSearchString}` : '';
36+
const href = parseUrl(location.href).url + search;
37+
3538
const newLocation: Location & {
3639
key: string;
3740
query: EncodedQueryWithNulls;
3841
} = {
3942
...location,
4043
key: `${Date.now()}`, // needed for some routers (e.g. react-router)
41-
search: encodedSearchString.length ? `?${encodedSearchString}` : '',
44+
href,
45+
search,
4246
query: encodedQuery, // needed for some routers (e.g. found)
4347
};
4448

0 commit comments

Comments
 (0)