Created
November 12, 2019 08:57
-
-
Save kroeder/0130e04294cd5e4a791e5c4e20797085 to your computer and use it in GitHub Desktop.
Test util for setting component properties and call ngOnChanges with these changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class TypedSimpleChange<T> { | |
constructor(public previousValue: T | undefined, public currentValue: T, public firstChange: boolean) {}; | |
} | |
declare type TypedSimpleChanges<T> = { | |
[Key in keyof T]?: TypedSimpleChange<T[Key]>; | |
}; | |
function updatePropsAndCallNgOnChanges<T extends object>(componentFixture: OnChanges, changes: TypedSimpleChanges<Partial<T>>) { | |
const simpleChanges: SimpleChanges = {}; | |
for (const key in changes) { | |
if (changes.hasOwnProperty(key)) { | |
const change = changes[key] as SimpleChange; | |
(componentFixture as any)[key] = change.currentValue; | |
simpleChanges[key] = new SimpleChange(change.previousValue, change.currentValue, change.firstChange); | |
} | |
} | |
componentFixture.ngOnChanges(simpleChanges); | |
} |
Author
kroeder
commented
Nov 12, 2019
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment