generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 160
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 #622 from Aryex82/develop
Change fetch implementation to RTK Query for Idle Members
- Loading branch information
Showing
6 changed files
with
167 additions
and
69 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { setupServer } from 'msw/node'; | ||
import handlers, { | ||
failedIdleMembersHandler, | ||
} from '../../../__mocks__/handlers/members.handler.js'; | ||
|
||
import { PropsWithChildren } from 'react'; | ||
import { act, renderHook } from '@testing-library/react-hooks'; | ||
import { Provider } from 'react-redux'; | ||
import { store } from '@/app/store'; | ||
import { useGetIdleMembersQuery } from '@/app/services/membersApi'; | ||
|
||
const server = setupServer(...handlers); | ||
|
||
beforeAll(() => { | ||
server.listen(); | ||
}); | ||
afterEach(() => server.resetHandlers()); | ||
afterAll(() => server.close()); | ||
|
||
function Wrapper({ | ||
children, | ||
}: PropsWithChildren<Record<string, never>>): JSX.Element { | ||
return <Provider store={store()}>{children}</Provider>; | ||
} | ||
|
||
describe('useGetIdleMembersQuery', () => { | ||
test('returns idle members', async () => { | ||
const { result, waitForNextUpdate } = renderHook( | ||
() => useGetIdleMembersQuery(), | ||
{ | ||
wrapper: Wrapper, | ||
} | ||
); | ||
|
||
const initialResponse = result.current; | ||
expect(initialResponse.data).toBeUndefined(); | ||
expect(initialResponse.isLoading).toBe(true); | ||
|
||
await act(() => waitForNextUpdate()); | ||
|
||
const nextResponse = result.current; | ||
expect(nextResponse.data).not.toBeUndefined(); | ||
expect(nextResponse.isLoading).toBe(false); | ||
expect(nextResponse.isSuccess).toBe(true); | ||
expect(nextResponse.data).toBeDefined(); | ||
}); | ||
|
||
test('checks for error response', async () => { | ||
server.use(failedIdleMembersHandler); | ||
const { result, waitForNextUpdate } = renderHook( | ||
() => useGetIdleMembersQuery(), | ||
{ | ||
wrapper: Wrapper, | ||
} | ||
); | ||
|
||
const initialResponse = result.current; | ||
expect(initialResponse.data).toBeUndefined(); | ||
expect(initialResponse.isLoading).toBe(true); | ||
|
||
await act(() => waitForNextUpdate()); | ||
|
||
const nextResponse = result.current; | ||
expect(nextResponse.isError).toBe(true); | ||
expect(nextResponse.error).toHaveProperty('status', 500); | ||
|
||
expect(nextResponse.error).toHaveProperty('data', { | ||
statusCode: 500, | ||
error: 'Internal Server Error', | ||
message: 'An internal server error occurred', | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -22,6 +22,7 @@ export const api = createApi({ | |
'User', | ||
'Tags', | ||
'Levels', | ||
'Idle_Members', | ||
'User_Standup', | ||
], | ||
/** | ||
|
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,22 @@ | ||
import { MEMBERS_IDLE } from '@/constants/url'; | ||
import { api } from './api'; | ||
|
||
type IdleMembersResponse = { idleMemberUserNames: string[] }; | ||
export const membersApi = api.injectEndpoints({ | ||
endpoints: (build) => ({ | ||
getIdleMembers: build.query<string[], void>({ | ||
query: () => MEMBERS_IDLE, | ||
providesTags: ['Idle_Members'], | ||
transformResponse: (response: IdleMembersResponse) => { | ||
const filterMembers = response.idleMemberUserNames.filter( | ||
(username: string) => username | ||
); | ||
const sortedIdleMembers = filterMembers.sort(); | ||
|
||
return sortedIdleMembers; | ||
}, | ||
}), | ||
}), | ||
}); | ||
|
||
export const { useGetIdleMembersQuery } = membersApi; |
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