Skip to content

Commit

Permalink
refactor tests and loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Bühler committed Jul 18, 2016
1 parent 8ad405b commit 6247018
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
39 changes: 22 additions & 17 deletions ElasticLoader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import chai = require('chai');
import asPromised = require('chai-as-promised');
import sinon = require('sinon');
import sinonChai = require('sinon-chai');
import {Observable} from 'rxjs';
import {ElasticLoader} from './ElasticLoader';

let should = chai.should();
Expand All @@ -10,30 +11,30 @@ chai.use(sinonChai);

describe('ElasticLoader', () => {

let loader:ElasticLoader;
let client:any;
let stub:any;
let loader: ElasticLoader;
let client: any;
let stub: any;

beforeEach(() => {
client = {
index: o => Promise.resolve()
index: o => Observable.of(o)
};

stub = sinon.stub(client, 'index', o => Promise.resolve());
stub = sinon.stub(client, 'index', o => Observable.of(o));
});

it('should resolve on correct usage', done => {
loader = new ElasticLoader({}, 'testIndex', 'testType');
(loader as any).esClient = client;

loader.write({id: 1, text: 'test'}).then(done, done);
loader.write({id: 1, text: 'test'}).subscribe(null, done, done);
});

it('should use correct index', done => {
loader = new ElasticLoader({}, 'testIndex', 'testType');
(loader as any).esClient = client;

loader.write({id: 1, text: 'test'}).then(() => {
loader.write({id: 1, text: 'test'}).subscribe(null, done, () => {
try {
client.index.should.have.been.calledOnce;
client.index.should.have.been.calledWithMatch({
Expand All @@ -50,7 +51,7 @@ describe('ElasticLoader', () => {
loader = new ElasticLoader({}, 'testIndex', 'testType');
(loader as any).esClient = client;

loader.write({id: 1, text: 'test'}).then(() => {
loader.write({id: 1, text: 'test'}).subscribe(null, done, () => {
try {
client.index.should.have.been.calledOnce;
client.index.should.have.been.calledWithMatch({
Expand All @@ -67,7 +68,7 @@ describe('ElasticLoader', () => {
loader = new ElasticLoader({}, 'testIndex', 'testType');
(loader as any).esClient = client;

loader.write({id: 1, text: 'test'}).then(() => {
loader.write({id: 1, text: 'test'}).subscribe(null, done, () => {
try {
client.index.should.have.been.calledOnce;
client.index.should.have.been.calledWithMatch({
Expand All @@ -84,7 +85,7 @@ describe('ElasticLoader', () => {
loader = new ElasticLoader({}, 'testIndex', 'testType');
(loader as any).esClient = client;

loader.write({id: 1, text: 'test'}).then(() => {
loader.write({id: 1, text: 'test'}).subscribe(null, done, () => {
try {
client.index.should.have.been.calledOnce;
client.index.should.have.been.calledWithMatch({
Expand All @@ -101,7 +102,7 @@ describe('ElasticLoader', () => {
loader = new ElasticLoader({}, 'testIndex', 'testType', o => o.text === 'test');
(loader as any).esClient = client;

loader.write({id: 1, text: 'test'}).then(() => {
loader.write({id: 1, text: 'test'}).subscribe(null, done, () => {
try {
client.index.should.have.been.calledOnce;
done();
Expand All @@ -115,7 +116,7 @@ describe('ElasticLoader', () => {
loader = new ElasticLoader({}, 'testIndex', 'testType', o => o.text !== 'test');
(loader as any).esClient = client;

loader.write({id: 1, text: 'test'}).then(() => {
loader.write({id: 1, text: 'test'}).subscribe(null, done, () => {
try {
client.index.should.not.have.been.called;
done();
Expand All @@ -129,7 +130,7 @@ describe('ElasticLoader', () => {
loader = new ElasticLoader({}, 'testIndex', 'testType', o => true, o => o.myId);
(loader as any).esClient = client;

loader.write({myId: 1, text: 'test'}).then(() => {
loader.write({myId: 1, text: 'test'}).subscribe(null, done, () => {
try {
client.index.should.have.been.calledOnce;
client.index.should.have.been.calledWithMatch({
Expand All @@ -143,12 +144,16 @@ describe('ElasticLoader', () => {
});
});

it('should reject when no id is provided', () => {
it('should reject when no id is provided', done => {
loader = new ElasticLoader({}, 'testIndex', 'testType');
(loader as any).esClient = client;

return loader.write({myId: 1, text: 'test'})
.should.eventually.be.rejected;
loader.write({myId: 1, text: 'test'}).subscribe(null, () => {
done();
}, () => {
done(new Error('did not throw.'));
})
});

});
})
;
2 changes: 1 addition & 1 deletion ElasticLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class ElasticLoader implements Loader {
let id = this.idSelector(object);

if (id === null || id === undefined) {
return Promise.reject(new NoIdProvidedError(object));
return Observable.throw(new NoIdProvidedError(object));
}

let promise = this.buffer
Expand Down

0 comments on commit 6247018

Please sign in to comment.