Skip to content

Commit

Permalink
Api changes (Choices-js#515)
Browse files Browse the repository at this point in the history
* Combine regexFilter and addItemFilter + minor tweaks

* Update tests to accomodate fixed dropdown notice

* Remove broken `toggleDropdown` method

* Unskip dropdown interaction tests

* Remove reference to removed method
  • Loading branch information
jshjohnson committed Feb 12, 2019
1 parent 55b356e commit 8540d5a
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 336 deletions.
26 changes: 6 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ will be returned. If you target one element, that instance will be returned.
renderChoiceLimit: -1,
maxItemCount: -1,
addItems: true,
addItemFilterFn: null,
removeItems: true,
removeItemButton: false,
editItems: false,
Expand All @@ -91,8 +92,6 @@ will be returned. If you target one element, that instance will be returned.
searchFields: ['label', 'value'],
position: 'auto',
resetScrollPosition: true,
regexFilter: null,
addItemFilter: null,
shouldSort: true,
shouldSortItems: false,
sortFn: () => {...},
Expand Down Expand Up @@ -340,31 +339,24 @@ Pass an array of objects:

**Usage:** Whether the scroll position should reset after adding an item.

### addItemFilter
### addItemFilterFn
**Type:** `Function` **Default:** `null`

**Input types affected:** `text`

**Usage:** A callback function that will need to return `true` for a user to successfully add an item.
**Usage:** A filter function that will need to return `true` for a user to successfully add an item.

**Example:**

```js
// Only adds items matching the text test
new Choices(element, {
addItemFilter: function (value) {
return (value !== 'test')
}
addItemFilterFn: (value) => {
return (value !== 'test');
};
});
```

### regexFilter
**Type:** `Regex` **Default:** `null`

**Input types affected:** `text`

**Usage:** A filter that will need to pass for a user to successfully add an item.

### shouldSort
**Type:** `Boolean` **Default:** `true`

Expand Down Expand Up @@ -752,12 +744,6 @@ choices.disable();
**Usage:** Hide option list dropdown (only affects select inputs).
### toggleDropdown();
**Input types affected:** `text`, `select-multiple`
**Usage:** Toggle dropdown between showing/hidden.
### setChoices(choices, value, label, replaceChoices);
**Input types affected:** `select-one`, `select-multiple`
Expand Down
40 changes: 1 addition & 39 deletions cypress/integration/select-multiple.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ describe('Choices - select multiple', () => {
@todo Investigate why
*/
describe.skip('interacting with dropdown', () => {
describe('interacting with dropdown', () => {
describe('opening dropdown', () => {
it('opens dropdown', () => {
cy.get('[data-test-hook=basic]')
Expand Down Expand Up @@ -247,44 +247,6 @@ describe('Choices - select multiple', () => {
.should('not.be.visible');
});
});

describe('toggling dropdown', () => {
describe('when open', () => {
it('closes dropdown', () => {
cy.get('[data-test-hook=basic]')
.find('button.open-dropdown')
.focus()
.click();

cy.get('[data-test-hook=basic]')
.find('button.toggle-dropdown')
.focus()
.click();

cy.get('[data-test-hook=basic]')
.find('.choices__list--dropdown')
.should('not.be.visible');
});
});

describe('when closed', () => {
it('opens dropdown', () => {
cy.get('[data-test-hook=basic]')
.find('button.close-dropdown')
.focus()
.click();

cy.get('[data-test-hook=basic]')
.find('button.toggle-dropdown')
.focus()
.click();

cy.get('[data-test-hook=basic]')
.find('.choices__list--dropdown')
.should('be.visible');
});
});
});
});

describe('disabling', () => {
Expand Down
40 changes: 1 addition & 39 deletions cypress/integration/select-one.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ describe('Choices - select one', () => {
@todo Investigate why
*/
describe.skip('interacting with dropdown', () => {
describe('interacting with dropdown', () => {
describe('opening dropdown', () => {
it('opens dropdown', () => {
cy.get('[data-test-hook=basic]')
Expand Down Expand Up @@ -165,44 +165,6 @@ describe('Choices - select one', () => {
.should('not.be.visible');
});
});

describe('toggling dropdown', () => {
describe('when open', () => {
it('closes dropdown', () => {
cy.get('[data-test-hook=basic]')
.find('button.open-dropdown')
.focus()
.click();

cy.get('[data-test-hook=basic]')
.find('button.toggle-dropdown')
.focus()
.click();

cy.get('[data-test-hook=basic]')
.find('.choices__list--dropdown')
.should('not.be.visible');
});
});

describe('when closed', () => {
it('opens dropdown', () => {
cy.get('[data-test-hook=basic]')
.find('button.close-dropdown')
.focus()
.click();

cy.get('[data-test-hook=basic]')
.find('button.toggle-dropdown')
.focus()
.click();

cy.get('[data-test-hook=basic]')
.find('.choices__list--dropdown')
.should('be.visible');
});
});
});
});

describe('disabling', () => {
Expand Down
75 changes: 28 additions & 47 deletions cypress/integration/text.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,9 @@ describe('Choices - text element', () => {
});

describe('input limit', () => {
const inputLimit = 5;
beforeEach(() => {
for (let index = 0; index < 6; index++) {
for (let index = 0; index < inputLimit + 1; index++) {
cy.get('[data-test-hook=input-limit]')
.find('.choices__input--cloned')
.type(`${textInput} + ${index}`)
Expand All @@ -212,29 +213,36 @@ describe('Choices - text element', () => {
.first()
.children()
.should($items => {
expect($items.length).to.equal(5);
expect($items.length).to.equal(inputLimit);
});
});

it('hides dropdown prompt once limit has been reached', () => {
cy.wait(500); // allow for animation frame
cy.get('[data-test-hook=input-limit]')
.find('.choices__list--dropdown')
.should('not.be.visible');
describe('reaching input limit', () => {
it('displays dropdown prompt', () => {
cy.get('[data-test-hook=input-limit]')
.find('.choices__list--dropdown')
.should('be.visible')
.should($dropdown => {
const dropdownText = $dropdown.text().trim();
expect(dropdownText).to.equal(
`Only ${inputLimit} values can be added`,
);
});
});
});
});

describe('regex filter', () => {
describe('inputting a value that satisfies the regex', () => {
describe('add item filter', () => {
describe('inputting a value that satisfies the filter', () => {
const input = '[email protected]';

it('allows me to add choice', () => {
cy.get('[data-test-hook=regex-filter]')
cy.get('[data-test-hook=add-item-filter]')
.find('.choices__input--cloned')
.type(input)
.type('{enter}');

cy.get('[data-test-hook=regex-filter]')
cy.get('[data-test-hook=add-item-filter]')
.find('.choices__list--multiple .choices__item')
.last()
.should($choice => {
Expand All @@ -245,14 +253,20 @@ describe('Choices - text element', () => {

describe('inputting a value that does not satisfy the regex', () => {
it('displays dropdown prompt', () => {
cy.get('[data-test-hook=regex-filter]')
cy.get('[data-test-hook=add-item-filter]')
.find('.choices__input--cloned')
.type(`this is not an email address`)
.type('{enter}');

cy.get('[data-test-hook=regex-filter]')
cy.get('[data-test-hook=add-item-filter]')
.find('.choices__list--dropdown')
.should('not.be.visible');
.should('be.visible')
.should($dropdown => {
const dropdownText = $dropdown.text().trim();
expect(dropdownText).to.equal(
'Only values matching specific conditions can be added',
);
});
});
});
});
Expand Down Expand Up @@ -293,39 +307,6 @@ describe('Choices - text element', () => {
});
});

describe('custom add item callback', () => {
describe('inputting a value that satisfies the addItemFilter', () => {
const input = 'test';

it('allows me to add choice', () => {
cy.get('[data-test-hook=add-item-callback]')
.find('.choices__input--cloned')
.type(input)
.type('{enter}');

cy.get('[data-test-hook=add-item-callback]')
.find('.choices__list--multiple .choices__item')
.last()
.should($choice => {
expect($choice.text().trim()).to.equal(input);
});
});
});

describe('inputting a value that does not satisfy the callback', () => {
it('displays dropdown prompt', () => {
cy.get('[data-test-hook=add-item-callback]')
.find('.choices__input--cloned')
.type(`this is not the allowed text`)
.type('{enter}');

cy.get('[data-test-hook=add-item-callback]')
.find('.choices__list--dropdown')
.should('not.be.visible');
});
});
});

describe('disabled via attribute', () => {
it('does not allow me to input data', () => {
cy.get('[data-test-hook=disabled-via-attr]')
Expand Down
10 changes: 9 additions & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,15 @@ <h2>Form interaction</h2>

var textEmailFilter = new Choices('#choices-text-email-filter', {
editItems: true,
regexFilter: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
addItemFilterFn: (value) => {
if (!value) {
return false;
}

const regex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
const expression = new RegExp(regex.source, 'i');
return expression.test(value);
},
}).setValue(['[email protected]']);

var textDisabled = new Choices('#choices-text-disabled', {
Expand Down
5 changes: 0 additions & 5 deletions public/test/select-multiple.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ <h2>Select multiple inputs</h2>
<label for="choices-basic">Basic</label>
<button class="open-dropdown push-bottom">Open dropdown</button>
<button class="close-dropdown push-bottom">Close dropdown</button>
<button class="toggle-dropdown push-bottom">Toggle dropdown</button>
<button class="disable push-bottom">Disable</button>
<button class="enable push-bottom">Enable</button>
<select class="form-control" name="choices-basic" id="choices-basic" multiple>
Expand Down Expand Up @@ -216,10 +215,6 @@ <h2>Select multiple inputs</h2>
choicesBasic.hideDropdown();
});

document.querySelector('button.toggle-dropdown').addEventListener('click', () => {
choicesBasic.toggleDropdown();
});

document.querySelector('button.disable').addEventListener('click', () => {
choicesBasic.disable();
});
Expand Down
5 changes: 0 additions & 5 deletions public/test/select-one.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ <h2>Select one inputs</h2>
<label for="choices-basic">Basic</label>
<button class="open-dropdown push-bottom">Open dropdown</button>
<button class="close-dropdown push-bottom">Close dropdown</button>
<button class="toggle-dropdown push-bottom">Toggle dropdown</button>
<button class="disable push-bottom">Disable</button>
<button class="enable push-bottom">Enable</button>
<select class="form-control" name="choices-basic" id="choices-basic">
Expand Down Expand Up @@ -220,10 +219,6 @@ <h2>Select one inputs</h2>
choicesBasic.hideDropdown();
});

document.querySelector('button.toggle-dropdown').addEventListener('click', () => {
choicesBasic.toggleDropdown(true);
});

document.querySelector('button.disable').addEventListener('click', () => {
choicesBasic.disable();
});
Expand Down
Loading

0 comments on commit 8540d5a

Please sign in to comment.