-
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.
[Carousel] Props change and revert (#15)
* Carousel prop changes * Commit reverts
- Loading branch information
Showing
13 changed files
with
407 additions
and
37 deletions.
There are no files selected for viewing
This file was deleted.
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
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,174 @@ | ||
import { renderHook, act } from '@testing-library/react'; | ||
import { useLoader } from './useLoader'; | ||
import { STATUS } from '../../types'; | ||
|
||
describe('useLoader', () => { | ||
describe('initialization', () => { | ||
test('loadingStatus should be INIT', () => { | ||
const defaultStatus = STATUS.INIT; | ||
const { result } = renderHook(() => useLoader()); | ||
|
||
expect(result.current.loadingStatus.status).toEqual(defaultStatus); | ||
}); | ||
|
||
test('data should be null', () => { | ||
const defaultDataState = null; | ||
const { result } = renderHook(() => useLoader()); | ||
|
||
expect(result.current.data).toEqual(defaultDataState); | ||
}); | ||
|
||
test('passing defaultStatus should change the default loadingStatus', () => { | ||
const defaultStatus = STATUS.REQUESTING; | ||
const { result } = renderHook(() => useLoader(defaultStatus)); | ||
|
||
expect(result.current.loadingStatus.status).toEqual(defaultStatus); | ||
}); | ||
|
||
test('passing defaultDataState should change the default data', () => { | ||
const defaultDataState = {}; | ||
const { result } = renderHook(() => useLoader(undefined, defaultDataState)); | ||
|
||
expect(result.current.data).toEqual(defaultDataState); | ||
}); | ||
}); | ||
|
||
describe('setLoading', () => { | ||
test('should set loadingStatus to REQUESTING', () => { | ||
const { result } = renderHook(() => useLoader()); | ||
act(() => { | ||
result.current.setLoading(); | ||
}); | ||
|
||
expect(result.current.loadingStatus.status).toEqual(STATUS.REQUESTING); | ||
}); | ||
|
||
test('after success, setLoading with nothing passed should preserve data', () => { | ||
const { result } = renderHook(() => useLoader()); | ||
const response = 'response'; | ||
act(() => { | ||
result.current.setSuccess(response); | ||
}); | ||
act(() => { | ||
result.current.setLoading(); | ||
}); | ||
|
||
expect(result.current.data).toEqual(response); | ||
}); | ||
|
||
test('isRefetch: true should change loadingStatus to REFETCH', () => { | ||
const { result } = renderHook(() => useLoader()); | ||
act(() => { | ||
result.current.setLoading(true); | ||
}); | ||
|
||
expect(result.current.loadingStatus.status).toEqual(STATUS.REFETCH); | ||
}); | ||
|
||
test('after success, setLoading with shouldClearData: true should clear data', () => { | ||
const { result } = renderHook(() => useLoader()); | ||
const response = 'response'; | ||
act(() => { | ||
result.current.setSuccess(response); | ||
}); | ||
act(() => { | ||
result.current.setLoading(false, true); | ||
}); | ||
|
||
expect(result.current.data).toEqual(null); | ||
}); | ||
}); | ||
|
||
describe('setSuccess', () => { | ||
test('should set loadingStatus to SUCCESS', () => { | ||
const { result } = renderHook(() => useLoader()); | ||
act(() => { | ||
result.current.setSuccess(); | ||
}); | ||
|
||
expect(result.current.loadingStatus.status).toEqual(STATUS.SUCCESS); | ||
}); | ||
|
||
test('passing the response should set data with the response', () => { | ||
const { result } = renderHook(() => useLoader()); | ||
const response = 'response'; | ||
act(() => { | ||
result.current.setSuccess(response); | ||
}); | ||
|
||
expect(result.current.data).toEqual(response); | ||
}); | ||
|
||
test('passing no response should set data to null', () => { | ||
const { result } = renderHook(() => useLoader()); | ||
act(() => { | ||
result.current.setSuccess(); | ||
}); | ||
|
||
expect(result.current.loadingStatus.status).toEqual(STATUS.SUCCESS); | ||
expect(result.current.data).toEqual(null); | ||
}); | ||
}); | ||
|
||
describe('onError', () => { | ||
test('should set loadingStatus to FAILURE', () => { | ||
const { result } = renderHook(() => useLoader()); | ||
act(() => { | ||
result.current.setError(); | ||
}); | ||
|
||
expect(result.current.loadingStatus.status).toEqual(STATUS.FAILURE); | ||
}); | ||
|
||
test('after success, passing error should set error', () => { | ||
const { result } = renderHook(() => useLoader()); | ||
const error = 'error'; | ||
act(() => { | ||
result.current.setError(error); | ||
}); | ||
|
||
expect(result.current.loadingStatus.reason).toEqual(error); | ||
}); | ||
|
||
test('after success, error should preserve data', () => { | ||
const { result } = renderHook(() => useLoader()); | ||
const response = 'response'; | ||
const error = 'error'; | ||
act(() => { | ||
result.current.setSuccess(response); | ||
}); | ||
act(() => { | ||
result.current.setError(error); | ||
}); | ||
|
||
expect(result.current.data).toEqual(response); | ||
}); | ||
|
||
test("after success, passing no error should set error to ''", () => { | ||
const { result } = renderHook(() => useLoader()); | ||
const response = 'response'; | ||
act(() => { | ||
result.current.setSuccess(response); | ||
}); | ||
act(() => { | ||
result.current.setError(); | ||
}); | ||
|
||
expect(result.current.loadingStatus.reason).toEqual(''); | ||
}); | ||
|
||
test('after success, shouldClearData: true should clear data', () => { | ||
const { result } = renderHook(() => useLoader()); | ||
const response = 'hello'; | ||
const error = 'error'; | ||
act(() => { | ||
result.current.setSuccess(response); | ||
}); | ||
act(() => { | ||
result.current.setError(error, true); | ||
}); | ||
|
||
expect(result.current.data).toEqual(null); | ||
}); | ||
}); | ||
}); |
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,90 @@ | ||
// testing utilities | ||
import { renderHook, act } from '@testing-library/react'; | ||
|
||
// mocks | ||
import { mockAxios } from '../../mocks/axios'; | ||
|
||
// constants | ||
import { RECOMMENDATION_URL } from '../../configs'; | ||
|
||
import { useRecommendations } from './useRecommendations'; | ||
|
||
const TIMEOUT = 100; | ||
|
||
describe('useRecommendations', () => { | ||
const API_KEY = 'API_KEY'; | ||
const SECRET = 'SECRET'; | ||
|
||
beforeEach(() => { | ||
const Setup = require('../../setup'); | ||
Setup.BreinifySetup({ apiKey: API_KEY, secret: SECRET }); | ||
}); | ||
|
||
test('init state when calling useRecommendations', () => { | ||
const defaultData = {}; | ||
const { result } = renderHook(({ defaultDataState }) => useRecommendations(defaultDataState), { | ||
initialProps: { defaultDataState: defaultData }, | ||
}); | ||
|
||
expect(result.current.data).toEqual(defaultData); | ||
expect(result.current.isInit).toEqual(true); | ||
expect(result.current.isLoading).toEqual(false); | ||
expect(result.current.isSuccess).toEqual(false); | ||
expect(result.current.isFailure).toEqual(false); | ||
expect(result.current.error).toEqual(''); | ||
}); | ||
|
||
test('getRecs should succeed if statusCode: 200 is returned', async () => { | ||
const mock = mockAxios(); | ||
const { result } = renderHook(() => useRecommendations()); | ||
const response = { | ||
response: {}, | ||
statusCode: 200, | ||
}; | ||
mock.onPost(RECOMMENDATION_URL).reply(200, response); | ||
|
||
await act(async () => { | ||
result.current.getRecs({ recommendation: {} }); | ||
}); | ||
|
||
expect(result.current.data).toEqual(response); | ||
expect(result.current.isInit).toEqual(false); | ||
expect(result.current.isLoading).toEqual(false); | ||
expect(result.current.isSuccess).toEqual(true); | ||
expect(result.current.isFailure).toEqual(false); | ||
expect(result.current.error).toEqual(''); | ||
}); | ||
|
||
const failedCodes = [100, 300, 400, 500, 600, 700, 800, 900]; | ||
test.each(failedCodes)('getRecs should fail if statusCode === %s', async (statusCode) => { | ||
const { result } = renderHook(() => useRecommendations()); | ||
const mock = mockAxios({ delayResponse: TIMEOUT }); | ||
const response = { | ||
response: { | ||
errorResponse: 'hi', | ||
errorCode: 123, | ||
}, | ||
statusCode, | ||
}; | ||
mock.onPost(RECOMMENDATION_URL).reply(200, response); | ||
jest.useFakeTimers(); | ||
|
||
await act(async () => { | ||
result.current.getRecs({ recommendation: {} }); | ||
}); | ||
|
||
// test loading states | ||
expect(result.current.isLoading).toEqual(true); | ||
|
||
await act(() => { | ||
jest.runAllTimers(); // trigger setTimeout | ||
}); | ||
|
||
expect(result.current.data).toEqual(null); | ||
expect(result.current.isInit).toEqual(false); | ||
expect(result.current.isLoading).toEqual(false); | ||
expect(result.current.isSuccess).toEqual(false); | ||
expect(result.current.isFailure).toEqual(true); | ||
expect(result.current.error).toEqual(response); | ||
}); | ||
}); |
Oops, something went wrong.