fix(shell): publish the active view, so both navs follow the back path
The nav components learned where the user was from the `navigate` CustomEvent, which only the outbound path dispatches: `popstate` calls `handleNavigate()` directly. So a back-navigation left both of them highlighting the view just left — desktop included, at any width, on any back across two primary views. Opening a detail view was the same cause wearing a different symptom: `app-sidebar` guarded on its own item list and kept its highlight, `bottom-nav` did not and lit nothing. It cannot be fixed by re-dispatching `navigate` — `index.ts` is that event's document listener, so that is an infinite loop, and "please go to X" is not the statement being made. `activeViewStore` is the shell saying "the active view is now X", once per navigation, `popstate` included; both navs read it through a controller and hold no `activeView` of their own. A store rather than an event because a component that mounts *after* a navigation still has to know: `bottom-nav`'s drawer builds its `app-sidebar` on open, and that copy had heard nothing at all, so the drawer opened on Home from any page in the app. Closes #72
This commit is contained in:
@@ -3,15 +3,21 @@
|
||||
*
|
||||
* Three of these are about the thing that makes a second nav dangerous:
|
||||
* it has to agree with the first one. `bottom-nav` emits the same
|
||||
* bubbling, composed `navigate` event `app-sidebar` does and listens
|
||||
* for that event globally, so a navigation from anywhere — a card, a
|
||||
* detail view, the drawer's own sidebar — moves its highlight too. A
|
||||
* tab bar that only tracks its own clicks looks right until the moment
|
||||
* the user arrives somewhere by another route.
|
||||
* bubbling, composed `navigate` event `app-sidebar` does, and reads
|
||||
* which tab is lit from `activeViewStore` — the shell's one statement
|
||||
* of where the user is — so it follows a navigation from anywhere: a
|
||||
* card, a detail view, the drawer's own sidebar, or the back gesture.
|
||||
*
|
||||
* That last one is why the source is the store and not the `navigate`
|
||||
* event these tests used to dispatch. `popstate` dispatches no
|
||||
* `navigate` (index.ts calls `handleNavigate` directly), so a tab bar
|
||||
* listening for the event looked right until the user pressed back —
|
||||
* #72.
|
||||
*/
|
||||
import { describe, expect, it, beforeEach } from 'vitest';
|
||||
|
||||
import '@components/bottom-nav/bottom-nav';
|
||||
import { activeViewStore } from '@store/active-view-store';
|
||||
import type { BottomNav } from '@components/bottom-nav/bottom-nav';
|
||||
import { fixture, shadow, shadowAll, update } from '@test/support/render';
|
||||
import { resetHarness } from '@test/support/harness';
|
||||
@@ -21,6 +27,12 @@ type Nav = BottomNav;
|
||||
const tabs = (el: HTMLElement) =>
|
||||
shadowAll<HTMLButtonElement>(el, 'nav button');
|
||||
|
||||
/** The testids of whatever the bar says is the current page. */
|
||||
const current = (el: HTMLElement) =>
|
||||
tabs(el)
|
||||
.filter((b) => b.getAttribute('aria-current') === 'page')
|
||||
.map((b) => b.dataset.testid);
|
||||
|
||||
/** Resolve on one occurrence of an event, or reject loudly on time. */
|
||||
const once = (el: Element, name: string, timeoutMs = 2000) =>
|
||||
new Promise<void>((resolve, reject) => {
|
||||
@@ -70,36 +82,55 @@ describe('bottom-nav', () => {
|
||||
it('follows a navigation it did not send', async () => {
|
||||
const el = await fixture<Nav>('bottom-nav');
|
||||
|
||||
document.dispatchEvent(new CustomEvent('navigate', {
|
||||
detail: { view: 'tracks' },
|
||||
bubbles: true,
|
||||
composed: true,
|
||||
}));
|
||||
activeViewStore.setView('tracks', true);
|
||||
await update(el, {});
|
||||
|
||||
const current = tabs(el)
|
||||
.filter((b) => b.getAttribute('aria-current') === 'page')
|
||||
.map((b) => b.dataset.testid);
|
||||
|
||||
expect(current).toEqual(['tab-tracks']);
|
||||
expect(current(el)).toEqual(['tab-tracks']);
|
||||
});
|
||||
|
||||
it('marks exactly one tab current, and none for a view it has no tab for', async () => {
|
||||
const el = await fixture<Nav>('bottom-nav');
|
||||
|
||||
document.dispatchEvent(new CustomEvent('navigate', {
|
||||
detail: { view: 'settings' },
|
||||
bubbles: true,
|
||||
composed: true,
|
||||
}));
|
||||
activeViewStore.setView('settings', true);
|
||||
await update(el, {});
|
||||
|
||||
// Settings lives in the drawer, so nothing in the bar is current.
|
||||
// Leaving Home highlighted would be a tab bar lying about where
|
||||
// the user is.
|
||||
expect(
|
||||
tabs(el).filter((b) => b.getAttribute('aria-current') === 'page'),
|
||||
).toHaveLength(0);
|
||||
expect(current(el)).toEqual([]);
|
||||
});
|
||||
|
||||
it('keeps the parent tab lit while a detail view is open', async () => {
|
||||
const el = await fixture<Nav>('bottom-nav');
|
||||
|
||||
activeViewStore.setView('albums', true);
|
||||
// A detail view reports itself and is not primary, so it changes
|
||||
// nothing. This is the first half of #72: the bar used to take the
|
||||
// name, match it against no tab, and light nothing at all — while
|
||||
// `app-sidebar`, which guarded on its own item list, kept the
|
||||
// highlight. Neither was deliberate and the two disagreed.
|
||||
activeViewStore.setView('explore-album-details', false);
|
||||
await update(el, {});
|
||||
|
||||
expect(current(el)).toEqual(['tab-albums']);
|
||||
});
|
||||
|
||||
it('follows the back path, which dispatches no navigate event', async () => {
|
||||
const el = await fixture<Nav>('bottom-nav');
|
||||
|
||||
activeViewStore.setView('albums', true);
|
||||
activeViewStore.setView('tracks', true);
|
||||
await update(el, {});
|
||||
expect(current(el)).toEqual(['tab-tracks']);
|
||||
|
||||
// What `popstate` does: the shell replays the entry through
|
||||
// `handleNavigate` without dispatching `navigate`. A bar listening
|
||||
// for the event stayed on Tracks — the view just left, confidently
|
||||
// wrong rather than merely blank.
|
||||
activeViewStore.setView('albums', true);
|
||||
await update(el, {});
|
||||
|
||||
expect(current(el)).toEqual(['tab-albums']);
|
||||
});
|
||||
|
||||
it('closes the drawer when a navigation happens', async () => {
|
||||
|
||||
@@ -10,6 +10,7 @@ import '@components/sidebar/app-sidebar';
|
||||
import '@components/library-filter/library-filter';
|
||||
import '@components/library-status-indicator/library-status-indicator';
|
||||
import { Events } from '../../src/events';
|
||||
import { activeViewStore } from '@store/active-view-store';
|
||||
import { emit, stub, flush, calls, lastArgs } from '@test/support/harness';
|
||||
import {
|
||||
fixture,
|
||||
@@ -49,6 +50,8 @@ describe('<app-sidebar>', () => {
|
||||
});
|
||||
|
||||
it('marks exactly one item as the current page', async () => {
|
||||
activeViewStore.setView('home', true);
|
||||
|
||||
const el = await fixture('app-sidebar');
|
||||
|
||||
const current = shadowAll(el, 'li button').filter(
|
||||
@@ -72,17 +75,49 @@ describe('<app-sidebar>', () => {
|
||||
expect(seen).toEqual(['artists']);
|
||||
});
|
||||
|
||||
it('moves aria-current to the clicked destination', async () => {
|
||||
it('moves aria-current with the shell, not with the click', async () => {
|
||||
activeViewStore.setView('home', true);
|
||||
|
||||
const el = await fixture('app-sidebar');
|
||||
|
||||
shadow<HTMLElement>(el, '[data-testid="nav-genres"]')?.click();
|
||||
await el.updateComplete;
|
||||
|
||||
// The click asks; it does not answer. The sidebar used to move its
|
||||
// own highlight optimistically, which is the second opinion #72
|
||||
// removed -- one component deciding where the user is, while the
|
||||
// shell decided separately and `bottom-nav` decided a third way.
|
||||
expect(
|
||||
shadow(el, '[data-testid="nav-genres"]')?.getAttribute('aria-current'),
|
||||
).toBe('false');
|
||||
|
||||
// What the shell does with that event, in one line.
|
||||
activeViewStore.setView('genres', true);
|
||||
await update(el, {});
|
||||
|
||||
expect(
|
||||
shadow(el, '[data-testid="nav-genres"]')?.getAttribute('aria-current'),
|
||||
).toBe('page');
|
||||
});
|
||||
|
||||
it('follows the back path, which dispatches no navigate event', async () => {
|
||||
activeViewStore.setView('albums', true);
|
||||
|
||||
const el = await fixture('app-sidebar');
|
||||
|
||||
// `popstate` replays an entry through `handleNavigate` directly, so
|
||||
// there is no `navigate` event to hear -- which is why the sidebar
|
||||
// stayed on the view the user had just left (#72).
|
||||
activeViewStore.setView('tracks', true);
|
||||
await update(el, {});
|
||||
|
||||
expect(
|
||||
shadowAll(el, 'li button')
|
||||
.filter((item) => item.getAttribute('aria-current') === 'page')
|
||||
.map((item) => item.getAttribute('data-testid')),
|
||||
).toEqual(['nav-tracks']);
|
||||
});
|
||||
|
||||
it('looks the way it did last time', async () => {
|
||||
const el = await fixture('app-sidebar');
|
||||
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
/**
|
||||
* The three small stores behind view chrome: the global search term,
|
||||
* the track list's column set, and the explore cache that keeps detail
|
||||
* pages from re-fetching what a search already returned.
|
||||
* The small stores behind view chrome: the global search term, the
|
||||
* active view both navs highlight, the track list's column set, and
|
||||
* the explore cache that keeps detail pages from re-fetching what a
|
||||
* search already returned.
|
||||
*/
|
||||
import { describe, expect, it, beforeEach } from 'vitest';
|
||||
|
||||
import { searchStore } from '@store/search-store';
|
||||
import { activeViewStore } from '@store/active-view-store';
|
||||
import { trackListStore } from '@store/tracklist-store';
|
||||
import { exploreCache, ARTIST_IMAGE_CACHE_LIMIT } from '@store/explore-cache';
|
||||
import { Events } from '../../src/events';
|
||||
@@ -80,6 +82,57 @@ describe('search store', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('active view store', () => {
|
||||
beforeEach(() => {
|
||||
activeViewStore.setView('home', true);
|
||||
});
|
||||
|
||||
it('holds the primary view the shell navigated to', () => {
|
||||
activeViewStore.setView('albums', true);
|
||||
|
||||
expect(activeViewStore.get()).toBe('albums');
|
||||
expect(activeViewStore.isActive('albums')).toBe(true);
|
||||
expect(activeViewStore.isActive('tracks')).toBe(false);
|
||||
});
|
||||
|
||||
it('leaves the primary view lit while a detail view is open', () => {
|
||||
activeViewStore.setView('albums', true);
|
||||
activeViewStore.setView('explore-album-details', false);
|
||||
|
||||
// #72's third finding, made deliberate: a detail view is not a
|
||||
// destination in either nav, and the tab it was opened from is
|
||||
// where the user still is. `app-sidebar` did this by accident (it
|
||||
// guarded on its own item list) and `bottom-nav` did not do it at
|
||||
// all, which is why one looked right and the other looked broken.
|
||||
expect(activeViewStore.get()).toBe('albums');
|
||||
});
|
||||
|
||||
it('does not notify when the view is unchanged', () => {
|
||||
let notifications = 0;
|
||||
const off = activeViewStore.subscribe(() => {
|
||||
notifications += 1;
|
||||
});
|
||||
|
||||
activeViewStore.setView('albums', true);
|
||||
activeViewStore.setView('albums', true);
|
||||
activeViewStore.setView('explore-album-details', false);
|
||||
off();
|
||||
|
||||
expect(notifications).toBe(1);
|
||||
});
|
||||
|
||||
it('lights nothing for a view with no name', () => {
|
||||
// The store starts empty rather than defaulting to a view, because
|
||||
// a written-down default is right only while `GetDefaultPage()`
|
||||
// agrees with it. That is only safe if the empty value matches
|
||||
// nothing: `isActive` compares strings, and a component asking
|
||||
// about an id it does not have must not light up.
|
||||
activeViewStore.setView('', true);
|
||||
|
||||
expect(activeViewStore.isActive('')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('track list store', () => {
|
||||
it('starts from the default column set', () => {
|
||||
expect(trackListStore.getState().columnIds.length).toBeGreaterThan(0);
|
||||
|
||||
Reference in New Issue
Block a user