Skip to content

Commit

Permalink
Merge branch 'master' into fix/AntonioMrtz#234
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonioMrtz authored Oct 5, 2024
2 parents 94d0c96 + e3b584f commit e7aaf5d
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ test('AddSongPlaylistAccordion submit playlist correct', async () => {
expect(component.getByText('Playlist Añadida')).toBeInTheDocument();
});

test('AddSongPlaylistAccordion submit song correct', async () => {
test('AddSongPlaylistAccordion submit valid song files', async () => {
const handleCloseMock = jest.fn();
const refreshSidebarDataMock = jest.fn();
const setIsCloseAllowed = jest.fn();
Expand Down Expand Up @@ -188,10 +188,15 @@ test('AddSongPlaylistAccordion submit song correct', async () => {
const dropdown = component.getByTestId('select-genre');

// Find the file input element by it's name or other suitable selector
const fileInputElement = component.getByTestId('sidebar-file-input');
const fileInputElement = component.getByTestId(
'sidebar-file-input',
) as HTMLInputElement;

// Create a sample file
const file = new File(['(⌐□_□)'], 'sample.mp3', { type: 'audio/mp3' });
// Create valid sample files
const mp3File = new File(['(⌐□_□)'], 'sample.mp3', { type: 'audio/mp3' });
const wavFile = new File(['(⌐□_□)'], 'sample.wav', { type: 'audio/wav' });
const flacFile = new File(['(⌐□_□)'], 'sample.flac', { type: 'audio/flac' });
const aacFile = new File(['(⌐□_□)'], 'sample.aac', { type: 'audio/aac' });

await act(async () => {
fireEvent.change(inputName, {
Expand All @@ -207,9 +212,29 @@ test('AddSongPlaylistAccordion submit song correct', async () => {
target: { value: 'Rock' },
});
fireEvent.change(dropdown, { target: { value: 'Rock' } });
fireEvent.change(fileInputElement, { target: { files: [file] } });
});

// Test valid file formats
await act(async () => {
fireEvent.change(fileInputElement, { target: { files: [mp3File] } });
});
expect(fileInputElement.files?.[0]).toBe(mp3File);

await act(async () => {
fireEvent.change(fileInputElement, { target: { files: [wavFile] } });
});
expect(fileInputElement.files?.[0]).toBe(wavFile);

await act(async () => {
fireEvent.change(fileInputElement, { target: { files: [flacFile] } });
});
expect(fileInputElement.files?.[0]).toBe(flacFile);

await act(async () => {
fireEvent.change(fileInputElement, { target: { files: [aacFile] } });
});
expect(fileInputElement.files?.[0]).toBe(aacFile);

const submitSongButton = component.getByTestId(
'sidebar-addsongplaylistaccordion-submit-song',
);
Expand All @@ -220,3 +245,100 @@ test('AddSongPlaylistAccordion submit song correct', async () => {

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

test('AddSongPlaylistAccordion rejects invalid song files', async () => {
const handleCloseMock = jest.fn();
const refreshSidebarDataMock = jest.fn();
const setIsCloseAllowed = jest.fn();

global.fetch = jest.fn(async (url: string) => {
if (url === `${Global.backendBaseUrl}/genres/`) {
return Promise.resolve({
json: () => JSON.stringify({ Rock: 'Rock', Pop: 'Pop' }),
status: 200,
ok: true,
headers: getMockHeaders(),
}).catch((error) => {
console.log(error);
});
}

return Promise.resolve({
json: () => {},
status: 201,
ok: true,
headers: getMockHeaders(),
}).catch((error) => {
console.log(error);
});
}) as jest.Mock;

const component = await act(() => {
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');

// Find the file input element by it's name or other suitable selector
const fileInputElement = component.getByTestId(
'sidebar-file-input',
) as HTMLInputElement;

// Create an invalid sample file
const invalidFile = new File(['a'], 'sample.txt', { type: 'text/plain' });

await act(async () => {
fireEvent.change(inputName, {
target: { value: 'testuser' },
});
fireEvent.change(inputPhoto, {
target: { value: 'testpassword' },
});
/* ! TODO For some reasion both fireEvent are necessary . Without one
the genre selected is empty causing the test to fail
*/
fireEvent.change(selectGenreOption, {
target: { value: 'Rock' },
});
fireEvent.change(dropdown, { target: { value: 'Rock' } });
});

// Test invalid file format
await act(async () => {
fireEvent.change(fileInputElement, { target: { files: [invalidFile] } });
});
expect(fileInputElement.value).toBe('');

const submitSongButton = component.getByTestId(
'sidebar-addsongplaylistaccordion-submit-song',
);

await act(async () => {
fireEvent.click(submitSongButton);
});

expect(component.queryByText('Canción Añadida')).not.toBeInTheDocument();
});
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,17 @@ export default function AddSongPlayListAccordion({

const handleChangeFile = (event: ChangeEvent<HTMLInputElement>) => {
if (event.target && event.target.files) {
setSongFile(event.target.files[0]);
const file = event.target.files[0];

if (file) {
if (file.type.startsWith('audio/')) {
setSongFile(file);
} else {
event.target.value = '';
}
} else {
event.target.value = '';
}
}
};

Expand Down Expand Up @@ -394,7 +404,7 @@ export default function AddSongPlayListAccordion({
id="file"
name="file"
onChange={handleChangeFile}
accept="audio/mp3"
accept="audio/*"
required
data-testid="sidebar-file-input"
/>
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ Check our [project goals and vision](docs/VISION.md).
<br />
<sub><b>ErikMisencik</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/perig99">
<img src="https://avatars.githubusercontent.com/u/104552862?v=4" width="90;" alt="ErikMisencik"/>
<br />
<sub><b>perig99</b></sub>
</a>
</td>
</tr>
</table>
122 changes: 94 additions & 28 deletions docs/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,100 @@ I would like to express my heartfelt gratitude to the following individuals for

Me gustaría expresar mi más sincero agradecimiento a las siguientes personas por sus invaluables contribuciones a Spotify Electron. Sus esfuerzos son inmensamente apreciados y tienen una gran importancia para mí y para el proyecto.

## Code Contributors

- [Ferbo12](https://github.com/contributor1)
- [Ercamarero](https://github.com/Ercamarero)
- [KarlosM2](https://github.com/KarlosM2)
- [mariete1223](https://github.com/mariete1223)
- [Javiks-P](https://github.com/Javiks-P)
- [raulZC ](https://github.com/raulZC)
- [TalhaBinNasir](https://github.com/TalhaBinNasir)
- [xiomaraR](https://github.com/xiomaraR)
- [aarshgupta24](https://github.com/aarshgupta24)
- [ErikMisencik](https://github.com/ErikMisencik)

## Bug Reporters

## Feature Requesters

## Translators

- [ariceron5](https://github.com/ariceron5)

## Documentation Contributors

- [ariceron5](https://github.com/ariceron5)

## Other Contributions

- [ariceron5](https://github.com/ariceron5)
## Contributors

<table>
<tr>
<td align="center">
<a href="https://github.com/Ferbo12">
<img src="https://avatars.githubusercontent.com/u/58307213?v=4" width="90;" alt="Ferbo12"/>
<br />
<sub><b>Ferbo12</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/Ercamarero">
<img src="https://avatars.githubusercontent.com/u/91611871?v=4" width="90;" alt="Ercamarero"/>
<br />
<sub><b>Ercamarero</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/KarlosM2">
<img src="https://avatars.githubusercontent.com/u/140536436?v=4" width="90;" alt="KarlosM2"/>
<br />
<sub><b>KarlosM2</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/mariete1223">
<img src="https://avatars.githubusercontent.com/u/71662461?v=4" width="90;" alt="mariete1223"/>
<br />
<sub><b>mariete1223</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/Javiks-P">
<img src="https://avatars.githubusercontent.com/u/72615168?v=4" width="90;" alt="Javiks-P"/>
<br />
<sub><b>Javiks-P</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/raulZC">
<img src="https://avatars.githubusercontent.com/u/78484498?v=4" width="90;" alt="raulZC"/>
<br />
<sub><b>raulZC</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/TalhaBinNasir">
<img src="https://avatars.githubusercontent.com/u/72547924?v=4" width="90;" alt="TalhaBinNasir"/>
<br />
<sub><b>TalhaBinNasir</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/ariceron5">
<img src="https://avatars.githubusercontent.com/u/103110478?v=4" width="90;" alt="ariceron5"/>
<br />
<sub><b>ariceron5</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/xiomaraR">
<img src="https://avatars.githubusercontent.com/u/81057963?v=4" width="90;" alt="xiomaraR"/>
<br />
<sub><b>xiomaraR</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/aarshgupta24">
<img src="https://avatars.githubusercontent.com/u/122194522?v=4" width="90;" alt="aarshgupta24"/>
<br />
<sub><b>aarshgupta24</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/ErikMisencik">
<img src="https://avatars.githubusercontent.com/u/108632576?v=4" width="90;" alt="ErikMisencik"/>
<br />
<sub><b>ErikMisencik</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/perig99">
<img src="https://avatars.githubusercontent.com/u/104552862?v=4" width="90;" alt="ErikMisencik"/>
<br />
<sub><b>perig99</b></sub>
</a>
</td>
</tr>
</table>

If you have contributed to this project and your name is not listed here, please accept our apologies and let us know so we can add you!

Expand Down

0 comments on commit e7aaf5d

Please sign in to comment.