Skip to content
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ The two `AlbumDetailPage` rows represent the separate chunks for the album detai

**Search and filtering:** Users can search for albums by title or artist using Search or Enter. Results can be sorted by relevance, title, release year or average rating, and filtered by genre, decade, artist country or whether an album has reviews. Search terms, sorting and filters are preserved in the URL.

**Search history:** The ten most recent distinct searches are saved per simulated user across page reloads. Users can select a recent search from the search dropdown or clear their full saved history.

**Results and pagination:** Searches initially display up to 20 albums. Additional results load automatically as the user scrolls, up to 60 albums. Load more is also available before this limit; further pages require the button. The interface provides loading, error and no-results feedback.

**Album details and reviews:** Album pages display tracks, average ratings and reviews. Users can write, edit and delete their own reviews.
Expand Down
4 changes: 4 additions & 0 deletions client/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
margin-inline: auto;
}
.search-section {
/* Keep the hero's suggestions above the catalogue's isolated album cards. */
z-index: 1;
--bg: var(--header-bg);
--text: var(--header-text);
--accent: var(--header-accent);
Expand Down Expand Up @@ -147,6 +149,8 @@
text-underline-offset: 0.2em;
}
.catalogue-layout {
position: relative;
z-index: 0;
border-top: 1px solid var(--border);
padding-top: 1.25rem;
}
Expand Down
98 changes: 54 additions & 44 deletions client/src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { MockedProvider } from '@apollo/client/testing/react';
import type { MockLink } from '@apollo/client/testing';
import { GraphQLError } from 'graphql';
import App from './App.js';
import { searchHistoryMocks } from './test/searchHistoryMocks.js';
import userEvent from '@testing-library/user-event';
import { SEARCH_ALBUMS } from './api/searchAlbums.js';
import type {
Expand All @@ -13,6 +14,12 @@ import type {
SearchAlbumsVariables,
} from './api/searchAlbums.js';

function resultStatus() {
return within(
screen.getByRole('region', { name: /^(Search results|Browse albums)$/ }),
).getByRole('status');
}

const album: AlbumSummary = {
id: '1',
title: 'Abbey Road',
Expand Down Expand Up @@ -58,23 +65,26 @@ function renderApp(
) {
return render(
<MockedProvider
mocks={
mocks.some(
mocks={[
...searchHistoryMocks,
...(mocks.some(
(mock) =>
typeof mock.request.variables !== 'function' &&
mock.request.variables?.input.query === '',
)
? mocks
: [mockSearch('', []), ...mocks]
}
: [mockSearch('', []), ...mocks]),
]}
>
<App />
</MockedProvider>,
);
}

function submitSearch(query: string) {
fireEvent.change(screen.getByRole('searchbox'), { target: { value: query } });
fireEvent.change(screen.getByRole('combobox', { name: 'Search by album or artist' }), {
target: { value: query },
});
fireEvent.submit(screen.getByRole('search'));
}

Expand Down Expand Up @@ -104,9 +114,7 @@ describe('App search', () => {
target: { value: 'TITLE_DESC' },
});
await waitFor(() =>
expect(screen.getByRole('status')).toHaveTextContent(
'Showing 1 of 1 matching albums.',
),
expect(resultStatus()).toHaveTextContent('Showing 1 of 1 matching albums.'),
);
expect(window.location.search).toBe(
'?sort=TITLE_DESC&genres=1&decades=1960&reviewed=1',
Expand Down Expand Up @@ -205,11 +213,11 @@ describe('App search', () => {
).not.toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Filters (2)' })).toHaveFocus();
await waitFor(() =>
expect(screen.getByRole('status')).toHaveTextContent(
'Showing 1 of 1 matching albums.',
),
expect(resultStatus()).toHaveTextContent('Showing 1 of 1 matching albums.'),
);
expect(screen.getByRole('searchbox')).toHaveValue('Abbey');
expect(
screen.getByRole('combobox', { name: 'Search by album or artist' }),
).toHaveValue('Abbey');
expect(screen.getByRole('combobox', { name: 'Sort by' })).toHaveValue('TITLE_DESC');
});

Expand All @@ -221,9 +229,7 @@ describe('App search', () => {
(mock.result as { data: SearchAlbumsData }).data.searchAlbums.totalCount = 100500;
renderApp([mock]);
await waitFor(() =>
expect(screen.getByRole('status')).toHaveTextContent(
'Showing 20 of 100,500 matching albums.',
),
expect(resultStatus()).toHaveTextContent('Showing 20 of 100,500 matching albums.'),
);
});

Expand Down Expand Up @@ -262,9 +268,7 @@ describe('App search', () => {
expect(rock).toHaveFocus();
await user.keyboard(' ');
await waitFor(() =>
expect(screen.getByRole('status')).toHaveTextContent(
'Showing 1 of 1 matching albums.',
),
expect(resultStatus()).toHaveTextContent('Showing 1 of 1 matching albums.'),
);
expect(window.location.search).toBe('?q=Abbey&genres=1');
expect(screen.getByRole('checkbox', { name: 'Rock 1 albums' })).toBeChecked();
Expand All @@ -280,9 +284,7 @@ describe('App search', () => {
).not.toBeInTheDocument();
await user.keyboard(' ');
await waitFor(() =>
expect(screen.getByRole('status')).toHaveTextContent(
'Showing 2 of 2 matching albums.',
),
expect(resultStatus()).toHaveTextContent('Showing 2 of 2 matching albums.'),
);
expect(window.location.search).toBe('?q=Abbey');
expect(rock).toHaveFocus();
Expand All @@ -306,6 +308,7 @@ describe('App search', () => {
it.each(['Enter', 'Search button'])(
'retries a failed query through %s',
async (method) => {
const user = userEvent.setup();
const failed = {
...mockSearch('Abbey'),
result: { errors: [new GraphQLError('Search failed')] },
Expand All @@ -315,15 +318,14 @@ describe('App search', () => {
await screen.findByRole('alert');

if (method === 'Enter') {
const input = screen.getByRole<HTMLInputElement>('searchbox');
input.focus();
const defaultAllowed = fireEvent.keyDown(input, { key: 'Enter', code: 'Enter' });
// jsdom needs the browser's implicit submission to be simulated explicitly.
if (defaultAllowed) act(() => input.form?.requestSubmit());
fireEvent.keyUp(input, { key: 'Enter', code: 'Enter' });
const input = screen.getByRole('combobox', {
name: 'Search by album or artist',
});
await user.click(input);
await user.keyboard('{Enter}');
expect(input).toHaveFocus();
} else {
fireEvent.click(screen.getByRole('button', { name: 'Search' }));
await user.click(screen.getByRole('button', { name: 'Search' }));
}

expect(
Expand Down Expand Up @@ -386,7 +388,7 @@ describe('App search', () => {
expect(retry.parentElement).toBe(panel);
expect(retry).toHaveFocus();
await waitFor(() => expect(retry).toHaveAttribute('aria-disabled', 'true'));
expect(screen.getByRole('status')).toHaveTextContent('Retrying search');
expect(resultStatus()).toHaveTextContent('Retrying search');
expect(screen.getByRole('region', { name: 'Search results' })).toHaveAttribute(
'aria-busy',
'true',
Expand All @@ -403,7 +405,7 @@ describe('App search', () => {
it('loads albums initially with labelled search and accessible landmarks', async () => {
renderApp([mockSearch('')]);
expect(
screen.getByRole('searchbox', { name: 'Search by album or artist' }),
screen.getByRole('combobox', { name: 'Search by album or artist' }),
).toHaveValue('');
expect(screen.getByRole('main')).toHaveAttribute('id', 'main');
const hero = screen.getByRole('region', { name: 'Music worth holding onto.' });
Expand All @@ -417,7 +419,7 @@ describe('App search', () => {
expect(screen.getByRole('combobox', { name: 'Sort by' })).toHaveValue(
'RELEASE_YEAR_DESC',
);
expect(screen.getByRole('status')).toHaveTextContent('Loading albums');
expect(resultStatus()).toHaveTextContent('Loading albums');
expect(screen.getByRole('combobox', { name: 'Sort by' })).toBeInTheDocument();
expect(screen.queryByRole('checkbox')).not.toBeInTheDocument();
expect(screen.queryByRole('button', { name: 'Load more' })).not.toBeInTheDocument();
Expand All @@ -428,13 +430,21 @@ describe('App search', () => {
it('writes the query to the URL on submission and not while typing', async () => {
renderApp([mockSearch('Abbey')]);

fireEvent.change(screen.getByRole('searchbox'), { target: { value: 'Abb' } });
fireEvent.change(screen.getByRole('searchbox'), { target: { value: 'Abbey' } });
fireEvent.change(
screen.getByRole('combobox', { name: 'Search by album or artist' }),
{ target: { value: 'Abb' } },
);
fireEvent.change(
screen.getByRole('combobox', { name: 'Search by album or artist' }),
{ target: { value: 'Abbey' } },
);
await act(() => new Promise((resolve) => setTimeout(resolve, 400)));

// Typing alone touches neither the URL nor the network.
expect(window.location.search).toBe('');
expect(screen.getByRole('searchbox')).toHaveValue('Abbey');
expect(
screen.getByRole('combobox', { name: 'Search by album or artist' }),
).toHaveValue('Abbey');

fireEvent.submit(screen.getByRole('search'));
await screen.findByRole('article', { name: 'Abbey Road' });
Expand All @@ -461,15 +471,17 @@ describe('App search', () => {
it('treats a pasted whitespace-only query as no query at all', async () => {
window.history.replaceState({}, '', '/?q=%20%20');
renderApp([mockSearch('')]);
expect(screen.getByRole('searchbox')).toHaveValue('');
expect(
screen.getByRole('combobox', { name: 'Search by album or artist' }),
).toHaveValue('');
await screen.findByRole('article', { name: 'Abbey Road' });
expect(screen.getByRole('region', { name: 'Browse albums' })).toBeInTheDocument();
});

it('shows loading and then renders GraphQL result metadata after submission', async () => {
renderApp([mockSearch('Abbey')]);
submitSearch(' Abbey ');
expect(screen.getByRole('status')).toHaveTextContent('Searching albums');
expect(resultStatus()).toHaveTextContent('Searching albums');
expect(screen.getAllByText('Searching albums…')).toHaveLength(1);
expect(
screen.queryByRole('button', { name: 'Retry search' }),
Expand All @@ -482,9 +494,7 @@ describe('App search', () => {
expect(within(card).getByText('Rock')).toBeInTheDocument();
expect(within(card).getByText('1,240 reviews')).toBeInTheDocument();
expect(card).toHaveTextContent('Average rating: 4.8 out of 5');
expect(screen.getByRole('status')).toHaveTextContent(
'Showing 1 of 1 matching albums.',
);
expect(resultStatus()).toHaveTextContent('Showing 1 of 1 matching albums.');
});

it.each(['missing', ''])(
Expand All @@ -510,7 +520,9 @@ describe('App search', () => {
renderApp([empty, mockSearch('', [album], 'TITLE_DESC')]);
await screen.findByRole('heading', { name: 'No albums found' });
fireEvent.click(screen.getByRole('button', { name: 'Reset search' }));
expect(screen.getByRole('searchbox')).toHaveValue('');
expect(
screen.getByRole('combobox', { name: 'Search by album or artist' }),
).toHaveValue('');
expect(window.location.search).toBe('?sort=TITLE_DESC');
expect(screen.getByRole('button', { name: 'Filters' })).toBeInTheDocument();
expect(screen.getByRole('combobox', { name: 'Sort by' })).toHaveValue('TITLE_DESC');
Expand Down Expand Up @@ -545,7 +557,7 @@ describe('App search', () => {
submitSearch('Abbey');

expect(await screen.findByRole('alert')).toHaveTextContent('Could not load albums');
expect(screen.getByRole('status')).toHaveTextContent('Search failed.');
expect(resultStatus()).toHaveTextContent('Search failed.');
expect(screen.queryByText('No albums found')).not.toBeInTheDocument();
expect(screen.queryByRole('article')).not.toBeInTheDocument();

Expand All @@ -555,9 +567,7 @@ describe('App search', () => {
await screen.findByRole('article', { name: 'Abbey Road' }),
).toBeInTheDocument();
expect(screen.queryByRole('alert')).not.toBeInTheDocument();
expect(screen.getByRole('status')).toHaveTextContent(
'Showing 1 of 1 matching albums.',
);
expect(resultStatus()).toHaveTextContent('Showing 1 of 1 matching albums.');
});

it('re-queries the server when the sort changes and keeps it in the URL', async () => {
Expand Down
11 changes: 9 additions & 2 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import './App.css';
import './App.css';

import SearchForm from './components/SearchForm.js';
import ResultsLayout from './components/ResultsLayout.js';
import { lazy, Suspense } from 'react';

import { useAlbumSearch } from './hooks/useAlbumSearch.js';
import { useSearchHistory } from './hooks/useSearchHistory.js';

export default function App() {
const basePath = import.meta.env.BASE_URL.replace(/\/$/, '');
Expand Down Expand Up @@ -69,6 +70,12 @@ export default function App() {

function DiscoveryPage() {
const search = useAlbumSearch();
const history = useSearchHistory();

function submitSearch(query: string) {
search.search(query);
history.recordSearch(query);
}

return (
<main id="main" className="discovery-main" tabIndex={-1}>
Expand All @@ -82,7 +89,7 @@ function DiscoveryPage() {
<p className="tagline">
Search, rate and collect albums you’ll want to return to.
</p>
<SearchForm query={search.query} onSearch={search.search} />
<SearchForm query={search.query} onSearch={submitSearch} history={history} />
<p className="hero-credit">
Photo by{' '}
<a href="https://unsplash.com/photos/black-turntable-on-brown-wooden-table-TcSckNRL9J8">
Expand Down
Loading