| /* |
| ****************************************************************************** |
| * 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, inject } from '@angular/core/testing'; |
| import { EntityValidator, ValidationFunc } from './entity-validator'; |
| import { MessageService } from '../../services/message.service'; |
| import { SessionContext } from '../../common/session-context'; |
| import { MessageScopeEn } from '../../common/enums'; |
| |
| class BoolValidator extends ValidationFunc< boolean > { |
| constructor( private result: boolean ) { |
| super(); |
| } |
| |
| public validate( b: boolean, msgService: MessageService ): boolean { |
| if ( !this.result ) { |
| msgService.emitWarning( 'Test message: ' + b, MessageScopeEn.local ); |
| } |
| return this.result; |
| } |
| } |
| |
| describe('EntityValidator', () => { |
| let messageService: MessageService; |
| let sessionContext: SessionContext; |
| |
| beforeEach(() => { |
| sessionContext = new SessionContext(); |
| messageService = new MessageService(sessionContext); |
| |
| TestBed.configureTestingModule({ |
| }); |
| }); |
| |
| it('should work correctly with stop on first error', (() => { |
| spyOn( messageService, 'emitWarning').and.callThrough(); |
| const validationList: ValidationFunc<boolean>[] = [ |
| new BoolValidator( true ), |
| new BoolValidator( false ), |
| new BoolValidator( false ) ]; |
| |
| const validator = new EntityValidator<boolean>(messageService, validationList); |
| validator.validateEntity( true, true ); |
| |
| expect( messageService.emitWarning ).toHaveBeenCalledTimes(1); |
| |
| })); |
| |
| |
| it('should work correctly with dont stop on first error', (() => { |
| spyOn( messageService, 'emitWarning').and.callThrough(); |
| const validationList: ValidationFunc<boolean>[] = [ |
| new BoolValidator( true ), |
| new BoolValidator( false ), |
| new BoolValidator( false ) ]; |
| |
| const validator = new EntityValidator<boolean>(messageService, validationList); |
| validator.validateEntity( true, false ); |
| |
| expect( messageService.emitWarning ).toHaveBeenCalledTimes(2); |
| |
| })); |
| }); |