From ebdc34669928ab0b50d2972471d43315a4f67ce3 Mon Sep 17 00:00:00 2001 From: Daniel Macak Date: Sun, 17 Dec 2023 18:01:29 +0100 Subject: [PATCH] fix: check that changes are defined It can easily happen that items are undefined as a change when ngOnChanges is called in GalleryComponent. This is a deficiency of the Angular's types that this is not checked, and it slipped through my test suite as well. So I created a stricter type that will keep the possibility of changes being undefined in check and reorganized the test so that the ngOnChanges with undefined `items` change is checked as well. --- .../src/lib/components/gallery/gallery.component.spec.ts | 2 ++ .../gallery/src/lib/components/gallery/gallery.component.ts | 6 +++--- libs/gallery/src/lib/core/ng.ts | 6 +++++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/libs/gallery/src/lib/components/gallery/gallery.component.spec.ts b/libs/gallery/src/lib/components/gallery/gallery.component.spec.ts index 7c1fadbf..844cba97 100644 --- a/libs/gallery/src/lib/components/gallery/gallery.component.spec.ts +++ b/libs/gallery/src/lib/components/gallery/gallery.component.spec.ts @@ -36,6 +36,8 @@ describe('GalleryComponent', () => { it('should handle empty items input in looping mode', () => { componentRef.setInput('items', undefined); + fixture.detectChanges(); + componentRef.setInput('loop', true); fixture.detectChanges(); diff --git a/libs/gallery/src/lib/components/gallery/gallery.component.ts b/libs/gallery/src/lib/components/gallery/gallery.component.ts index ca0f48b4..415eb547 100644 --- a/libs/gallery/src/lib/components/gallery/gallery.component.ts +++ b/libs/gallery/src/lib/components/gallery/gallery.component.ts @@ -9,7 +9,6 @@ import { Input, OnChanges, Output, - SimpleChanges, TemplateRef, ViewChild, } from '@angular/core'; @@ -27,6 +26,7 @@ import { VerticalOrientation, } from '../../core'; import { defaultAria } from '../../core/aria'; +import { StrictSimpleChanges } from '../../core/ng'; import { ThumbsComponent } from '../thumbs/thumbs.component'; import { ViewerComponent } from '../viewer/viewer.component'; @@ -191,8 +191,8 @@ export class GalleryComponent implements OnChanges { : OrientationFlag.VERTICAL; } - ngOnChanges({ items }: SimpleChanges) { - if (!items.currentValue) { + ngOnChanges({ items }: StrictSimpleChanges) { + if (!items?.currentValue) { this.items = []; } } diff --git a/libs/gallery/src/lib/core/ng.ts b/libs/gallery/src/lib/core/ng.ts index 13572fd4..8c152d94 100644 --- a/libs/gallery/src/lib/core/ng.ts +++ b/libs/gallery/src/lib/core/ng.ts @@ -1,6 +1,10 @@ -import { ComponentRef } from '@angular/core'; +import { ComponentRef, SimpleChange, SimpleChanges } from '@angular/core'; export interface StrictComponentRef extends ComponentRef { // `& string` because otherwise keyof would return string | number setInput(name: keyof C & string, value: unknown): void; } + +export type StrictSimpleChanges = SimpleChanges & { + [propName in keyof T]: SimpleChange | undefined; +};