|
| 1 | +import { OverlayContainer } from '@angular/cdk/overlay'; |
| 2 | +import { HarnessLoader } from '@angular/cdk/testing'; |
| 3 | +import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed'; |
| 4 | +import { Component, Input, OnInit } from '@angular/core'; |
| 5 | +import { waitForAsync, ComponentFixture, TestBed } from '@angular/core/testing'; |
| 6 | +import { MatButtonHarness } from '@angular/material/button/testing'; |
| 7 | +import { MAT_DIALOG_DATA, MatDialog, MatDialogModule, MatDialogRef } from '@angular/material/dialog'; |
| 8 | +import { MatDialogHarness } from '@angular/material/dialog/testing'; |
| 9 | +import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; |
| 10 | +import { MockResource, ReadIntValue, ReadValue } from '@dasch-swiss/dsp-js'; |
| 11 | +import { ConfirmationDialogComponent, ConfirmationDialogValueDeletionPayload } from './confirmation-dialog.component'; |
| 12 | + |
| 13 | +/** |
| 14 | + * test host component to simulate parent component with a confirmation dialog. |
| 15 | + */ |
| 16 | +@Component({ |
| 17 | + template: ` |
| 18 | + <p> {{confirmationDialogResponse}} </p>` |
| 19 | +}) |
| 20 | +class ConfirmationDialogTestHostComponent implements OnInit { |
| 21 | + |
| 22 | + confirmationDialogResponse: string; |
| 23 | + |
| 24 | + testValue: ReadIntValue; |
| 25 | + |
| 26 | + constructor(private dialog: MatDialog) { |
| 27 | + } |
| 28 | + |
| 29 | + ngOnInit() { |
| 30 | + MockResource.getTestThing().subscribe(res => { |
| 31 | + this.testValue = res.getValuesAs('http://0.0.0.0:3333/ontology/0001/anything/v2#hasInteger', ReadIntValue)[0]; |
| 32 | + }); |
| 33 | + } |
| 34 | + |
| 35 | + openDialog() { |
| 36 | + |
| 37 | + this.dialog.open(ConfirmationDialogComponent, { |
| 38 | + data: { |
| 39 | + value: this.testValue, |
| 40 | + buttonTextOk: 'OK', |
| 41 | + buttonTextCancel: 'Cancel' |
| 42 | + } |
| 43 | + }).afterClosed().subscribe((payload: ConfirmationDialogValueDeletionPayload) => { |
| 44 | + if (payload.confirmed) { |
| 45 | + this.confirmationDialogResponse = 'Action was confirmed!'; |
| 46 | + } else { |
| 47 | + this.confirmationDialogResponse = 'Action was cancelled'; |
| 48 | + } |
| 49 | + }); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +@Component({ selector: 'app-confirmation-message', template: '' }) |
| 54 | +class MockConfirmationMessageComponent { |
| 55 | + @Input() value: ReadValue; |
| 56 | + |
| 57 | + constructor() { } |
| 58 | +} |
| 59 | + |
| 60 | +describe('ConfirmationDialogComponent', () => { |
| 61 | + let testHostComponent: ConfirmationDialogTestHostComponent; |
| 62 | + let testHostFixture: ComponentFixture<ConfirmationDialogTestHostComponent>; |
| 63 | + let rootLoader: HarnessLoader; |
| 64 | + let overlayContainer: OverlayContainer; |
| 65 | + |
| 66 | + beforeEach(waitForAsync(() => { |
| 67 | + TestBed.configureTestingModule({ |
| 68 | + declarations: [ |
| 69 | + ConfirmationDialogComponent, |
| 70 | + ConfirmationDialogTestHostComponent, |
| 71 | + MockConfirmationMessageComponent |
| 72 | + ], |
| 73 | + imports: [ |
| 74 | + MatDialogModule, |
| 75 | + BrowserAnimationsModule |
| 76 | + ], |
| 77 | + providers: [ |
| 78 | + { |
| 79 | + provide: MAT_DIALOG_DATA, |
| 80 | + useValue: {} |
| 81 | + }, |
| 82 | + { |
| 83 | + provide: MatDialogRef, |
| 84 | + useValue: {} |
| 85 | + } |
| 86 | + ] |
| 87 | + }) |
| 88 | + .compileComponents(); |
| 89 | + |
| 90 | + overlayContainer = TestBed.inject(OverlayContainer); |
| 91 | + })); |
| 92 | + |
| 93 | + beforeEach(() => { |
| 94 | + testHostFixture = TestBed.createComponent(ConfirmationDialogTestHostComponent); |
| 95 | + testHostComponent = testHostFixture.componentInstance; |
| 96 | + rootLoader = TestbedHarnessEnvironment.documentRootLoader(testHostFixture); |
| 97 | + testHostFixture.detectChanges(); |
| 98 | + expect(testHostComponent).toBeTruthy(); |
| 99 | + }); |
| 100 | + |
| 101 | + afterEach(async () => { |
| 102 | + const dialogs = await rootLoader.getAllHarnesses(MatDialogHarness); |
| 103 | + await Promise.all(dialogs.map(async d => await d.close())); |
| 104 | + |
| 105 | + // angular won't call this for us so we need to do it ourselves to avoid leaks. |
| 106 | + overlayContainer.ngOnDestroy(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should display a confirmation dialog', async () => { |
| 110 | + |
| 111 | + testHostComponent.openDialog(); |
| 112 | + |
| 113 | + testHostFixture.detectChanges(); |
| 114 | + |
| 115 | + await testHostFixture.whenStable(); |
| 116 | + |
| 117 | + const dialogDiv = document.querySelector('mat-dialog-container'); |
| 118 | + expect(dialogDiv).toBeTruthy(); |
| 119 | + |
| 120 | + const dspConfirmMsg = document.querySelector('app-confirmation-message'); |
| 121 | + expect(dspConfirmMsg).toBeTruthy(); |
| 122 | + |
| 123 | + const dialogTitle = dialogDiv.querySelector('.title'); |
| 124 | + expect(dialogTitle.innerHTML.trim()).toEqual('Are you sure you want to delete this value from Integer?'); |
| 125 | + |
| 126 | + }); |
| 127 | + |
| 128 | + it('should return a confirmation message when the OK button is clicked', async () => { |
| 129 | + |
| 130 | + testHostComponent.openDialog(); |
| 131 | + |
| 132 | + let dialogHarnesses = await rootLoader.getAllHarnesses(MatDialogHarness); |
| 133 | + |
| 134 | + expect(dialogHarnesses.length).toEqual(1); |
| 135 | + |
| 136 | + const okButton = await rootLoader.getHarness(MatButtonHarness.with({ selector: '.ok' })); |
| 137 | + |
| 138 | + await okButton.click(); |
| 139 | + |
| 140 | + dialogHarnesses = await rootLoader.getAllHarnesses(MatDialogHarness); |
| 141 | + |
| 142 | + expect(dialogHarnesses.length).toEqual(0); |
| 143 | + |
| 144 | + expect(testHostComponent.confirmationDialogResponse).toEqual('Action was confirmed!'); |
| 145 | + |
| 146 | + }); |
| 147 | + |
| 148 | + it('should return a cancelled message when the cancel button is clicked', async () => { |
| 149 | + |
| 150 | + testHostComponent.openDialog(); |
| 151 | + |
| 152 | + let dialogHarnesses = await rootLoader.getAllHarnesses(MatDialogHarness); |
| 153 | + |
| 154 | + expect(dialogHarnesses.length).toEqual(1); |
| 155 | + |
| 156 | + const cancelButton = await rootLoader.getHarness(MatButtonHarness.with({ selector: '.cancel' })); |
| 157 | + |
| 158 | + await cancelButton.click(); |
| 159 | + |
| 160 | + dialogHarnesses = await rootLoader.getAllHarnesses(MatDialogHarness); |
| 161 | + |
| 162 | + expect(dialogHarnesses.length).toEqual(0); |
| 163 | + |
| 164 | + expect(testHostComponent.confirmationDialogResponse).toEqual('Action was cancelled'); |
| 165 | + |
| 166 | + |
| 167 | + }); |
| 168 | +}); |
0 commit comments