From 81b2e23d0da05570f6eb12438d98f5584ea36325 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Thu, 11 Oct 2018 19:33:19 +0100 Subject: [PATCH] Use test.html for testing + add additional tests --- cypress/integration/choices_spec.js | 108 +++++++++++++++++++------- public/test.html | 115 ++++++++++++++++++++++++++++ src/scripts/choices.js | 10 ++- 3 files changed, 202 insertions(+), 31 deletions(-) create mode 100644 public/test.html diff --git a/cypress/integration/choices_spec.js b/cypress/integration/choices_spec.js index a1bdf60..28f9aa6 100644 --- a/cypress/integration/choices_spec.js +++ b/cypress/integration/choices_spec.js @@ -1,28 +1,12 @@ describe('Choices', () => { beforeEach(() => { - cy.visit('/'); + cy.visit('/test.html'); }); describe('text element', () => { + const textInput = 'testing'; + describe('adding choices', () => { - const textInput = 'testing'; - - it('shows a dropdown prompt when inputting data', () => { - cy.get('.choices') - .first() - .find('.choices__input--cloned') - .type(textInput); - - cy.get('.choices') - .first() - .find('.choices__list--dropdown') - .should('be.visible') - .should($dropdown => { - const dropdownText = $dropdown.text().trim(); - expect(dropdownText).to.equal(`Press Enter to add "${textInput}"`); - }); - }); - it('allows me to input choices', () => { cy.get('.choices') .first() @@ -39,8 +23,28 @@ describe('Choices', () => { }); }); + describe('inputting data', () => { + it('shows a dropdown prompt', () => { + cy.get('.choices') + .first() + .find('.choices__input--cloned') + .type(textInput); + + cy.get('.choices') + .first() + .find('.choices__list--dropdown') + .should('be.visible') + .should($dropdown => { + const dropdownText = $dropdown.text().trim(); + expect(dropdownText).to.equal( + `Press Enter to add "${textInput}"`, + ); + }); + }); + }); + describe('input limit', () => { - it('does not let me input more than 5 choices', () => { + beforeEach(() => { for (let index = 0; index < 6; index++) { cy.get('.choices') .first() @@ -48,35 +52,81 @@ describe('Choices', () => { .type(`${textInput} + ${index}`) .type('{enter}'); } + }); + it('does not let me input more than 5 choices', () => { cy.get('.choices') .first() - .find('.choices__list') + .find('.choices__list--multiple') .first() .children() .should($items => { expect($items.length).to.equal(5); }); }); + + it('hides dropdown prompt once limit has been reached', () => { + cy.get('.choices') + .first() + .find('.choices__list--dropdown') + .should('not.be.visible'); + }); }); - describe.skip('unique values', () => { - it('only allows me to input unique values', () => { + describe('unique values', () => { + beforeEach(() => { cy.get('.choices') .eq(1) // second choices instance - .find('.choices__list .choices__item') - .should($choices => { - expect($choices.length).to.equal(0); + .find('.choices__input--cloned') + .type(`${textInput}`) + .type('{enter}') + .type(`${textInput}`) + .type('{enter}'); + }); + + it('only allows me to input unique values', () => { + cy.get('.choices') + .eq(1) + .find('.choices__list--multiple') + .first() + .children() + .should($items => { + expect($items.length).to.equal(1); }); }); + + describe('inputting a non-unique value', () => { + it('displays dropdown prompt', () => { + cy.get('.choices') + .eq(1) + .find('.choices__list--dropdown') + .should('be.visible') + .should($dropdown => { + const dropdownText = $dropdown.text().trim(); + expect(dropdownText).to.equal( + 'Only unique values can be added.', + ); + }); + }); + }); }); }); describe('removing choices', () => { + beforeEach(() => { + cy.get('.choices') + .first() // second choices instance + .find('.choices__input--cloned') + .type(`${textInput}`) + .type('{enter}') + .type(`${textInput}`) + .type('{enter}'); + }); + it('allows me to remove inputted choices', () => { cy.get('.choices') .first() - .find('.choices__list') + .find('.choices__list--multiple') .first() .children() .should($items => { @@ -85,7 +135,7 @@ describe('Choices', () => { cy.get('.choices') .first() - .find('.choices__list .choices__item') + .find('.choices__list--multiple .choices__item') .last() .find('.choices__button') .focus() @@ -93,7 +143,7 @@ describe('Choices', () => { cy.get('.choices') .first() - .find('.choices__list') + .find('.choices__list--multiple') .first() .should($items => { expect($items.length).to.equal(1); diff --git a/public/test.html b/public/test.html new file mode 100644 index 0000000..c8fbdb6 --- /dev/null +++ b/public/test.html @@ -0,0 +1,115 @@ + + + + + + + Choices + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ +

Text inputs

+ + + + + + + + + + + + + + + +
+ +

Multiple select input

+ +
+ +

Single select input

+ +
+
+ + + + + + + + + diff --git a/src/scripts/choices.js b/src/scripts/choices.js index 00ac946..ea5096a 100644 --- a/src/scripts/choices.js +++ b/src/scripts/choices.js @@ -71,6 +71,12 @@ class Choices { ? document.querySelector(element) : element; + if (!passedElement) { + return console.error( + 'Could not find passed element or passed element was of an invalid type', + ); + } + this._isTextElement = passedElement.type === 'text'; this._isSelectOneElement = passedElement.type === 'select-one'; this._isSelectMultipleElement = passedElement.type === 'select-multiple'; @@ -90,8 +96,8 @@ class Choices { }); } - if (!this.passedElement) { - throw new Error('Could not wrap passed element'); + if (!passedElement) { + return console.error('Passed element was of an invalid type'); } if (