diff --git a/src-ui/src/app/components/common/tag/tag.component.spec.ts b/src-ui/src/app/components/common/tag/tag.component.spec.ts index 06d79d96ab..e7ce719702 100644 --- a/src-ui/src/app/components/common/tag/tag.component.spec.ts +++ b/src-ui/src/app/components/common/tag/tag.component.spec.ts @@ -6,6 +6,7 @@ import { of } from 'rxjs' import { Tag } from 'src/app/data/tag' import { PermissionsService } from 'src/app/services/permissions.service' import { TagService } from 'src/app/services/rest/tag.service' +import { SettingsService } from 'src/app/services/settings.service' import { TagComponent } from './tag.component' const tag: Tag = { @@ -69,4 +70,88 @@ describe('TagComponent', () => { expect(getCachedSpy).toHaveBeenCalledWith(1) expect(component.tag()).toEqual(tag) }) + describe('split badge', () => { + const root: Tag = { id: 1, name: 'Root', color: '#000000' } + const mid: Tag = { id: 2, name: 'Mid', color: '#111111', parent: 1 } + const leaf: Tag = { id: 3, name: 'Leaf', color: '#222222', parent: 2 } + + const segments = (): HTMLElement[] => + fixture.debugElement + .queryAll(By.css('.split-pill > .badge')) + .map((d) => d.nativeElement) + + beforeEach(() => { + jest.spyOn(permissionsService, 'currentUserCan').mockReturnValue(true) + jest.spyOn(tagService, 'getCached').mockReturnValue(of(leaf)) + jest + .spyOn(tagService, 'listAll') + .mockReturnValue( + of({ count: 3, all: [1, 2, 3], results: [root, mid, leaf] }) + ) + fixture.componentRef.setInput('tagID', 3) + fixture.componentRef.setInput('split', true) + fixture.detectChanges() + }) + + it('should render ancestors as segments with the full path as title', () => { + expect(segments().map((s) => s.textContent.trim())).toEqual([ + 'Root', + 'Mid', + 'Leaf', + ]) + expect( + fixture.debugElement.query(By.css('.split-pill')).nativeElement.title + ).toEqual('Root › Mid › Leaf') + }) + + it('should render a plain badge when the setting is off', () => { + jest.spyOn(TestBed.inject(SettingsService), 'get').mockReturnValue(false) + fixture.componentRef.setInput('split', false) + fixture.detectChanges() + fixture.componentRef.setInput('split', true) + fixture.detectChanges() + expect(fixture.debugElement.query(By.css('.split-pill'))).toBeNull() + expect(component.ancestorSegments).toEqual([]) + }) + + it('should emit the ancestor id when an ancestor segment is clicked', () => { + const emitSpy = jest.spyOn(component.parentClick, 'emit') + const event = new MouseEvent('click', { bubbles: true }) + const stopSpy = jest.spyOn(event, 'stopImmediatePropagation') + segments()[1].dispatchEvent(event) + expect(emitSpy).toHaveBeenCalledWith(2) + expect(stopSpy).toHaveBeenCalled() + }) + + it('should focus the hovered segment and animate width changes', () => { + const [rootSeg, midSeg, leafSeg] = segments() + const pill = rootSeg.parentElement + // before / after widths per segment, as measured by setFocus + const widths = new Map([ + [rootSeg, [20, 4]], + [midSeg, [20, 60]], + [leafSeg, [40, 40]], + ]) + const animate = jest.fn() + for (const [seg, [before, after]] of widths) { + seg.getAnimations = jest.fn(() => []) + seg.animate = animate + jest + .spyOn(seg, 'getBoundingClientRect') + .mockReturnValueOnce({ width: before } as DOMRect) + .mockReturnValueOnce({ width: after } as DOMRect) + } + + midSeg.dispatchEvent(new MouseEvent('mouseenter')) + expect(midSeg.classList).toContain('focused') + expect(rootSeg.classList).not.toContain('focused') + // only the segments whose width changed are animated + expect(animate).toHaveBeenCalledTimes(2) + + pill.dispatchEvent(new MouseEvent('mouseleave')) + expect(segments().some((s) => s.classList.contains('focused'))).toBe( + false + ) + }) + }) }) diff --git a/src-ui/src/app/pipes/collapse-tags.pipe.spec.ts b/src-ui/src/app/pipes/collapse-tags.pipe.spec.ts new file mode 100644 index 0000000000..d9f3b23220 --- /dev/null +++ b/src-ui/src/app/pipes/collapse-tags.pipe.spec.ts @@ -0,0 +1,67 @@ +import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http' +import { provideHttpClientTesting } from '@angular/common/http/testing' +import { TestBed } from '@angular/core/testing' +import { firstValueFrom, of, throwError } from 'rxjs' +import { Tag } from '../data/tag' +import { PermissionsService } from '../services/permissions.service' +import { TagService } from '../services/rest/tag.service' +import { SettingsService } from '../services/settings.service' +import { CollapseTagsPipe } from './collapse-tags.pipe' + +const tags: Tag[] = [ + { id: 1, name: 'Parent' }, + { id: 2, name: 'Child', parent: 1 }, + { id: 3, name: 'Other' }, +] + +describe('CollapseTagsPipe', () => { + let pipe: CollapseTagsPipe + let tagService: TagService + let settingsService: SettingsService + let permissionsService: PermissionsService + + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [ + CollapseTagsPipe, + provideHttpClient(withInterceptorsFromDi()), + provideHttpClientTesting(), + ], + }) + pipe = TestBed.inject(CollapseTagsPipe) + tagService = TestBed.inject(TagService) + settingsService = TestBed.inject(SettingsService) + permissionsService = TestBed.inject(PermissionsService) + jest.spyOn(permissionsService, 'currentUserCan').mockReturnValue(true) + jest.spyOn(settingsService, 'get').mockReturnValue(true) + jest + .spyOn(tagService, 'listAll') + .mockReturnValue(of({ count: 3, all: [1, 2, 3], results: tags })) + }) + + it('should drop parents implied by a selected child', async () => { + expect(await firstValueFrom(pipe.transform([1, 2, 3]))).toEqual([2, 3]) + }) + + it('should pass ids through when hierarchy display is off or not permitted', async () => { + jest.spyOn(settingsService, 'get').mockReturnValue(false) + expect(await firstValueFrom(pipe.transform([1, 2]))).toEqual([1, 2]) + + jest.spyOn(settingsService, 'get').mockReturnValue(true) + jest.spyOn(permissionsService, 'currentUserCan').mockReturnValue(false) + expect(await firstValueFrom(pipe.transform([1, 2]))).toEqual([1, 2]) + expect(tagService.listAll).not.toHaveBeenCalled() + }) + + it('should handle empty input', async () => { + expect(await firstValueFrom(pipe.transform(null))).toEqual([]) + expect(await firstValueFrom(pipe.transform([]))).toEqual([]) + }) + + it('should fall back to the original ids if tags fail to load', async () => { + jest + .spyOn(tagService, 'listAll') + .mockReturnValue(throwError(() => new Error('failed'))) + expect(await firstValueFrom(pipe.transform([1, 2]))).toEqual([1, 2]) + }) +}) diff --git a/src-ui/src/app/utils/collapse-tags.spec.ts b/src-ui/src/app/utils/collapse-tags.spec.ts index a685460cb1..f63ed36dfd 100644 --- a/src-ui/src/app/utils/collapse-tags.spec.ts +++ b/src-ui/src/app/utils/collapse-tags.spec.ts @@ -33,4 +33,15 @@ describe('getTagAncestors', () => { expect(getTagAncestors(3, tagsById).map((t) => t.id)).toEqual([1, 2]) expect(getTagAncestors(5, tagsById)).toEqual([]) }) + + it('stops at a parent that is not visible or forms a cycle', () => { + const hidden = new Map([[6, { id: 6, name: 'x', parent: 99 }]]) + expect(getTagAncestors(6, hidden)).toEqual([]) + + const cyclic = new Map([ + [7, { id: 7, name: 'a', parent: 8 }], + [8, { id: 8, name: 'b', parent: 7 }], + ]) + expect(getTagAncestors(7, cyclic).map((t) => t.id)).toEqual([7, 8]) + }) })