Skip to content

Commit

Permalink
[savedObjectsClient/tests] use sinon assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Oct 2, 2017
1 parent 977728f commit f98c009
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 85 deletions.
135 changes: 68 additions & 67 deletions src/server/saved_objects/client/__tests__/saved_objects_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,9 @@ describe('SavedObjectsClient', () => {
title: 'Logstash'
});

expect(callAdminCluster.calledOnce).to.be(true);
sinon.assert.calledOnce(callAdminCluster);
sinon.assert.calledWith(callAdminCluster, 'index');
sinon.assert.calledOnce(onBeforeWrite);

const args = callAdminCluster.getCall(0).args;
expect(args[0]).to.be('index');
});

it('should use create action if ID defined and overwrite=false', async () => {
Expand All @@ -141,35 +139,35 @@ describe('SavedObjectsClient', () => {
id: 'logstash-*',
});

expect(callAdminCluster.calledOnce).to.be(true);
sinon.assert.calledOnce(callAdminCluster);
sinon.assert.calledWith(callAdminCluster, 'create');
sinon.assert.calledOnce(onBeforeWrite);

const args = callAdminCluster.getCall(0).args;
expect(args[0]).to.be('create');
});

it('allows for id to be provided', async () => {
await savedObjectsClient.create('index-pattern', {
title: 'Logstash'
}, { id: 'logstash-*' });

expect(callAdminCluster.calledOnce).to.be(true);
sinon.assert.calledOnce(onBeforeWrite);
sinon.assert.calledOnce(callAdminCluster);
sinon.assert.calledWithExactly(callAdminCluster, sinon.match.string, sinon.match({
id: 'index-pattern:logstash-*'
}));

const args = callAdminCluster.getCall(0).args;
expect(args[1].id).to.be('index-pattern:logstash-*');
sinon.assert.calledOnce(onBeforeWrite);
});

it('self-generates an ID', async () => {
await savedObjectsClient.create('index-pattern', {
title: 'Logstash'
});

expect(callAdminCluster.calledOnce).to.be(true);
sinon.assert.calledOnce(onBeforeWrite);
sinon.assert.calledOnce(callAdminCluster);
sinon.assert.calledWithExactly(callAdminCluster, sinon.match.string, sinon.match({
id: sinon.match(/index-pattern:[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}/)
}));

const args = callAdminCluster.getCall(0).args;
expect(args[1].id).to.match(/index-pattern:[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}/);
sinon.assert.calledOnce(onBeforeWrite);
});
});

Expand All @@ -182,26 +180,24 @@ describe('SavedObjectsClient', () => {
{ type: 'index-pattern', id: 'two', attributes: { title: 'Test Two' } }
]);

expect(callAdminCluster.calledOnce).to.be(true);
sinon.assert.calledOnce(onBeforeWrite);

const args = callAdminCluster.getCall(0).args;
sinon.assert.calledOnce(callAdminCluster);
sinon.assert.calledWithExactly(callAdminCluster, 'bulk', sinon.match({
body: [
{ create: { _type: 'doc', _id: 'config:one' } },
{ type: 'config', ...mockTimestampFields, config: { title: 'Test One' } },
{ create: { _type: 'doc', _id: 'index-pattern:two' } },
{ type: 'index-pattern', ...mockTimestampFields, 'index-pattern': { title: 'Test Two' } }
]
}));

expect(args[0]).to.be('bulk');
expect(args[1].body).to.eql([
{ create: { _type: 'doc', _id: 'config:one' } },
{ type: 'config', ...mockTimestampFields, config: { title: 'Test One' } },
{ create: { _type: 'doc', _id: 'index-pattern:two' } },
{ type: 'index-pattern', ...mockTimestampFields, 'index-pattern': { title: 'Test Two' } }
]);
sinon.assert.calledOnce(onBeforeWrite);
});

it('should overwrite objects if overwrite is truthy', async () => {
callAdminCluster.returns({ items: [] });

await savedObjectsClient.bulkCreate([{ type: 'foo', id: 'bar', attributes: {} }], { overwrite: false });
sinon.assert.calledOnce(callAdminCluster);
sinon.assert.calledOnce(onBeforeWrite);
sinon.assert.calledWithExactly(callAdminCluster, 'bulk', sinon.match({
body: [
// uses create because overwriting is not allowed
Expand All @@ -210,12 +206,13 @@ describe('SavedObjectsClient', () => {
]
}));

sinon.assert.calledOnce(onBeforeWrite);

callAdminCluster.reset();
onBeforeWrite.reset();

await savedObjectsClient.bulkCreate([{ type: 'foo', id: 'bar', attributes: {} }], { overwrite: true });
sinon.assert.calledOnce(callAdminCluster);
sinon.assert.calledOnce(onBeforeWrite);
sinon.assert.calledWithExactly(callAdminCluster, 'bulk', sinon.match({
body: [
// uses index because overwriting is allowed
Expand All @@ -224,7 +221,7 @@ describe('SavedObjectsClient', () => {
]
}));


sinon.assert.calledOnce(onBeforeWrite);
});

it('returns document errors', async () => {
Expand Down Expand Up @@ -330,18 +327,16 @@ describe('SavedObjectsClient', () => {
});
await savedObjectsClient.delete('index-pattern', 'logstash-*');

expect(callAdminCluster.calledOnce).to.be(true);
sinon.assert.calledOnce(onBeforeWrite);

const [method, args] = callAdminCluster.getCall(0).args;
expect(method).to.be('delete');
expect(args).to.eql({
sinon.assert.calledOnce(callAdminCluster);
sinon.assert.calledWithExactly(callAdminCluster, 'delete', {
type: 'doc',
id: 'index-pattern:logstash-*',
refresh: 'wait_for',
index: '.kibana-test',
ignore: [404],
});

sinon.assert.calledOnce(onBeforeWrite);
});
});

Expand Down Expand Up @@ -421,24 +416,26 @@ describe('SavedObjectsClient', () => {
it('accepts per_page/page', async () => {
await savedObjectsClient.find({ perPage: 10, page: 6 });

expect(callAdminCluster.calledOnce).to.be(true);
sinon.assert.notCalled(onBeforeWrite);
sinon.assert.calledOnce(callAdminCluster);
sinon.assert.calledWithExactly(callAdminCluster, sinon.match.string, sinon.match({
size: 10,
from: 50
}));

const options = callAdminCluster.getCall(0).args[1];
expect(options.size).to.be(10);
expect(options.from).to.be(50);
sinon.assert.notCalled(onBeforeWrite);
});

it('can filter by fields', async () => {
await savedObjectsClient.find({ fields: ['title'] });

expect(callAdminCluster.calledOnce).to.be(true);
sinon.assert.notCalled(onBeforeWrite);
sinon.assert.calledOnce(callAdminCluster);
sinon.assert.calledWithExactly(callAdminCluster, sinon.match.string, sinon.match({
_source: [
'*.title', 'type', 'title'
]
}));

const options = callAdminCluster.getCall(0).args[1];
expect(options._source).to.eql([
'*.title', 'type', 'title'
]);
sinon.assert.notCalled(onBeforeWrite);
});
});

Expand Down Expand Up @@ -476,9 +473,11 @@ describe('SavedObjectsClient', () => {
await savedObjectsClient.get('index-pattern', 'logstash-*');

sinon.assert.notCalled(onBeforeWrite);
const [, args] = callAdminCluster.getCall(0).args;
expect(args.id).to.eql('index-pattern:logstash-*');
expect(args.type).to.eql('doc');
sinon.assert.calledOnce(callAdminCluster);
sinon.assert.calledWithExactly(callAdminCluster, sinon.match.string, sinon.match({
id: 'index-pattern:logstash-*',
type: 'doc'
}));
});
});

Expand All @@ -491,14 +490,17 @@ describe('SavedObjectsClient', () => {
{ id: 'two', type: 'index-pattern' }
]);

expect(callAdminCluster.calledOnce).to.be(true);
sinon.assert.notCalled(onBeforeWrite);
sinon.assert.calledOnce(callAdminCluster);
sinon.assert.calledWithExactly(callAdminCluster, sinon.match.string, sinon.match({
body: {
docs: [
{ _type: 'doc', _id: 'config:one' },
{ _type: 'doc', _id: 'index-pattern:two' }
]
}
}));

const options = callAdminCluster.getCall(0).args[1];
expect(options.body.docs).to.eql([
{ _type: 'doc', _id: 'config:one' },
{ _type: 'doc', _id: 'index-pattern:two' }
]);
sinon.assert.notCalled(onBeforeWrite);
});

it('returns early for empty objects argument', async () => {
Expand All @@ -507,7 +509,7 @@ describe('SavedObjectsClient', () => {
const response = await savedObjectsClient.bulkGet([]);

expect(response.saved_objects).to.have.length(0);
expect(callAdminCluster.notCalled).to.be(true);
sinon.assert.notCalled(callAdminCluster);
sinon.assert.notCalled(onBeforeWrite);
});

Expand Down Expand Up @@ -583,20 +585,17 @@ describe('SavedObjectsClient', () => {
{ version: newVersion - 1 }
);

const esParams = callAdminCluster.getCall(0).args[1];
expect(esParams.version).to.be(newVersion - 1);
sinon.assert.calledOnce(callAdminCluster);
sinon.assert.calledWithExactly(callAdminCluster, sinon.match.string, sinon.match({
version: newVersion - 1
}));
});

it('passes the parameters to callAdminCluster', async () => {
await savedObjectsClient.update('index-pattern', 'logstash-*', { title: 'Testing' });

expect(callAdminCluster.calledOnce).to.be(true);
sinon.assert.calledOnce(onBeforeWrite);

const args = callAdminCluster.getCall(0).args;

expect(args[0]).to.be('update');
expect(args[1]).to.eql({
sinon.assert.calledOnce(callAdminCluster);
sinon.assert.calledWithExactly(callAdminCluster, 'update', {
type: 'doc',
id: 'index-pattern:logstash-*',
version: undefined,
Expand All @@ -607,6 +606,8 @@ describe('SavedObjectsClient', () => {
refresh: 'wait_for',
index: '.kibana-test'
});

sinon.assert.calledOnce(onBeforeWrite);
});
});

Expand Down
43 changes: 25 additions & 18 deletions src/ui/public/saved_objects/__tests__/saved_objects_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('SavedObjectsClient', () => {

savedObjectsClient._request('POST', '/api/path', params);

expect($http.calledOnce).to.be(true);
sinon.assert.calledOnce($http);
});

it('throws error when body is provided for GET', async () => {
Expand Down Expand Up @@ -227,8 +227,9 @@ describe('SavedObjectsClient', () => {

savedObjectsClient.update('index-pattern', 'logstash-*', attributes, options);
sinon.assert.calledOnce($http);

expect($http.getCall(0).args[0].data).to.eql(body);
sinon.assert.calledWithExactly($http, sinon.match({
data: body
}));
});
});

Expand Down Expand Up @@ -268,15 +269,22 @@ describe('SavedObjectsClient', () => {
savedObjectsClient.create('index-pattern', attributes, { id: 'myId' });

sinon.assert.calledOnce($http);
expect($http.getCall(0).args[0].url).to.eql(url);
sinon.assert.calledWithExactly($http, sinon.match({
url
}));
});

it('makes HTTP call', () => {
const attributes = { foo: 'Foo', bar: 'Bar' };
savedObjectsClient.create('index-pattern', attributes);

sinon.assert.calledOnce($http);
expect($http.getCall(0).args[0].data.attributes).to.eql(attributes);
sinon.assert.calledWithExactly($http, sinon.match({
url: sinon.match.string,
data: {
attributes
}
}));
});
});

Expand All @@ -295,31 +303,30 @@ describe('SavedObjectsClient', () => {
const body = { type: 'index-pattern', invalid: true };

savedObjectsClient.find(body);
expect($http.calledOnce).to.be(true);

const options = $http.getCall(0).args[0];
expect(options.url).to.eql(`${basePath}/api/saved_objects/?type=index-pattern&invalid=true`);
sinon.assert.calledOnce($http);
sinon.assert.calledWithExactly($http, sinon.match({
url: `${basePath}/api/saved_objects/?type=index-pattern&invalid=true`
}));
});

it('accepts fields', () => {
const body = { fields: ['title', 'description'] };

savedObjectsClient.find(body);
expect($http.calledOnce).to.be(true);

const options = $http.getCall(0).args[0];
expect(options.url).to.eql(`${basePath}/api/saved_objects/?fields=title&fields=description`);
sinon.assert.calledOnce($http);
sinon.assert.calledWithExactly($http, sinon.match({
url: `${basePath}/api/saved_objects/?fields=title&fields=description`
}));
});

it('accepts from/size', () => {
const body = { from: 50, size: 10 };

savedObjectsClient.find(body);
expect($http.calledOnce).to.be(true);

const options = $http.getCall(0).args[0];
expect(options.url).to.eql(`${basePath}/api/saved_objects/?from=50&size=10`);

sinon.assert.calledOnce($http);
sinon.assert.alwaysCalledWith($http, sinon.match({
url: `${basePath}/api/saved_objects/?from=50&size=10`
}));
});
});
});

0 comments on commit f98c009

Please sign in to comment.