-
Notifications
You must be signed in to change notification settings - Fork 52
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
348b267
commit bbd8982
Showing
6 changed files
with
260 additions
and
8 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { executeShellCommandOnServer } from '../api'; | ||
import { WebService } from '@/services/webServices'; | ||
|
||
// Mock WebService | ||
jest.mock('@/services/webServices', () => ({ | ||
WebService: { | ||
post: jest.fn(), | ||
}, | ||
})); | ||
|
||
describe('executeShellCommandOnServer', () => { | ||
const mockCmd = 'testCommand'; | ||
const mockCmdOptions = { option1: 'value1' }; | ||
const mockCmdExecURL = `/shell/exec/${mockCmd}`; | ||
let consoleErrorSpy: jest.SpyInstance; | ||
|
||
beforeEach(() => { | ||
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
}); | ||
|
||
afterEach(() => { | ||
consoleErrorSpy.mockRestore(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return data when WebService.post is successful', async () => { | ||
const mockResponse = { data: 'Success Response' }; | ||
(WebService.post as jest.Mock).mockResolvedValueOnce(mockResponse); | ||
|
||
const result = await executeShellCommandOnServer(mockCmd, mockCmdOptions); | ||
|
||
expect(WebService.post).toHaveBeenCalledWith( | ||
mockCmdExecURL, | ||
mockCmdOptions, | ||
); | ||
expect(result).toEqual('Success Response'); | ||
}); | ||
|
||
it('should return error message when response structure is unexpected', async () => { | ||
const mockResponse = {}; // Simulate unexpected response structure | ||
(WebService.post as jest.Mock).mockResolvedValueOnce(mockResponse); | ||
|
||
const result = await executeShellCommandOnServer(mockCmd, mockCmdOptions); | ||
|
||
expect(result).toBe('Error: Error: Unexpected response structure'); | ||
expect(WebService.post).toHaveBeenCalledWith( | ||
mockCmdExecURL, | ||
mockCmdOptions, | ||
); | ||
}); | ||
|
||
it('should return error message when WebService.post throws an error', async () => { | ||
const mockError = new Error('Network Error'); | ||
|
||
(WebService.post as jest.Mock).mockRejectedValueOnce(mockError); | ||
const result = await executeShellCommandOnServer(mockCmd, mockCmdOptions); | ||
expect(WebService.post).toHaveBeenCalledWith( | ||
mockCmdExecURL, | ||
mockCmdOptions, | ||
); | ||
expect(result).toBe(`Error: ${mockError}`); | ||
consoleErrorSpy.mockRestore(); | ||
}); | ||
|
||
it('should log error to the console when WebService.post throws an error', async () => { | ||
const mockError = new Error('Request Failed'); | ||
const consoleSpy = jest | ||
.spyOn(console, 'error') | ||
.mockImplementation(() => {}); | ||
(WebService.post as jest.Mock).mockRejectedValueOnce(mockError); | ||
|
||
await executeShellCommandOnServer(mockCmd, mockCmdOptions); | ||
|
||
expect(consoleSpy).toHaveBeenCalledWith( | ||
'Error executing command:', | ||
mockError, | ||
); | ||
consoleSpy.mockRestore(); | ||
}); | ||
|
||
it('should always include the cmd parameter in the URL', async () => { | ||
const mockResponse = { data: 'Some Response' }; | ||
(WebService.post as jest.Mock).mockResolvedValueOnce(mockResponse); | ||
|
||
const result = await executeShellCommandOnServer(mockCmd, mockCmdOptions); | ||
|
||
expect(WebService.post).toHaveBeenCalledWith( | ||
expect.stringContaining(`/shell/exec/${mockCmd}`), | ||
mockCmdOptions, | ||
); | ||
expect(result).toEqual('Some Response'); | ||
}); | ||
}); |
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