-
Notifications
You must be signed in to change notification settings - Fork 1
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
8318208
commit a489015
Showing
3 changed files
with
272 additions
and
0 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,153 @@ | ||
import { act, renderHook } from '@testing-library/react'; | ||
import { setInitialGlobalState } from '@src/test/__mocks__/store'; | ||
import BaseApi from '@common/api/base.api'; | ||
import { UserNotificationFactory } from '@common/services/userNotification'; | ||
import { StatusType } from '@common/constants/status.constants'; | ||
import { useApi } from '@common/hooks/useApi'; | ||
import { useLoadingState, useStatusState } from '@src/store'; | ||
|
||
jest.mock('@common/api/base.api'); | ||
jest.mock('@common/services/userNotification', () => ({ | ||
UserNotificationFactory: { | ||
createMessage: jest.fn(), | ||
}, | ||
})); | ||
|
||
describe('useApi', () => { | ||
const mockSetIsLoading = jest.fn(); | ||
const mockAddStatusMessagesItem = jest.fn(); | ||
const mockResponse = { data: 'test data' }; | ||
|
||
beforeEach(() => { | ||
setInitialGlobalState([ | ||
{ | ||
store: useLoadingState, | ||
state: { setIsLoading: mockSetIsLoading }, | ||
}, | ||
{ | ||
store: useStatusState, | ||
state: { addStatusMessagesItem: mockAddStatusMessagesItem }, | ||
}, | ||
]); | ||
|
||
(BaseApi.getJson as jest.Mock).mockResolvedValue(mockResponse); | ||
(BaseApi.generateUrl as jest.Mock).mockImplementation(url => url); | ||
}); | ||
|
||
it('initializes with null data', () => { | ||
const { result } = renderHook(() => useApi()); | ||
|
||
expect(result.current.data).toBeNull(); | ||
}); | ||
|
||
describe('makeRequest', () => { | ||
it('handles successful GET request', async () => { | ||
const { result } = renderHook(() => useApi()); | ||
|
||
await act(async () => { | ||
await result.current.makeRequest({ url: '/test-url' }); | ||
}); | ||
|
||
expect(mockSetIsLoading).toHaveBeenCalledWith(true); | ||
expect(BaseApi.getJson).toHaveBeenCalledWith({ | ||
url: '/test-url', | ||
urlParams: undefined, | ||
requestParams: { | ||
method: 'GET', | ||
headers: { 'content-type': 'application/json' }, | ||
}, | ||
}); | ||
expect(result.current.data).toEqual(mockResponse); | ||
expect(mockSetIsLoading).toHaveBeenCalledWith(false); | ||
}); | ||
|
||
it('handles POST request with body', async () => { | ||
const { result } = renderHook(() => useApi()); | ||
const testBody = { key: 'value' }; | ||
|
||
await act(async () => { | ||
await result.current.makeRequest({ | ||
url: '/test-url', | ||
method: 'POST', | ||
body: testBody, | ||
}); | ||
}); | ||
|
||
expect(BaseApi.getJson).toHaveBeenCalledWith({ | ||
url: '/test-url', | ||
urlParams: undefined, | ||
requestParams: { | ||
method: 'POST', | ||
headers: { 'content-type': 'application/json' }, | ||
body: JSON.stringify(testBody, null, 2), | ||
}, | ||
}); | ||
}); | ||
|
||
it('handles URL parameters correctly', async () => { | ||
const { result } = renderHook(() => useApi()); | ||
const urlParam = { name: 'id', value: '123' }; | ||
|
||
await act(async () => { | ||
await result.current.makeRequest({ | ||
url: '/test-url', | ||
urlParam, | ||
}); | ||
}); | ||
|
||
expect(BaseApi.generateUrl).toHaveBeenCalledWith('/test-url', urlParam); | ||
}); | ||
|
||
it('handles errors and shows notification', async () => { | ||
const error = new Error('API Error'); | ||
(BaseApi.getJson as jest.Mock).mockRejectedValue(error); | ||
|
||
const { result } = renderHook(() => useApi()); | ||
|
||
await act(async () => { | ||
await result.current.makeRequest({ url: '/test-url' }); | ||
}); | ||
|
||
expect(UserNotificationFactory.createMessage).toHaveBeenCalledWith(StatusType.error, 'ld.errorMakingApiRequest'); | ||
expect(mockSetIsLoading).toHaveBeenCalledWith(false); | ||
}); | ||
|
||
it('uses custom error message when provided', async () => { | ||
const error = new Error('API Error'); | ||
(BaseApi.getJson as jest.Mock).mockRejectedValue(error); | ||
|
||
const { result } = renderHook(() => useApi()); | ||
|
||
await act(async () => { | ||
await result.current.makeRequest({ | ||
url: '/test-url', | ||
errorMessageId: 'custom.error', | ||
}); | ||
}); | ||
|
||
expect(UserNotificationFactory.createMessage).toHaveBeenCalledWith(StatusType.error, 'custom.error'); | ||
}); | ||
|
||
it('handles request params', async () => { | ||
const { result } = renderHook(() => useApi()); | ||
const customRequestParams = { credentials: 'include' as RequestCredentials }; | ||
|
||
await act(async () => { | ||
await result.current.makeRequest({ | ||
url: '/test-url', | ||
requestParams: customRequestParams, | ||
}); | ||
}); | ||
|
||
expect(BaseApi.getJson).toHaveBeenCalledWith({ | ||
url: '/test-url', | ||
urlParams: undefined, | ||
requestParams: { | ||
method: 'GET', | ||
headers: { 'content-type': 'application/json' }, | ||
...customRequestParams, | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |
65 changes: 65 additions & 0 deletions
65
src/test/__tests__/common/hooks/useComplexLookupValidation.test.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,65 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { setInitialGlobalState } from '@src/test/__mocks__/store'; | ||
import { useComplexLookupValidation } from '@src/common/hooks/useComplexLookupValidation'; | ||
import { useComplexLookupStore } from '@src/store/stores/complexLookup'; | ||
|
||
describe('useComplexLookupValidation', () => { | ||
const mockAddAuthorityAssignmentCheckFailedIdsItem = jest.fn(); | ||
const mockResetAuthorityAssignmentCheckFailedIds = jest.fn(); | ||
|
||
beforeEach(() => { | ||
setInitialGlobalState([ | ||
{ | ||
store: useComplexLookupStore, | ||
state: { | ||
authorityAssignmentCheckFailedIds: ['existing-id-1', 'existing-id-2'], | ||
addAuthorityAssignmentCheckFailedIdsItem: mockAddAuthorityAssignmentCheckFailedIdsItem, | ||
resetAuthorityAssignmentCheckFailedIds: mockResetAuthorityAssignmentCheckFailedIds, | ||
}, | ||
}, | ||
]); | ||
}); | ||
|
||
describe('addFailedEntryId', () => { | ||
it('addAuthorityAssignmentCheckFailedIdsItem with provided id', () => { | ||
const testId = 'test-id'; | ||
|
||
const { result } = renderHook(() => useComplexLookupValidation()); | ||
result.current.addFailedEntryId(testId); | ||
|
||
expect(mockAddAuthorityAssignmentCheckFailedIdsItem).toHaveBeenCalledWith(testId); | ||
}); | ||
}); | ||
|
||
describe('clearFailedEntryIds', () => { | ||
it('calls resetAuthorityAssignmentCheckFailedIds', () => { | ||
const { result } = renderHook(() => useComplexLookupValidation()); | ||
result.current.clearFailedEntryIds(); | ||
|
||
expect(mockResetAuthorityAssignmentCheckFailedIds).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('checkFailedId', () => { | ||
it('returns true for existing id', () => { | ||
const { result } = renderHook(() => useComplexLookupValidation()); | ||
const exists = result.current.checkFailedId('existing-id-1'); | ||
|
||
expect(exists).toBe(true); | ||
}); | ||
|
||
it('returns false for non-existing id', () => { | ||
const { result } = renderHook(() => useComplexLookupValidation()); | ||
const exists = result.current.checkFailedId('non-existing-id'); | ||
|
||
expect(exists).toBe(false); | ||
}); | ||
|
||
it('returns false for undefined id', () => { | ||
const { result } = renderHook(() => useComplexLookupValidation()); | ||
const exists = result.current.checkFailedId(); | ||
|
||
expect(exists).toBe(false); | ||
}); | ||
}); | ||
}); |