-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1644 from notaphplover/refactor/add-use-url-like-…
…location Add useUrlLikeLocation
- Loading branch information
Showing
9 changed files
with
842 additions
and
686 deletions.
There are no files selected for viewing
781 changes: 394 additions & 387 deletions
781
packages/frontend/web-ui/src/auth/hooks/useRegisterConfirm.spec.ts
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/frontend/web-ui/src/common/hooks/useUrlLikeLocation.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { beforeAll, describe, expect, it, jest } from '@jest/globals'; | ||
|
||
jest.mock('react-router-dom'); | ||
|
||
jest.mock('../models/UrlLikeLocation'); | ||
|
||
import { Location, useLocation } from 'react-router-dom'; | ||
|
||
import { UrlLikeLocation } from '../models/UrlLikeLocation'; | ||
import { Writable } from '../models/Writable'; | ||
import { useUrlLikeLocation } from './useUrlLikeLocation'; | ||
|
||
describe(UrlLikeLocation.name, () => { | ||
describe('when called', () => { | ||
let locationFixture: Location; | ||
let pathNameFixture: string; | ||
|
||
let result: unknown; | ||
|
||
beforeAll(() => { | ||
locationFixture = Symbol() as unknown as Location; | ||
pathNameFixture = 'pathname-fixture'; | ||
|
||
(useLocation as jest.Mock<typeof useLocation>).mockReturnValueOnce( | ||
locationFixture, | ||
); | ||
|
||
( | ||
UrlLikeLocation as unknown as jest.Mock< | ||
(this: Writable<UrlLikeLocation>) => void | ||
> | ||
).mockImplementationOnce(function (this: Writable<UrlLikeLocation>) { | ||
this.pathname = pathNameFixture; | ||
}); | ||
|
||
result = useUrlLikeLocation(); | ||
}); | ||
|
||
it('should call useLocation', () => { | ||
expect(useLocation).toHaveBeenCalledTimes(1); | ||
expect(useLocation).toHaveBeenCalledWith(); | ||
}); | ||
|
||
it('should call UrlLikeLocation()', () => { | ||
expect(UrlLikeLocation).toHaveBeenCalledTimes(1); | ||
expect(UrlLikeLocation).toHaveBeenCalledWith(locationFixture); | ||
}); | ||
|
||
it('should return expected result', () => { | ||
const expectedProperties: Partial<UrlLikeLocation> = { | ||
pathname: pathNameFixture, | ||
}; | ||
|
||
expect(result).toStrictEqual(expect.objectContaining(expectedProperties)); | ||
}); | ||
}); | ||
}); |
7 changes: 7 additions & 0 deletions
7
packages/frontend/web-ui/src/common/hooks/useUrlLikeLocation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { useLocation } from 'react-router-dom'; | ||
|
||
import { UrlLikeLocation } from '../models/UrlLikeLocation'; | ||
|
||
export const useUrlLikeLocation = (): UrlLikeLocation => { | ||
return new UrlLikeLocation(useLocation()); | ||
}; |
81 changes: 81 additions & 0 deletions
81
packages/frontend/web-ui/src/common/models/UrlLikeLocation.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { beforeAll, describe, expect, it } from '@jest/globals'; | ||
|
||
import { Location } from 'react-router-dom'; | ||
|
||
import { UrlLikeLocation } from './UrlLikeLocation'; | ||
|
||
describe(UrlLikeLocation.name, () => { | ||
describe.each< | ||
[string, Location, (locationFixture: Location) => Partial<UrlLikeLocation>] | ||
>([ | ||
[ | ||
'with empty query', | ||
{ | ||
hash: '', | ||
key: 'sample-key', | ||
pathname: '/', | ||
search: '', | ||
state: undefined, | ||
}, | ||
(locationFixture: Location): Partial<UrlLikeLocation> => ({ | ||
pathname: locationFixture.pathname, | ||
searchParams: new URLSearchParams(), | ||
}), | ||
], | ||
[ | ||
'with query with single value', | ||
{ | ||
hash: '', | ||
key: 'sample-key', | ||
pathname: '/foo', | ||
search: '?bar=baz', | ||
state: undefined, | ||
}, | ||
(locationFixture: Location): Partial<UrlLikeLocation> => ({ | ||
pathname: locationFixture.pathname, | ||
searchParams: new URLSearchParams([['bar', 'baz']]), | ||
}), | ||
], | ||
[ | ||
'with query with multiple values', | ||
{ | ||
hash: '', | ||
key: 'sample-key', | ||
pathname: '/foo', | ||
search: '?bar=baz&bar=baz2', | ||
state: undefined, | ||
}, | ||
(locationFixture: Location): Partial<UrlLikeLocation> => ({ | ||
pathname: locationFixture.pathname, | ||
searchParams: new URLSearchParams([ | ||
['bar', 'baz'], | ||
['bar', 'baz2'], | ||
]), | ||
}), | ||
], | ||
])( | ||
'having a Location %s', | ||
( | ||
_: string, | ||
locationFixture: Location, | ||
buildExpectedUrlLikeLocation: ( | ||
locationFixture: Location, | ||
) => Partial<UrlLikeLocation>, | ||
) => { | ||
describe('when called', () => { | ||
let result: UrlLikeLocation; | ||
|
||
beforeAll(() => { | ||
result = new UrlLikeLocation(locationFixture); | ||
}); | ||
|
||
it('should return expected UrlLikeLocation', () => { | ||
const expected: Partial<UrlLikeLocation> = | ||
buildExpectedUrlLikeLocation(locationFixture); | ||
|
||
expect(result).toStrictEqual(expect.objectContaining(expected)); | ||
}); | ||
}); | ||
}, | ||
); | ||
}); |
19 changes: 19 additions & 0 deletions
19
packages/frontend/web-ui/src/common/models/UrlLikeLocation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Location } from 'react-router-dom'; | ||
|
||
export class UrlLikeLocation { | ||
readonly #pathname: string; | ||
readonly #searchParams: URLSearchParams; | ||
|
||
constructor(location: Location) { | ||
this.#pathname = location.pathname; | ||
this.#searchParams = new URLSearchParams(location.search); | ||
} | ||
|
||
public get pathname(): string { | ||
return this.#pathname; | ||
} | ||
|
||
public get searchParams(): URLSearchParams { | ||
return this.#searchParams; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type Writable<T> = { | ||
-readonly [TKey in keyof T]: T[TKey]; | ||
}; |
Oops, something went wrong.