Adds some unit tests

This commit is contained in:
Aaron Louie 2020-01-30 15:34:11 -05:00
parent cb0f726a48
commit fd42a55654
3 changed files with 161 additions and 20 deletions

View File

@ -6,7 +6,8 @@ import {MatFormFieldModule} from '@angular/material/form-field';
import {MatIconModule} from '@angular/material/icon';
import {MatInputModule} from '@angular/material/input';
import {BrowserAnimationsModule, NoopAnimationsModule} from '@angular/platform-browser/animations';
import {ApiService, MockEnvironment} from 'sartography-workflow-lib';
import {ApiService, MockEnvironment, mockFileMeta0} from 'sartography-workflow-lib';
import {OpenFileDialogData} from '../../_interfaces/dialog-data';
import { OpenFileDialogComponent } from './open-file-dialog.component';
@ -62,4 +63,37 @@ describe('OpenFileDialogComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});
it('should save data on submit', () => {
const closeSpy = spyOn(component.dialogRef, 'close').and.stub();
const expectedData: OpenFileDialogData = { file: mockFileMeta0.file };
component.diagramFile = expectedData.file;
component.onSubmit();
expect(closeSpy).toHaveBeenCalledWith(expectedData);
});
it('should not change data on cancel', () => {
const closeSpy = spyOn(component.dialogRef, 'close').and.stub();
const expectedData: OpenFileDialogData = { file: mockFileMeta0.file };
component.diagramFile = expectedData.file;
component.onNoClick();
expect(closeSpy).toHaveBeenCalledWith();
});
it('should load XML from URL, then set and clean up filename', () => {
const url = 'whatever/ 🍳 green_eggs.v1-2020-01-01.XML.bmnp 🍖 ';
const expectedName = 'green_eggs.v1-2020-01-01.XML.bpmn';
const onSubmitSpy = spyOn(component, 'onSubmit').and.stub();
component.url = url;
component.onSubmitUrl();
const req = httpMock.expectOne(url);
expect(req.request.method).toEqual('GET');
req.flush('<xml></xml>');
expect(component.diagramFile).toBeTruthy();
expect(component.diagramFile.name).toEqual(expectedName);
expect(onSubmitSpy).toHaveBeenCalled();
});
});

View File

@ -8,8 +8,16 @@ import {MatSnackBarModule} from '@angular/material/snack-bar';
import {BrowserDynamicTestingModule} from '@angular/platform-browser-dynamic/testing';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {RouterTestingModule} from '@angular/router/testing';
import {ApiService, MockEnvironment, mockWorkflowSpec0, mockWorkflowSpecs} from 'sartography-workflow-lib';
import {of} from 'rxjs';
import {
ApiService,
MockEnvironment,
mockWorkflowSpec0,
mockWorkflowSpec1,
mockWorkflowSpecs
} from 'sartography-workflow-lib';
import {DeleteWorkflowSpecDialogComponent} from '../_dialogs/delete-workflow-spec-dialog/delete-workflow-spec-dialog.component';
import {DeleteWorkflowSpecDialogData, WorkflowSpecDialogData} from '../_interfaces/dialog-data';
import {GetIconCodePipe} from '../_pipes/get-icon-code.pipe';
import {FileListComponent} from '../file-list/file-list.component';
@ -45,7 +53,7 @@ describe('WorkflowSpecListComponent', () => {
provide: MatDialogRef,
useValue: {
close: (dialogResult: any) => {
}
},
}
},
{provide: MAT_DIALOG_DATA, useValue: []},
@ -82,13 +90,101 @@ describe('WorkflowSpecListComponent', () => {
expect(component).toBeTruthy();
});
it('should show a metadata dialog when editing a workflow spec', () => {
let mockSpecData: WorkflowSpecDialogData = {
id: '',
name: '',
display_name: '',
description: '',
};
const _upsertWorkflowSpecificationSpy = spyOn((component as any), '_upsertWorkflowSpecification')
.and.stub();
const openDialogSpy = spyOn(component.dialog, 'open')
.and.returnValue({afterClosed: () => of(mockSpecData)} as any);
component.editWorkflowSpec(mockWorkflowSpec0);
expect(openDialogSpy).toHaveBeenCalled();
expect(_upsertWorkflowSpecificationSpy).not.toHaveBeenCalled();
mockSpecData = mockWorkflowSpec0 as WorkflowSpecDialogData;
component.editWorkflowSpec(mockWorkflowSpec0);
expect(openDialogSpy).toHaveBeenCalled();
expect(_upsertWorkflowSpecificationSpy).toHaveBeenCalled();
});
it('should edit an existing workflow spec but add a new workflow spec', () => {
const _addWorkflowSpecSpy = spyOn((component as any), '_addWorkflowSpec').and.stub();
const _updateWorkflowSpecSpy = spyOn((component as any), '_updateWorkflowSpec').and.stub();
component.selectedSpec = undefined;
(component as any)._upsertWorkflowSpecification(mockWorkflowSpec1 as WorkflowSpecDialogData);
expect(_addWorkflowSpecSpy).toHaveBeenCalled();
expect(_updateWorkflowSpecSpy).not.toHaveBeenCalled();
_addWorkflowSpecSpy.calls.reset();
_updateWorkflowSpecSpy.calls.reset();
component.selectedSpec = mockWorkflowSpec0;
const modifiedData: WorkflowSpecDialogData = JSON.parse(JSON.stringify(mockWorkflowSpec0));
modifiedData.display_name = 'Modified';
(component as any)._upsertWorkflowSpecification(modifiedData);
expect(_addWorkflowSpecSpy).not.toHaveBeenCalled();
expect(_updateWorkflowSpecSpy).toHaveBeenCalled();
});
it('should add a workflow spec', () => {
const _loadWorkflowSpecsSpy = spyOn((component as any), '_loadWorkflowSpecs').and.stub();
const _displayMessageSpy = spyOn((component as any), '_displayMessage').and.stub();
(component as any)._addWorkflowSpec(mockWorkflowSpec0);
const wfsReq = httpMock.expectOne(`apiRoot/workflow-specification`);
expect(wfsReq.request.method).toEqual('POST');
wfsReq.flush(mockWorkflowSpec0);
expect(_loadWorkflowSpecsSpy).toHaveBeenCalled();
expect(_displayMessageSpy).toHaveBeenCalled();
});
it('should edit a workflow spec', () => {
const _loadWorkflowSpecsSpy = spyOn((component as any), '_loadWorkflowSpecs').and.stub();
const _displayMessageSpy = spyOn((component as any), '_displayMessage').and.stub();
(component as any)._updateWorkflowSpec(mockWorkflowSpec0.id, mockWorkflowSpec0);
const wfsReq = httpMock.expectOne(`apiRoot/workflow-specification/${mockWorkflowSpec0.id}`);
expect(wfsReq.request.method).toEqual('PUT');
wfsReq.flush(mockWorkflowSpec0);
expect(_loadWorkflowSpecsSpy).toHaveBeenCalled();
expect(_displayMessageSpy).toHaveBeenCalled();
});
it('should show a confirmation dialog before deleting a workflow spec', () => {
const mockConfirmDeleteData: DeleteWorkflowSpecDialogData = {
confirm: false,
workflowSpec: mockWorkflowSpec0
};
const _deleteWorkflowSpecSpy = spyOn((component as any), '_deleteWorkflowSpec').and.stub();
const openDialogSpy = spyOn(component.dialog, 'open')
.and.returnValue({afterClosed: () => of(mockConfirmDeleteData)} as any);
component.confirmDeleteWorkflowSpec(mockWorkflowSpec0);
expect(openDialogSpy).toHaveBeenCalled();
expect(_deleteWorkflowSpecSpy).not.toHaveBeenCalled();
mockConfirmDeleteData.confirm = true;
component.confirmDeleteWorkflowSpec(mockWorkflowSpec0);
expect(openDialogSpy).toHaveBeenCalled();
expect(_deleteWorkflowSpecSpy).toHaveBeenCalled();
});
it('should delete a workflow spec', () => {
const loadWorkflowSpecsSpy = spyOn((component as any), '_loadWorkflowSpecs').and.stub();
(component as any)._deleteWorkflowSpec(mockWorkflowSpec0);
const fmsReq = httpMock.expectOne(`apiRoot/workflow-specification/${mockWorkflowSpec0.id}`);
expect(fmsReq.request.method).toEqual('DELETE');
fmsReq.flush(null);
const wfsReq = httpMock.expectOne(`apiRoot/workflow-specification/${mockWorkflowSpec0.id}`);
expect(wfsReq.request.method).toEqual('DELETE');
wfsReq.flush(null);
expect(loadWorkflowSpecsSpy).toHaveBeenCalled();
});
});

View File

@ -43,7 +43,7 @@ export class WorkflowSpecListComponent implements OnInit {
dialogRef.afterClosed().subscribe((data: WorkflowSpecDialogData) => {
if (data && data.id && data.name && data.display_name && data.description) {
this._upsertSpecAndFileMeta(data);
this._upsertWorkflowSpecification(data);
}
});
}
@ -69,10 +69,10 @@ export class WorkflowSpecListComponent implements OnInit {
});
}
private _upsertSpecAndFileMeta(data: WorkflowSpecDialogData) {
private _upsertWorkflowSpecification(data: WorkflowSpecDialogData) {
if (data.id && data.name && data.display_name && data.description) {
// Save old workflow spec id, if user wants to change it
// Save old workflow spec id, in case it's changed
const specId = this.selectedSpec ? this.selectedSpec.id : undefined;
const newSpec: WorkflowSpec = {
@ -83,25 +83,36 @@ export class WorkflowSpecListComponent implements OnInit {
};
if (specId) {
// Update existing workflow spec and file
this.api.updateWorkflowSpecification(specId, newSpec).subscribe(spec => {
this.snackBar.open('Saved changes to workflow spec.', 'Ok', {duration: 3000});
this._loadWorkflowSpecs();
});
this._updateWorkflowSpec(specId, newSpec);
} else {
// Add new workflow spec and file
this.api.addWorkflowSpecification(newSpec).subscribe(spec => {
this.snackBar.open('Saved new workflow spec.', 'Ok', {duration: 3000});
this._loadWorkflowSpecs();
});
this._addWorkflowSpec(newSpec);
}
}
}
private _updateWorkflowSpec(specId: string, newSpec: WorkflowSpec) {
this.api.updateWorkflowSpecification(specId, newSpec).subscribe(spec => {
this._loadWorkflowSpecs();
this._displayMessage('Saved changes to workflow spec.');
});
}
private _addWorkflowSpec(newSpec: WorkflowSpec) {
this.api.addWorkflowSpecification(newSpec).subscribe(spec => {
this._loadWorkflowSpecs();
this._displayMessage('Saved new workflow spec.');
});
}
private _deleteWorkflowSpec(workflowSpec: WorkflowSpec) {
this.api.deleteWorkflowSpecification(workflowSpec.id).subscribe(() => {
this._loadWorkflowSpecs();
this.snackBar.open(`Deleted workflow spec ${workflowSpec.name}.`, 'Ok', {duration: 3000});
this._displayMessage(`Deleted workflow spec ${workflowSpec.name}.`);
});
}
private _displayMessage(message: string) {
this.snackBar.open(message, 'Ok', {duration: 3000});
}
}