forked from platanus/angular-restmod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputed-spec.js
More file actions
107 lines (84 loc) · 2.52 KB
/
Copy pathcomputed-spec.js
File metadata and controls
107 lines (84 loc) · 2.52 KB
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
'use strict';
describe('RMBuilderComputed', function() {
var $injector, restmod, RMBuilderComputed, UserModel;
beforeEach(module('restmod'));
beforeEach(module(function($provide) {
$provide.factory('UserModel', function(restmod) {
return restmod.model('/api/users', {
firstName: ''
});
});
}));
// cache entities to be used in tests
beforeEach(inject(['$injector',
function(_$injector) {
$injector = _$injector;
restmod = $injector.get('restmod');
RMBuilderComputed = $injector.get('RMBuilderComputed');
UserModel = restmod.model('/api/users', {
firstName: ''
});
}
]));
describe('computed property', function() {
describe('basics', function() {
var DeviceModel, device;
beforeEach(function() {
DeviceModel = restmod.model('/api/devices', {
vendor: 'default vendor',
model: 'default model',
fancyName: {
computed: function() {
return this.vendor + ': ' + this.model;
}
}
});
device = DeviceModel.$new().$decode({});
});
it('calculates using given function', function() {
expect(device.fancyName).toEqual('default vendor: default model');
});
it('changes when model properties update', function() {
device.vendor = 'Apple';
device.model = 'iPhone';
expect(device.fancyName).toEqual('Apple: iPhone');
});
it('should be masked by default',function() {
var encoded = device.$encode();
expect(encoded.fancyName).toBeUndefined();
});
it('should be listed in $each',function() {
var test = {};
device.$each(function(v, k) { test[k] = v; });
expect(test.fancyName).toBeDefined();
});
});
describe('with relations', function() {
var DeviceModel, device;
beforeEach(function() {
DeviceModel = restmod.model('/api/devices', {
vendor: '',
model: '',
user: {
hasOne: 'UserModel'
},
ownedBy: {
computed: function() {
return this.user.firstName + '\'s ' + this.model;
}
}
});
device = DeviceModel.$new().$decode({
vendor: 'Apple',
model: 'Watch',
user: {
firstName: 'Johnny'
}
});
});
it('can access related models', function() {
expect(device.ownedBy).toEqual('Johnny\'s Watch');
});
});
});
});