generated from graasp/graasp-repo
-
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.
feat: add password status hook (#958)
* feat: add get password status hook * fix: return data * refactor: invalidate password status * test: add test password status hook * refactor: apply PR requested changes
- Loading branch information
Showing
7 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
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,12 @@ | ||
import { PartialQueryConfigForApi } from '../../types.js'; | ||
import { buildGetPasswordStatusRoute } from './routes.js'; | ||
|
||
export const getPasswordStatus = async ({ | ||
API_HOST, | ||
axios, | ||
}: PartialQueryConfigForApi) => | ||
axios | ||
.get<{ | ||
hasPassword: boolean; | ||
}>(`${API_HOST}/${buildGetPasswordStatusRoute()}`) | ||
.then(({ data }) => data); |
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,52 @@ | ||
import { StatusCodes } from 'http-status-codes'; | ||
import nock from 'nock'; | ||
import { afterEach, describe, expect, it } from 'vitest'; | ||
|
||
import { UNAUTHORIZED_RESPONSE } from '../../../test/constants.js'; | ||
import { mockHook, setUpTest } from '../../../test/utils.js'; | ||
import { memberKeys } from '../../keys.js'; | ||
import { buildGetPasswordStatusRoute } from './routes.js'; | ||
|
||
const { hooks, wrapper, queryClient } = setUpTest(); | ||
describe('Member Hooks', () => { | ||
afterEach(() => { | ||
queryClient.clear(); | ||
nock.cleanAll(); | ||
}); | ||
|
||
describe('usePasswordStatus', () => { | ||
const route = `/${buildGetPasswordStatusRoute()}`; | ||
const hook = hooks.usePasswordStatus; | ||
const key = memberKeys.current().passwordStatus; | ||
|
||
it(`Receive password status`, async () => { | ||
const response = { hasPassword: true }; | ||
const endpoints = [{ route, response }]; | ||
const { data } = await mockHook({ endpoints, hook, wrapper }); | ||
|
||
expect(data).toMatchObject(response); | ||
// verify cache keys | ||
expect(queryClient.getQueryData(key)).toMatchObject(data!); | ||
}); | ||
|
||
it(`Unauthorized`, async () => { | ||
const endpoints = [ | ||
{ | ||
route, | ||
response: UNAUTHORIZED_RESPONSE, | ||
statusCode: StatusCodes.UNAUTHORIZED, | ||
}, | ||
]; | ||
const { data, isError } = await mockHook({ | ||
endpoints, | ||
hook, | ||
wrapper, | ||
}); | ||
|
||
expect(data).toBeFalsy(); | ||
|
||
expect(isError).toBeTruthy(); | ||
expect(queryClient.getQueryData(key)).toBeFalsy(); | ||
}); | ||
}); | ||
}); |
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,18 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
import { memberKeys } from '../../keys.js'; | ||
import { QueryClientConfig } from '../../types.js'; | ||
import { getPasswordStatus } from './api.js'; | ||
|
||
export default (queryConfig: QueryClientConfig) => { | ||
const { defaultQueryOptions } = queryConfig; | ||
|
||
return { | ||
usePasswordStatus: () => | ||
useQuery({ | ||
queryKey: memberKeys.current().passwordStatus, | ||
queryFn: () => getPasswordStatus(queryConfig), | ||
...defaultQueryOptions, | ||
}), | ||
}; | ||
}; |
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,4 @@ | ||
import { MEMBERS_ROUTE } from '../routes.js'; | ||
|
||
export const buildGetPasswordStatusRoute = () => | ||
`${MEMBERS_ROUTE}/current/password/status`; |