|
| 1 | +import React from 'react'; |
| 2 | +import CurrentPromptModal from 'components/WeeklySummary/CurrentPromptModal'; |
| 3 | +import { render, screen, fireEvent, waitFor, act } from '@testing-library/react'; |
| 4 | +import '@testing-library/jest-dom/extend-expect'; |
| 5 | +import { Provider } from 'react-redux'; |
| 6 | +import thunk from 'redux-thunk'; |
| 7 | +import configureStore from 'redux-mock-store'; |
| 8 | +import axios from 'axios'; |
| 9 | + |
| 10 | +const mockStore = configureStore([thunk]); |
| 11 | + |
| 12 | +const theme = { darkMode: false }; |
| 13 | + |
| 14 | +let store; |
| 15 | + |
| 16 | +beforeEach(() => { |
| 17 | + store = mockStore({ |
| 18 | + theme: theme, |
| 19 | + }); |
| 20 | +}); |
| 21 | + |
| 22 | +const mockWriteText = jest.fn(); |
| 23 | +const mockToastSuccess = jest.fn(); |
| 24 | +jest.mock('axios'); |
| 25 | + |
| 26 | +beforeAll(() => { |
| 27 | + Object.defineProperty(navigator, 'clipboard', { |
| 28 | + value: { writeText: mockWriteText }, |
| 29 | + writable: true, |
| 30 | + }); |
| 31 | + |
| 32 | + jest.mock('react-toastify', () => ({ |
| 33 | + toast: { success: mockToastSuccess }, |
| 34 | + })); |
| 35 | +}); |
| 36 | + |
| 37 | +afterEach(() => { |
| 38 | + jest.clearAllMocks(); |
| 39 | +}); |
| 40 | + |
| 41 | +describe('CurrentPromptModal component', () => { |
| 42 | + it('render component without crashing', async () => { |
| 43 | + axios.get.mockResolvedValue({ |
| 44 | + status: 200, |
| 45 | + data: [], |
| 46 | + }); |
| 47 | + await act(async () => { |
| 48 | + render( |
| 49 | + <Provider store={store}> |
| 50 | + <CurrentPromptModal userId={'abc123'} userRole={'Manager'} darkMode={theme} /> |
| 51 | + </Provider>, |
| 52 | + ); |
| 53 | + }); |
| 54 | + }); |
| 55 | + it('check view and copy current AI prompt button', async () => { |
| 56 | + axios.get.mockResolvedValue({ |
| 57 | + status: 200, |
| 58 | + data: [], |
| 59 | + }); |
| 60 | + await act(async () => { |
| 61 | + const { container } = render( |
| 62 | + <Provider store={store}> |
| 63 | + <CurrentPromptModal userId={'abc123'} userRole={'Manager'} darkMode={theme} /> |
| 64 | + </Provider>, |
| 65 | + ); |
| 66 | + await waitFor(() => { |
| 67 | + const buttonElement = container.querySelector('[class="btn btn-info"]'); |
| 68 | + expect(buttonElement.textContent).toBe('View and Copy Current AI Prompt'); |
| 69 | + fireEvent.click(buttonElement); |
| 70 | + const modalElement = screen.getByRole('dialog'); |
| 71 | + expect(modalElement).toBeInTheDocument(); |
| 72 | + }); |
| 73 | + }); |
| 74 | + }); |
| 75 | + it('check tooltip associated with the button', async () => { |
| 76 | + axios.get.mockResolvedValue({ |
| 77 | + status: 200, |
| 78 | + data: [], |
| 79 | + }); |
| 80 | + await act(async () => { |
| 81 | + const { container } = render( |
| 82 | + <Provider store={store}> |
| 83 | + <CurrentPromptModal userId={'abc123'} userRole={'Manager'} darkMode={theme} /> |
| 84 | + </Provider>, |
| 85 | + ); |
| 86 | + await waitFor(async () => { |
| 87 | + const iconElement = container.querySelector('.fa.fa-info-circle'); |
| 88 | + fireEvent.mouseEnter(iconElement); |
| 89 | + await waitFor(() => { |
| 90 | + expect(iconElement).toHaveAttribute('aria-describedby'); |
| 91 | + }); |
| 92 | + fireEvent.mouseLeave(iconElement); |
| 93 | + await waitFor(() => { |
| 94 | + expect(iconElement).not.toHaveAttribute('aria-describedby'); |
| 95 | + }); |
| 96 | + }); |
| 97 | + }); |
| 98 | + }); |
| 99 | + it('check current AI prompt modal content', async () => { |
| 100 | + axios.get.mockResolvedValue({ |
| 101 | + status: 200, |
| 102 | + aIPromptText: `Please edit the following summary of my week's work. Make sure it is professionally written in 3rd person format. |
| 103 | + Write it as only one paragraph. It must be only one paragraph. Keep it less than 500 words. Start the paragraph with 'This week'. |
| 104 | + Make sure the paragraph contains no links or URLs and write it in a tone that is matter-of-fact and without embellishment. |
| 105 | + Do not add flowery language, keep it simple and factual. Do not add a final summary sentence. Apply all this to the following:`, |
| 106 | + }); |
| 107 | + await act(async () => { |
| 108 | + const { container } = render( |
| 109 | + <Provider store={store}> |
| 110 | + <CurrentPromptModal userId={'abc123'} userRole={'Manager'} darkMode={theme} /> |
| 111 | + </Provider>, |
| 112 | + ); |
| 113 | + await waitFor(() => { |
| 114 | + const buttonElement = container.querySelector('[class="btn btn-info"]'); |
| 115 | + fireEvent.click(buttonElement); |
| 116 | + |
| 117 | + const modalDialogElement = screen.getByRole('document'); |
| 118 | + const modalContentElement = modalDialogElement.querySelector('.modal-content'); |
| 119 | + const modalHeaderElement = modalContentElement.querySelector('.modal-header'); |
| 120 | + const modalTitleElement = modalHeaderElement.querySelector('.modal-title'); |
| 121 | + const modalBodyElement = modalContentElement.querySelector('.modal-body'); |
| 122 | + expect(modalTitleElement.textContent).toBe('Current AI Prompt'); |
| 123 | + expect(modalBodyElement.textContent).toContain( |
| 124 | + "Please edit the following summary of my week's work. Make sure it is professionally written in 3rd person format.", |
| 125 | + ); |
| 126 | + expect(modalBodyElement.textContent).toContain( |
| 127 | + "Write it as only one paragraph. It must be only one paragraph. Keep it less than 500 words. Start the paragraph with 'This week'.", |
| 128 | + ); |
| 129 | + expect(modalBodyElement.textContent).toContain( |
| 130 | + 'Make sure the paragraph contains no links or URLs and write it in a tone that is matter-of-fact and without embellishment.', |
| 131 | + ); |
| 132 | + expect(modalBodyElement.textContent).toContain( |
| 133 | + 'Do not add flowery language, keep it simple and factual. Do not add a final summary sentence. Apply all this to the following:', |
| 134 | + ); |
| 135 | + }); |
| 136 | + }); |
| 137 | + }); |
| 138 | + it('check close modal button', async () => { |
| 139 | + axios.get.mockResolvedValue({ |
| 140 | + status: 200, |
| 141 | + data: [], |
| 142 | + }); |
| 143 | + await act(async () => { |
| 144 | + const { container } = render( |
| 145 | + <Provider store={store}> |
| 146 | + <CurrentPromptModal userId={'abc123'} userRole={'Manager'} darkMode={theme} /> |
| 147 | + </Provider>, |
| 148 | + ); |
| 149 | + |
| 150 | + await waitFor(async () => { |
| 151 | + const buttonElement = container.querySelector('[class="btn btn-info"]'); |
| 152 | + fireEvent.click(buttonElement); |
| 153 | + await waitFor(() => { |
| 154 | + expect(screen.getByRole('document')).toBeInTheDocument(); |
| 155 | + }); |
| 156 | + const modalDialogElement = screen.getByRole('document'); |
| 157 | + const modalContentElement = modalDialogElement.querySelector('.modal-content'); |
| 158 | + const modalHeaderElement = modalContentElement.querySelector('.modal-header'); |
| 159 | + const closeElement = modalHeaderElement.querySelector('.close'); |
| 160 | + fireEvent.click(closeElement); |
| 161 | + await waitFor(() => { |
| 162 | + expect(screen.queryByRole('document')).not.toBeInTheDocument(); |
| 163 | + }); |
| 164 | + }); |
| 165 | + }); |
| 166 | + }); |
| 167 | + it('check copy button inside the modal', async () => { |
| 168 | + axios.put.mockResolvedValue({ |
| 169 | + status: 200, |
| 170 | + data: [], |
| 171 | + }); |
| 172 | + await act(async () => { |
| 173 | + const { container } = render( |
| 174 | + <Provider store={store}> |
| 175 | + <CurrentPromptModal userId={'abc123'} userRole={'Manager'} darkMode={theme} /> |
| 176 | + </Provider>, |
| 177 | + ); |
| 178 | + const currentPrompt = `Please edit the following summary of my week's work. Make sure it is professionally written in 3rd person format. |
| 179 | + Write it as only one paragraph. It must be only one paragraph. Keep it less than 500 words. Start the paragraph with 'This week'. |
| 180 | + Make sure the paragraph contains no links or URLs and write it in a tone that is matter-of-fact and without embellishment. |
| 181 | + Do not add flowery language, keep it simple and factual. Do not add a final summary sentence. Apply all this to the following:`; |
| 182 | + |
| 183 | + await waitFor(async () => { |
| 184 | + const buttonElement = container.querySelector('[class="btn btn-info"]'); |
| 185 | + fireEvent.click(buttonElement); |
| 186 | + await waitFor(() => { |
| 187 | + expect(screen.getByRole('document')).toBeInTheDocument(); |
| 188 | + }); |
| 189 | + const modalDialogElement = screen.getByRole('document'); |
| 190 | + const modalContentElement = modalDialogElement.querySelector('.modal-content'); |
| 191 | + const modalFooterElement = modalContentElement.querySelector('.btn.btn-primary'); |
| 192 | + expect(modalFooterElement.textContent).toBe('Copy Prompt'); |
| 193 | + fireEvent.click(modalFooterElement); |
| 194 | + expect(mockWriteText).toHaveBeenCalledWith(currentPrompt); |
| 195 | + setTimeout(() => { |
| 196 | + expect(mockToastSuccess).toHaveBeenCalledWith('Prompt Copied!'); |
| 197 | + }, 1000); |
| 198 | + }); |
| 199 | + }); |
| 200 | + }); |
| 201 | +}); |
0 commit comments