-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app): Parse JSON protocols into state (#2231)
- Loading branch information
Showing
26 changed files
with
485 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// protocol actions tests | ||
import configureMockStore from 'redux-mock-store' | ||
import thunk from 'redux-thunk' | ||
|
||
import {openProtocol} from '..' | ||
|
||
const middlewares = [thunk] | ||
const mockStore = configureMockStore(middlewares) | ||
|
||
describe('protocol actions', () => { | ||
let store | ||
let _FileReader | ||
let mockReader | ||
|
||
beforeEach(() => { | ||
_FileReader = global.FileReader | ||
mockReader = {readAsText: jest.fn()} | ||
|
||
store = mockStore({}) | ||
global.FileReader = jest.fn(() => mockReader) | ||
}) | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks() | ||
global.FileReader = _FileReader | ||
}) | ||
|
||
describe('openProtocol with python protocol', () => { | ||
const pythonFile = { | ||
name: 'foobar.py', | ||
type: 'application/x-python-code', | ||
lastModified: 123, | ||
} | ||
|
||
const jsonFile = { | ||
name: 'foobar.json', | ||
type: 'application/json', | ||
lastModified: 456, | ||
} | ||
|
||
test('dispatches a protocol:OPEN', () => { | ||
const result = store.dispatch(openProtocol(pythonFile)) | ||
const expected = { | ||
type: 'protocol:OPEN', | ||
payload: {file: pythonFile}, | ||
} | ||
|
||
expect(result).toEqual(expected) | ||
expect(store.getActions()).toEqual([expected]) | ||
}) | ||
|
||
test('reads a file', () => { | ||
store.dispatch(openProtocol(pythonFile)) | ||
expect(mockReader.readAsText).toHaveBeenCalledWith(pythonFile) | ||
expect(mockReader.onload).toEqual(expect.any(Function)) | ||
}) | ||
|
||
test('dispatches protocol:UPLOAD on python read completion', () => { | ||
store.dispatch(openProtocol(pythonFile)) | ||
mockReader.result = 'file contents' | ||
mockReader.onload() | ||
|
||
const actions = store.getActions() | ||
expect(actions).toHaveLength(2) | ||
expect(actions[1]).toEqual({ | ||
type: 'protocol:UPLOAD', | ||
meta: {robot: true}, | ||
payload: {contents: 'file contents', data: null}, | ||
}) | ||
}) | ||
|
||
test('dispatches protocol:UPLOAD on JSON read completion', () => { | ||
const protocol = {metadata: {}} | ||
|
||
store.dispatch(openProtocol(jsonFile)) | ||
mockReader.result = JSON.stringify(protocol) | ||
mockReader.onload() | ||
|
||
expect(store.getActions()[1]).toEqual({ | ||
type: 'protocol:UPLOAD', | ||
meta: {robot: true}, | ||
payload: { | ||
contents: mockReader.result, | ||
data: protocol, | ||
}, | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// protocol state reducer tests | ||
|
||
import {protocolReducer} from '..' | ||
|
||
describe('protocolReducer', () => { | ||
test('initial state', () => { | ||
expect(protocolReducer(undefined, {})).toEqual({ | ||
file: null, | ||
contents: null, | ||
data: null, | ||
}) | ||
}) | ||
|
||
const SPECS = [ | ||
{ | ||
name: 'handles protocol:OPEN', | ||
action: {type: 'protocol:OPEN', payload: {file: {name: 'proto.py'}}}, | ||
initialState: {file: {}, contents: 'foobar', data: {}}, | ||
expectedState: {file: {name: 'proto.py'}, contents: null, data: null}, | ||
}, | ||
{ | ||
name: 'handles protocol:UPLOAD', | ||
action: {type: 'protocol:UPLOAD', payload: {contents: 'foo', data: {}}}, | ||
initialState: {file: {name: 'proto.py'}, contents: null, data: null}, | ||
expectedState: {file: {name: 'proto.py'}, contents: 'foo', data: {}}, | ||
}, | ||
{ | ||
name: 'handles robot:SESSION_RESPONSE with non-JSON protocol', | ||
action: { | ||
type: 'robot:SESSION_RESPONSE', | ||
payload: {name: 'foo', protocolText: 'bar'}, | ||
}, | ||
initialState: {file: null, contents: null, data: null}, | ||
expectedState: { | ||
file: {name: 'foo', type: null, lastModified: null}, | ||
contents: 'bar', | ||
data: null, | ||
}, | ||
}, | ||
{ | ||
name: 'handles robot:SESSION_RESPONSE with JSON protocol', | ||
action: { | ||
type: 'robot:SESSION_RESPONSE', | ||
payload: {name: 'foo.json', protocolText: '{"metadata": {}}'}, | ||
}, | ||
initialState: {file: null, contents: null, data: null}, | ||
expectedState: { | ||
file: {name: 'foo.json', type: 'application/json', lastModified: null}, | ||
contents: '{"metadata": {}}', | ||
data: {metadata: {}}, | ||
}, | ||
}, | ||
{ | ||
name: 'handles robot:DISCONNECT by clearing state', | ||
action: {type: 'robot:DISCONNECT_RESPONSE'}, | ||
initialState: {file: {name: 'proto.py'}, contents: 'foo', data: {}}, | ||
expectedState: {file: null, contents: null, data: null}, | ||
}, | ||
] | ||
|
||
SPECS.forEach(spec => { | ||
const {name, action, initialState, expectedState} = spec | ||
|
||
test(name, () => { | ||
expect(protocolReducer(initialState, action)).toEqual(expectedState) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// protocol state selector tests | ||
|
||
import * as protocol from '..' | ||
|
||
const SPECS = [ | ||
{ | ||
name: 'getProtocolFile', | ||
selector: protocol.getProtocolFile, | ||
state: {protocol: {file: {name: 'proto.json'}}}, | ||
expected: {name: 'proto.json'}, | ||
}, | ||
{ | ||
name: 'getProtocolFilename with no file', | ||
selector: protocol.getProtocolFilename, | ||
state: {protocol: {file: null}}, | ||
expected: null, | ||
}, | ||
{ | ||
name: 'getProtocolFilename', | ||
selector: protocol.getProtocolFilename, | ||
state: {protocol: {file: {name: 'proto.json'}}}, | ||
expected: 'proto.json', | ||
}, | ||
] | ||
|
||
describe('protocol selectors', () => { | ||
SPECS.forEach(spec => { | ||
const {name, selector, state, expected} = spec | ||
test(name, () => expect(selector(state)).toEqual(expected)) | ||
}) | ||
}) |
Oops, something went wrong.