|
| 1 | +import { FacetMultiselectComponent } from './facet-multiselect.component'; |
| 2 | +import { IDocumentFacetBucket } from '../../models/docs-api.model'; |
| 3 | + |
| 4 | +/** |
| 5 | + * 🛑 Regression guard for the Documents-hub main-thread wedge. |
| 6 | + * |
| 7 | + * The filter bar binds `[buckets]` to getters and `[selected]="value?.x || []"`, both of which |
| 8 | + * yield a NEW array identity on every change-detection cycle. `FacetMultiselectComponent` used to |
| 9 | + * rebuild `options` (new array + new objects) on every resulting `ngOnChanges`, and its |
| 10 | + * `*ngFor` had no `trackBy` — so `<nb-select>` recreated every `<nb-option>` each cycle, whose |
| 11 | + * `ngAfterViewInit` + the content-query change retriggered change detection. That self-sustaining |
| 12 | + * loop pegged the main thread the moment the hub rendered (silent, zero HTTP). |
| 13 | + * |
| 14 | + * The load-bearing invariant is: **when the bucket/selection CONTENT is unchanged, `options` keeps |
| 15 | + * the same array reference across `ngOnChanges` calls** — even when the inputs are new arrays. |
| 16 | + */ |
| 17 | +describe('FacetMultiselectComponent — option reference stability', () => { |
| 18 | + const bucket = (value: string, count?: number, label?: string): IDocumentFacetBucket => |
| 19 | + ({ value, count, label } as IDocumentFacetBucket); |
| 20 | + |
| 21 | + let component: FacetMultiselectComponent; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + component = new FacetMultiselectComponent(); |
| 25 | + }); |
| 26 | + |
| 27 | + /** Feed inputs the way the template does: a brand-new array identity every cycle. */ |
| 28 | + const applyInputs = (values: string[], selected: string[] = []): void => { |
| 29 | + component.buckets = values.map((v) => bucket(v, 1)); |
| 30 | + component.selected = [...selected]; |
| 31 | + component.ngOnChanges(); |
| 32 | + }; |
| 33 | + |
| 34 | + it('keeps the SAME options reference when the content has not changed across cycles', () => { |
| 35 | + applyInputs(['FOLDER', 'PAGE', 'FILE']); |
| 36 | + const first = component.options; |
| 37 | + |
| 38 | + // Three more cycles with fresh input identities but identical content. |
| 39 | + applyInputs(['FOLDER', 'PAGE', 'FILE']); |
| 40 | + applyInputs(['FOLDER', 'PAGE', 'FILE']); |
| 41 | + applyInputs(['FOLDER', 'PAGE', 'FILE']); |
| 42 | + |
| 43 | + expect(component.options).toBe(first); |
| 44 | + }); |
| 45 | + |
| 46 | + it('rebuilds options (new reference) only when the content actually changes', () => { |
| 47 | + applyInputs(['FOLDER', 'PAGE']); |
| 48 | + const first = component.options; |
| 49 | + |
| 50 | + applyInputs(['FOLDER', 'PAGE', 'FILE']); // a real change |
| 51 | + const second = component.options; |
| 52 | + |
| 53 | + expect(second).not.toBe(first); |
| 54 | + expect(second.map((o) => o.value)).toEqual(['FOLDER', 'PAGE', 'FILE']); |
| 55 | + }); |
| 56 | + |
| 57 | + it('treats a changed count as a content change (facet counts arriving from the API)', () => { |
| 58 | + component.buckets = [bucket('READY', undefined)]; |
| 59 | + component.selected = []; |
| 60 | + component.ngOnChanges(); |
| 61 | + const before = component.options; |
| 62 | + |
| 63 | + component.buckets = [bucket('READY', 12)]; |
| 64 | + component.ngOnChanges(); |
| 65 | + |
| 66 | + expect(component.options).not.toBe(before); |
| 67 | + expect(component.options[0].count).toBe(12); |
| 68 | + }); |
| 69 | + |
| 70 | + it('appends stale selected values as options and stays stable while they persist', () => { |
| 71 | + applyInputs(['FOLDER'], ['ARCHIVED_KIND_NOT_IN_BUCKETS']); |
| 72 | + const first = component.options; |
| 73 | + |
| 74 | + expect(first.map((o) => o.value)).toEqual(['FOLDER', 'ARCHIVED_KIND_NOT_IN_BUCKETS']); |
| 75 | + |
| 76 | + applyInputs(['FOLDER'], ['ARCHIVED_KIND_NOT_IN_BUCKETS']); |
| 77 | + expect(component.options).toBe(first); |
| 78 | + }); |
| 79 | + |
| 80 | + it('exposes a value-based trackBy so unchanged options are never recreated', () => { |
| 81 | + expect(component.trackByValue(0, { value: 'FILE' })).toBe('FILE'); |
| 82 | + }); |
| 83 | +}); |
0 commit comments