loopback-connector-remote/test/remote-connector.test.js

151 lines
4.0 KiB
JavaScript
Raw Normal View History

2019-05-07 17:34:15 +00:00
// Copyright IBM Corp. 2014,2018. All Rights Reserved.
2016-05-06 19:02:21 +00:00
// Node module: loopback-connector-remote
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
2016-09-01 08:12:12 +00:00
'use strict';
const assert = require('assert');
const helper = require('./helper');
const loopback = require('loopback');
2014-09-25 21:06:06 +00:00
describe('RemoteConnector', function() {
let serverApp, clientApp, ServerModel, ClientModel;
2016-01-07 06:45:29 +00:00
2016-02-05 12:29:43 +00:00
before(function setupServer(done) {
const app = serverApp = helper.createRestAppAndListen();
const db = helper.createMemoryDataSource(app);
ServerModel = app.registry.createModel({
name: 'TestModel',
2016-01-07 06:45:29 +00:00
});
app.model(ServerModel, {dataSource: db});
app.locals.handler.on('listening', function() { done(); });
2016-02-05 12:29:43 +00:00
});
before(function setupRemoteClient() {
const app = clientApp = loopback({localRegistry: true});
const remoteDs = helper.createRemoteDataSource(clientApp, serverApp);
ClientModel = app.registry.createModel({
name: 'TestModel',
2016-01-07 06:45:29 +00:00
});
app.model(ClientModel, {dataSource: remoteDs});
2014-09-25 21:06:06 +00:00
});
2016-01-07 06:45:29 +00:00
after(function() {
serverApp.locals.handler.close();
ServerModel = null;
ClientModel = null;
2014-09-25 21:06:06 +00:00
});
2014-09-25 21:19:39 +00:00
it('should support the save method', function(done) {
let calledServerCreate = false;
2014-09-25 21:06:06 +00:00
ServerModel.create = function(data, options, cb, callback) {
if (typeof options === 'function') {
callback = cb;
cb = options;
options = {};
}
2014-09-25 21:06:06 +00:00
calledServerCreate = true;
data.id = 1;
2016-01-07 06:45:29 +00:00
if (callback) callback(null, data);
else cb(null, data);
};
2014-09-25 21:06:06 +00:00
const m = new ClientModel({foo: 'bar'});
2016-01-07 06:45:29 +00:00
m.save(function(err, instance) {
if (err) return done(err);
assert(instance);
assert(instance instanceof ClientModel);
2014-09-25 21:06:06 +00:00
assert(calledServerCreate);
done();
});
});
2015-03-27 00:14:35 +00:00
it('should support aliases', function(done) {
let calledServerUpsert = false;
ServerModel.patchOrCreate =
ServerModel.upsert = function(id, options, cb) {
if (typeof options === 'function') {
cb = options;
options = {};
}
2016-01-07 06:45:29 +00:00
calledServerUpsert = true;
cb();
2015-03-27 00:14:35 +00:00
};
ClientModel.updateOrCreate({}, function(err, instance) {
2015-03-27 00:14:35 +00:00
if (err) return done(err);
2016-01-07 06:45:29 +00:00
assert(instance);
assert(instance instanceof ClientModel);
2016-09-01 08:12:12 +00:00
assert(calledServerUpsert, 'server upsert should have been called');
2016-01-07 06:45:29 +00:00
done();
2015-03-27 00:14:35 +00:00
});
});
2014-09-25 21:06:06 +00:00
});
describe('Custom Path', function() {
let serverApp, clientApp, ServerModel, ClientModel;
2016-02-05 12:29:43 +00:00
before(function setupServer(done) {
const app = serverApp = helper.createRestAppAndListen();
const db = helper.createMemoryDataSource(app);
ServerModel = app.registry.createModel({
name: 'TestModel',
2016-01-07 06:45:29 +00:00
options: {
http: {path: '/custom'},
},
});
app.model(ServerModel, {dataSource: db});
serverApp.locals.handler.on('listening', function() { done(); });
2016-02-05 12:29:43 +00:00
});
before(function setupRemoteClient() {
const app = clientApp = loopback({localRegistry: true});
const remoteDs = helper.createRemoteDataSource(clientApp, serverApp);
ClientModel = app.registry.createModel({
name: 'TestModel',
2016-01-07 06:45:29 +00:00
options: {
dataSource: 'remote',
http: {path: '/custom'},
},
});
app.model(ClientModel, {dataSource: remoteDs});
2016-01-07 06:45:29 +00:00
});
after(function() {
serverApp.locals.handler.close();
ServerModel = null;
ClientModel = null;
});
it('should support http.path configuration', function(done) {
ClientModel.create({}, function(err, instance) {
2016-01-07 06:45:29 +00:00
if (err) return done(err);
assert(instance);
done();
});
});
2016-01-07 06:45:29 +00:00
});
describe('RemoteConnector with options', () => {
it('should have the remoting options passed to the remote object', () => {
const app = loopback();
const dataSource = app.dataSource('remote', {
url: 'http://example.com',
connector: require('..'),
options: {'test': 'abc'},
});
assert.deepEqual(dataSource.connector.remotes.options, {test: 'abc'});
});
});