forked from juristr/angular-testing-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
domtesting.component.spec.ts
70 lines (56 loc) · 2.2 KB
/
domtesting.component.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { tick } from '@angular/core/testing';
/* tslint:disable:no-unused-variable */
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { Component, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'test',
template: `
<div class="container" *ngIf="isVisible">Hi there!</div>
<button (click)="isVisible = !isVisible">toggle</button>
`
})
class DomTestingComponent {
isVisible: boolean = false;
}
describe('DomTestingComponent', () => {
let component: DomTestingComponent;
let fixture: ComponentFixture<DomTestingComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [DomTestingComponent]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(DomTestingComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should not have the DOM element if boolean is set to false', () => {
let containerElement = fixture.debugElement.query(By.css('.container'));
expect(containerElement).toBeNull();
});
it('should have the DOM element if boolean is set to true', () => {
component.isVisible = true;
fixture.detectChanges();
fixture.whenStable().then(() => {
let containerElement = fixture.debugElement.query(By.css('.container'));
expect(containerElement).not.toBeNull();
});
});
it('clicking the button should toggle visiblity', async(() => {
let button = fixture.debugElement.query(By.css('button'));
expect(fixture.debugElement.query(By.css('.container'))).toBeNull();
button.triggerEventHandler('click', <Event>{});
fixture.detectChanges();
expect(fixture.debugElement.query(By.css('.container'))).not.toBeNull();
button.triggerEventHandler('click', <Event>{});
fixture.detectChanges();
expect(fixture.debugElement.query(By.css('.container'))).toBeNull();
}));
});