|
1 | 1 | import {expect} from 'chai';
|
2 |
| -import {deepAssign} from '../../../lib/utils/object'; |
| 2 | +import {unique} from '../../../lib/utils/array'; |
3 | 3 |
|
4 | 4 | /* tslint:disable:max-classes-per-file */
|
5 | 5 |
|
6 | 6 | describe('utils', () => {
|
7 | 7 |
|
8 |
| - describe('object', () => { |
| 8 | + describe('array', () => { |
9 | 9 |
|
10 |
| - describe('deepAssign', () => { |
| 10 | + describe('unique', () => { |
11 | 11 |
|
12 |
| - const childSourceF = {}; |
13 |
| - const childSourceA = {f: childSourceF}; |
14 |
| - const childSourceB = {}; |
15 |
| - const source1 = {a: childSourceA, b: childSourceB, c: 1, d: 'd', over: 'ride', regex: /reg/gim}; |
16 |
| - const source2 = {e: 'für elisa', g: () => null, arr: [{h: 1}, {}, 'e'], over: 'ridden'}; |
17 |
| - const sourceKeys = [].concat(Object.keys(source1), Object.keys(source2)); |
| 12 | + const duplicates = [1, 'a', 'b', 1, 'a', 'c', 2, 'd', 'b', 2, 3, 'd', 'b']; |
18 | 13 |
|
19 |
| - it('should not be undefined', () => { |
| 14 | + it('should not throw', () => { |
20 | 15 |
|
21 |
| - const copy = deepAssign({}, source1, source2); |
22 |
| - |
23 |
| - expect(copy).not.to.be.undefined; |
24 |
| - }); |
25 |
| - |
26 |
| - it('should have all keys of sources', () => { |
27 |
| - |
28 |
| - const copy = deepAssign({}, source1, source2); |
29 |
| - |
30 |
| - sourceKeys |
31 |
| - .forEach(key => expect(copy).to.have.property(key)) |
32 |
| - ; |
33 |
| - }); |
34 |
| - |
35 |
| - it('should override previous properties', () => { |
36 |
| - |
37 |
| - const copy = deepAssign({}, source1, source2); |
38 |
| - |
39 |
| - expect(copy).to.have.property('over', 'ridden'); |
40 |
| - }); |
41 |
| - |
42 |
| - it('should have all primitive & function values of sources', () => { |
43 |
| - |
44 |
| - const copy = deepAssign({}, source1, source2); |
45 |
| - |
46 |
| - sourceKeys |
47 |
| - .forEach(key => { |
48 |
| - |
49 |
| - if (typeof copy[key] !== 'object') { |
50 |
| - |
51 |
| - expect(copy[key]).to.equal(source2[key] || source1[key]); |
52 |
| - } |
53 |
| - }) |
54 |
| - ; |
| 16 | + expect(() => unique(duplicates)).not.to.throw(); |
55 | 17 | });
|
56 | 18 |
|
57 |
| - it('should have copies of all non-primitive values of sources', () => { |
58 |
| - |
59 |
| - const copy = deepAssign({}, source1, source2); |
60 |
| - |
61 |
| - sourceKeys |
62 |
| - .forEach(key => { |
63 |
| - |
64 |
| - if (typeof copy[key] === 'object') { |
65 |
| - |
66 |
| - expect(copy[key]).not.to.equal(source1[key] || source2[key]); |
67 |
| - expect(copy[key]).to.eql(source1[key] || source2[key]); |
68 |
| - } |
69 |
| - }) |
70 |
| - ; |
71 |
| - }); |
72 |
| - |
73 |
| - it('should have copies of child source', () => { |
74 |
| - |
75 |
| - const copy = deepAssign({}, source1, source2); |
76 |
| - |
77 |
| - expect(copy.a).to.have.property('f'); |
78 |
| - expect(copy.a.f).to.not.equal(source1.a.f); |
79 |
| - expect(copy.a.f).to.eql(source1.a.f); |
80 |
| - }); |
81 |
| - |
82 |
| - it('should have copy of array items', () => { |
83 |
| - |
84 |
| - const copy = deepAssign({}, source1, source2); |
85 |
| - |
86 |
| - expect(copy.arr).to.be.an('array'); |
87 |
| - |
88 |
| - copy.arr.forEach((value, index) => { |
89 |
| - |
90 |
| - const isObject = typeof value === 'object'; |
91 |
| - |
92 |
| - if (isObject) { |
93 |
| - expect(value).not.to.equal(source2.arr[index]); |
94 |
| - expect(value).to.eql(source2.arr[index]); |
| 19 | + it('should remove duplicates from array', () => { |
95 | 20 |
|
96 |
| - } else { |
| 21 | + const unified = unique(duplicates); |
97 | 22 |
|
98 |
| - expect(value).to.equal(source2.arr[index]); |
99 |
| - } |
100 |
| - }); |
| 23 | + expect(unified).to.have.property('length', 7); |
101 | 24 | });
|
102 | 25 |
|
103 | 26 | });
|
|
0 commit comments