From 423e72ce3619f3c9407499e39c1811e5970afbea Mon Sep 17 00:00:00 2001 From: ObuMan <111461001+ObuMan@users.noreply.github.com> Date: Sat, 12 Oct 2024 16:12:20 +0200 Subject: [PATCH] fix : final fix AddSongPlayListAccordion.test.tsx --- .../Sidebar/AddSongPlayListAccordion.test.tsx | 72 ++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/Electron/src/__tests__/components/Sidebar/AddSongPlayListAccordion.test.tsx b/Electron/src/__tests__/components/Sidebar/AddSongPlayListAccordion.test.tsx index 23a14ff4..3094ae39 100644 --- a/Electron/src/__tests__/components/Sidebar/AddSongPlayListAccordion.test.tsx +++ b/Electron/src/__tests__/components/Sidebar/AddSongPlayListAccordion.test.tsx @@ -343,7 +343,12 @@ test('AddSongPlaylistAccordion rejects invalid song files', async () => { expect(component.queryByText('Canción Añadida')).not.toBeInTheDocument(); }); -// Mocking the fetch API globally +test('AddSongPlaylistAccordion disables the upload button while song is uploading', async () => { + const handleCloseMock = jest.fn(); + const refreshSidebarDataMock = jest.fn(); + const setIsCloseAllowed = jest.fn(); + + // Mocking the fetch API globally global.fetch = jest.fn((url: string) => { return new Promise((resolve) => { if (url === `${Global.backendBaseUrl}/genres/`) { @@ -365,3 +370,68 @@ test('AddSongPlaylistAccordion rejects invalid song files', async () => { } }); }) as jest.Mock; + + const component = await act(async () => { + return render( + + + , + ); + }); + + const accordionExpandSong = component.getByTestId( + 'accordion-expand-submit-song', + ); + + await act(async () => { + fireEvent.click(accordionExpandSong); + }); + + expect(component.getByText('Subir canción')).toBeInTheDocument(); + + const inputName = component.getByPlaceholderText('Nombre de la canción'); + const inputPhoto = component.getByPlaceholderText( + 'URL de la miniatura de la canción', + ); + const selectGenreOption = component.getByText('❗ Elige un género'); + const dropdown = component.getByTestId('select-genre'); + + const fileInputElement = component.getByTestId( + 'sidebar-file-input', + ) as HTMLInputElement; + + const validFile = new File([''], 'test.mp3', { type: 'audio/mpeg' }); + + await act(async () => { + fireEvent.change(inputName, { target: { value: 'Test Song' } }); + fireEvent.change(inputPhoto, { + target: { value: 'http://example.com/image.jpg' }, + }); + fireEvent.change(selectGenreOption, { target: { value: 'Rock' } }); + fireEvent.change(dropdown, { target: { value: 'Rock' } }); + fireEvent.change(fileInputElement, { target: { files: [validFile] } }); + }); + + const uploadSongButton = component.getByTestId( + 'sidebar-addsongplaylistaccordion-submit-song', + ); + + // Verify that the upload button is enabled + await waitFor(() => { + expect(uploadSongButton).toBeEnabled(); + }); + + await act(async () => { + fireEvent.click(uploadSongButton); + }); + + // Verify that the upload button is disabled while uploading + await waitFor(() => { + expect(uploadSongButton).toBeDisabled(); + }); +}); +