forked from fullcube/loopback-ds-computed-mixin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
107 lines (87 loc) · 2.68 KB
/
test.js
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
/* jshint mocha: true */
var debug = require('debug')('loopback-ds-computed-mixin');
var utils = require('loopback-datasource-juggler/lib/utils');
var loopback = require('loopback');
var lt = require('loopback-testing');
var chai = require('chai');
var expect = chai.expect;
var sinon = require('sinon');
chai.use(require('sinon-chai'));
require('mocha-sinon');
// Create a new loopback app.
var app = loopback();
// Set up promise support for loopback in non-ES6 runtime environment.
global.Promise = require('bluebird');
// import our Changed mixin.
require('./')(app);
// Connect to db
var dbConnector = loopback.memory();
// Main test
describe('loopback datasource property', function() {
var Item;
var now = new Date();
lt.beforeEach.withApp(app);
beforeEach(function(done) {
Item = this.Item = loopback.PersistedModel.extend('item', {
name: String,
status: String,
readonly: Boolean,
promised: String,
requestedAt: Date
}, {
mixins: {
Computed: {
"properties": {
"readonly": "computedReadonly",
"requestedAt": "computedRequestedAt",
"promised": "computedPromised"
}
}
}
});
Item.computedReadonly = function computedReadonly(item) {
return item.status === 'archived';
};
Item.computedRequestedAt = function computedRequestedAt(item) {
return now;
};
Item.computedPromised = function computedPromised(item, cb) {
cb = cb || utils.createPromiseCallback();
process.nextTick(function() {
cb(null, 'As promised I get back to you!');
});
return cb.promise;
};
// Attach model to db
Item.attachTo(dbConnector);
app.model(Item);
app.use(loopback.rest());
app.set('legacyExplorer', false);
new lt.TestDataBuilder()
.define('item1', Item, {
name: 'Item 1',
status: 'new'
})
.define('item2', Item, {
name: 'Item 23',
status: 'archived'
})
.buildTo(this, done);
});
it('The first item is not readonly', function(done) {
Item.findById(this.item1.id).then(function(item) {
expect(item.requestedAt.toString()).to.equal(now.toString());
expect(item.readonly).to.equal(false);
expect(item.promised).to.equal('As promised I get back to you!');
done();
}).catch(done);
});
it('The second item is readonly', function(done) {
Item.findById(this.item2.id).then(function(item) {
expect(item.requestedAt.toString()).to.equal(now.toString());
expect(item.readonly).to.equal(true);
expect(item.promised).to.equal('As promised I get back to you!');
done();
}).catch(done);
});
});