Skip to content

Commit

Permalink
fix(enhancer): Pick up on actions created by later middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
motiz88 committed Jun 4, 2016
1 parent e3dacdd commit a8149a1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ export const makeMidiEnhancer = ({midiOptions, stateKey = 'midi', requestMIDIAcc

const enhancedStoreMethods = {
dispatch (action) {
const {payload} = action;
action = store.dispatch(action);
if (action.type === SEND_MIDI_MESSAGE) {
const {payload} = action;
const {timestamp, data, device} = payload;
if (midiAccess) {
const {outputs} = midiAccess;
Expand All @@ -114,7 +115,7 @@ export const makeMidiEnhancer = ({midiOptions, stateKey = 'midi', requestMIDIAcc
}
}
}
return store.dispatch(action);
return action;
}
};

Expand Down
20 changes: 18 additions & 2 deletions test/specs/midi-io.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@ import MidiApi from 'web-midi-test-api';
import createMockStore from '../utils/mockStore';
import unique from 'lodash.uniq';
import sinon from 'sinon';
import { applyMiddleware, compose } from 'redux';

describe('MIDI I/O', () => {
let store, api;
beforeEach(() => {
api = new MidiApi();
store = createMockStore(undefined, makeMidiEnhancer({
const middleware = () => next => action => {
if (action.type === 'NOT_A_MIDI_MESSAGE') {
return next({...action, type: SEND_MIDI_MESSAGE});
}
return next(action);
};
store = createMockStore(undefined, compose(makeMidiEnhancer({
requestMIDIAccess: api.requestMIDIAccess
}));
}), applyMiddleware(middleware)));
return new Promise(resolve => setImmediate(resolve));
});
it('should see no devices to begin with', () => {
Expand Down Expand Up @@ -93,5 +100,14 @@ describe('MIDI I/O', () => {
store.dispatch({ type: SEND_MIDI_MESSAGE, payload: {data: [0x80, 0x7f, 0x7f], timestamp: 1234, device: outputIds[0]} });
device.inputs[0].onmidimessage.should.have.been.calledWith({data: new Uint8Array([0x80, 0x7f, 0x7f]), receivedTime: 1234});
});
it('should pick up on actions created by later middleware', () => {
const devices = store.getState().midi.devices;
const outputs = devices.filter(device => device.type === 'output');
console.log(store.getState());
const outputIds = outputs.map(device => device.id);
device.inputs[0].onmidimessage = sinon.spy();
store.dispatch({ type: 'NOT_A_MIDI_MESSAGE', payload: {data: [0x80, 0x7f, 0x7f], timestamp: 1234, device: outputIds[0]} });
device.inputs[0].onmidimessage.should.have.been.called;
});
});
});

0 comments on commit a8149a1

Please sign in to comment.