|
| 1 | +import axios from 'axios'; // Import axios for making HTTP requests |
| 2 | +import configureMockStore from 'redux-mock-store'; // Import redux-mock-store for creating a mock store |
| 3 | +import thunk from 'redux-thunk'; // Import redux-thunk for handling asynchronous actions |
| 4 | +import * as types from '../../constants/BluequareEmailBccConstants'; // Import the action type constants |
| 5 | +import { setBlueSquareEmailAssignement, deleteBlueSquareEmailAssignement } from '../blueSquareEmailBCCAction'; // Import the action creator |
| 6 | + |
| 7 | +// Mock axios to control its behavior in tests |
| 8 | +jest.mock('axios'); |
| 9 | + |
| 10 | +// Define middlewares to be used in the mock store |
| 11 | +const middlewares = [thunk]; |
| 12 | +// Create a mock store with the defined middlewares |
| 13 | +const mockStore = configureMockStore(middlewares); |
| 14 | + |
| 15 | +// Define the test suite for the setBlueSquareEmailAssignement action creator |
| 16 | +describe('setBlueSquareEmailAssignement action creator', () => { |
| 17 | + // Define a test case for the success scenario |
| 18 | + it('should handle setting a blue square email assignment successfully', async () => { |
| 19 | + // Create an empty mock store |
| 20 | + const store = mockStore({}); |
| 21 | + // Define the email to be used in the test |
| 22 | + const email = 'test@example.com'; |
| 23 | + // Define the expected response data |
| 24 | + const responseData = { id: 1, email }; |
| 25 | + |
| 26 | + // Mock the axios.post method to resolve with the expected response data |
| 27 | + axios.post.mockResolvedValue({ status: 200, data: responseData }); |
| 28 | + |
| 29 | + // Dispatch the setBlueSquareEmailAssignement action with the email |
| 30 | + await store.dispatch(setBlueSquareEmailAssignement(email)); |
| 31 | + |
| 32 | + // Assert that the actions dispatched include the setBlueSquareEmailBcc action |
| 33 | + const actionsDispatched = store.getActions(); |
| 34 | + expect(actionsDispatched).toContainEqual({ |
| 35 | + type: types.SET_BLUE_SQUARE_EMAIL_ASSIGNMENT, |
| 36 | + payload: responseData, |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + // Define a test case for the failure scenario |
| 41 | + it('should handle setting a blue square email assignment failure', async () => { |
| 42 | + // Create an empty mock store |
| 43 | + const store = mockStore({}); |
| 44 | + // Define the email to be used in the test |
| 45 | + const email = 'test@example.com'; |
| 46 | + // Define the expected error message |
| 47 | + const errorMessage = 'Network Error'; |
| 48 | + |
| 49 | + // Mock the axios.post method to reject with an error |
| 50 | + axios.post.mockRejectedValue(new Error(errorMessage)); |
| 51 | + |
| 52 | + // Dispatch the setBlueSquareEmailAssignement action with the email |
| 53 | + await store.dispatch(setBlueSquareEmailAssignement(email)); |
| 54 | + |
| 55 | + // Assert that the actions dispatched include the blueSquareEmailBccError action |
| 56 | + const actionsDispatched = store.getActions(); |
| 57 | + expect(actionsDispatched).toContainEqual({ |
| 58 | + type: types.BLUE_SQUARE_EMAIL_ASSIGNMENT_ERROR, |
| 59 | + payload: new Error(errorMessage), |
| 60 | + }); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +// Define the test suite for the deleteBlueSquareEmailAssignement action creator |
| 65 | +describe('deleteBlueSquareEmailAssignement action creator', () => { |
| 66 | + // Define a test case for the success scenario |
| 67 | + it('should handle deleting a blue square email assignment successfully', async () => { |
| 68 | + // Create an empty mock store |
| 69 | + const store = mockStore({}); |
| 70 | + // Define the ID to be used in the test |
| 71 | + const id = 1; |
| 72 | + // Define the expected response data |
| 73 | + const responseData = { id }; |
| 74 | + |
| 75 | + // Mock the axios.delete method to resolve with the expected response data |
| 76 | + axios.delete.mockResolvedValue({ status: 200, data: responseData }); |
| 77 | + |
| 78 | + // Dispatch the deleteBlueSquareEmailAssignement action with the ID |
| 79 | + await store.dispatch(deleteBlueSquareEmailAssignement(id)); |
| 80 | + |
| 81 | + // Assert that the actions dispatched include the deleteBlueSquareEmailBcc action |
| 82 | + const actionsDispatched = store.getActions(); |
| 83 | + expect(actionsDispatched).toContainEqual({ |
| 84 | + type: types.DELETE_BLUE_SQUARE_EMAIL_ASSIGNMENT, |
| 85 | + payload: id, |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + // Define a test case for the failure scenario |
| 90 | + it('should handle deleting a blue square email assignment failure', async () => { |
| 91 | + // Create an empty mock store |
| 92 | + const store = mockStore({}); |
| 93 | + // Define the ID to be used in the test |
| 94 | + const id = 1; |
| 95 | + // Define the expected error message |
| 96 | + const errorMessage = 'Network Error'; |
| 97 | + |
| 98 | + // Mock the axios.delete method to reject with an error |
| 99 | + axios.delete.mockRejectedValue(new Error(errorMessage)); |
| 100 | + |
| 101 | + // Dispatch the deleteBlueSquareEmailAssignement action with the ID |
| 102 | + await store.dispatch(deleteBlueSquareEmailAssignement(id)); |
| 103 | + |
| 104 | + // Assert that the actions dispatched include the blueSquareEmailBccError action |
| 105 | + const actionsDispatched = store.getActions(); |
| 106 | + expect(actionsDispatched).toContainEqual({ |
| 107 | + type: types.BLUE_SQUARE_EMAIL_ASSIGNMENT_ERROR, |
| 108 | + payload: new Error(errorMessage), |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments