1
|
|
|
var arrayToObject = require('../src/arrayToObject.js'); |
2
|
|
|
|
3
|
|
|
describe('arrayToObject', function() { |
4
|
|
|
describe('failing tests', function() { |
5
|
|
|
it("should fail because the arr parameter is undefined", function() { |
6
|
|
|
expect(arrayToObject).toThrowError(TypeError); |
7
|
|
|
}); |
8
|
|
|
|
9
|
|
|
it("should fail because the arr parameter is not an Array", function() { |
10
|
|
|
expect(arrayToObject.bind(null, {})).toThrowError(TypeError); |
11
|
|
|
}); |
12
|
|
|
|
13
|
|
|
it("should fail because the mapBy not of type String, Array or Function and not falsy", function() { |
14
|
|
|
expect(arrayToObject.bind(null, [], {})).toThrowError(TypeError); |
15
|
|
|
}); |
16
|
|
|
}); |
17
|
|
|
|
18
|
|
|
describe('array of primitives arrayToObject', function() { |
19
|
|
|
// test example from README.md |
20
|
|
|
var states = ['Sachsen', 'Sachsen-Anhalt', 'Berlin', 'Hamburg']; |
21
|
|
|
var statesObject = arrayToObject(states); |
22
|
|
|
// end test example from README.md |
23
|
|
|
|
24
|
|
|
it("should be able to access object by index", function() { |
25
|
|
|
expect(statesObject[0]).toEqual('Sachsen'); |
26
|
|
|
}); |
27
|
|
|
|
28
|
|
|
it("should have 4 keys", function() { |
29
|
|
|
expect(Object.keys(statesObject).length).toEqual(4); |
30
|
|
|
}); |
31
|
|
|
}); |
32
|
|
|
|
33
|
|
|
var news = [ |
34
|
|
|
{ |
35
|
|
|
id: 12001, |
36
|
|
|
headline: 'Tiger goes limp', |
37
|
|
|
subHeadline: 'Pulls out after 9 holes' |
38
|
|
|
},{ |
39
|
|
|
id: 666, |
40
|
|
|
headline: 'Croc has beef with cow', |
41
|
|
|
subHeadline: '' |
42
|
|
|
},{ |
43
|
|
|
id: 1337, |
44
|
|
|
headline: 'Germans wurst at penalties', |
45
|
|
|
subHeadline: 'New stats prove England are better from the spot' |
46
|
|
|
} |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
describe('array of objects arrayToObject - string mapBy', function() { |
50
|
|
|
var newsObject = arrayToObject(news, 'id'); |
51
|
|
|
|
52
|
|
|
it("should be able to access object by ID", function() { |
53
|
|
|
expect(newsObject[1337].headline).toEqual('Germans wurst at penalties'); |
54
|
|
|
}); |
55
|
|
|
|
56
|
|
|
it("should have 3 keys", function() { |
57
|
|
|
expect(Object.keys(newsObject).length).toEqual(3); |
58
|
|
|
}); |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
|
describe('array of objects arrayToObject - array mapBy', function() { |
62
|
|
|
var newsObject = arrayToObject(news, ['id', 'id']); |
63
|
|
|
|
64
|
|
|
it("should be able to access object by ID_ID", function() { |
65
|
|
|
expect(newsObject['1337_1337'].headline).toEqual('Germans wurst at penalties'); |
66
|
|
|
}); |
67
|
|
|
|
68
|
|
|
it("should have 3 keys", function() { |
69
|
|
|
expect(Object.keys(newsObject).length).toEqual(3); |
70
|
|
|
}); |
71
|
|
|
}); |
72
|
|
|
|
73
|
|
|
describe('array of objects arrayToObject - function mapBy', function() { |
74
|
|
|
var newsObject = arrayToObject(news, function(val, i) { |
75
|
|
|
return val.id + '_' + i; |
76
|
|
|
}); |
77
|
|
|
|
78
|
|
|
it("should be able to access object by ID_index", function() { |
79
|
|
|
expect(newsObject['1337_2'].headline).toEqual('Germans wurst at penalties'); |
80
|
|
|
}); |
81
|
|
|
|
82
|
|
|
it("should have 3 keys", function() { |
83
|
|
|
expect(Object.keys(newsObject).length).toEqual(3); |
84
|
|
|
}); |
85
|
|
|
}); |
86
|
|
|
}); |
87
|
|
|
|