-
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.
- Loading branch information
1 parent
984c38c
commit 05f3793
Showing
2 changed files
with
72 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; | ||
import { useHttpClient } from './http-hook'; | ||
import { StatusContext } from '../context/status-context'; | ||
import { StatusType } from '../types/types'; | ||
import BackendError from '../error/backend-error'; | ||
|
||
describe('http-hook', () => { | ||
|
||
it('should render hook with resolved fetch and loading', async() => { | ||
|
||
global.fetch = jest.fn().mockResolvedValue({ | ||
ok: true, | ||
json: () => Promise.resolve({message: 'ok'}) | ||
}); | ||
|
||
const status = { | ||
isLoading: false, | ||
setIsLoading: jest.fn(), | ||
status: StatusType.SUCCESS, | ||
setStatus: jest.fn(), | ||
message: null, | ||
setMessage: jest.fn() | ||
}; | ||
const { result } = renderHook(() => useHttpClient(), { | ||
wrapper: ({ children }) => <StatusContext.Provider value={status}>{children}</StatusContext.Provider>, | ||
}); | ||
|
||
const sendRequest = result.current.sendRequest; | ||
await sendRequest( | ||
`/recipes`, 'GET', null, { | ||
Authorization: 'Bearer ' + 'token' | ||
}, true, true | ||
); | ||
expect(status.setIsLoading).toHaveBeenNthCalledWith(1, true); | ||
expect(status.setIsLoading).toHaveBeenNthCalledWith(2, false); | ||
expect(status.setStatus).toHaveBeenCalledWith(StatusType.SUCCESS); | ||
}) | ||
|
||
it('should render hook with error', async() => { | ||
|
||
global.fetch = jest.fn().mockResolvedValue({ | ||
ok: false, | ||
json: () => Promise.resolve({message: 'Backend error'}) | ||
}); | ||
|
||
const status = { | ||
isLoading: false, | ||
setIsLoading: jest.fn(), | ||
status: StatusType.SUCCESS, | ||
setStatus: jest.fn(), | ||
message: null, | ||
setMessage: jest.fn() | ||
}; | ||
const { result } = renderHook(() => useHttpClient(), { | ||
wrapper: ({ children }) => <StatusContext.Provider value={status}>{children}</StatusContext.Provider>, | ||
}); | ||
|
||
const sendRequest = result.current.sendRequest; | ||
await expect(sendRequest( | ||
`/recipes`, 'GET', null, { | ||
Authorization: 'Bearer ' + 'token' | ||
}, true, true | ||
)).rejects.toThrow(BackendError); | ||
expect(status.setIsLoading).toHaveBeenNthCalledWith(1, true); | ||
expect(status.setIsLoading).toHaveBeenNthCalledWith(2, false); | ||
expect(status.setStatus).toHaveBeenCalledWith(StatusType.ERROR); | ||
expect(status.setMessage).toHaveBeenCalledWith('Backend error'); | ||
}) | ||
|
||
}) |
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