diff options
| author | Andrej Mihajlov <and@codeispoetry.ru> | 2017-03-09 14:33:05 +0000 |
|---|---|---|
| committer | Andrej Mihajlov <and@codeispoetry.ru> | 2017-03-09 14:33:05 +0000 |
| commit | 2e4756eaf97fd70c4917d01a1284887bdec28956 (patch) | |
| tree | 035c41b684ea54d0901a2d04b3d06833d39a10b6 /test | |
| parent | 4005868e4c28e526f26858f5721da7d50809d05e (diff) | |
| download | mullvadvpn-2e4756eaf97fd70c4917d01a1284887bdec28956.tar.xz mullvadvpn-2e4756eaf97fd70c4917d01a1284887bdec28956.zip | |
- Add basic tests for AccountInput
- Add JSDOM
Diffstat (limited to 'test')
| -rw-r--r-- | test/actions.spec.js | 2 | ||||
| -rw-r--r-- | test/components/AccountInput.spec.js | 48 | ||||
| -rw-r--r-- | test/mocks/backend.js (renamed from test/support.js) | 0 | ||||
| -rw-r--r-- | test/mocks/dom.js | 34 | ||||
| -rw-r--r-- | test/routing.spec.js | 2 |
5 files changed, 84 insertions, 2 deletions
diff --git a/test/actions.spec.js b/test/actions.spec.js index 0c691c2bd6..8abf18a027 100644 --- a/test/actions.spec.js +++ b/test/actions.spec.js @@ -1,5 +1,5 @@ import { expect } from 'chai'; -import { filterIpUpdateActions, mockBackend, mockState, mockStore } from './support'; +import { filterIpUpdateActions, mockBackend, mockState, mockStore } from './mocks/backend'; import Backend from '../app/lib/backend'; import userActions from '../app/actions/user'; import connectActions from '../app/actions/connect'; diff --git a/test/components/AccountInput.spec.js b/test/components/AccountInput.spec.js new file mode 100644 index 0000000000..ba2522c224 --- /dev/null +++ b/test/components/AccountInput.spec.js @@ -0,0 +1,48 @@ +import { expect } from 'chai'; +import { KeyType, createKeyEvent } from '../mocks/dom'; + +import React from 'react'; +import ReactTestUtils from 'react-addons-test-utils'; +import AccountInput from '../../app/components/AccountInput'; + +describe('components: AccountInput', () => { + + it('should call onEnter', (done) => { + const onEnter = () => { + done(); + }; + + const component = ReactTestUtils.renderIntoDocument( + <AccountInput onEnter={ onEnter } /> + ); + + ReactTestUtils.Simulate.keyUp(component._ref, createKeyEvent(KeyType.Enter)); + }); + + it('should call onChange', (done) => { + const onChange = (val) => { + expect(val).equal('1'); + done(); + }; + + const component = ReactTestUtils.renderIntoDocument( + <AccountInput onChange={ onChange } /> + ); + + ReactTestUtils.Simulate.keyDown(component._ref, createKeyEvent(KeyType._1)); + }); + + it('should remove last character', (done) => { + const onChange = (val) => { + expect(val).equal('123'); + done(); + }; + + const component = ReactTestUtils.renderIntoDocument( + <AccountInput value="1234" onChange={ onChange } /> + ); + + ReactTestUtils.Simulate.keyDown(component._ref, createKeyEvent(KeyType.Backspace)); + }); + +});
\ No newline at end of file diff --git a/test/support.js b/test/mocks/backend.js index 5cbf4630ba..5cbf4630ba 100644 --- a/test/support.js +++ b/test/mocks/backend.js diff --git a/test/mocks/dom.js b/test/mocks/dom.js new file mode 100644 index 0000000000..9b72f25be2 --- /dev/null +++ b/test/mocks/dom.js @@ -0,0 +1,34 @@ +import jsdom from 'jsdom'; + +global.document = jsdom.jsdom('<!doctype html><html><body></body></html>'); +global.window = document.defaultView; +global.navigator = window.navigator; + +const keyMap = { + _1: { key: '1', which: 49, keyCode: 49 }, + _2: { key: '2', which: 50, keyCode: 50 }, + _3: { key: '3', which: 51, keyCode: 51 }, + _4: { key: '4', which: 52, keyCode: 52 }, + _5: { key: '5', which: 53, keyCode: 53 }, + _6: { key: '6', which: 54, keyCode: 54 }, + _7: { key: '7', which: 55, keyCode: 55 }, + _8: { key: '8', which: 56, keyCode: 56 }, + _9: { key: '9', which: 57, keyCode: 57 }, + _0: { key: '0', which: 48, keyCode: 48 }, + Tab: { which: 9, keyCode: 9 }, + Enter: { which: 13, keyCode: 13 }, + Backspace: { which: 8, keyCode: 8 } +}; + +export const KeyType = (() => { + let dict = {}; + for(const i of Object.keys(keyMap)) { + dict[i] = i; + } + + return dict; +})(); + +export function createKeyEvent(key) { + return Object.assign({ key }, keyMap[key]); +}
\ No newline at end of file diff --git a/test/routing.spec.js b/test/routing.spec.js index 18259e889f..5b546e8851 100644 --- a/test/routing.spec.js +++ b/test/routing.spec.js @@ -1,6 +1,6 @@ import { expect } from 'chai'; -import { filterIpUpdateActions, mockBackend, mockState, mockStore } from './support'; +import { filterIpUpdateActions, mockBackend, mockState, mockStore } from './mocks/backend'; import userActions from '../app/actions/user'; import mapBackendEventsToRouter from '../app/lib/backend-routing'; import { LoginState } from '../app/enums'; |
