-
Notifications
You must be signed in to change notification settings - Fork 187
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
6ba075d
commit 088b3f0
Showing
16 changed files
with
400 additions
and
5 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
plugins/main/public/components/endpoints-summary/hooks/agents.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
78 changes: 78 additions & 0 deletions
78
plugins/main/public/components/endpoints-summary/hooks/upgrade-tasks.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,78 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { getTasks } from '../services'; | ||
import { useGetUpgradeTasks } from './upgrade-tasks'; | ||
import { API_NAME_TASK_STATUS } from '../../../../common/constants'; | ||
|
||
jest.mock('../services', () => ({ | ||
getTasks: jest.fn(), | ||
})); | ||
|
||
jest.useFakeTimers(); | ||
jest.spyOn(global, 'clearInterval'); | ||
|
||
describe('useGetUpgradeTasks hook', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should fetch initial data without any error', async () => { | ||
const mockGetTasks = jest.requireMock('../services').getTasks; | ||
mockGetTasks.mockImplementation(async ({ status }) => { | ||
if (status === API_NAME_TASK_STATUS.IN_PROGRESS) { | ||
return { total_affected_items: 5 }; | ||
} | ||
return { total_affected_items: 2 }; | ||
}); | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => | ||
useGetUpgradeTasks(false), | ||
); | ||
|
||
expect(result.current.getInProgressIsLoading).toBe(true); | ||
expect(result.current.totalInProgressTasks).toBeUndefined(); | ||
expect(result.current.getInProgressError).toBeUndefined(); | ||
|
||
expect(result.current.getErrorIsLoading).toBe(true); | ||
expect(result.current.totalErrorUpgradeTasks).toBeUndefined(); | ||
expect(result.current.getErrorTasksError).toBeUndefined(); | ||
|
||
await waitForNextUpdate(); | ||
jest.advanceTimersByTime(500); | ||
|
||
expect(result.current.getInProgressIsLoading).toBe(false); | ||
expect(result.current.totalInProgressTasks).toBe(5); | ||
expect(result.current.getInProgressError).toBeUndefined(); | ||
|
||
jest.advanceTimersByTime(500); | ||
|
||
expect(result.current.getErrorIsLoading).toBe(false); | ||
expect(result.current.totalErrorUpgradeTasks).toBe(2); | ||
expect(result.current.getErrorTasksError).toBeUndefined(); | ||
}); | ||
|
||
it('should clear interval when totalInProgressTasks is 0', async () => { | ||
const mockGetTasks = jest.requireMock('../services').getTasks; | ||
mockGetTasks.mockResolvedValue({ total_affected_items: 0 }); | ||
|
||
const { waitForNextUpdate } = renderHook(() => useGetUpgradeTasks(false)); | ||
|
||
await waitForNextUpdate(); | ||
jest.advanceTimersByTime(500); | ||
|
||
expect(clearInterval).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('should handle error while fetching data', async () => { | ||
const mockErrorMessage = 'Some error occurred'; | ||
(getTasks as jest.Mock).mockRejectedValue(mockErrorMessage); | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => | ||
useGetUpgradeTasks(0), | ||
); | ||
|
||
expect(result.current.getInProgressIsLoading).toBeTruthy(); | ||
await waitForNextUpdate(); | ||
expect(result.current.getInProgressError).toBe(mockErrorMessage); | ||
expect(result.current.getInProgressIsLoading).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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
78 changes: 78 additions & 0 deletions
78
plugins/main/public/components/endpoints-summary/services/get-tasks.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,78 @@ | ||
import { API_NAME_TASK_STATUS } from '../../../../common/constants'; | ||
import { WzRequest } from '../../../react-services/wz-request'; | ||
import { getTasks } from './get-tasks'; | ||
|
||
jest.mock('../../../react-services/wz-request', () => ({ | ||
WzRequest: { | ||
apiReq: jest.fn(), | ||
}, | ||
})); | ||
|
||
describe('getTasks', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should retrieve tasks', async () => { | ||
(WzRequest.apiReq as jest.Mock).mockResolvedValue({ | ||
data: { | ||
data: { | ||
affected_items: [ | ||
{ | ||
task_id: 1, | ||
agent_id: '001', | ||
status: API_NAME_TASK_STATUS.DONE, | ||
command: 'upgrade', | ||
}, | ||
{ | ||
task_id: 2, | ||
agent_id: '002', | ||
status: API_NAME_TASK_STATUS.DONE, | ||
command: 'upgrade', | ||
}, | ||
], | ||
total_affected_items: 2, | ||
failed_items: [], | ||
total_failed_items: 0, | ||
}, | ||
error: 0, | ||
message: 'Success', | ||
}, | ||
}); | ||
|
||
const result = await getTasks({ | ||
status: API_NAME_TASK_STATUS.DONE, | ||
command: 'upgrade', | ||
limit: 10, | ||
}); | ||
|
||
expect(WzRequest.apiReq).toHaveBeenCalledWith('GET', '/tasks/status', { | ||
params: { | ||
status: API_NAME_TASK_STATUS.DONE, | ||
command: 'upgrade', | ||
limit: 10, | ||
offset: 0, | ||
q: undefined, | ||
wait_for_complete: true, | ||
}, | ||
}); | ||
|
||
expect(result).toEqual({ | ||
affected_items: [ | ||
{ | ||
task_id: 1, | ||
agent_id: '001', | ||
status: API_NAME_TASK_STATUS.DONE, | ||
command: 'upgrade', | ||
}, | ||
{ | ||
task_id: 2, | ||
agent_id: '002', | ||
status: API_NAME_TASK_STATUS.DONE, | ||
command: 'upgrade', | ||
}, | ||
], | ||
total_affected_items: 2, | ||
}); | ||
}); | ||
}); |
File renamed without changes.
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
8 changes: 8 additions & 0 deletions
8
...omponents/endpoints-summary/table/actions/__snapshots__/upgrade-agent-modal.test.tsx.snap
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,8 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`UpgradeAgentModal component should return the component 1`] = ` | ||
<div | ||
aria-hidden="true" | ||
data-aria-hidden="true" | ||
/> | ||
`; |
62 changes: 62 additions & 0 deletions
62
plugins/main/public/components/endpoints-summary/table/actions/upgrade-agent-modal.test.tsx
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,62 @@ | ||
import React from 'react'; | ||
import { render, fireEvent, waitFor, act } from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; | ||
import { UpgradeAgentModal } from './upgrade-agent-modal'; | ||
|
||
jest.mock('../../services', () => ({ | ||
upgradeAgentsService: jest.fn(), | ||
})); | ||
|
||
jest.mock('../../../../react-services/common-services', () => ({ | ||
getErrorOrchestrator: () => ({ | ||
handleError: () => {}, | ||
}), | ||
})); | ||
|
||
describe('UpgradeAgentModal component', () => { | ||
test('should return the component', async () => { | ||
const { container, getByText, getByRole } = render( | ||
<UpgradeAgentModal | ||
agent={{ | ||
id: '001', | ||
name: 'agent1', | ||
group: ['default'], | ||
}} | ||
onClose={() => {}} | ||
reloadAgents={() => {}} | ||
/>, | ||
); | ||
|
||
expect(container).toMatchSnapshot(); | ||
|
||
const agentName = getByText('Upgrade agent agent1?'); | ||
expect(agentName).toBeInTheDocument(); | ||
|
||
const saveButton = getByRole('button', { name: 'Upgrade' }); | ||
expect(saveButton).toBeInTheDocument(); | ||
|
||
const cancelButton = getByRole('button', { name: 'Cancel' }); | ||
expect(cancelButton).toBeInTheDocument(); | ||
}); | ||
|
||
test('should send to upgrade', async () => { | ||
const { getByText, getByRole } = render( | ||
<UpgradeAgentModal | ||
agent={{ | ||
id: '001', | ||
name: 'agent1', | ||
group: ['default'], | ||
}} | ||
onClose={() => {}} | ||
reloadAgents={() => {}} | ||
/>, | ||
); | ||
|
||
const saveButton = getByRole('button', { name: 'Upgrade' }); | ||
expect(saveButton).toBeInTheDocument(); | ||
|
||
act(() => { | ||
fireEvent.click(saveButton); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.