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

Testing #219

Merged
merged 23 commits into from
May 16, 2024
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
13 changes: 13 additions & 0 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ jobs:
run: yarn ci
continue-on-error: false

test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Install vitest
run: yarn add vitest

- name: Run tests
run: yarn test

analyze:
name: Analyze
runs-on: ubuntu-latest
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
"editor.codeActionsOnSave": {
"quickfix.biome": "explicit",
"source.organizeImports.biome": "explicit"
}
},
"jestrunner.codeLens": []
}
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"lint": "yarn biome check src/",
"lint:fix": "yarn biome check src/ --apply",
"format": "yarn biome format src/ --write",
"ci": "yarn biome ci src/"
"ci": "yarn biome ci src/",
"test": "vitest run --coverage"
},
"browserslist": {
"production": [
Expand All @@ -47,10 +48,13 @@
"@types/react-redux": "^7.1.26",
"@types/react-router-dom": "^5.3.3",
"@vitejs/plugin-react": "^4.2.1",
"@vitest/coverage-v8": "^1.6.0",
"jsdom": "^22.1.0",
"vite": "^5.2.9",
"vite-plugin-checker": "^0.6.4",
"vite-plugin-svgr": "^4.2.0",
"vite-tsconfig-paths": "^4.3.2"
"vite-tsconfig-paths": "^4.3.2",
"vitest": "^1.6.0"
},
"repository": "https://github.com/bastiendmt/spotify-like-web.git",
"author": "Bastien DUMONT <[email protected]>",
Expand Down
11 changes: 10 additions & 1 deletion src/components/Loader/Loader.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import styles from './Loader.module.scss';

const Loader = () => <div className={styles.Loader} />;
const Loader = () => (
<div
className={styles.Loader}
aria-busy="true"
role="progressbar"
aria-valuenow={0}
aria-valuemin={0}
aria-valuemax={100}
/>
);

export default Loader;
16 changes: 16 additions & 0 deletions src/components/NotFound/NotFound.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { render, screen } from '@testing-library/react';
import { BrowserRouter } from 'react-router-dom';
import { describe, expect, test } from 'vitest';
import NotFound from './NotFound';

describe('NotFound', () => {
test('should render NotFound', async () => {
render(
<BrowserRouter>
<NotFound />
</BrowserRouter>,
);

expect((await screen.findAllByRole('link')).length).toBeTruthy();
});
});
34 changes: 34 additions & 0 deletions src/components/Player/Player.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { render, screen } from '@testing-library/react';
import { Provider } from 'react-redux';
import { describe, expect, test } from 'vitest';
import { mockTrack } from '../../../tests/mockData';
import { createMockStore } from '../../store/store';
import Player from './Player';

const playerStore = createMockStore({
currentSong: { song: null, playing: false },
});

const playerStoreWithSong = createMockStore({
currentSong: { song: mockTrack, playing: true },
});

describe('Player', () => {
test('should render nothing', async () => {
render(
<Provider store={playerStore}>
<Player />
</Provider>,
);
expect(screen.queryByTestId('audioEml')).toBeFalsy();
});

test('should render audio element', async () => {
render(
<Provider store={playerStoreWithSong}>
<Player />
</Provider>,
);
expect(screen.getByTestId('audioEml')).toBeTruthy();
});
});
22 changes: 15 additions & 7 deletions src/components/Player/Player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,22 @@ const Player = () => {
// If the songs changes, plays it
// biome-ignore lint/correctness/useExhaustiveDependencies: <explanation>
useEffect(() => {
audioEml.current?.play().catch(() => {
console.log('Unable to play');
});
try {
audioEml.current?.play();
} catch (error) {
console.error('Unable to play', error);
}
resetTime();
}, [song]);

// Handles play / pause
useEffect(() => {
if (playing) {
audioEml.current?.play().catch(() => {
console.log('Unable to play');
});
try {
audioEml.current?.play();
} catch (error) {
console.error('Unable to play', error);
}
toggleStopwatch(true);
} else {
audioEml.current?.pause();
Expand Down Expand Up @@ -86,7 +90,11 @@ const Player = () => {
</div>

<div className={styles.Controls}>
<audio ref={audioEml} src={song.track.preview_url}>
<audio
ref={audioEml}
src={song.track.preview_url ?? ''}
data-testid="audioEml"
>
<track kind="captions" />
</audio>
<button type="button" onClick={() => dispatch(playPause())}>
Expand Down
18 changes: 18 additions & 0 deletions src/components/SideBar/SideBar.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { render, screen } from '@testing-library/react';
import { BrowserRouter } from 'react-router-dom';
import { describe, expect, test } from 'vitest';
import { mockPlaylists } from '../../../tests/mockData';
import SideBar from './SideBar';

describe('SideBar', () => {
test('should render playlists', async () => {
render(
<BrowserRouter>
<SideBar playlists={mockPlaylists} />
</BrowserRouter>,
);

expect((await screen.findByRole('heading')).textContent).toBe('Playlists');
expect((await screen.findAllByRole('link')).length).toBeTruthy();
});
});
14 changes: 8 additions & 6 deletions src/components/SideBar/SideBar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Link } from 'react-router-dom';
import Logo from '../../assets/logo.svg?react';
import type { PlaylistType } from '../../types/playlist.interface';
import type { PlaylistsType } from '../../types/playlists.interface';
import type {
PlaylistTrackDetails,
PlaylistsType,
} from '../../types/playlists.interface';
import styles from './SideBar.module.scss';

const SideBar = ({ playlists }: { playlists: PlaylistsType }) => (
Expand All @@ -16,15 +18,15 @@ const SideBar = ({ playlists }: { playlists: PlaylistsType }) => (
</Link>
<h1 className={styles.Title}>Playlists</h1>
<hr className={styles.Separator} />
<div className={styles.List}>
{playlists.items?.map((item: PlaylistType) => (
<nav className={styles.List}>
{playlists.items?.map((item) => (
<ListItem playlist={item} key={item.id} />
))}
</div>
</nav>
</div>
);

const ListItem = ({ playlist }: { playlist: PlaylistType }) => (
const ListItem = ({ playlist }: { playlist: PlaylistTrackDetails }) => (
<Link to={`/playlist/${playlist.id}`} className={styles.ListItem}>
<span className={styles.ItemTitle}>{playlist.name}</span>
</Link>
Expand Down
54 changes: 54 additions & 0 deletions src/pages/PlaylistDetail/PlaylistDetail.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { cleanup, render, screen } from '@testing-library/react';
import { Provider } from 'react-redux';
import { afterEach, describe, expect, test } from 'vitest';
import { mockPlaylistDetails } from '../../../tests/mockData';
import { createMockStore } from '../../store/store';
import PlaylistDetail from './PlaylistDetail';

const store = createMockStore({
playlistDetail: {
playlist: mockPlaylistDetails,
loading: false,
error: '',
},
});

const loadingStore = createMockStore({
playlistDetail: {
playlist: null,
loading: true,
error: '',
},
});

describe('Playlist details', () => {
afterEach(cleanup);

test('should render loader', async () => {
render(
<Provider store={loadingStore}>
<PlaylistDetail />
</Provider>,
);
expect(screen.getByRole('progressbar', { busy: true })).toBeTruthy();
});

test('should render playlist details', async () => {
render(
<Provider store={store}>
<PlaylistDetail />
</Provider>,
);
expect(screen.findByText('Hits du Moment')).toBeTruthy();
});

// test('should have a background color', async () => {
// render(
// <Provider store={store}>
// <PlaylistDetail />
// </Provider>,
// );

// expect(screen.getByTestId('Background').style.backgroundColor).toBeTruthy();
// });
});
12 changes: 7 additions & 5 deletions src/pages/PlaylistDetail/PlaylistDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import Time from '../../assets/time.svg?react';
import Loader from '../../components/Loader/Loader';
import NotFound from '../../components/NotFound/NotFound';
import { useAppDispatch, useAppSelector } from '../../store/hooks';
import {
loadSong,
selectCurrentSong,
} from '../../store/reducers/currentSong.slice';
import { selectCurrentSong } from '../../store/reducers/currentSong.slice';
import { loadSong } from '../../store/reducers/currentSong.slice';
import {
fetchPlaylistById,
playlistDetailsSelector,
Expand Down Expand Up @@ -80,7 +78,11 @@ const PlaylistDetail = () => {
{playlist != null && (
<div className={styles.PlaylistDetail}>
<div className={styles.Cover}>
<div className={styles.Background} id="Background" />
<div
className={styles.Background}
id="Background"
data-testid="Background"
/>
<div className={styles.Gradient} />
<img
src={playlist.images[0].url}
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Playlists/PlaylistItem/PlaylistItem.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Link } from 'react-router-dom';
import Play from '../../../assets/play.svg?react';
import type { PlaylistType } from '../../../types/playlist.interface';
import type { PlaylistTrackDetails } from '../../../types/playlists.interface';
import styles from './PlaylistItem.module.scss';

const PlaylistItem = ({ playlist }: { playlist: PlaylistType }) => (
const PlaylistItem = ({ playlist }: { playlist: PlaylistTrackDetails }) => (
<Link to={`/playlist/${playlist.id}`} className={styles.LinkPlaylist}>
<div className={styles.imgContainer}>
<img src={playlist.images[0].url} alt="Tokyo" />
Expand Down
20 changes: 20 additions & 0 deletions src/pages/Playlists/Playlists.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { render, screen } from '@testing-library/react';
import { BrowserRouter } from 'react-router-dom';
import { describe, expect, test } from 'vitest';
import { mockPlaylists } from '../../../tests/mockData';
import Playlists from './Playlists';

describe('Playlists', () => {
test('should render playlists', async () => {
render(
<BrowserRouter>
<Playlists playlists={mockPlaylists} message="mock message" />
</BrowserRouter>,
);

expect((await screen.findByRole('heading')).textContent).toBe(
'Playlists - mock message',
);
expect(screen.getByText('Hits du Moment')).toBeTruthy();
});
});
3 changes: 1 addition & 2 deletions src/pages/Playlists/Playlists.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { PlaylistType } from '../../types/playlist.interface';
import type { PlaylistsType } from '../../types/playlists.interface';
import PlaylistItem from './PlaylistItem/PlaylistItem';
import styles from './Playlists.module.scss';
Expand All @@ -13,7 +12,7 @@ const Playlists = ({
<div className={styles.Playlists}>
<h1 className={styles.Title}>Playlists - {message}</h1>
<div className={styles.Container}>
{playlists.items.map((item: PlaylistType) => (
{playlists.items.map((item) => (
<PlaylistItem key={item.id} playlist={item} />
))}
</div>
Expand Down
6 changes: 3 additions & 3 deletions src/store/reducers/currentSong.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ interface CurrentSongState {
song: Track | null;
}

const initialState: CurrentSongState = {
playing: true,
export const initialCurrentSongState: CurrentSongState = {
playing: false,
song: null,
};

const currentSongSlice = createSlice({
name: 'currentSong',
initialState,
initialState: initialCurrentSongState,
reducers: {
loadSong: (state, action: PayloadAction<Track>) => {
state.playing = true;
Expand Down
28 changes: 28 additions & 0 deletions src/store/reducers/currentSong.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, expect, it } from 'vitest';
import { mockTrack } from '../../../tests/mockData';
import currentSongSlice, {
initialCurrentSongState,
loadSong,
playPause,
} from './currentSong.slice';

describe('currentSong', () => {
const initialState = currentSongSlice.reducer(undefined, { type: 'unknown' });

it('should return the initial state', () => {
expect(initialState).toEqual(initialCurrentSongState);
});

it('should handle loadSong', () => {
const newState = currentSongSlice.reducer(undefined, loadSong(mockTrack));
expect(newState.playing).toBe(true);
expect(newState.song).toEqual(mockTrack);
});
it('should handle playPause', () => {
const newState = currentSongSlice.reducer(
{ playing: true, song: null },
playPause(),
);
expect(newState.playing).toBe(false);
});
});
Loading
Loading