2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 import { async, ComponentFixture, TestBed } from "@angular/core/testing";
23 import { ConfirmDialogComponent } from "./confirm-dialog.component";
24 import { MatDialogModule } from "@angular/material/dialog";
25 import { MAT_DIALOG_DATA, MatDialogRef } from "@angular/material/dialog";
26 import { TestbedHarnessEnvironment } from "@angular/cdk/testing/testbed";
27 import { HarnessLoader } from "@angular/cdk/testing";
28 import { MatButtonHarness } from "@angular/material/button/testing";
30 describe("ConfirmDialogComponent", () => {
31 let component: ConfirmDialogComponent;
32 let fixture: ComponentFixture<ConfirmDialogComponent>;
33 let loader: HarnessLoader;
35 let dialogRefSpy: jasmine.SpyObj<MatDialogRef<any>>;
37 beforeEach(async(() => {
38 dialogRefSpy = jasmine.createSpyObj("MatDialogRef", ["close"]);
40 TestBed.configureTestingModule({
41 declarations: [ConfirmDialogComponent],
42 imports: [MatDialogModule],
45 provide: MAT_DIALOG_DATA,
46 useValue: { heading: "Delete Policy", message: "Do?" },
48 { provide: MatDialogRef, useValue: dialogRefSpy },
50 }).compileComponents();
54 fixture = TestBed.createComponent(ConfirmDialogComponent);
55 component = fixture.componentInstance;
56 fixture.detectChanges();
57 loader = TestbedHarnessEnvironment.loader(fixture);
60 it("should create and contain correct title, message and buttons", async () => {
61 expect(component).toBeTruthy();
63 const title = fixture.debugElement.nativeElement.querySelector("#title");
64 expect(title.innerText).toEqual("Delete Policy");
66 const message = fixture.debugElement.nativeElement.querySelector(
69 expect(message.innerText).toEqual("Do?");
71 const cancelButton = fixture.debugElement.nativeElement.querySelector(
74 expect(cancelButton).toBeTruthy();
76 const okButton = fixture.debugElement.nativeElement.querySelector(
79 expect(okButton).toBeTruthy();
82 it("should close dialog with true when Ok button clicked", async () =>{
83 let okButton: MatButtonHarness = await loader.getHarness(
84 MatButtonHarness.with({ selector: "#okButton" })
86 await okButton.click();
88 expect(dialogRefSpy.close).toHaveBeenCalledWith(true);
91 it("should close dialog without data when Cancel button clicked", async () =>{
92 let cancelButton: MatButtonHarness = await loader.getHarness(
93 MatButtonHarness.with({ selector: "#cancelButton" })
95 await cancelButton.click();
97 expect(dialogRefSpy.close).toHaveBeenCalledWith("");