Passed
Pull Request — master (#24)
by Eric
01:06
created

describe(ꞌutilities.toObjectꞌ)   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 84

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 84
rs 8.7169
c 2
b 0
f 2

5 Functions

Rating   Name   Duplication   Size   Complexity  
A ��) 0 14 1
A ��) 0 13 1
A ��) 0 13 1
A ��) 0 11 1
A ��) 0 11 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
var utilities = require('../utilities.js');
2
3
describe('utilities.toObject', function() {
4
    describe('failing tests', function() {
5
        it("should fail because the arr parameter is undefined", function() {
6
            expect(utilities.toObject).toThrowError(TypeError);
7
        });
8
9
        it("should fail because the arr parameter is not an Array", function() {
10
            expect(utilities.toObject.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(utilities.toObject.bind(null, [], {})).toThrowError(TypeError);
15
        });
16
    });
17
18
    describe('array of primitives toObject', function() {
19
        // test example from README.md
20
        var states = ['Sachsen', 'Sachsen-Anhalt', 'Berlin', 'Hamburg'];
21
        var statesObject = utilities.toObject(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 toObject - string mapBy', function() {
50
        var newsObject = utilities.toObject(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 toObject - array mapBy', function() {
62
        var newsObject = utilities.toObject(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 toObject - function mapBy', function() {
74
        var newsObject = utilities.toObject(news, function(val, i, arr) {
0 ignored issues
show
Unused Code introduced by
The parameter arr is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
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