Skip to content
This repository was archived by the owner on Apr 29, 2020. It is now read-only.

Commit 7f0ffdd

Browse files
committed
fix(act): allow act API to be bindable to a context - e.g. REPL vs WebSocket/HTTP.
Change `act` API to be able to allow `newAct = api.act.bind(api, ctxt)` so a developer can replace the `req, res` properties of `ctxt` in a REPL context - e.g. `ctxt.res.write = console.log` BREAKING CHANGE: change `act(e, ctxt, cb)` to `act(ctxt, e)`
1 parent 7430469 commit 7f0ffdd

3 files changed

Lines changed: 16 additions & 54 deletions

File tree

lib/api/act.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
77
'use strict';
88

99
module.exports = function exported(_, async) {
10-
return function act(pattern, context, done) {
10+
return function act(context, pattern) {
1111
var api = this;
1212
var handlers = api.actions.list(pattern);
13-
var callActions = _callActions.bind(
13+
var callHandlers = _callHandlers.bind(
1414
api,
15-
pattern, context
15+
context, pattern
1616
);
17-
var callback = done ? done : _.noop;
1817

19-
async.each(handlers, callActions, callback);
18+
async.each(handlers, callHandlers);
2019
};
2120
};
2221

23-
function _callActions(pattern, context, handler, done) {
22+
function _callHandlers(context, pattern, handler, done) {
2423
var api = this;
25-
handler.call(api, pattern, context, done);
24+
25+
handler.call(api, context, pattern, done);
2626
}

test/test.api.js

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,14 @@ test('api.act() with no callback', function(assert) {
1818

1919
// MOCKS
2020
var mockActions = bloomrun();
21-
mockActions.add(expectedPattern, function(args, ctxt, done) {
22-
assert.deepEqual(args, expectedPattern);
21+
mockActions.add(expectedPattern, function(ctxt, args, done) {
2322
assert.deepEqual(ctxt, expectedContext);
23+
assert.deepEqual(args, expectedPattern);
2424
done();
2525
});
2626

27-
var mockLodash = _.defaults({
28-
noop: function() {
29-
assert.pass();
30-
}
31-
}, _);
32-
3327
// TEST
34-
var act = require('../lib/api/act.js')(mockLodash, async);
28+
var act = require('../lib/api/act.js')(_, async);
3529
var fixture = {
3630
actions: mockActions,
3731
act: act
@@ -41,38 +35,8 @@ test('api.act() with no callback', function(assert) {
4135
// - the expected pattern; and,
4236
// - the expected context
4337
// assert that api.act() would call _.noop if no callback was provided
44-
assert.plan(3);
45-
fixture.act(expectedPattern, expectedContext);
46-
});
47-
48-
test('api.act() with callback', function(assert) {
49-
// EXPECTED
50-
var expectedPattern = {
51-
pattern: 1
52-
};
53-
var expectedContext = {
54-
anon: true
55-
};
56-
var expectedError = 'expected error';
57-
58-
// MOCKS
59-
var mockActions = bloomrun();
60-
mockActions.add(expectedPattern, function(args, ctxt, done) {
61-
done(expectedError);
62-
});
63-
64-
// TEST
65-
var act = require('../lib/api/act.js')(_, async);
66-
var fixture = {
67-
actions: mockActions,
68-
act: act
69-
};
70-
71-
// assert that api.act() would call the provided callback
72-
assert.plan(1);
73-
fixture.act(expectedPattern, expectedContext, function(err) {
74-
assert.equal(err, expectedError);
75-
});
38+
assert.plan(2);
39+
fixture.act(expectedContext, expectedPattern);
7640
});
7741

7842
// api.view() -------------------------------------

test/test.index.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,19 @@ test('register() an action and act() on it', function(t) {
4545
};
4646
var testContext = 'some arbitrary context object';
4747

48-
t.plan(3);
48+
t.plan(2);
4949
api.register({actions: [
5050
{
5151
name: 'test action',
5252
pattern: testPattern,
53-
handler: function(args, context, done) {
54-
t.equal(args, testPattern);
53+
handler: function(context, args, done) {
5554
t.equal(context, testContext);
55+
t.equal(args, testPattern);
5656
done();
5757
}
5858
}
5959
]}, function() {
60-
api.act(testPattern, testContext, function() {
61-
t.pass('passed handler callback');
62-
});
60+
api.act(testContext, testPattern);
6361
});
6462
});
6563

0 commit comments

Comments
 (0)