Converted `fakeIpcProvider` to 2 spaces
This commit is contained in:
parent
9b41fa8ac2
commit
89651dde2f
|
@ -2,100 +2,100 @@ const assert = require('assert');
|
|||
const _ = require('lodash');
|
||||
|
||||
const FakeIpcProvider = function IpcProvider() {
|
||||
var _this = this;
|
||||
this.countId = 1;
|
||||
this.notificationCount = 1;
|
||||
this.getResponseStub = function () {
|
||||
return {
|
||||
jsonrpc: '2.0',
|
||||
id: _this.countId,
|
||||
result: null
|
||||
};
|
||||
var _this = this;
|
||||
this.countId = 1;
|
||||
this.notificationCount = 1;
|
||||
this.getResponseStub = function () {
|
||||
return {
|
||||
jsonrpc: '2.0',
|
||||
id: _this.countId,
|
||||
result: null
|
||||
};
|
||||
this.getErrorStub = function () {
|
||||
return {
|
||||
jsonrpc: '2.0',
|
||||
id: _this.countId,
|
||||
error: {
|
||||
code: 1234,
|
||||
message: 'Stub error'
|
||||
}
|
||||
};
|
||||
};
|
||||
this.getErrorStub = function () {
|
||||
return {
|
||||
jsonrpc: '2.0',
|
||||
id: _this.countId,
|
||||
error: {
|
||||
code: 1234,
|
||||
message: 'Stub error'
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
this.response = [];
|
||||
this.error = [];
|
||||
this.validation = [];
|
||||
this.notificationCallbacks = [];
|
||||
this.response = [];
|
||||
this.error = [];
|
||||
this.validation = [];
|
||||
this.notificationCallbacks = [];
|
||||
};
|
||||
|
||||
FakeIpcProvider.prototype.send = function (payload, callback) {
|
||||
var _this = this;
|
||||
var _this = this;
|
||||
|
||||
// set id
|
||||
if(payload.id)
|
||||
this.countId = payload.id;
|
||||
// else
|
||||
// this.countId++;
|
||||
// set id
|
||||
if (payload.id)
|
||||
this.countId = payload.id;
|
||||
// else
|
||||
// this.countId++;
|
||||
|
||||
assert.equal(_.isArray(payload) || _.isObject(payload), true);
|
||||
assert.equal(_.isFunction(callback), true);
|
||||
assert.equal(_.isArray(payload) || _.isObject(payload), true);
|
||||
assert.equal(_.isFunction(callback), true);
|
||||
|
||||
var validation = this.validation.shift();
|
||||
var validation = this.validation.shift();
|
||||
|
||||
if (validation) {
|
||||
// imitate plain json object
|
||||
validation(JSON.parse(JSON.stringify(payload)), callback);
|
||||
}
|
||||
if (validation) {
|
||||
// imitate plain json object
|
||||
validation(JSON.parse(JSON.stringify(payload)), callback);
|
||||
}
|
||||
|
||||
var response = this.getResponseOrError('response', payload);
|
||||
var error = this.getResponseOrError('error', payload);
|
||||
var response = this.getResponseOrError('response', payload);
|
||||
var error = this.getResponseOrError('error', payload);
|
||||
|
||||
setTimeout(function(){
|
||||
callback(error, response);
|
||||
}, 1);
|
||||
setTimeout(function () {
|
||||
callback(error, response);
|
||||
}, 1);
|
||||
};
|
||||
|
||||
FakeIpcProvider.prototype.on = function (type, callback) {
|
||||
if(type === 'data') {
|
||||
this.notificationCallbacks.push(callback);
|
||||
}
|
||||
if (type === 'data') {
|
||||
this.notificationCallbacks.push(callback);
|
||||
}
|
||||
};
|
||||
|
||||
FakeIpcProvider.prototype.getResponseOrError = function (type, payload) {
|
||||
var _this = this;
|
||||
var response;
|
||||
var _this = this;
|
||||
var response;
|
||||
|
||||
if(type === 'error') {
|
||||
response = this.error.shift();
|
||||
} else {
|
||||
response = this.response.shift() || this.getResponseStub();
|
||||
}
|
||||
if (type === 'error') {
|
||||
response = this.error.shift();
|
||||
} else {
|
||||
response = this.response.shift() || this.getResponseStub();
|
||||
}
|
||||
|
||||
|
||||
if(response) {
|
||||
if(_.isArray(response)) {
|
||||
response = response.map(function(resp, index) {
|
||||
resp.id = payload[index] ? payload[index].id : _this.countId++;
|
||||
return resp;
|
||||
});
|
||||
} else
|
||||
response.id = payload.id;
|
||||
}
|
||||
if (response) {
|
||||
if (_.isArray(response)) {
|
||||
response = response.map(function (resp, index) {
|
||||
resp.id = payload[index] ? payload[index].id : _this.countId++;
|
||||
return resp;
|
||||
});
|
||||
} else
|
||||
response.id = payload.id;
|
||||
}
|
||||
|
||||
return response;
|
||||
return response;
|
||||
};
|
||||
|
||||
FakeIpcProvider.prototype.injectNotification = function (notification) {
|
||||
var _this = this;
|
||||
setTimeout(function(){
|
||||
_this.notificationCallbacks.forEach(function(cb){
|
||||
if(notification && cb)
|
||||
cb(null, notification);
|
||||
});
|
||||
}, 100 + this.notificationCount);
|
||||
var _this = this;
|
||||
setTimeout(function () {
|
||||
_this.notificationCallbacks.forEach(function (cb) {
|
||||
if (notification && cb)
|
||||
cb(null, notification);
|
||||
});
|
||||
}, 100 + this.notificationCount);
|
||||
|
||||
this.notificationCount += 10;
|
||||
this.notificationCount += 10;
|
||||
};
|
||||
|
||||
// FakeHttpProvider.prototype.injectResponse = function (response) {
|
||||
|
@ -103,35 +103,35 @@ FakeIpcProvider.prototype.injectNotification = function (notification) {
|
|||
// };
|
||||
|
||||
FakeIpcProvider.prototype.injectBatchResults = function (results, error) {
|
||||
var _this = this;
|
||||
this.response.push(results.map(function (r) {
|
||||
if(error) {
|
||||
var response = _this.getErrorStub();
|
||||
response.error.message = r;
|
||||
} else {
|
||||
var response = _this.getResponseStub();
|
||||
response.result = r;
|
||||
}
|
||||
return response;
|
||||
}));
|
||||
var _this = this;
|
||||
this.response.push(results.map(function (r) {
|
||||
if (error) {
|
||||
var response = _this.getErrorStub();
|
||||
response.error.message = r;
|
||||
} else {
|
||||
var response = _this.getResponseStub();
|
||||
response.result = r;
|
||||
}
|
||||
return response;
|
||||
}));
|
||||
};
|
||||
|
||||
FakeIpcProvider.prototype.injectResult = function (result) {
|
||||
var response = this.getResponseStub();
|
||||
response.result = result;
|
||||
var response = this.getResponseStub();
|
||||
response.result = result;
|
||||
|
||||
this.response.push(response);
|
||||
this.response.push(response);
|
||||
};
|
||||
|
||||
FakeIpcProvider.prototype.injectError = function (error) {
|
||||
var errorStub = this.getErrorStub();
|
||||
errorStub.error = error; // message, code
|
||||
var errorStub = this.getErrorStub();
|
||||
errorStub.error = error; // message, code
|
||||
|
||||
this.error.push(errorStub);
|
||||
this.error.push(errorStub);
|
||||
};
|
||||
|
||||
FakeIpcProvider.prototype.injectValidation = function (callback) {
|
||||
this.validation.push(callback);
|
||||
this.validation.push(callback);
|
||||
};
|
||||
|
||||
module.exports = FakeIpcProvider;
|
||||
|
|
Loading…
Reference in New Issue