refactor(ui): add shelf menu remove-all option (#2309)

This commit is contained in:
Alex
2026-09-21 13:01:56 +01:00
committed by GitHub
parent 04467d7d68
commit 2f8cc656a8
3 changed files with 33 additions and 20 deletions
@@ -37,7 +37,7 @@ describe('ShelfMembershipMenuComponent', () => {
} }
function menuElement(): HTMLElement { function menuElement(): HTMLElement {
return document.querySelector('app-menu[aria-label="Add to shelf"]') as HTMLElement; return document.querySelector('app-menu[aria-label="Add to Shelf"]') as HTMLElement;
} }
function rowLabels(): string[] { function rowLabels(): string[] {
@@ -58,8 +58,9 @@ describe('ShelfMembershipMenuComponent', () => {
it('renders translated menu labels', async () => { it('renders translated menu labels', async () => {
await render(shelves(3)); await render(shelves(3));
expect(menuElement().getAttribute('aria-label')).toBe('Add to shelf'); expect(menuElement().getAttribute('aria-label')).toBe('Add to Shelf');
expect(menuElement().querySelector('app-menu-item')?.textContent?.trim()).toBe('New shelf…'); expect(Array.from(menuElement().querySelectorAll('app-menu-item'))
.map(item => item.textContent?.trim())).toEqual(['New Shelf', 'Remove from Your Shelves']);
}); });
it('hides the filter input for a short, scannable list', async () => { it('hides the filter input for a short, scannable list', async () => {
@@ -105,7 +106,7 @@ describe('ShelfMembershipMenuComponent', () => {
await render(shelves(12)); await render(shelves(12));
const input = menuElement().querySelector('input') as HTMLInputElement; const input = menuElement().querySelector('input') as HTMLInputElement;
const rows = menuElement().querySelectorAll('app-menu-checkbox'); const rows = menuElement().querySelectorAll('app-menu-checkbox');
const newShelf = menuElement().querySelector('app-menu-item') as HTMLElement; const removeAll = menuElement().querySelectorAll('app-menu-item')[1] as HTMLElement;
input.focus(); input.focus();
input.dispatchEvent(new KeyboardEvent('keydown', {key: 'ArrowDown', bubbles: true, cancelable: true})); input.dispatchEvent(new KeyboardEvent('keydown', {key: 'ArrowDown', bubbles: true, cancelable: true}));
@@ -113,7 +114,7 @@ describe('ShelfMembershipMenuComponent', () => {
input.focus(); input.focus();
input.dispatchEvent(new KeyboardEvent('keydown', {key: 'ArrowUp', bubbles: true, cancelable: true})); input.dispatchEvent(new KeyboardEvent('keydown', {key: 'ArrowUp', bubbles: true, cancelable: true}));
expect(document.activeElement).toBe(newShelf); expect(document.activeElement).toBe(removeAll);
}); });
it('emits toggleShelf with the shelf id and next state', async () => { it('emits toggleShelf with the shelf id and next state', async () => {
@@ -157,22 +158,28 @@ describe('ShelfMembershipMenuComponent', () => {
expect(menuElement().querySelector('input')).toBeNull(); expect(menuElement().querySelector('input')).toBeNull();
}); });
it('emits createShelf when the New shelf item is chosen', async () => { it('emits both trailing shelf commands', async () => {
await render(shelves(3)); await render(shelves(3));
const spy = vi.fn(); const create = vi.fn();
fixture.componentInstance.createShelf.subscribe(spy); const removeAll = vi.fn();
const newShelf = menuElement().querySelector('app-menu-item') as HTMLElement; fixture.componentInstance.createShelf.subscribe(create);
newShelf.dispatchEvent(new MouseEvent('click', {bubbles: true, cancelable: true})); fixture.componentInstance.removeFromAllShelves.subscribe(removeAll);
expect(spy).toHaveBeenCalledTimes(1); const items = menuElement().querySelectorAll('app-menu-item');
items[0].dispatchEvent(new MouseEvent('click', {bubbles: true, cancelable: true}));
items[1].dispatchEvent(new MouseEvent('click', {bubbles: true, cancelable: true}));
expect(create).toHaveBeenCalledOnce();
expect(removeAll).toHaveBeenCalledOnce();
}); });
it('renders the filter, rows and New shelf in logical order', async () => { it('renders the filter, rows and commands in logical order', async () => {
await render(shelves(12)); await render(shelves(12));
const order = Array.from(menuElement().querySelectorAll('input, app-menu-checkbox, app-menu-item')).map(el => { const order = Array.from(menuElement().querySelectorAll('input, app-menu-checkbox, app-menu-item'))
const tag = el.tagName.toLowerCase(); .map(el => el.tagName.toLowerCase() === 'input'
return tag === 'input' ? 'filter' : tag === 'app-menu-item' ? 'newShelf' : 'row'; ? 'filter'
}); : el.tagName.toLowerCase() === 'app-menu-checkbox'
? 'row'
: el.textContent?.trim());
expect(order[0]).toBe('filter'); expect(order[0]).toBe('filter');
expect(order.at(-1)).toBe('newShelf'); expect(order.slice(-2)).toEqual(['New Shelf', 'Remove from Your Shelves']);
}); });
}); });
@@ -57,7 +57,7 @@ const FILTER_THRESHOLD = 8;
</div> </div>
<app-menu-separator /> <app-menu-separator />
} }
<div class="max-h-80 overflow-y-auto overscroll-contain"> <div class="max-h-80 overflow-x-hidden overflow-y-auto overscroll-contain">
@for (shelf of filteredShelves(); track shelf.id) { @for (shelf of filteredShelves(); track shelf.id) {
<app-menu-checkbox <app-menu-checkbox
[value]="shelf.id" [value]="shelf.id"
@@ -74,6 +74,10 @@ const FILTER_THRESHOLD = 8;
<app-menu-separator /> <app-menu-separator />
} }
<app-menu-item value="create-shelf" (selected)="createShelf.emit()">{{ 'shared.ui.shelfMenu.newShelf' | transloco }}</app-menu-item> <app-menu-item value="create-shelf" (selected)="createShelf.emit()">{{ 'shared.ui.shelfMenu.newShelf' | transloco }}</app-menu-item>
<app-menu-separator />
<app-menu-item value="remove-from-all-shelves" (selected)="removeFromAllShelves.emit()">
{{ 'shared.ui.shelfMenu.removeFromAllShelves' | transloco }}
</app-menu-item>
</ng-template> </ng-template>
</app-menu> </app-menu>
`, `,
@@ -83,6 +87,7 @@ export class ShelfMembershipMenuComponent {
readonly toggleShelf = output<{shelfId: number; checked: boolean}>(); readonly toggleShelf = output<{shelfId: number; checked: boolean}>();
readonly createShelf = output<void>(); readonly createShelf = output<void>();
readonly removeFromAllShelves = output<void>();
readonly menu = viewChild.required(AppMenuComponent); readonly menu = viewChild.required(AppMenuComponent);
readonly ariaMenu = viewChild.required(AppMenuComponent, {read: Menu}); readonly ariaMenu = viewChild.required(AppMenuComponent, {read: Menu});
+3 -2
View File
@@ -1815,8 +1815,9 @@
"selectedCount": "{{ count }} selected" "selectedCount": "{{ count }} selected"
}, },
"shelfMenu": { "shelfMenu": {
"addToShelf": "Add to shelf", "addToShelf": "Add to Shelf",
"newShelf": "New shelf…" "newShelf": "New Shelf",
"removeFromAllShelves": "Remove from Your Shelves"
}, },
"spinner": { "spinner": {
"loading": "Loading" "loading": "Loading"