Skip to content

Commit

Permalink
fix(angular): call ngOnChanges after mount
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanpowell88 committed Aug 29, 2022
1 parent 2f337db commit 43cf95f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
9 changes: 6 additions & 3 deletions npm/angular/src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,9 @@ function setupFixture<T> (
* @param {ComponentFixture<T>} fixture Fixture for debugging and testing a component.
* @returns {T} Component being mounted
*/
function setupComponent<T> (
function setupComponent<T extends { ngOnChanges? (): void }> (
config: MountConfig<T>,
fixture: ComponentFixture<T>,
): T {
fixture: ComponentFixture<T>): T {
let component: T = fixture.componentInstance

if (config?.componentProperties) {
Expand All @@ -230,6 +229,10 @@ function setupComponent<T> (
})
}

if (component.ngOnChanges) {
component.ngOnChanges()
}

return component
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Component, Input, OnInit, OnChanges, SimpleChanges } from '@angular/core'

@Component({
selector: 'app-lifecycle',
template: `<p>Hi {{ name }}. ngOnInit fired: {{ ngOnInitFired }} and ngOnChanges fired: {{ ngOnChangesFired }}</p>`
})
export class LifecycleComponent implements OnInit, OnChanges {
@Input() name = ''
ngOnInitFired = false
ngOnChangesFired = false

ngOnInit(): void {
this.ngOnInitFired = true
}

ngOnChanges(changes: SimpleChanges): void {
this.ngOnChangesFired = true;
}
}
24 changes: 23 additions & 1 deletion system-tests/project-fixtures/angular/src/app/mount.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ButtonOutputComponent } from "./components/button-output.component";
import { createOutputSpy } from 'cypress/angular';
import { EventEmitter, Component } from '@angular/core';
import { ProjectionComponent } from "./components/projection.component";
import { LifecycleComponent } from "./components/lifecycle.component";

@Component({
template: `<app-projection>Hello World</app-projection>`
Expand Down Expand Up @@ -164,7 +165,28 @@ describe("angular mount", () => {
})
cy.get('h3').contains('Hello World')
})


it('handles ngOnChanges on mount', () => {
cy.mount(LifecycleComponent, {
componentProperties: {
name: 'Angular'
}
})

cy.get('p').should('have.text', 'Hi Angular. ngOnInit fired: true and ngOnChanges fired: true')
})

it('handles ngOnChanges on mount', () => {
cy.mount('<app-lifecycle [name]="name"></app-lifecycle>', {
declarations: [LifecycleComponent],
componentProperties: {
name: 'Angular'
}
})

cy.get('p').should('have.text', 'Hi Angular. ngOnInit fired: true and ngOnChanges fired: true')
})

describe("teardown", () => {
beforeEach(() => {
cy.get("[id^=root]").should("not.exist");
Expand Down

0 comments on commit 43cf95f

Please sign in to comment.