Simplify and add tests of confirm dialog
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / ui / confirm-dialog / confirm-dialog.component.spec.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 import { async, ComponentFixture, TestBed } from "@angular/core/testing";
22
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";
29
30 describe("ConfirmDialogComponent", () => {
31   let component: ConfirmDialogComponent;
32   let fixture: ComponentFixture<ConfirmDialogComponent>;
33   let loader: HarnessLoader;
34
35   let dialogRefSpy: jasmine.SpyObj<MatDialogRef<any>>;
36
37   beforeEach(async(() => {
38     dialogRefSpy = jasmine.createSpyObj("MatDialogRef", ["close"]);
39
40     TestBed.configureTestingModule({
41       declarations: [ConfirmDialogComponent],
42       imports: [MatDialogModule],
43       providers: [
44         {
45           provide: MAT_DIALOG_DATA,
46           useValue: { heading: "Delete Policy", message: "Do?" },
47         },
48         { provide: MatDialogRef, useValue: dialogRefSpy },
49       ],
50     }).compileComponents();
51   }));
52
53   beforeEach(() => {
54     fixture = TestBed.createComponent(ConfirmDialogComponent);
55     component = fixture.componentInstance;
56     fixture.detectChanges();
57     loader = TestbedHarnessEnvironment.loader(fixture);
58   });
59
60   it("should create and contain correct title, message and buttons", async () => {
61     expect(component).toBeTruthy();
62
63     const title = fixture.debugElement.nativeElement.querySelector("#title");
64     expect(title.innerText).toEqual("Delete Policy");
65
66     const message = fixture.debugElement.nativeElement.querySelector(
67       "#message"
68     );
69     expect(message.innerText).toEqual("Do?");
70
71     const cancelButton = fixture.debugElement.nativeElement.querySelector(
72       "#cancelButton"
73     );
74     expect(cancelButton).toBeTruthy();
75
76     const okButton = fixture.debugElement.nativeElement.querySelector(
77       "#okButton"
78     );
79     expect(okButton).toBeTruthy();
80   });
81
82   it("should close dialog with true when Ok button clicked", async () =>{
83     let okButton: MatButtonHarness = await loader.getHarness(
84       MatButtonHarness.with({ selector: "#okButton" })
85     );
86     await okButton.click();
87
88     expect(dialogRefSpy.close).toHaveBeenCalledWith(true);
89   });
90
91   it("should close dialog without data when Cancel button clicked", async () =>{
92     let cancelButton: MatButtonHarness = await loader.getHarness(
93       MatButtonHarness.with({ selector: "#cancelButton" })
94     );
95     await cancelButton.click();
96
97     expect(dialogRefSpy.close).toHaveBeenCalledWith("");
98   });
99 });