Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor test suite #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--recursive
--require test/setup.js
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@
"babel-preset-react": "^6.11.1",
"babel-preset-react-optimize": "^1.0.1",
"babel-preset-stage-0": "^6.5.0",
"chai": "^3.5.0",
"chai-enzyme": "^0.5.0",
"cheerio": "^0.20.0",
"chai": "^4.1.2",
"chai-enzyme": "1.0.0-beta.0",
"cheerio": "^1.0.0-0",
"css-loader": "^0.15.6",
"enzyme": "^2.4.1",
"enzyme": "^3.1.0",
"enzyme-adapter-react-15": "^1.0.1",
"eslint": "^3.10.2",
"eslint-config-airbnb": "^13.0.0",
"eslint-import-resolver-webpack": "^0.7.0",
Expand All @@ -71,9 +72,11 @@
"less-loader": "^2.2.0",
"lodash": "^4.15.0",
"mocha": "^2.2.5",
"react": "^0.14.8 || ^15",
"react": "^15.6.2",
"react-addons-test-utils": "^0.14.8 || ^15",
"react-dom": "^0.14.8 || ^15",
"react-dom": "^15.5.0",
"react-dom-factories": "^1.0.2",
"react-test-renderer": "^15.5.0",
"simulant": "^0.2.2",
"sinon": "^1.17.5",
"sinon-chai": "^2.8.0",
Expand Down
8 changes: 6 additions & 2 deletions test/keymap.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@ export default {
'OPEN': 'enter',
'CLOSE': 'esc',
},
'NON-EXISTING': {},
}
'PARENT': {
'OPEN': 'enter',
'NEXT': 'tab',
},
'NO-SHORTCUTS': {},
};
36 changes: 36 additions & 0 deletions test/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* This file initialises the test environment with tools used for
* defining the test suite.
*
* It is run once before the test suite is executed and should only
* include setup code that is applicable to the entire suite.
*
* For configuration options of mocha itself, see the mocha.opts file.
*/

// React testing framework for traversing React components' output
import Enzyme from 'enzyme';
import Adaptor from 'enzyme-adapter-react-15';

// Assertion library for more expressive syntax
import chai from 'chai';

// chai plugin that allows React-specific assertions for enzyme
import chaiEnzyme from 'chai-enzyme';

// chai plugin that allows assertions on function calls
import sinonChai from 'sinon-chai';

// JS implementation of DOM and HTML spec
import jsdom from 'jsdom';

chai.use(chaiEnzyme());
chai.use(sinonChai);

Enzyme.configure({ adapter: new Adaptor() });

global.document = jsdom.jsdom('<html><body></body></html>');
global.window = document.defaultView;
global.Image = window.Image;
global.navigator = window.navigator;
global.CustomEvent = window.CustomEvent;
219 changes: 127 additions & 92 deletions test/shortcut-manager.spec.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,42 @@
import jsdom from 'jsdom'
import chai from 'chai'
import _ from 'lodash'
import sinonChai from 'sinon-chai'
import sinon from 'sinon'
import chai from 'chai';
import _ from 'lodash';
import sinon from 'sinon';

import keymap from './keymap'
import simulant from 'simulant';

chai.use(sinonChai)
import keymap from './keymap';

const { expect } = chai
const { expect } = chai;

import renderComponent from './support/renderComponent';
import KeyCodes from './support/KeyCodes';

describe('Shortcut manager', () => {
let ShortcutManager = null
let ShortcutManager = null;

before(() => {
global.document = jsdom.jsdom('<html><body></body></html>')
global.window = document.defaultView
global.Image = window.Image
global.navigator = window.navigator
global.CustomEvent = window.CustomEvent

ShortcutManager = require('../src').ShortcutManager
})
ShortcutManager = require('../src').ShortcutManager;
});

it('should return empty object when calling empty constructor', () => {
const manager = new ShortcutManager()
expect(manager.getAllShortcuts()).to.be.empty
})
const manager = new ShortcutManager();
expect(manager.getAllShortcuts()).to.be.empty;
});

it('should return all shortcuts', () => {
const manager = new ShortcutManager(keymap)
expect(manager.getAllShortcuts()).to.not.be.empty
expect(manager.getAllShortcuts()).to.be.equal(keymap)
const manager = new ShortcutManager(keymap);
expect(manager.getAllShortcuts()).to.not.be.empty;
expect(manager.getAllShortcuts()).to.be.equal(keymap);

manager.setKeymap({})
expect(manager.getAllShortcuts()).to.be.empty
manager.setKeymap({});
expect(manager.getAllShortcuts()).to.be.empty;

manager.setKeymap(keymap)
expect(manager.getAllShortcuts()).to.be.equal(keymap)
})
manager.setKeymap(keymap);
expect(manager.getAllShortcuts()).to.be.equal(keymap);
});

it('should return all shortcuts for the Windows platform', () => {
const manager = new ShortcutManager(keymap)
const manager = new ShortcutManager(keymap);
const keyMapResult = {
'Test': {
MOVE_LEFT: 'left',
Expand All @@ -58,14 +53,18 @@ describe('Shortcut manager', () => {
'OPEN': 'enter',
'CLOSE': 'esc',
},
'NON-EXISTING': {},
}
'PARENT': {
'OPEN': 'enter',
'NEXT': 'tab',
},
'NO-SHORTCUTS': {},
};

expect(manager.getAllShortcutsForPlatform('windows')).to.eql(keyMapResult)
})
expect(manager.getAllShortcutsForPlatform('windows')).to.eql(keyMapResult);
});

it('should return all shortcuts for the macOs platform', () => {
const manager = new ShortcutManager(keymap)
const manager = new ShortcutManager(keymap);
const keyMapResult = {
'Test': {
MOVE_LEFT: 'left',
Expand All @@ -82,89 +81,125 @@ describe('Shortcut manager', () => {
'OPEN': 'enter',
'CLOSE': 'esc',
},
'NON-EXISTING': {},
}
'PARENT': {
'OPEN': 'enter',
'NEXT': 'tab',
},
'NO-SHORTCUTS': {},
};

expect(manager.getAllShortcutsForPlatform('osx')).to.eql(keyMapResult)
})
expect(manager.getAllShortcutsForPlatform('osx')).to.eql(keyMapResult);
});

it('should expose the change event type as a static constant', () =>
expect(ShortcutManager.CHANGE_EVENT).to.exist
)
);

it('should have static CHANGE_EVENT', () =>
expect(ShortcutManager.CHANGE_EVENT).to.be.equal('shortcuts:update')
)
);

it('should call onUpdate', () => {
const manager = new ShortcutManager()
const spy = sinon.spy()
manager.addUpdateListener(spy)
manager.setKeymap({})
expect(spy).to.have.beenCalled
})
const manager = new ShortcutManager();
const spy = sinon.spy();
manager.addUpdateListener(spy);
manager.setKeymap({});
expect(spy).to.have.been.called;
});

it('should throw an error when setKeymap is called without arg', () => {
const manager = new ShortcutManager(keymap)
const error = /setKeymap: keymap argument is not defined or falsy./
expect(manager.setKeymap).to.throw(error)
})
const manager = new ShortcutManager(keymap);
const error = /setKeymap: keymap argument is not defined or falsy./;
expect(manager.setKeymap).to.throw(error);
});

it('should extend the keymap', () => {
const manager = new ShortcutManager()
const newKeymap = { 'TESTING-NAMESPACE': {} }
const extendedKeymap = Object.assign({}, keymap, newKeymap)
manager.setKeymap(keymap)
manager.extendKeymap(newKeymap)
const manager = new ShortcutManager();
const newKeymap = { 'TESTING-NAMESPACE': {} };
const extendedKeymap = Object.assign({}, keymap, newKeymap);
manager.setKeymap(keymap);
manager.extendKeymap(newKeymap);

expect(manager.getAllShortcuts()).to.eql(extendedKeymap)
})
expect(manager.getAllShortcuts()).to.eql(extendedKeymap);
});

it('should return array of shortcuts', () => {
const manager = new ShortcutManager(keymap)
let shortcuts = manager.getShortcuts('Test')
expect(shortcuts).to.be.an.array
const manager = new ShortcutManager(keymap);
let shortcuts = manager.getShortcuts('Test');
expect(shortcuts).to.be.an('array');

let shouldContainStrings = _.every(shortcuts, _.isString)
expect(shouldContainStrings).to.be.equal(true)
expect(shortcuts.length).to.be.equal(5)
let shouldContainStrings = _.every(shortcuts, _.isString);
expect(shouldContainStrings).to.be.equal(true);
expect(shortcuts.length).to.be.equal(5);

shortcuts = manager.getShortcuts('Next')
expect(shortcuts).to.be.an.array
shouldContainStrings = _.every(shortcuts, _.isString)
expect(shouldContainStrings).to.be.equal(true)
expect(shortcuts.length).to.be.equal(5)
})
shortcuts = manager.getShortcuts('Next');
expect(shortcuts).to.be.an('array');
shouldContainStrings = _.every(shortcuts, _.isString);
expect(shouldContainStrings).to.be.equal(true);
expect(shortcuts.length).to.be.equal(5);
});

it('should not throw an error when getting not existing key from keymap', () => {
const manager = new ShortcutManager(keymap)
const notExist = () => manager.getShortcuts('NotExist')
expect(notExist).to.not.throw()
})
const manager = new ShortcutManager(keymap);
const notExist = () => manager.getShortcuts('NotExist');
expect(notExist).to.not.throw();
});

it('should return correct key label', () => {
const manager = new ShortcutManager()
manager.setKeymap(keymap)
const manager = new ShortcutManager();
manager.setKeymap(keymap);

// Test
expect(manager.findShortcutName('alt+backspace', 'Test')).to.be.equal('DELETE')
expect(manager.findShortcutName('w', 'Test')).to.be.equal('MOVE_UP')
expect(manager.findShortcutName('up', 'Test')).to.be.equal('MOVE_UP')
expect(manager.findShortcutName('left', 'Test')).to.be.equal('MOVE_LEFT')
expect(manager.findShortcutName('right', 'Test')).to.be.equal('MOVE_RIGHT')
expect(manager.findShortcutName('alt+backspace', 'Test')).to.be.equal('DELETE');
expect(manager.findShortcutName('w', 'Test')).to.be.equal('MOVE_UP');
expect(manager.findShortcutName('up', 'Test')).to.be.equal('MOVE_UP');
expect(manager.findShortcutName('left', 'Test')).to.be.equal('MOVE_LEFT');
expect(manager.findShortcutName('right', 'Test')).to.be.equal('MOVE_RIGHT');

// Next
expect(manager.findShortcutName('alt+o', 'Next')).to.be.equal('OPEN')
expect(manager.findShortcutName('d', 'Next')).to.be.equal('ABORT')
expect(manager.findShortcutName('c', 'Next')).to.be.equal('ABORT')
expect(manager.findShortcutName('esc', 'Next')).to.be.equal('CLOSE')
expect(manager.findShortcutName('enter', 'Next')).to.be.equal('CLOSE')
})
expect(manager.findShortcutName('alt+o', 'Next')).to.be.equal('OPEN');
expect(manager.findShortcutName('d', 'Next')).to.be.equal('ABORT');
expect(manager.findShortcutName('c', 'Next')).to.be.equal('ABORT');
expect(manager.findShortcutName('esc', 'Next')).to.be.equal('CLOSE');
expect(manager.findShortcutName('enter', 'Next')).to.be.equal('CLOSE');
});

it('should throw an error', () => {
const manager = new ShortcutManager()
const fn = () => manager.findShortcutName('left')
expect(manager.findShortcutName).to.throw(/findShortcutName: keyName argument is not defined or falsy./)
expect(fn).to.throw(/findShortcutName: componentName argument is not defined or falsy./)
})
})
const manager = new ShortcutManager();
const fn = () => manager.findShortcutName('left');
expect(manager.findShortcutName).to.throw(/findShortcutName: keyName argument is not defined or falsy./);
expect(fn).to.throw(/findShortcutName: componentName argument is not defined or falsy./);
});

context('when the keymap is updated', () => {
beforeEach(function () {
const { wrapper, node, context } = renderComponent();
node.focus();

this.node = node;
this.wrapper = wrapper;
this.shortcuts = context.shortcuts;
this.handler = wrapper.props().handler;
});

it('then uses the new keymap', function () {
simulant.fire(this.node, 'keydown', { keyCode: KeyCodes.SPACE });

expect(this.handler).to.not.have.been.called;

this.shortcuts.setKeymap({
...keymap,
'TESTING': {
'SPACE': 'space',
},
});

setTimeout(() => {
simulant.fire(this.node, 'keydown', { keyCode: KeyCodes.SPACE });
expect(this.handler).to.have.been.calledWith('SPACE');

this.shortcuts.setKeymap(keymap);
}, 100);
});
});
});
Loading