Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#133: Disable upload song button while processing request #250

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import '@testing-library/jest-dom';
import { act, fireEvent, render } from '@testing-library/react';
import { act, fireEvent, render, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import { BrowserRouter } from 'react-router-dom';
import Global from 'global/global';
Expand Down Expand Up @@ -342,3 +342,95 @@ test('AddSongPlaylistAccordion rejects invalid song files', async () => {

expect(component.queryByText('Canción Añadida')).not.toBeInTheDocument();
});

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/`) {
resolve({
json: async () => ({ Rock: 'Rock', Pop: 'Pop' }),
status: 200,
ok: true,
headers: getMockHeaders(),
});
} else {
setTimeout(() => {
resolve({
json: async () => ({}),
status: 201,
ok: true,
headers: getMockHeaders(),
});
}, 1000);
}
});
}) as jest.Mock;

const component = await act(async () => {
return render(
<BrowserRouter>
<AddSongPlayListAccordion
handleClose={handleCloseMock}
refreshSidebarData={refreshSidebarDataMock}
setIsCloseAllowed={setIsCloseAllowed}
/>
</BrowserRouter>,
);
});

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();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ export default function AddSongPlayListAccordion({
onClick={handleSubmitSong}
className={`btn btn-lg ${styles.btnSend} d-flex flex-row justify-content-center`}
data-testid="sidebar-addsongplaylistaccordion-submit-song"
disabled={loadingUploadSong}
>
Subir {loadingUploadSong && <LoadingCircleSmall />}
</button>
Expand Down
Loading