Skip to content

Commit

Permalink
Merge 5c4da80 into 3ef620b
Browse files Browse the repository at this point in the history
  • Loading branch information
modosc committed Jan 27, 2017
2 parents 3ef620b + 5c4da80 commit a4b691f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/trigger.js
Expand Up @@ -3,6 +3,16 @@ import propName from './propName';
export default (name, components, locals) => {
const promises = (Array.isArray(components) ? components : [components])

// Handle multiple named components
.reduce((p, c) => {
if (typeof c === 'object') {
for (let k in c) p.push(c[k]);
} else {
p.push(c);
}
return p;
}, [])

// Filter out falsy components
.filter(component => component)

Expand Down
17 changes: 13 additions & 4 deletions test/index.js
Expand Up @@ -28,43 +28,50 @@ const makeTestObject = (hookName) => {

describe('Given a series of components have been decorated with hooks', () => {

let hook_a, hook_b;
let hook_a, hook_b, hook_c, hook_d;

beforeEach(() => {
hook_a = makeTestObject('foobar');
hook_b = makeTestObject('foobar');
hook_c = makeTestObject('foobar');
hook_d = makeTestObject('foobar');
});

describe('When a handled lifecycle event is triggered', () => {

let resolveSpy, rejectSpy;

beforeEach(() => {
const componentsWithFalsyValues = [
const componentsWithFalsyAndNamedValues = [
undefined,
hook_a.component,
null,
hook_b.component,
false
false,
{ hook_c: hook_c.component, hook_d: hook_d.component }
];

resolveSpy = spy();
rejectSpy = spy();

trigger('foobar', componentsWithFalsyValues, { some: 'data' })
trigger('foobar', componentsWithFalsyAndNamedValues, { some: 'data' })
.then(resolveSpy, rejectSpy);
});

it('Then the hooks should have locals passed to them', () => {
assert.deepEqual(hook_a.stub.getCall(0).args[0], { some: 'data' });
assert.deepEqual(hook_b.stub.getCall(0).args[0], { some: 'data' });
assert.deepEqual(hook_c.stub.getCall(0).args[0], { some: 'data' });
assert.deepEqual(hook_d.stub.getCall(0).args[0], { some: 'data' });
});

describe('And the hook promises are resolved', () => {

beforeEach(done => {
hook_a.resolve();
hook_b.resolve();
hook_c.resolve();
hook_d.resolve();
setImmediate(done);
});

Expand All @@ -80,6 +87,8 @@ describe('Given a series of components have been decorated with hooks', () => {
beforeEach(done => {
hook_a.resolve();
hook_b.reject();
hook_c.resolve();
hook_d.reject();
setImmediate(done);
});

Expand Down

0 comments on commit a4b691f

Please sign in to comment.