1
|
|
|
/** |
2
|
|
|
* Created by Emmy on 10/7/2017. |
3
|
|
|
*/ |
4
|
|
|
|
5
|
|
|
import * as Utility from '../../../../src/plugin/js/utilities' |
6
|
|
|
import should from 'should' |
7
|
|
|
|
8
|
|
|
describe('Utility #cloneObj', function () { |
9
|
|
|
it('The problem: Altering an object "objA" affects "objB"', function () { |
10
|
|
|
let objA = {foo: 'abc', bar: '123'}; |
11
|
|
|
let objB = objA; |
12
|
|
|
objA.bar = '1234'; |
13
|
|
|
should(objA).be.exactly(objB) |
14
|
|
|
}) |
15
|
|
|
|
16
|
|
|
it('The solution: cloned object should not be equal to original after altering', function () { |
17
|
|
|
let objA = {foo: 'abc', bar: '123'}; |
18
|
|
|
let objB = Utility.cloneObj(objA); |
19
|
|
|
objA.bar = '1234'; |
20
|
|
|
should(objA).not.be.exactly(objB) |
21
|
|
|
}) |
22
|
|
|
}) |
23
|
|
|
|
24
|
|
|
describe('Utility #mergeObjs', function () { |
25
|
|
|
it('"objC" should be "objA" and "objB"', function () { |
26
|
|
|
let objA = {foo: 'abc'}; |
27
|
|
|
let objB = {bar: '123'}; |
28
|
|
|
let objC = Utility.mergeObjs(objA, objB); |
29
|
|
|
|
30
|
|
|
should(objC).have.keys('foo', 'bar') |
31
|
|
|
}) |
32
|
|
|
}) |
33
|
|
|
|
34
|
|
|
describe('Utility #firstIndex', function () { |
35
|
|
|
let arr = [{color: 'yellow'}, {color: 'red'}, {color: 'blue'}] |
36
|
|
|
|
37
|
|
|
it('First index of red should be 1', function () { |
38
|
|
|
should(Utility.firstIndex(arr, 'red', 'color')).be.exactly(1).and.a.Number() |
39
|
|
|
}) |
40
|
|
|
|
41
|
|
|
it('First index of yellow should be 0', function () { |
42
|
|
|
should(Utility.firstIndex(arr, 'yellow', 'color')).be.exactly(0).and.a.Number() |
43
|
|
|
}) |
44
|
|
|
|
45
|
|
|
it('First index of blue should be 0', function () { |
46
|
|
|
should(Utility.firstIndex(arr, 'blue', 'color')).be.exactly(2).and.a.Number() |
47
|
|
|
}) |
48
|
|
|
|
49
|
|
|
it('First index of black should be undefined', function () { |
50
|
|
|
should(Utility.firstIndex(arr, 'black', 'color')).be.exactly(-1).and.a.Number() |
51
|
|
|
}) |
52
|
|
|
}) |