summaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/shared/components/formControls/component/multiselect/multiselect.formControl.service.spec.ts
blob: 3c3c344a39cfa41d12520f621032b22d6e97f0b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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);
    })
  }));
});