|
| 1 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 2 | + |
| 3 | +import { ContainerModule } from '../container/container.module'; |
| 4 | +import { EventBindingComponent } from './event-binding.component'; |
| 5 | +import { createCustomEvent } from '../../../../testing/dom-utils'; |
| 6 | + |
| 7 | +describe('Getting Started Event Binding Component', () => { |
| 8 | + let component: EventBindingComponent; |
| 9 | + let fixture: ComponentFixture<EventBindingComponent>; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + TestBed.configureTestingModule({ |
| 13 | + imports: [ ContainerModule ], |
| 14 | + declarations: [ EventBindingComponent ] |
| 15 | + }); |
| 16 | + |
| 17 | + fixture = TestBed.createComponent(EventBindingComponent); |
| 18 | + component = fixture.componentInstance; |
| 19 | + fixture.detectChanges(); |
| 20 | + |
| 21 | + spyOn(window, 'alert'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should update the name property on input change', () => { |
| 25 | + const text = 'Hello Angular'; |
| 26 | + const compiled = fixture.debugElement.nativeElement; |
| 27 | + const input: HTMLInputElement = compiled.querySelector('input'); |
| 28 | + |
| 29 | + |
| 30 | + input.value = text; |
| 31 | + input.dispatchEvent(createCustomEvent(document, 'input', '')); |
| 32 | + |
| 33 | + fixture.detectChanges(); |
| 34 | + |
| 35 | + expect(component.name).toBe(text); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should display an alert when the button is clicked', () => { |
| 39 | + const compiled = fixture.debugElement.nativeElement; |
| 40 | + const button: HTMLButtonElement = compiled.querySelector('button'); |
| 41 | + |
| 42 | + button.click(); |
| 43 | + |
| 44 | + expect(window.alert).toHaveBeenCalledWith('Hello, Angular!'); |
| 45 | + }); |
| 46 | +}); |
0 commit comments