From 7b948b0422cf9e33309e83e4e05764b729867d37 Mon Sep 17 00:00:00 2001 From: David LJ Date: Fri, 29 Nov 2024 11:37:09 +0100 Subject: [PATCH] test: improve coverage on collapsible tree node sibling test --- .../collapsible-tree.component.spec.ts | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/app/resume-page/collapsible-tree/collapsible-tree.component.spec.ts b/src/app/resume-page/collapsible-tree/collapsible-tree.component.spec.ts index 168a9101..7257fb7e 100644 --- a/src/app/resume-page/collapsible-tree/collapsible-tree.component.spec.ts +++ b/src/app/resume-page/collapsible-tree/collapsible-tree.component.spec.ts @@ -208,14 +208,7 @@ describe('CollapsibleTreeComponent', () => { ] as const satisfies readonly ExpandedTestCase[] for (const testCase of EXPANDED_TEST_CASES) { describe(`when ${testCase.name}`, () => { - let mockParentComponent: jasmine.SpyObj - beforeEach(() => { - mockParentComponent = - jasmine.createSpyObj('node', [ - 'collapseAllChildren', - ]) - component.parent = mockParentComponent component.isExpanded = testCase.isExpanded fixture.detectChanges() @@ -250,21 +243,30 @@ describe('CollapsibleTreeComponent', () => { 'true', ) }) + }) + } + + it('should collapse rest of siblings when expanding a collapsed node', () => { + fixture.detectChanges() - if (testCase.name === 'collapsed') { - it('should collapse rest of siblings', () => { - const buttonElement = fixture.debugElement.query(BUTTON_PREDICATE) - buttonElement.triggerEventHandler('click') + const childrenComponents = fixture.debugElement + .queryAll(byComponent(CollapsibleTreeComponent)) + .map((c) => c.componentInstance as CollapsibleTreeComponent) - fixture.detectChanges() + expect(childrenComponents.length).toEqual(DUMMY_CHILDREN.length) - expect( - mockParentComponent.collapseAllChildren, - ).toHaveBeenCalledOnceWith({ except: component }) - }) - } + childrenComponents.forEach( + (child) => (child.collapse = jasmine.createSpy()), + ) + const [firstChild, ...restChildrenComponents] = childrenComponents + + firstChild.expand() + + expect(firstChild.collapse).not.toHaveBeenCalled() + restChildrenComponents.forEach((child) => { + expect(child.collapse).toHaveBeenCalledOnceWith() }) - } + }) }) describe('when non collapsible', () => {