From 0b3965e87f9d7c42f3c20b281d541a4a4666de8a Mon Sep 17 00:00:00 2001 From: Yoav Schneiderman Date: Thu, 28 Nov 2019 16:25:05 +0200 Subject: adding testing to multi select service. Issue-ID: VID-722 Signed-off-by: Yoav Schneiderman Change-Id: Ieb4ddb06a76f3d85b8227574ea519abedad85a09 --- .../multiselect.formControl.service.spec.ts | 159 +++++++++++++++++++++ .../multiselect/multiselect.formControl.service.ts | 18 +-- 2 files changed, 168 insertions(+), 9 deletions(-) create mode 100644 vid-webpack-master/src/app/shared/components/formControls/component/multiselect/multiselect.formControl.service.spec.ts (limited to 'vid-webpack-master/src/app/shared') diff --git a/vid-webpack-master/src/app/shared/components/formControls/component/multiselect/multiselect.formControl.service.spec.ts b/vid-webpack-master/src/app/shared/components/formControls/component/multiselect/multiselect.formControl.service.spec.ts new file mode 100644 index 000000000..3c3c344a3 --- /dev/null +++ b/vid-webpack-master/src/app/shared/components/formControls/component/multiselect/multiselect.formControl.service.spec.ts @@ -0,0 +1,159 @@ +import {async, getTestBed, TestBed} from '@angular/core/testing'; +import {MultiselectFormControlService} from "./multiselect.formControl.service"; +import {MultiselectFormControl} from "../../../../models/formControlModels/multiselectFormControl.model"; +import {MultiSelectItem} from "./multiselect.model"; + +describe('Multi Select Form Control Service', () => { + + let injector; + let service: MultiselectFormControlService; + + beforeAll(done => (async () => { + TestBed.configureTestingModule({ + imports: [], + providers: [MultiselectFormControlService] + }); + await TestBed.compileComponents(); + injector = getTestBed(); + service = injector.get(MultiselectFormControlService); + })().then(done).catch(done.fail)); + + + const options = [ + { + id: 'A', + name: 'a' + }, + { + id: 'B', + name: 'b', + keepMe: -42 + }, + { + id: 'C', + name: 'c' + } + ], + selectedFieldName = 'name', + ngValue = 'id'; + + + test('convertOriginalItems should convert options array to list', async(() => { + let control: MultiselectFormControl = { + ngValue, + selectedFieldName, + options$: options + }; + + service.convertOriginalItems(control).then((result: MultiSelectItem[]) => { + expect(result).toEqual([ + {"id": 1, "itemId": 'A', "itemName": 'a'}, + {"id": 2, "itemId": 'B', "itemName": 'b'}, + {"id": 3, "itemId": 'C', "itemName": 'c'} + ]); + }); + })); + + test('convertOriginalItems should return empty list when options list is empty', async(() => { + let control: MultiselectFormControl = { + ngValue, + selectedFieldName, + options$: [] + }; + + service.convertOriginalItems(control).then((result) => { + expect(result).toEqual([]); + }); + })); + + test('convertOptionsToHashMap - should convert any object to hash map with ngValue', async(() => { + + let control: MultiselectFormControl = { + ngValue, + selectedFieldName, + options$: options + }; + + let map = service.convertOptionsToHashMap(control); + + expect(Object.keys(map)).toHaveLength(3); + expect(map).toEqual({ + 'A': { + id: 'A', + name: 'a', + index: 1 + }, + 'B': { + id: 'B', + name: 'b', + keepMe: -42, + index: 2 + }, + 'C': { + id: 'C', + name: 'c', + index: 3 + } + + }) + })); + + test('convertOptionsToHashMap - should convert any object to hash map with ngValue: empty options', async(() => { + let control: MultiselectFormControl = { + ngValue, + selectedFieldName, + options$: [] + }; + + let map = service.convertOptionsToHashMap(control); + + expect(Object.keys(map)).toHaveLength(0) + })); + + test('convertSelectedItems - should convert select item to multi select list', async(() => { + let control: MultiselectFormControl = { + ngValue, + selectedFieldName, + options$: options, + value: ['A', 'C'] + }; + + service.convertSelectedItems(control).then((selectedOptions) => { + expect(selectedOptions).toHaveLength(2); + expect(selectedOptions[0].itemName).toEqual('a'); + expect(selectedOptions[1].itemName).toEqual('c'); + }) + })); + + test('convertSelectedItems - should convert select item to multi select list with special convert function', async(() => { + let control: MultiselectFormControl = { + ngValue, + selectedFieldName, + options$: options, + value: 'A,C', + convertOriginalDataToArray: (value) => { + return value.split(','); + } + }; + + service.convertSelectedItems(control).then((selectedOptions) => { + expect(selectedOptions).toHaveLength(2); + expect(selectedOptions[0].itemName).toEqual('a'); + expect(selectedOptions[1].itemName).toEqual('c'); + }) + })); + + + test('convertSelectedItems - should return empty list iof value is empty list', async(() => { + let control: MultiselectFormControl = { + ngValue, + selectedFieldName, + options$: options, + value: [] + }; + + service.convertSelectedItems(control).then((selectedOptions) => { + expect(selectedOptions).toHaveLength(0); + }) + })); +}); diff --git a/vid-webpack-master/src/app/shared/components/formControls/component/multiselect/multiselect.formControl.service.ts b/vid-webpack-master/src/app/shared/components/formControls/component/multiselect/multiselect.formControl.service.ts index 4a9580563..0b50f4d28 100644 --- a/vid-webpack-master/src/app/shared/components/formControls/component/multiselect/multiselect.formControl.service.ts +++ b/vid-webpack-master/src/app/shared/components/formControls/component/multiselect/multiselect.formControl.service.ts @@ -7,13 +7,13 @@ import * as _ from "lodash"; @Injectable() export class MultiselectFormControlService { - convertOriginalItems = (data : MultiselectFormControl) : Promise => { + convertOriginalItems = (control : MultiselectFormControl) : Promise => { return new Promise((resolve) =>{ let result: MultiSelectItem[] = []; - if(data.options$) { + if(control.options$) { let index: number = 1; - data.options$.map((originalItems: any) => { - result.push(new MultiSelectItem(index, originalItems[data.ngValue], originalItems[data.selectedFieldName])); + control.options$.map((originalItems: any) => { + result.push(new MultiSelectItem(index, originalItems[control.ngValue], originalItems[control.selectedFieldName])); index++; }); } @@ -32,16 +32,16 @@ export class MultiselectFormControlService { }, {}); }; - convertSelectedItems(data : MultiselectFormControl) : Promise{ + convertSelectedItems(control : MultiselectFormControl) : Promise{ return new Promise((resolve) =>{ let result: MultiSelectItem[] = []; - const hashMap = this.convertOptionsToHashMap(data); + const hashMap = this.convertOptionsToHashMap(control); - if(data.options$ && data.value) { - const convertArray = data.convertOriginalDataToArray ? data.convertOriginalDataToArray(data.value) : data.value; + if(control.options$ && control.value) { + const convertArray = control.convertOriginalDataToArray ? control.convertOriginalDataToArray(control.value) : control.value; convertArray.map((itemId) => { const uniqueIdentifier = itemId.trim(); - result.push(new MultiSelectItem(hashMap[uniqueIdentifier].index, hashMap[uniqueIdentifier][data.ngValue], hashMap[uniqueIdentifier][data.selectedFieldName])); + result.push(new MultiSelectItem(hashMap[uniqueIdentifier].index, hashMap[uniqueIdentifier][control.ngValue], hashMap[uniqueIdentifier][control.selectedFieldName])); }); } resolve(result); -- cgit 1.2.3-korg