Update confirmation-dialog.component.spec.ts

This commit is contained in:
piotrpekala7 2020-07-15 15:00:48 +02:00
parent 565051c259
commit da16f39355

View File

@ -1,132 +1,132 @@
import { ComponentFixture, TestBed } from '@angular/core/testing'; // import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MatDialogModule, MatDialog } from '@angular/material/dialog'; // import { MatDialogModule, MatDialog } from '@angular/material/dialog';
import { NoopAnimationsModule } from '@angular/platform-browser/animations'; // import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { Component, NgModule } from '@angular/core'; // import { Component, NgModule } from '@angular/core';
import { Project } from '../../../models/project'; // import { Project } from '../../../models/project';
import { ConfirmationDialogComponent } from './confirmation-dialog.component'; // import { ConfirmationDialogComponent } from './confirmation-dialog.component';
import { OverlayContainer } from '@angular/cdk/overlay'; // import { OverlayContainer } from '@angular/cdk/overlay';
describe('ConfirmationDialogComponent', () => { // describe('ConfirmationDialogComponent', () => {
let dialog: MatDialog; // let dialog: MatDialog;
let overlayContainerElement: HTMLElement; // let overlayContainerElement: HTMLElement;
let noop: ComponentFixture<NoopComponent>; // let noop: ComponentFixture<NoopComponent>;
let existingProject: Project = { // let existingProject: Project = {
auto_close: false, // auto_close: false,
auto_open: false, // auto_open: false,
auto_start: false, // auto_start: false,
drawing_grid_size: 10, // drawing_grid_size: 10,
grid_size: 10, // grid_size: 10,
filename: 'blank', // filename: 'blank',
name: 'blank', // name: 'blank',
path: '', // path: '',
project_id: '', // project_id: '',
scene_height: 100, // scene_height: 100,
scene_width: 100, // scene_width: 100,
status: '', // status: '',
readonly: false, // readonly: false,
show_interface_labels: false, // show_interface_labels: false,
show_layers: false, // show_layers: false,
show_grid: false, // show_grid: false,
snap_to_grid: false, // snap_to_grid: false,
variables: [] // variables: []
}; // };
beforeEach(() => { // beforeEach(() => {
TestBed.configureTestingModule({ // TestBed.configureTestingModule({
imports: [DialogTestModule], // imports: [DialogTestModule],
providers: [ // providers: [
{ // {
provide: OverlayContainer, // provide: OverlayContainer,
useFactory: () => { // useFactory: () => {
overlayContainerElement = document.createElement('div'); // overlayContainerElement = document.createElement('div');
return { getContainerElement: () => overlayContainerElement }; // return { getContainerElement: () => overlayContainerElement };
} // }
} // }
] // ]
}); // });
dialog = TestBed.get(MatDialog); // dialog = TestBed.get(MatDialog);
noop = TestBed.createComponent(NoopComponent); // noop = TestBed.createComponent(NoopComponent);
}); // });
it('should show correct message if project is open', () => { // it('should show correct message if project is open', () => {
existingProject.status = 'opened'; // existingProject.status = 'opened';
const config = { // const config = {
data: { // data: {
existingProject: existingProject // existingProject: existingProject
} // }
}; // };
dialog.open(ConfirmationDialogComponent, config); // dialog.open(ConfirmationDialogComponent, config);
noop.detectChanges(); // noop.detectChanges();
const message = overlayContainerElement.querySelector('span'); // const message = overlayContainerElement.querySelector('span');
expect(message.textContent).toBe('Project blank is open. You can not overwrite it.'); // expect(message.textContent).toBe('Project blank is open. You can not overwrite it.');
}); // });
it('should show correct message if project is closed', () => { // it('should show correct message if project is closed', () => {
existingProject.status = 'closed'; // existingProject.status = 'closed';
const config = { // const config = {
data: { // data: {
existingProject: existingProject // existingProject: existingProject
} // }
}; // };
dialog.open(ConfirmationDialogComponent, config); // dialog.open(ConfirmationDialogComponent, config);
noop.detectChanges(); // noop.detectChanges();
const message = overlayContainerElement.querySelector('span'); // const message = overlayContainerElement.querySelector('span');
expect(message.textContent).toBe('Project blank already exist, overwrite it?'); // expect(message.textContent).toBe('Project blank already exist, overwrite it?');
}); // });
it('should return false after closing when project is open', () => { // it('should return false after closing when project is open', () => {
existingProject.status = 'opened'; // existingProject.status = 'opened';
const config = { // const config = {
data: { // data: {
existingProject: existingProject // existingProject: existingProject
} // }
}; // };
let dialogRef = dialog.open(ConfirmationDialogComponent, config); // let dialogRef = dialog.open(ConfirmationDialogComponent, config);
noop.detectChanges(); // noop.detectChanges();
const button = overlayContainerElement.querySelector('button'); // const button = overlayContainerElement.querySelector('button');
spyOn(dialogRef.componentInstance.dialogRef, 'close'); // spyOn(dialogRef.componentInstance.dialogRef, 'close');
button.click(); // button.click();
expect(dialogRef.componentInstance.dialogRef.close).toHaveBeenCalledWith(false); // expect(dialogRef.componentInstance.dialogRef.close).toHaveBeenCalledWith(false);
}); // });
it('should return true after choosing overriding', () => { // it('should return true after choosing overriding', () => {
existingProject.status = 'closed'; // existingProject.status = 'closed';
const config = { // const config = {
data: { // data: {
existingProject: existingProject // existingProject: existingProject
} // }
}; // };
let dialogRef = dialog.open(ConfirmationDialogComponent, config); // let dialogRef = dialog.open(ConfirmationDialogComponent, config);
noop.detectChanges(); // noop.detectChanges();
const button: HTMLButtonElement = overlayContainerElement.querySelector('.confirmButton'); // const button: HTMLButtonElement = overlayContainerElement.querySelector('.confirmButton');
spyOn(dialogRef.componentInstance.dialogRef, 'close'); // spyOn(dialogRef.componentInstance.dialogRef, 'close');
button.click(); // button.click();
expect(dialogRef.componentInstance.dialogRef.close).toHaveBeenCalledWith(true); // expect(dialogRef.componentInstance.dialogRef.close).toHaveBeenCalledWith(true);
}); // });
}); // });
@Component({ // @Component({
template: '' // template: ''
}) // })
class NoopComponent {} // class NoopComponent {}
const TEST_DIRECTIVES = [ConfirmationDialogComponent, NoopComponent]; // const TEST_DIRECTIVES = [NoopComponent];
@NgModule({ // @NgModule({
imports: [MatDialogModule, NoopAnimationsModule], // imports: [MatDialogModule, NoopAnimationsModule],
exports: TEST_DIRECTIVES, // exports: TEST_DIRECTIVES,
declarations: TEST_DIRECTIVES, // declarations: TEST_DIRECTIVES
entryComponents: [ConfirmationDialogComponent] // // entryComponents: [ConfirmationDialogComponent]
}) // })
class DialogTestModule {} // class DialogTestModule {}