|
| 1 | +import axios from 'axios'; // Import axios for mocking HTTP requests |
| 2 | +import { getDashboardDataAI, updateDashboardData, updateCopiedPromptDate, getCopiedDateOfPrompt } from '../weeklySummariesAIPrompt'; // Import the actions to be tested |
| 3 | +import { getAIPrompt, updateAIPrompt, updateCopiedPrompt, getCopiedPromptDate } from '../../constants/weeklySummariesAIPrompt'; // Import the action creators |
| 4 | + |
| 5 | +jest.mock('axios'); // Mock axios to control its behavior in tests |
| 6 | + |
| 7 | +describe('getDashboardDataAI', () => { |
| 8 | + it('should dispatch getAIPrompt with data on success', async () => { |
| 9 | + const mockData = { prompt: 'AI Prompt' }; // Mock data to be returned by axios |
| 10 | + axios.get.mockResolvedValue({ data: mockData }); // Mock axios.get to resolve with mock data |
| 11 | + |
| 12 | + const dispatch = jest.fn(); // Mock dispatch function |
| 13 | + await getDashboardDataAI()(dispatch); // Call the action with the mock dispatch |
| 14 | + |
| 15 | + expect(axios.get).toHaveBeenCalledWith(expect.any(String)); // Assert axios.get was called with any URL |
| 16 | + expect(dispatch).toHaveBeenCalledWith(getAIPrompt(mockData)); // Assert dispatch was called with the correct action |
| 17 | + }); |
| 18 | + |
| 19 | + it('should dispatch getAIPrompt with undefined on failure', async () => { |
| 20 | + axios.get.mockRejectedValue(new Error('Network Error')); // Mock axios.get to reject with an error |
| 21 | + |
| 22 | + const dispatch = jest.fn(); // Mock dispatch function |
| 23 | + await getDashboardDataAI()(dispatch); // Call the action with the mock dispatch |
| 24 | + |
| 25 | + expect(axios.get).toHaveBeenCalledWith(expect.any(String)); // Assert axios.get was called with any URL |
| 26 | + expect(dispatch).toHaveBeenCalledWith(getAIPrompt(undefined)); // Assert dispatch was called with the correct action |
| 27 | + }); |
| 28 | +}); |
| 29 | + |
| 30 | +describe('updateDashboardData', () => { |
| 31 | + it('should dispatch updateAIPrompt with textPrompt on success', async () => { |
| 32 | + const textPrompt = 'New AI Prompt'; // Mock text prompt |
| 33 | + axios.put.mockResolvedValue({ status: 200 }); // Mock axios.put to resolve with a success status |
| 34 | + |
| 35 | + const dispatch = jest.fn(); // Mock dispatch function |
| 36 | + await updateDashboardData(textPrompt)(dispatch); // Call the action with the mock dispatch |
| 37 | + |
| 38 | + expect(axios.put).toHaveBeenCalledWith(expect.any(String), { aIPromptText: textPrompt }); // Assert axios.put was called with the correct URL and data |
| 39 | + expect(dispatch).toHaveBeenCalledWith(updateAIPrompt(textPrompt)); // Assert dispatch was called with the correct action |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +describe('updateCopiedPromptDate', () => { |
| 44 | + it('should dispatch updateCopiedPrompt with userId on success', async () => { |
| 45 | + const userId = '12345'; // Mock user ID |
| 46 | + axios.put.mockResolvedValue({ status: 200 }); // Mock axios.put to resolve with a success status |
| 47 | + |
| 48 | + const dispatch = jest.fn(); // Mock dispatch function |
| 49 | + await updateCopiedPromptDate(userId)(dispatch); // Call the action with the mock dispatch |
| 50 | + |
| 51 | + expect(axios.put).toHaveBeenCalledWith(expect.any(String)); // Assert axios.put was called with the correct URL |
| 52 | + expect(dispatch).toHaveBeenCalledWith(updateCopiedPrompt(userId)); // Assert dispatch was called with the correct action |
| 53 | + }); |
| 54 | +}); |
| 55 | + |
| 56 | +describe('getCopiedDateOfPrompt', () => { |
| 57 | + it('should dispatch getCopiedPromptDate with data on success', async () => { |
| 58 | + const userId = '12345'; // Mock user ID |
| 59 | + const mockData = { message: '2023-10-01' }; // Mock data to be returned by axios |
| 60 | + axios.get.mockResolvedValue({ data: mockData }); // Mock axios.get to resolve with mock data |
| 61 | + |
| 62 | + const dispatch = jest.fn(); // Mock dispatch function |
| 63 | + await getCopiedDateOfPrompt(userId)(dispatch); // Call the action with the mock dispatch |
| 64 | + |
| 65 | + expect(axios.get).toHaveBeenCalledWith(expect.any(String)); // Assert axios.get was called with the correct URL |
| 66 | + expect(dispatch).toHaveBeenCalledWith(getCopiedPromptDate(mockData.message)); // Assert dispatch was called with the correct action |
| 67 | + }); |
| 68 | + |
| 69 | + it('should dispatch getCopiedPromptDate with undefined on failure', async () => { |
| 70 | + const userId = '12345'; // Mock user ID |
| 71 | + axios.get.mockRejectedValue(new Error('Network Error')); // Mock axios.get to reject with an error |
| 72 | + |
| 73 | + const dispatch = jest.fn(); // Mock dispatch function |
| 74 | + await getCopiedDateOfPrompt(userId)(dispatch); // Call the action with the mock dispatch |
| 75 | + |
| 76 | + expect(axios.get).toHaveBeenCalledWith(expect.any(String)); // Assert axios.get was called with the correct URL |
| 77 | + expect(dispatch).toHaveBeenCalledWith(getCopiedPromptDate(undefined)); // Assert dispatch was called with the correct action |
| 78 | + }); |
| 79 | +}); |
0 commit comments