adding testing to multi select service.
Issue-ID: VID-722
Signed-off-by: Yoav Schneiderman <yoav.schneiderman@intl.att.com>
Change-Id: Ieb4ddb06a76f3d85b8227574ea519abedad85a09
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 0000000..3c3c344
--- /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 <MultiSelectItem> list', async(() => {
+ let control: MultiselectFormControl = <any>{
+ 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 = <any>{
+ 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 = <any>{
+ 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 = <any>{
+ 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 = <any>{
+ 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 = <any>{
+ 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 = <any>{
+ 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 4a95805..0b50f4d 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 @@
@Injectable()
export class MultiselectFormControlService {
- convertOriginalItems = (data : MultiselectFormControl) : Promise<MultiSelectItem[]> => {
+ convertOriginalItems = (control : MultiselectFormControl) : Promise<MultiSelectItem[]> => {
return new Promise<MultiSelectItem[]>((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 @@
}, {});
};
- convertSelectedItems(data : MultiselectFormControl) : Promise<MultiSelectItem[]>{
+ convertSelectedItems(control : MultiselectFormControl) : Promise<MultiSelectItem[]>{
return new Promise<MultiSelectItem[]>((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);