loopback/test/remote-connector.test.js

79 lines
2.1 KiB
JavaScript
Raw Normal View History

2016-05-04 00:10:46 +00:00
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
// Node module: loopback
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
var loopback = require('../');
var defineModelTestsWithDataSource = require('./util/model-tests');
describe('RemoteConnector', function() {
var remoteApp;
var remote;
defineModelTestsWithDataSource({
beforeEach: function(done) {
var test = this;
remoteApp = loopback();
remoteApp.use(loopback.rest());
remoteApp.listen(0, function() {
test.dataSource = loopback.createDataSource({
host: 'localhost',
port: remoteApp.get('port'),
connector: loopback.Remote
});
done();
});
},
onDefine: function(Model) {
var RemoteModel = Model.extend('Remote' + Model.modelName, {},
{ plural: Model.pluralModelName });
2014-05-03 03:04:06 +00:00
RemoteModel.attachTo(loopback.createDataSource({
connector: loopback.Memory
}));
remoteApp.model(RemoteModel);
}
});
beforeEach(function(done) {
var test = this;
remoteApp = this.remoteApp = loopback();
remoteApp.use(loopback.rest());
2014-06-05 07:45:09 +00:00
var ServerModel = this.ServerModel = loopback.PersistedModel.extend('TestModel');
remoteApp.model(ServerModel);
remoteApp.listen(0, function() {
test.remote = loopback.createDataSource({
host: 'localhost',
port: remoteApp.get('port'),
connector: loopback.Remote
});
done();
});
});
2014-11-21 02:35:36 +00:00
it('should support the save method', function(done) {
var calledServerCreate = false;
2014-06-05 07:45:09 +00:00
var RemoteModel = loopback.PersistedModel.extend('TestModel');
RemoteModel.attachTo(this.remote);
var ServerModel = this.ServerModel;
ServerModel.create = function(data, cb) {
calledServerCreate = true;
data.id = 1;
cb(null, data);
};
ServerModel.setupRemoting();
var m = new RemoteModel({foo: 'bar'});
m.save(function(err, inst) {
if (err) return done(err);
assert(inst instanceof RemoteModel);
assert(calledServerCreate);
done();
});
});
});