Skip to content

Commit

Permalink
test(decoratePipes): add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiphe committed May 22, 2016
1 parent bf09395 commit 4d0b69a
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
72 changes: 72 additions & 0 deletions test/plugins/decoratePipes/PluginDecoratePipesSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';

const proxyquire = require('proxyquire');

describe('PluginDecoratePipes', () => {
let PluginDecoratePipes = null;
let noopPipe = null;

function getHelper(pipes, plugins) {
const pluginDecoratePipes = new PluginDecoratePipes(pipes, plugins);

return pluginDecoratePipes.decorateHelper({ helper: {} }).helper;
}

function errorMatching(str) {
return {
asymmetricMatch(actual) {
expect(actual).toEqual(jasmine.any(Error));
expect(actual.message).toEqual(jasmine.stringMatching(str));
return true;
},
};
}

beforeEach(() => {
noopPipe = { id: Symbol('someId') };
PluginDecoratePipes = proxyquire(
'../../../lib/plugins/decoratePipes/PluginDecoratePipes',
{
'./noopPipe': noopPipe,
}
);
});

it('decorates a getPipe method to helper', () => {
const helper = getHelper([], []);

expect(helper.getPipe).toEqual(jasmine.any(Function));
});

describe('getPipe', () => {
it('provides pipes passed to constructor', () => {
const name = 'foo';
const fakePipe = Symbol('fakePipe');
const fakePipeProvider = {
meta: {
name: `gulp-toolbox-pipe-${name}`,
},
get() {
return fakePipe;
},
};
const helper = getHelper([fakePipeProvider], []);

expect(helper.getPipe(name)).toBe(fakePipe);
});

it('throws if pipe is not registered', () => {
const helper = getHelper([], []);
const name = 'bar';

expect(() => {
helper.getPipe(name);
}).toThrow(errorMatching(`Pipe '${name}' not defined`));
});

it('provides a noop-pipe if called with optional param', () => {
expect(getHelper([], []).getPipe('bar', { optional: true }))
.toBe(noopPipe);
});
});
});
33 changes: 33 additions & 0 deletions test/plugins/decoratePipes/noopPipeSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const proxyquire = require('proxyquire');

describe('noopPipe', () => {
it('passes noop callbacks to through2', () => {
const throughWrappedThing = Symbol('throughWrappedThing');
const obj = jasmine.createSpy('through2.obj')
.and.returnValue(throughWrappedThing);
const noopPipe = proxyquire(
'../../../lib/plugins/decoratePipes/noopPipe',
{
through2: { obj },
}
);

expect(noopPipe).toBe(throughWrappedThing);
expect(obj).toHaveBeenCalledWith(
jasmine.any(Function),
jasmine.any(Function)
);

const cb1 = jasmine.createSpy('cb1');
const someChunk = Symbol('someChunk');

obj.calls.argsFor(0)[0](someChunk, undefined, cb1);
expect(cb1).toHaveBeenCalledWith(null, someChunk);

const cb2 = jasmine.createSpy('cb2');
obj.calls.argsFor(0)[1](cb2);
expect(cb2).toHaveBeenCalled();
});
});

0 comments on commit 4d0b69a

Please sign in to comment.