constructor(public modalService: BsModalService) {}
openCtrlDriven() {
- this.ctrlRef = this.modalService.show(DeletionModalComponent);
- this.ctrlRef.content.setUp({
- metaType: 'Controller delete handling',
- deletionMethod: this.fakeDeleteController.bind(this),
- description: this.ctrlDescription,
- modalRef: this.ctrlRef
+ this.ctrlRef = this.modalService.show(DeletionModalComponent, {
+ initialState: {
+ submitAction: this.fakeDeleteController.bind(this),
+ bodyTemplate: this.ctrlDescription
+ }
});
}
openModalDriven() {
- this.modalRef = this.modalService.show(DeletionModalComponent);
- this.modalRef.content.setUp({
- metaType: 'Modal delete handling',
- deletionObserver: this.fakeDelete(),
- description: this.modalDescription,
- modalRef: this.modalRef
+ this.modalRef = this.modalService.show(DeletionModalComponent, {
+ initialState: {
+ submitActionObservable: this.fakeDelete(),
+ bodyTemplate: this.modalDescription
+ }
});
}
configureTestBed({
declarations: [MockComponent, DeletionModalComponent],
schemas: [NO_ERRORS_SCHEMA],
- imports: [ModalModule.forRoot(), ReactiveFormsModule, MockModule, DirectivesModule]
+ imports: [ModalModule.forRoot(), ReactiveFormsModule, MockModule, DirectivesModule],
+ providers: [BsModalRef]
});
beforeEach(() => {
mockFixture = TestBed.createComponent(MockComponent);
mockComponent = mockFixture.componentInstance;
// Mocking the modals as a lot would be left over
- spyOn(mockComponent.modalService, 'show').and.callFake(() => {
+ spyOn(mockComponent.modalService, 'show').and.callFake((modalComp, config) => {
const ref = new BsModalRef();
fixture = TestBed.createComponent(DeletionModalComponent);
component = fixture.componentInstance;
+ if (config.initialState) {
+ component = Object.assign(component, config.initialState);
+ }
fixture.detectChanges();
ref.content = component;
return ref;
});
it('should focus the checkbox form field', () => {
- fixture = TestBed.createComponent(DeletionModalComponent);
fixture.detectChanges();
fixture.whenStable().then(() => {
const focused = fixture.debugElement.query(By.css(':focus'));
});
});
- describe('setUp', () => {
- const clearSetup = () => {
- component.metaType = undefined;
- component.deletionObserver = undefined;
- component.description = undefined;
- component.modalRef = undefined;
- };
-
- const expectSetup = (metaType, observer: boolean, method: boolean, template: boolean) => {
- expect(component.modalRef).toBeTruthy();
- expect(component.metaType).toBe(metaType);
- expect(!!component.deletionObserver).toBe(observer);
- expect(!!component.deletionMethod).toBe(method);
- expect(!!component.description).toBe(template);
- };
-
- beforeEach(() => {
- clearSetup();
- });
-
- it('should throw error if no modal reference is given', () => {
- expect(() =>
- component.setUp({
- metaType: undefined,
- modalRef: undefined
- })
- ).toThrowError('No modal reference');
- });
-
- it('should throw error if no meta type is given', () => {
- expect(() =>
- component.setUp({
- metaType: undefined,
- modalRef: mockComponent.ctrlRef
- })
- ).toThrowError('No meta type');
- });
-
- it('should throw error if no deletion method is given', () => {
- expect(() =>
- component.setUp({
- metaType: 'Sth',
- modalRef: mockComponent.ctrlRef
- })
- ).toThrowError('No deletion method');
- });
-
- it('should throw no errors if metaType, modalRef and a deletion method were given', () => {
- component.setUp({
- metaType: 'Observer',
- modalRef: mockComponent.ctrlRef,
- deletionObserver: mockComponent.fakeDelete()
- });
- expectSetup('Observer', true, false, false);
- clearSetup();
- component.setUp({
- metaType: 'Controller',
- modalRef: mockComponent.ctrlRef,
- deletionMethod: mockComponent.fakeDeleteController
- });
- expectSetup('Controller', false, true, false);
- });
-
- it('should test optional parameters - description', () => {
- component.setUp({
- metaType: 'Description only',
- modalRef: mockComponent.ctrlRef,
- deletionObserver: mockComponent.fakeDelete(),
- description: mockComponent.modalDescription
- });
- expectSetup('Description only', true, false, true);
+ it('should throw an error if no action is defined', () => {
+ component = Object.assign(component, {
+ submitAction: null,
+ submitActionObservable: null
});
+ expect(() => component.ngOnInit()).toThrowError('No submit action defined');
});
it('should test if the ctrl driven mock is set correctly through mock component', () => {
- expect(component.metaType).toBe('Controller delete handling');
- expect(component.description).toBeTruthy();
- expect(component.modalRef).toBeTruthy();
- expect(component.deletionMethod).toBeTruthy();
- expect(component.deletionObserver).not.toBeTruthy();
+ expect(component.bodyTemplate).toBeTruthy();
+ expect(component.submitAction).toBeTruthy();
+ expect(component.submitActionObservable).not.toBeTruthy();
});
it('should test if the modal driven mock is set correctly through mock component', () => {
mockComponent.openModalDriven();
- expect(component.metaType).toBe('Modal delete handling');
- expect(component.description).toBeTruthy();
- expect(component.modalRef).toBeTruthy();
- expect(component.deletionObserver).toBeTruthy();
- expect(component.deletionMethod).not.toBeTruthy();
+ expect(component.bodyTemplate).toBeTruthy();
+ expect(component.submitActionObservable).toBeTruthy();
+ expect(component.submitAction).not.toBeTruthy();
});
describe('component functions', () => {
describe('Controller driven', () => {
beforeEach(() => {
- spyOn(component, 'deletionMethod').and.callThrough();
+ spyOn(component, 'submitAction').and.callThrough();
spyOn(mockComponent.ctrlRef, 'hide').and.callThrough();
});
it('should test fake deletion that closes modal', <any>fakeAsync(() => {
// Before deletionCall
- expect(component.deletionMethod).not.toHaveBeenCalled();
+ expect(component.submitAction).not.toHaveBeenCalled();
// During deletionCall
- component.deletionCall();
+ component.callSubmitAction();
expect(component.stopLoadingSpinner).not.toHaveBeenCalled();
expect(component.hideModal).not.toHaveBeenCalled();
expect(mockComponent.ctrlRef.hide).not.toHaveBeenCalled();
- expect(component.deletionMethod).toHaveBeenCalled();
+ expect(component.submitAction).toHaveBeenCalled();
expect(mockComponent.finished).toBe(undefined);
// After deletionCall
tick(2000);
describe('Modal driven', () => {
beforeEach(() => {
mockComponent.openModalDriven();
- spyOn(mockComponent.modalRef, 'hide').and.callThrough();
spyOn(component, 'stopLoadingSpinner').and.callThrough();
spyOn(component, 'hideModal').and.callThrough();
spyOn(mockComponent, 'fakeDelete').and.callThrough();
it('should delete and close modal', <any>fakeAsync(() => {
// During deletionCall
- component.deletionCall();
+ component.callSubmitAction();
expect(mockComponent.finished).toBe(undefined);
expect(component.hideModal).not.toHaveBeenCalled();
- expect(mockComponent.modalRef.hide).not.toHaveBeenCalled();
// After deletionCall
tick(2000);
expect(mockComponent.finished).toEqual([6, 7, 8, 9]);
- expect(mockComponent.modalRef.hide).toHaveBeenCalled();
expect(component.stopLoadingSpinner).not.toHaveBeenCalled();
expect(component.hideModal).toHaveBeenCalled();
}));
export class DeletionModalComponent implements OnInit {
@ViewChild(SubmitButtonComponent)
submitButton: SubmitButtonComponent;
- description: TemplateRef<any>;
- metaType: string;
- deletionObserver: () => Observable<any>;
- deletionMethod: Function;
- modalRef: BsModalRef;
-
+ bodyTemplate: TemplateRef<any>;
+ submitActionObservable: () => Observable<any>;
+ submitAction: Function;
deletionForm: CdFormGroup;
+ itemDescription: 'entry';
+ actionDescription = 'delete';
- // Parameters are destructed here than assigned to specific types and marked as optional
- setUp({
- modalRef,
- metaType,
- deletionMethod,
- deletionObserver,
- description
- }: {
- modalRef: BsModalRef;
- metaType: string;
- deletionMethod?: Function;
- deletionObserver?: () => Observable<any>;
- description?: TemplateRef<any>;
- }) {
- if (!modalRef) {
- throw new Error('No modal reference');
- } else if (!metaType) {
- throw new Error('No meta type');
- } else if (!(deletionMethod || deletionObserver)) {
- throw new Error('No deletion method');
- }
- this.metaType = metaType;
- this.modalRef = modalRef;
- this.deletionMethod = deletionMethod;
- this.deletionObserver = deletionObserver;
- this.description = description;
- }
+ constructor(public modalRef: BsModalRef) {}
ngOnInit() {
this.deletionForm = new CdFormGroup({
confirmation: new FormControl(false, [Validators.requiredTrue])
});
+
+ if (!(this.submitAction || this.submitActionObservable)) {
+ throw new Error('No submit action defined');
+ }
}
- deletionCall() {
- if (this.deletionObserver) {
- this.deletionObserver().subscribe(
- undefined,
+ callSubmitAction() {
+ if (this.submitActionObservable) {
+ this.submitActionObservable().subscribe(
+ null,
this.stopLoadingSpinner.bind(this),
this.hideModal.bind(this)
);
} else {
- this.deletionMethod();
+ this.submitAction();
}
}