Skip to content

Commit

Permalink
Improve client stub for unit tests
Browse files Browse the repository at this point in the history
Provide a method `properties(id)` on ClientStub that returns the
accumulated properties from create and set calls to a given object.

This allows testing for properties being set on the client regardless
of whether these properties have been set in a create or in a set
operation.

Change-Id: I3034bb2f02e9259991854109f6ad3ea8e95b3042
  • Loading branch information
ralfstx authored and Gerrit Code Review committed Jan 19, 2017
1 parent 8c07b9f commit 432a94c
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 92 deletions.
58 changes: 26 additions & 32 deletions test/tabris/ClientStub.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,44 @@
export default function ClientStub() {
this._calls = [];
this._objects = {};
}

ClientStub.prototype = {

create() {
this._calls.push({
op: 'create',
id: arguments[0],
type: arguments[1],
properties: arguments[2]
});
let [id, type, properties] = arguments;
this._calls.push({op: 'create', id, type, properties});
this._objects[id] = {type, properties};
},

get() {
this._calls.push({
op: 'get',
id: arguments[0],
property: arguments[1]
});
let [id, property] = arguments;
this._calls.push({op: 'get', id, property});
},

set() {
this._calls.push({
op: 'set',
id: arguments[0],
properties: arguments[1]
});
let [id, properties] = arguments;
this._calls.push({op: 'set', id, properties});
if (!(id in this._objects)) {
this._objects[id] = {properties: {}};
}
Object.assign(this._objects[id].properties, properties);
},

call() {
this._calls.push({
op: 'call',
id: arguments[0],
method: arguments[1],
parameters: arguments[2]
});
let [id, method, parameters] = arguments;
this._calls.push({op: 'call', id, method, parameters});
},

listen() {
this._calls.push({
op: 'listen',
id: arguments[0],
event: arguments[1],
listen: arguments[2]
});
let [id, event, listen] = arguments;
this._calls.push({op: 'listen', id, event, listen});
},

destroy() {
this._calls.push({
op: 'destroy',
id: arguments[0]
});
let [id] = arguments;
this._calls.push({op: 'destroy', id});
delete this._objects[id];
},

load(url) {
Expand All @@ -66,6 +53,13 @@ ClientStub.prototype = {
resetCalls() {
tabris._nativeBridge.flush();
this._calls = [];
},

properties(id) {
if (!(id in this._objects)) {
throw new Error('No object with id ' + id);
}
return this._objects[id].properties;
}

};
Expand Down
180 changes: 120 additions & 60 deletions test/tabris/ClientStub.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,92 +19,152 @@ describe('ClientStub', function() {

afterEach(restore);

describe('calls are recorded', function() {
describe('calls', function() {

it('create', function() {
let props = {};
client.create('id', 'type', props);

let call = client.calls()[0];
expect(call.op).to.equal('create');
expect(call.id).to.equal('id');
expect(call.type).to.equal('type');
expect(call.properties).to.equal(props);
it('returns empty list by default', function() {
expect(client.calls().length).to.equal(0);
});

it('get', function() {
client.get('id', 'prop');
describe('returns recorded calls', function() {

it('create', function() {
let props = {};
client.create('id', 'type', props);

let call = client.calls()[0];
expect(call).to.deep.equal({
op: 'create',
id: 'id',
type: 'type',
properties: props
});
});

it('get', function() {
client.get('id', 'prop');

let call = client.calls()[0];
expect(call).to.deep.equal({
op: 'get',
id: 'id',
property: 'prop'
});
});

it('set', function() {
let props = {};
client.set('id', props);

let call = client.calls()[0];
expect(call).to.deep.equal({
op: 'set',
id: 'id',
properties: props
});
});

it('call', function() {
let params = {};
client.call('id', 'method', params);

let call = client.calls()[0];
expect(call).to.deep.equal({
op: 'call',
id: 'id',
method: 'method',
parameters: params
});
});

it('listen', function() {
client.listen('id', 'event', true);

let call = client.calls()[0];
expect(call).to.deep.equal({
op: 'listen',
id: 'id',
event: 'event',
listen: true
});
});

it('destroy', function() {
client.destroy('id');

let call = client.calls()[0];
expect(call).to.deep.equal({
op: 'destroy',
id: 'id'
});
});

describe('after multiple calls', function() {

beforeEach(function() {
client.create('id1', 'type1', {foo: 1});
client.create('id2', 'type2', {foo: 2});
client.set('id1', {bar: 1});
client.set('id2', {bar: 2});
});

it('returns all calls', function() {
expect(client.calls().length).to.equal(4);
});

it('result list can be filtered', function() {
expect(client.calls({id: 'id1'}).length).to.equal(2);
});

});

let call = client.calls()[0];
expect(call.op).to.equal('get');
expect(call.id).to.equal('id');
expect(call.property).to.equal('prop');
});

it('set', function() {
let props = {};
client.set('id', props);

let call = client.calls()[0];
expect(call.op).to.equal('set');
expect(call.id).to.equal('id');
expect(call.properties).to.equal(props);
});
});

it('call', function() {
let params = {};
client.call('id', 'method', params);
describe('properties', function() {

let call = client.calls()[0];
expect(call.op).to.equal('call');
expect(call.id).to.equal('id');
expect(call.method).to.equal('method');
expect(call.parameters).to.equal(params);
it('throws with unknown object id', function() {
expect(() => client.properties('foo')).to.throw(Error, 'No object with id foo');
});

it('listen', function() {
client.listen('id', 'event', true);

let call = client.calls()[0];
expect(call.op).to.equal('listen');
expect(call.id).to.equal('id');
expect(call.event).to.equal('event');
expect(call.listen).to.equal(true);
it('returns properties after create', function() {
client.create('w1', 'widget', {foo: 23});
expect(client.properties('w1')).to.deep.equal({foo: 23});
});

it('destroy', function() {
client.destroy('id');

let call = client.calls()[0];
expect(call.op).to.equal('destroy');
expect(call.id).to.equal('id');
it('returns properties after set', function() {
client.set('w1', {foo: 23});
expect(client.properties('w1')).to.deep.equal({foo: 23});
});

});

describe('without any calls', function() {
it('aggregates properties after multiple calls', function() {
client.create('w1', 'widget', {foo: 23, bar: 42});
client.set('w1', {bar: 47, baz: 11});
expect(client.properties('w1')).to.deep.equal({foo: 23, bar: 47, baz: 11});
});

it('result list is empty', function() {
expect(client.calls().length).to.equal(0);
it('removes properties after destroy', function() {
client.create('w1', 'widget', {foo: 23, bar: 42});
client.destroy('w1');
expect(() => client.properties('w1')).to.throw(Error, 'No object with id w1');
});

});

describe('when calls have been made', function() {
describe('resetCalls', function() {

beforeEach(function() {
client.create('id1', 'type1', {foo: 1});
client.create('id2', 'type2', {foo: 2});
client.set('id1', {bar: 1});
client.set('id2', {bar: 2});
client.set('id1', {bar: 2});
client.resetCalls();
});

it('result list has contains all calls', function() {
expect(client.calls().length).to.equal(4);
it('clears calls', function() {
expect(client.calls().length).to.equal(0);
});

it('result list can be filtered', function() {
expect(client.calls({id: 'id1'}).length).to.equal(2);
it('retains properties', function() {
expect(client.properties('id1')).to.deep.equal({foo: 1, bar: 2});
});

});
Expand Down

0 comments on commit 432a94c

Please sign in to comment.