aboutsummaryrefslogtreecommitdiffstats
path: root/moon_dashboard/moon/static/moon/js/util.service.spec.js
blob: d8e3ed31ea866b9d5bc6209947dcf9582f602b74 (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
(function () {
    'use strict';

    describe('moon.util.service', function () {
        var service;

        beforeEach(module('horizon.app.core'));
        beforeEach(module('horizon.framework'));
        beforeEach(module('moon'));

        beforeEach(inject(function ($injector) {
            service = $injector.get('moon.util.service');
        }));

        it('should push all', function () {
            var a1 = [0, 1, 2];
            var a2 = [3, 4];
            service.pushAll(a1, a2)

            expect(a1.length).toBe(5);
            expect(a1).toEqual([0, 1, 2, 3, 4]);
        });

        it('should index of', function () {
            var a = [{ name: 'n0' }, { name: 'n1' }, { name: 'n2' }];
            var result = service.indexOf(a, 'name', 'n1');

            expect(result).toBe(1);
        });

        it('should map to array', function () {
            var map = { "a": { name: "a" }, "b": { name: "b" } };
            var result = service.mapToArray(map);

            expect(result.length).toBe(2);
        });

        it('should map ID to item', function () {
            var map = { "a": { name: "a" }, "b": { name: "b" } };
            var array = ["a", "b"];
            service.mapIdToItem(array, map);

            expect(array.length).toBe(2);
            expect(array[0].name).toBe("a");
            expect(array[1].name).toBe("b");
        });

        it('should map item to ID', function () {
            var array = [{ id: "a" }, { id: "b" }];
            service.mapItemToId(array);

            expect(array.length).toBe(2);
            expect(array[0]).toBe("a");
            expect(array[1]).toBe("b");
        });

        it('should add to map', function () {
            var map = { "a": { name: "a" }, "b": { name: "b" } };
            var array = [{ id: "c" }];
            service.addToMap(array, map);

            expect(map.c).toEqual({ id: "c" });
        });

        it('should update object', function () {
            var object = { a: 1, b: "test" };
            var update = { a: 2, c: "test2" };
            service.updateObject(object, update);

            expect(object.a).toBe(2);
            expect(object.b).toBe("test");
            expect(object.c).toBe("test2");
        });

        it('should clean object', function () {
            var object = { a: 1, b: "test" };
            service.cleanObject(object);

            expect(object.a).not.toBeDefined();
            expect(object.b).not.toBeDefined();
            expect(object).toEqual({});
        });
    });


})();