Add service specs #9
					 5 changed files with 112 additions and 10 deletions
				
			
		|  | @ -1,15 +1,40 @@ | ||||||
| import { TestBed, inject } from '@angular/core/testing'; | import {inject, TestBed} from '@angular/core/testing'; | ||||||
| 
 | 
 | ||||||
| import {ConverterRegistryService} from './converter-registry.service'; | import {ConverterRegistryService} from './converter-registry.service'; | ||||||
|  | import {NativeLibraryWrapperService} from './native-library-wrapper.service'; | ||||||
|  | import {Converter} from './converter/converter'; | ||||||
|  | import {Base64Decoder} from './converter/base64-decoder'; | ||||||
|  | import createSpyObj = jasmine.createSpyObj; | ||||||
| 
 | 
 | ||||||
| describe('ConverterRegistryService', () => { | describe('ConverterRegistryService', () => { | ||||||
|   beforeEach(() => { |   beforeEach(() => { | ||||||
|     TestBed.configureTestingModule({ |     TestBed.configureTestingModule({ | ||||||
|       providers: [ConverterRegistryService] |       providers: [ | ||||||
|  |         ConverterRegistryService, | ||||||
|  |         {provide: NativeLibraryWrapperService, useValue: createSpyObj(['punycode', 'quotedPrintable', 'utf8'])} | ||||||
|  |       ] | ||||||
|     }); |     }); | ||||||
|   }); |   }); | ||||||
| 
 | 
 | ||||||
|   it('should be created', inject([ConverterRegistryService], (service: ConverterRegistryService) => { |   it('should be created', inject([ConverterRegistryService], (service: ConverterRegistryService) => { | ||||||
|     expect(service).toBeTruthy(); |     expect(service).toBeTruthy(); | ||||||
|   })); |   })); | ||||||
|  | 
 | ||||||
|  |   it('should register converters upon creation', inject([ConverterRegistryService], (service: ConverterRegistryService) => { | ||||||
|  |     expect(service.getAllConverters()).toBeTruthy(); | ||||||
|  |     expect(service.getAllConverters().length).toBeGreaterThan(0); | ||||||
|  |   })); | ||||||
|  | 
 | ||||||
|  |   it('must not allow the same converter ID to be regisgered more than once', | ||||||
|  |     inject([ConverterRegistryService], (service: ConverterRegistryService) => { | ||||||
|  |       // arrange
 | ||||||
|  |       const duplicateConverter: Converter = new Base64Decoder(); | ||||||
|  |       const duplicateConverterId = duplicateConverter.getId(); | ||||||
|  |       expect(() => { | ||||||
|  |         // act
 | ||||||
|  |         (service as any).registerConverter(duplicateConverter); | ||||||
|  |       }) | ||||||
|  |       // assert
 | ||||||
|  |         .toThrowError(`Converter-ID ${duplicateConverterId} is already registered!`); | ||||||
|  |     })); | ||||||
| }); | }); | ||||||
|  |  | ||||||
|  | @ -1,6 +1,7 @@ | ||||||
| import { TestBed, inject } from '@angular/core/testing'; | import {inject, TestBed} from '@angular/core/testing'; | ||||||
| 
 | 
 | ||||||
| import {InputComponentManagerService} from './input-component-manager.service'; | import {InputComponentManagerService} from './input-component-manager.service'; | ||||||
|  | import {Step} from './step'; | ||||||
| 
 | 
 | ||||||
| describe('InputComponentManagerService', () => { | describe('InputComponentManagerService', () => { | ||||||
|   beforeEach(() => { |   beforeEach(() => { | ||||||
|  | @ -12,4 +13,59 @@ describe('InputComponentManagerService', () => { | ||||||
|   it('should be created', inject([InputComponentManagerService], (service: InputComponentManagerService) => { |   it('should be created', inject([InputComponentManagerService], (service: InputComponentManagerService) => { | ||||||
|     expect(service).toBeTruthy(); |     expect(service).toBeTruthy(); | ||||||
|   })); |   })); | ||||||
|  | 
 | ||||||
|  |   it('should create a component if requesting the first one', | ||||||
|  |     inject([InputComponentManagerService], (service: InputComponentManagerService) => { | ||||||
|  |       const firstStep: Step = service.getFirst(); | ||||||
|  |       expect(firstStep).toBeTruthy(); | ||||||
|  |       expect(service.getFirst()).toBe(firstStep); | ||||||
|  |       expect(service.getAllComponents().length).toEqual(1); | ||||||
|  |     }) | ||||||
|  |   ); | ||||||
|  | 
 | ||||||
|  |   it('must not add a null Step', inject([InputComponentManagerService], (service: InputComponentManagerService) => { | ||||||
|  |     expect(() => service.register(null)).toThrowError(); | ||||||
|  |     expect(service.getAllComponents().length).toEqual(0); | ||||||
|  |   })); | ||||||
|  | 
 | ||||||
|  |   it('must not add an undefined Step', inject([InputComponentManagerService], (service: InputComponentManagerService) => { | ||||||
|  |     expect(() => service.register(undefined)).toThrowError(); | ||||||
|  |     expect(service.getAllComponents().length).toEqual(0); | ||||||
|  |   })); | ||||||
|  | 
 | ||||||
|  |   it('should register new Steps', inject([InputComponentManagerService], (service: InputComponentManagerService) => { | ||||||
|  |     service.register(new Step(99)); | ||||||
|  |     expect(service.getAllComponents().length).toEqual(1); | ||||||
|  |     service.register(new Step(100)); | ||||||
|  |     expect(service.getAllComponents().length).toEqual(2); | ||||||
|  |   })); | ||||||
|  | 
 | ||||||
|  |   it('should return a new step if the next step doesn\'t exist', | ||||||
|  |     inject([InputComponentManagerService], (service: InputComponentManagerService) => { | ||||||
|  |       // arrange
 | ||||||
|  |       const firstStep: Step = new Step(0); | ||||||
|  |       service.register(firstStep); | ||||||
|  | 
 | ||||||
|  |       // act
 | ||||||
|  |       const nextStep: Step = service.getNext(firstStep); | ||||||
|  | 
 | ||||||
|  |       // assert
 | ||||||
|  |       expect(service.getAllComponents().length).toEqual(2); | ||||||
|  |       expect(nextStep.index).toEqual(1); | ||||||
|  |     }) | ||||||
|  |   ); | ||||||
|  | 
 | ||||||
|  |   it('should return the next step if requested', inject([InputComponentManagerService], (service: InputComponentManagerService) => { | ||||||
|  |     // arrange
 | ||||||
|  |     const firstStep: Step = new Step(0); | ||||||
|  |     const nextStep: Step = new Step(1); | ||||||
|  |     service.register(firstStep); | ||||||
|  |     service.register(nextStep); | ||||||
|  | 
 | ||||||
|  |     // act
 | ||||||
|  |     const requestedStep: Step = service.getNext(firstStep); | ||||||
|  |     // assert
 | ||||||
|  |     expect(service.getAllComponents().length).toEqual(2); | ||||||
|  |     expect(requestedStep).toEqual(nextStep); | ||||||
|  |   })); | ||||||
| }); | }); | ||||||
|  |  | ||||||
|  | @ -11,6 +11,9 @@ export class InputComponentManagerService { | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   public register(component: Step): void { |   public register(component: Step): void { | ||||||
|  |     if (!component) { | ||||||
|  |       throw new Error('component to add must not be empty or undefined'); | ||||||
|  |     } | ||||||
|     this.components.push(component); |     this.components.push(component); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1,4 +1,4 @@ | ||||||
| import { TestBed, inject } from '@angular/core/testing'; | import {inject, TestBed} from '@angular/core/testing'; | ||||||
| 
 | 
 | ||||||
| import {NativeLibraryWrapperService} from './native-library-wrapper.service'; | import {NativeLibraryWrapperService} from './native-library-wrapper.service'; | ||||||
| 
 | 
 | ||||||
|  | @ -12,4 +12,22 @@ describe('NativeLibraryWrapperService', () => { | ||||||
|   it('should be created', inject([NativeLibraryWrapperService], (service: NativeLibraryWrapperService) => { |   it('should be created', inject([NativeLibraryWrapperService], (service: NativeLibraryWrapperService) => { | ||||||
|     expect(service).toBeTruthy(); |     expect(service).toBeTruthy(); | ||||||
|   })); |   })); | ||||||
|  | 
 | ||||||
|  |   it('should convert punycode', inject([NativeLibraryWrapperService], (service: NativeLibraryWrapperService) => { | ||||||
|  |     expect(service.punycode).toBeTruthy(); | ||||||
|  |     expect(service.punycode.encode('bärneruhr')).toEqual('brneruhr-0za'); | ||||||
|  |     expect(service.punycode.decode('brneruhr-0za')).toEqual('bärneruhr'); | ||||||
|  |   })); | ||||||
|  | 
 | ||||||
|  |   it('should convert utf8', inject([NativeLibraryWrapperService], (service: NativeLibraryWrapperService) => { | ||||||
|  |     expect(service.utf8).toBeTruthy(); | ||||||
|  |     expect(service.utf8.encode('bärneruhr')).toEqual('bärneruhr'); | ||||||
|  |     expect(service.utf8.decode('bärneruhr')).toEqual('bärneruhr'); | ||||||
|  |   })); | ||||||
|  | 
 | ||||||
|  |   it('should convert quoted printable', inject([NativeLibraryWrapperService], (service: NativeLibraryWrapperService) => { | ||||||
|  |     expect(service.quotedPrintable).toBeTruthy(); | ||||||
|  |     expect(service.quotedPrintable.encode('bärneruhr')).toEqual('b=E4rneruhr'); | ||||||
|  |     expect(service.quotedPrintable.decode('b=E4rneruhr')).toEqual('bärneruhr'); | ||||||
|  |   })); | ||||||
| }); | }); | ||||||
|  |  | ||||||
|  | @ -7,9 +7,9 @@ import * as NativeUtf8 from 'utf8'; | ||||||
|   providedIn: 'root' |   providedIn: 'root' | ||||||
| }) | }) | ||||||
| export class NativeLibraryWrapperService { | export class NativeLibraryWrapperService { | ||||||
|   public utf8: Utf8; |   public readonly utf8: Utf8; | ||||||
|   public quotedPrintable: QuotedPrintable; |   public readonly quotedPrintable: QuotedPrintable; | ||||||
|   public punycode: Punycode; |   public readonly punycode: Punycode; | ||||||
| 
 | 
 | ||||||
|   constructor() { |   constructor() { | ||||||
|     this.utf8 = NativeUtf8; |     this.utf8 = NativeUtf8; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue