| /* |
| ****************************************************************************** |
| * Copyright © 2018 PTA GmbH. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| ****************************************************************************** |
| */ |
| |
| import { TestBed } from '@angular/core/testing'; |
| import { EntityValidator, ValidationFunc } from './entity-validator'; |
| import { SessionContext } from '../../common/session-context'; |
| import { ToasterMessageService } from '../../services/toaster-message.service'; |
| import { MessageService } from 'primeng/api'; |
| |
| class BoolValidator extends ValidationFunc<boolean> { |
| constructor(private result: boolean) { |
| super(); |
| } |
| |
| public validate(b: boolean, msgService: ToasterMessageService): boolean { |
| if (!this.result) { |
| msgService.showWarn('Test message: ' + b); |
| } |
| return this.result; |
| } |
| } |
| |
| describe('EntityValidator', () => { |
| let toasterMessageService: ToasterMessageService; |
| let sessionContext: SessionContext; |
| let messageService: MessageService; |
| |
| beforeEach(() => { |
| messageService = new MessageService; |
| sessionContext = new SessionContext(); |
| toasterMessageService = new ToasterMessageService(sessionContext, messageService); |
| |
| TestBed.configureTestingModule({ |
| }); |
| }); |
| |
| it('should work correctly with stop on first error', (() => { |
| spyOn(toasterMessageService, 'showWarn').and.callThrough(); |
| const validationList: ValidationFunc<boolean>[] = [ |
| new BoolValidator(true), |
| new BoolValidator(false), |
| new BoolValidator(false)]; |
| |
| const validator = new EntityValidator<boolean>(toasterMessageService, validationList); |
| validator.validateEntity(true, true); |
| |
| expect(toasterMessageService.showWarn).toHaveBeenCalledTimes(1); |
| |
| })); |
| |
| |
| it('should work correctly with dont stop on first error', (() => { |
| spyOn(toasterMessageService, 'showWarn').and.callThrough(); |
| const validationList: ValidationFunc<boolean>[] = [ |
| new BoolValidator(true), |
| new BoolValidator(false), |
| new BoolValidator(false)]; |
| |
| const validator = new EntityValidator<boolean>(toasterMessageService, validationList); |
| validator.validateEntity(true, false); |
| |
| expect(toasterMessageService.showWarn).toHaveBeenCalledTimes(2); |
| |
| })); |
| }); |