summaryrefslogtreecommitdiffstats
path: root/js/tests/unit/service/ResourceSpec.js
blob: 23831613d51c8419e04fd27d4c1fa9d44e3b54a3 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
 * ownCloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Bernhard Posselt <dev@bernhard-posselt.com>
 * @copyright Bernhard Posselt 2014
 */
describe('Resource', function () {
    'use strict';

    var childResource;

    beforeEach(module('News'));

    beforeEach(inject(function (Resource, $http) {
        var ChildResource = function ($http) {
            Resource.call(this, $http, 'base');
        };

        ChildResource.prototype = Object.create(Resource.prototype);

        childResource = new ChildResource($http);
    }));


    it('should receive an object', function () {
        var objects = [
            {
                id: 2
            },
            {
                id: 3
            }
        ];

        childResource.receive(objects);

        expect(childResource.size()).toBe(2);
    });


    it('should add an object', function () {
        var object = {
            id: 3,
            name: 'test'
        };
        childResource.add(object);

        expect(childResource.get(3)).toBe(object);
    });


    it('should overwrite an object if it already exists', function () {
        var object1 = {
            id: 3,
            name: 'test',
            test: 'ho'
        };

        var object2 = {
            id: 3,
            name: 'test2'
        };

        childResource.add(object1);
        childResource.add(object2);

        expect(childResource.get(3).name).toBe('test2');
        expect(childResource.get(3).test).toBe('ho');
        expect(childResource.size()).toBe(1);
    });


    it('should delete a Resource', function () {
        var object1 = {
            id: 3,
            name: 'test',
            test: 'ho'
        };

        var object2 = {
            id: 4,
            name: 'test2'
        };

        childResource.add(object1);
        childResource.add(object2);

        childResource.delete(3);

        expect(childResource.get(3)).not.toBeDefined();
        expect(childResource.get(4).name).toBe('test2');
        expect(childResource.size()).toBe(1);
    });


    it('should clear all models', function () {
        var object1 = {
            id: 3,
            name: 'test',
            test: 'ho'
        };

        var object2 = {
            id: 4,
            name: 'test2'
        };

        childResource.add(object1);
        childResource.add(object2);

        childResource.clear();

        expect(childResource.get(3)).not.toBeDefined();
        expect(childResource.get(4)).not.toBeDefined();
        expect(childResource.size()).toBe(0);
    });


    it('should get all models', function () {
        var object1 = {
            id: 3,
            name: 'test',
            test: 'ho'
        };

        var object2 = {
            id: 4,
            name: 'test2'
        };

        childResource.add(object1);
        childResource.add(object2);

        expect(childResource.getAll()[1].id).toBe(4);
    });

});