-
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.
* Abstract callback handling into generic dispatchAPICallbackToast * Test dispatchAPICallbackToast
- Loading branch information
1 parent
30341df
commit 42d4a4a
Showing
7 changed files
with
173 additions
and
76 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,96 @@ | ||
import { dispatchAPICallbackToast } from './api_helper'; | ||
|
||
jest.mock('foremanReact/components/ToastsList', () => ({ | ||
addToast: jest.fn(param => param), | ||
})); | ||
|
||
jest.mock('foremanReact/common/I18n', () => ({ | ||
translate: jest.fn(param => param), | ||
})); | ||
|
||
const dispatch = jest.fn(); | ||
|
||
describe('dispatchAPICallbackToast', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); // Reset mock calls after each test | ||
}); | ||
|
||
it('should dispatch a success toast', () => { | ||
const isSuccess = true; | ||
const response = {}; | ||
const successMessage = 'Success message'; | ||
const errorMessage = 'Error message'; | ||
|
||
dispatchAPICallbackToast( | ||
dispatch, | ||
isSuccess, | ||
response, | ||
successMessage, | ||
errorMessage | ||
); | ||
|
||
expect(dispatch).toHaveBeenCalledTimes(1); | ||
expect(dispatch).toHaveBeenCalledWith({ | ||
type: 'success', | ||
message: 'Success message', | ||
}); | ||
}); | ||
|
||
it('should dispatch a warning toast with error', () => { | ||
const isSuccess = false; | ||
const response = { | ||
response: { | ||
data: { | ||
error: { | ||
full_messages: 'Some nice error', | ||
}, | ||
}, | ||
}, | ||
}; | ||
const successMessage = 'Success message'; | ||
const errorMessage = 'Error message'; | ||
|
||
dispatchAPICallbackToast( | ||
dispatch, | ||
isSuccess, | ||
response, | ||
successMessage, | ||
errorMessage | ||
); | ||
|
||
expect(dispatch).toHaveBeenCalledTimes(1); | ||
expect(dispatch).toHaveBeenCalledWith({ | ||
type: 'warning', | ||
message: 'Error message Some nice error', | ||
}); | ||
}); | ||
|
||
it('should dispatch a warning toast without error', () => { | ||
const isSuccess = false; | ||
const response = { | ||
response: { | ||
data: { | ||
notTheExpectedErrorFormat: { | ||
full_messages: 'Some nice error', | ||
}, | ||
}, | ||
}, | ||
}; | ||
const successMessage = 'Success message'; | ||
const errorMessage = 'Error message'; | ||
|
||
dispatchAPICallbackToast( | ||
dispatch, | ||
isSuccess, | ||
response, | ||
successMessage, | ||
errorMessage | ||
); | ||
|
||
expect(dispatch).toHaveBeenCalledTimes(1); | ||
expect(dispatch).toHaveBeenCalledWith({ | ||
type: 'warning', | ||
message: 'Error message', | ||
}); | ||
}); | ||
}); |
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
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