Resolve eslint issues

This commit is contained in:
Josh Johnson 2017-08-26 15:41:11 +01:00
parent c9c1eab014
commit 9c4f1f4963
3 changed files with 72 additions and 34 deletions

View file

@ -131,12 +131,7 @@ class Choices {
// Merge options with user options
this.config = extend(defaultConfig, userConfig);
if (this.config.renderSelectedChoices !== 'auto' && this.config.renderSelectedChoices !== 'always') {
if (!this.config.silent) {
console.warn(
'renderSelectedChoices: Possible values are \'auto\' and \'always\'. Falling back to \'auto\'.',
);
}
if (!['auto', 'always'].includes(this.config.renderSelectedChoices)) {
this.config.renderSelectedChoices = 'auto';
}
@ -170,7 +165,9 @@ class Choices {
if (this.config.shouldSortItems === true && this.isSelectOneElement) {
if (!this.config.silent) {
console.warn('shouldSortElements: Type of passed element is \'select-one\', falling back to false.');
console.warn(
'shouldSortElements: Type of passed element is \'select-one\', falling back to false.',
);
}
}
@ -1398,13 +1395,17 @@ class Choices {
*/
_searchChoices(value) {
const newValue = isType('String', value) ? value.trim() : value;
const currentValue = isType('String', this.currentValue) ? this.currentValue.trim() : this.currentValue;
const currentValue = isType('String', this.currentValue) ?
this.currentValue.trim() :
this.currentValue;
// If new value matches the desired length and is not the same as the current value with a space
if (newValue.length >= 1 && newValue !== `${currentValue} `) {
const haystack = this.store.getSearchableChoices();
const needle = newValue;
const keys = isType('Array', this.config.searchFields) ? this.config.searchFields : [this.config.searchFields];
const keys = isType('Array', this.config.searchFields) ?
this.config.searchFields :
[this.config.searchFields];
const options = Object.assign(this.config.fuseOptions, { keys });
const fuse = new Fuse(haystack, options);
const results = fuse.search(needle);
@ -1585,7 +1586,9 @@ class Choices {
if (hasActiveDropdown) {
e.preventDefault();
const highlighted = this.dropdown.element.querySelector(`.${this.config.classNames.highlightedState}`);
const highlighted = this.dropdown.element.querySelector(
`.${this.config.classNames.highlightedState}`,
);
// If we have a highlighted choice
if (highlighted) {
@ -1625,12 +1628,16 @@ class Choices {
let nextEl;
if (skipKey) {
if (directionInt > 0) {
nextEl = Array.from(this.dropdown.element.querySelectorAll('[data-choice-selectable]')).pop();
nextEl = Array.from(
this.dropdown.element.querySelectorAll('[data-choice-selectable]'),
).pop();
} else {
nextEl = this.dropdown.element.querySelector('[data-choice-selectable]');
}
} else {
const currentEl = this.dropdown.element.querySelector(`.${this.config.classNames.highlightedState}`);
const currentEl = this.dropdown.element.querySelector(
`.${this.config.classNames.highlightedState}`,
);
if (currentEl) {
nextEl = getAdjacentEl(currentEl, '[data-choice-selectable]', directionInt);
} else {
@ -2307,7 +2314,9 @@ class Choices {
* @private
*/
_addGroup(group, id, valueKey = 'value', labelKey = 'label') {
const groupChoices = isType('Object', group) ? group.choices : Array.from(group.getElementsByTagName('OPTION'));
const groupChoices = isType('Object', group) ?
group.choices :
Array.from(group.getElementsByTagName('OPTION'));
const groupId = id || Math.floor(new Date().valueOf() * Math.random());
const isDisabled = group.disabled ? group.disabled : false;

View file

@ -1,4 +1,4 @@
var webpack = require('karma-webpack');
let webpack = require('karma-webpack');
module.exports = function(config) {
config.set({
@ -13,35 +13,35 @@ module.exports = function(config) {
'karma-coverage',
'karma-spec-reporter',
'karma-htmlfile-reporter',
'es6-shim'
'es6-shim',
],
browsers: ['PhantomJS'],
preprocessors: {
'**/*_spec.js': ['webpack'],
'src/**/*.js': ['webpack']
'src/**/*.js': ['webpack'],
},
reporters: ['spec', 'coverage', 'html'],
coverageReporter: {
dir: '../tests/reports/coverage',
reporters: [{
type: 'html',
}]
}],
},
webpack: {
module: {
loaders: [{
test: /\.(js|jsx)$/,
exclude: /(bower_components|node_modules)/,
loader: 'babel-loader'
loader: 'babel-loader',
}],
}
},
},
colors: true,
webpackMiddleware: {
noInfo: true
noInfo: true,
},
htmlReporter: {
outputFile: 'results/unit-tests.html'
}
outputFile: 'results/unit-tests.html',
},
});
};
};

View file

@ -1,6 +1,7 @@
import 'whatwg-fetch';
import 'es6-promise';
import 'core-js/fn/object/assign';
import 'core-js/fn/array/includes';
import Choices from '../../assets/scripts/src/choices';
import itemReducer from '../../assets/scripts/src/reducers/items';
import choiceReducer from '../../assets/scripts/src/reducers/choices';
@ -217,7 +218,9 @@ describe('Choices', () => {
ctrlKey: false,
});
expect(this.choices.currentState.items[this.choices.currentState.items.length - 1]).not.toContain(this.choices.input.element.value);
expect(
this.choices.currentState.items[this.choices.currentState.items.length - 1]
).not.toContain(this.choices.input.element.value);
});
it('should filter input if regexFilter is passed', function() {
@ -312,7 +315,9 @@ describe('Choices', () => {
it('should open the choice list on focusing', function() {
this.choices = new Choices(this.input);
this.choices.input.element.focus();
expect(this.choices.dropdown.element.classList).toContain(this.choices.config.classNames.activeState);
expect(
this.choices.dropdown.element.classList,
).toContain(this.choices.config.classNames.activeState);
});
it('should select the first choice', function() {
@ -405,7 +410,10 @@ describe('Choices', () => {
preventDefault: () => {},
});
expect(document.activeElement === this.choices.input.element && container.classList.contains('is-open')).toBe(true);
expect(
document.activeElement === this.choices.input.element &&
container.classList.contains('is-open'),
).toBe(true);
});
it('should close the dropdown on double click', function() {
@ -425,7 +433,10 @@ describe('Choices', () => {
preventDefault: () => {},
});
expect(document.activeElement === this.choices.input.element && container.classList.contains(openState)).toBe(false);
expect(
document.activeElement === this.choices.input.element &&
container.classList.contains(openState)
).toBe(false);
});
it('should set scrolling flag and not hide dropdown when scrolling on IE', function() {
@ -742,18 +753,26 @@ describe('Choices', () => {
it('should handle showDropdown()', function() {
this.choices.showDropdown();
const hasOpenState = this.choices.containerOuter.element.classList.contains(this.choices.config.classNames.openState);
const hasOpenState = this.choices.containerOuter.element.classList.contains(
this.choices.config.classNames.openState,
);
const hasAttr = this.choices.containerOuter.element.getAttribute('aria-expanded') === 'true';
const hasActiveState = this.choices.dropdown.element.classList.contains(this.choices.config.classNames.activeState);
const hasActiveState = this.choices.dropdown.element.classList.contains(
this.choices.config.classNames.activeState,
);
expect(hasOpenState && hasAttr && hasActiveState).toBe(true);
});
it('should handle hideDropdown()', function() {
this.choices.showDropdown();
this.choices.hideDropdown();
const hasOpenState = this.choices.containerOuter.element.classList.contains(this.choices.config.classNames.openState);
const hasOpenState = this.choices.containerOuter.element.classList.contains(
this.choices.config.classNames.openState,
);
const hasAttr = this.choices.containerOuter.element.getAttribute('aria-expanded') === 'true';
const hasActiveState = this.choices.dropdown.element.classList.contains(this.choices.config.classNames.activeState);
const hasActiveState = this.choices.dropdown.element.classList.contains(
this.choices.config.classNames.activeState,
);
expect(hasOpenState && hasAttr && hasActiveState).toBe(false);
});
@ -767,7 +786,9 @@ describe('Choices', () => {
it('should handle hideDropdown()', function() {
this.choices.showDropdown();
expect(this.choices.containerOuter.element.classList).toContain(this.choices.config.classNames.openState);
expect(
this.choices.containerOuter.element.classList,
).toContain(this.choices.config.classNames.openState);
});
it('should handle getValue()', function() {
@ -873,7 +894,11 @@ describe('Choices', () => {
this.choices.disable();
expect(this.choices.input.element.disabled).toBe(true);
expect(this.choices.containerOuter.element.classList.contains(this.choices.config.classNames.disabledState)).toBe(true);
expect(
this.choices.containerOuter.element.classList.contains(
this.choices.config.classNames.disabledState
),
).toBe(true);
expect(this.choices.containerOuter.element.getAttribute('aria-disabled')).toBe('true');
});
@ -881,7 +906,11 @@ describe('Choices', () => {
this.choices.enable();
expect(this.choices.input.element.disabled).toBe(false);
expect(this.choices.containerOuter.element.classList.contains(this.choices.config.classNames.disabledState)).toBe(false);
expect(
this.choices.containerOuter.element.classList.contains(
this.choices.config.classNames.disabledState,
),
).toBe(false);
expect(this.choices.containerOuter.element.hasAttribute('aria-disabled')).toBe(false);
});