|
8 | 8 | } from '@angular/core'; |
9 | 9 | import {ComponentFixture, TestBed} from '@angular/core/testing'; |
10 | 10 | import {By} from '@angular/platform-browser'; |
| 11 | +import {waitForMicrotasks} from '../private/testing/test-helpers'; |
11 | 12 | import {provideFakeDirectionality, runAccessibilityChecks} from '@angular/cdk/testing/private'; |
12 | 13 | import {Toolbar} from './toolbar'; |
13 | 14 | import {ToolbarWidgetGroup} from './toolbar-widget-group'; |
@@ -98,6 +99,33 @@ describe('Toolbar', () => { |
98 | 99 |
|
99 | 100 | afterEach(async () => await runAccessibilityChecks(fixture.nativeElement)); |
100 | 101 |
|
| 102 | + describe('dynamic updates', () => { |
| 103 | + it('should update widget order correctly after widgets are shuffled', async () => { |
| 104 | + TestBed.configureTestingModule({imports: [ShuffledToolbarExample]}); |
| 105 | + fixture = TestBed.createComponent( |
| 106 | + ShuffledToolbarExample, |
| 107 | + ) as unknown as ComponentFixture<ToolbarExample>; |
| 108 | + fixture.detectChanges(); |
| 109 | + const shuffledToolbarDebugEl = fixture.debugElement.query(By.directive(Toolbar)); |
| 110 | + const shuffledToolbarInstance = shuffledToolbarDebugEl.injector.get(Toolbar); |
| 111 | + |
| 112 | + const widgetsBefore = shuffledToolbarInstance._itemPatterns(); |
| 113 | + expect(widgetsBefore.length).toBe(3); |
| 114 | + expect(widgetsBefore[0].element()?.textContent?.trim()).toBe('item 0'); |
| 115 | + |
| 116 | + const items = (fixture.componentInstance as unknown as ShuffledToolbarExample).items(); |
| 117 | + const firstItem = items.shift()!; |
| 118 | + items.push(firstItem); |
| 119 | + (fixture.componentInstance as unknown as ShuffledToolbarExample).items.set([...items]); |
| 120 | + fixture.detectChanges(); |
| 121 | + await waitForMicrotasks(); |
| 122 | + |
| 123 | + const widgetsAfter = shuffledToolbarInstance._itemPatterns(); |
| 124 | + expect(widgetsAfter.length).toBe(3); |
| 125 | + expect(widgetsAfter[0].element()?.textContent?.trim()).toBe('item 1'); |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
101 | 129 | describe('Navigation', () => { |
102 | 130 | describe('with horizontal orientation', () => { |
103 | 131 | it('should navigate on click (horizontal)', () => { |
@@ -724,3 +752,18 @@ export class SimpleToolbarButton { |
724 | 752 | changeDetection: ChangeDetectionStrategy.Eager, |
725 | 753 | }) |
726 | 754 | class WrappedToolbarExample {} |
| 755 | + |
| 756 | +@Component({ |
| 757 | + template: ` |
| 758 | + <div ngToolbar> |
| 759 | + @for (item of items(); track item) { |
| 760 | + <button ngToolbarWidget [value]="item.value">{{item.value}}</button> |
| 761 | + } |
| 762 | + </div> |
| 763 | + `, |
| 764 | + imports: [Toolbar, ToolbarWidget], |
| 765 | + changeDetection: ChangeDetectionStrategy.Eager, |
| 766 | +}) |
| 767 | +class ShuffledToolbarExample { |
| 768 | + items = signal([{value: 'item 0'}, {value: 'item 1'}, {value: 'item 2'}]); |
| 769 | +} |
0 commit comments