From 865ba7d485457077fce38e0dda25cb2c92b33349 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Thu, 14 Jul 2016 23:21:28 +0100 Subject: [PATCH] Build --- assets/scripts/src/choices.js | 2 +- index.html | 6 +-- package.json | 4 +- tests/spec/choices_spec.js | 73 ++++++++++++++++++++++++++++++++++- 4 files changed, 78 insertions(+), 7 deletions(-) diff --git a/assets/scripts/src/choices.js b/assets/scripts/src/choices.js index 44995c5..1276734 100644 --- a/assets/scripts/src/choices.js +++ b/assets/scripts/src/choices.js @@ -122,7 +122,7 @@ export class Choices { if(canInit) { // If element has already been initalised with Choices - if(this.passedElement.getAttribute('data-choice') === 'active') return + if(this.passedElement.getAttribute('data-choice') === 'active') return; // Let's go this.init(); diff --git a/index.html b/index.html index d106cef..838abc9 100644 --- a/index.html +++ b/index.html @@ -206,9 +206,7 @@ }); }); - const choices14 = new Choices('#choices-14', { - logState: true - }).ajax((callback) => { + const choices14 = new Choices('#choices-14').ajax((callback) => { fetch('https://restcountries.eu/rest/v1/all') .then((response) => { response.json().then((data) => { @@ -218,7 +216,7 @@ .catch((error) => { console.log(error); }); - }) + }); const choicesMultiple = new Choices('[data-choice]', { placeholderValue: 'This is a placeholder set in the config', diff --git a/package.json b/package.json index ac90016..c126aa0 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "babel-preset-es2015": "^6.6.0", "babel-preset-stage-0": "^6.5.0", "csso": "^1.7.0", + "es6-promise": "^3.2.1", "eslint": "^2.4.0", "fuse.js": "^2.2.0", "jasmine-core": "^2.4.1", @@ -49,7 +50,8 @@ "postcss-cli": "^2.5.1", "sifter": "^0.4.5", "webpack": "^1.12.14", - "webpack-dev-server": "^1.14.1" + "webpack-dev-server": "^1.14.1", + "whatwg-fetch": "^1.0.0" }, "dependencies": { "redux": "^3.3.1" diff --git a/tests/spec/choices_spec.js b/tests/spec/choices_spec.js index c54e887..d9f8bec 100644 --- a/tests/spec/choices_spec.js +++ b/tests/spec/choices_spec.js @@ -1,3 +1,5 @@ +import 'whatwg-fetch'; +import 'es6-promise'; import Choices from '../../assets/scripts/src/choices.js'; describe('Choices', function() { @@ -26,6 +28,12 @@ describe('Choices', function() { expect(this.choices.initialised).toBe(true); }); + it('should not re-initialise if passed element again', function() { + const reinitialise = new Choices(this.choices.passedElement); + spyOn(reinitialise, '_createTemplates'); + expect(reinitialise._createTemplates).not.toHaveBeenCalled(); + }) + it('should have a blank state', function() { const blankState = { items: [], @@ -35,7 +43,31 @@ describe('Choices', function() { expect(this.choices.currentState).toEqual(blankState); }); - it('should expose public methods', function(){ + it('should have config options', function() { + expect(this.choices.config.items).toEqual(jasmine.any(Array)); + expect(this.choices.config.maxItemCount).toEqual(jasmine.any(Number)); + expect(this.choices.config.addItems).toEqual(jasmine.any(Boolean)); + expect(this.choices.config.removeItems).toEqual(jasmine.any(Boolean)); + expect(this.choices.config.removeItemButton).toEqual(jasmine.any(Boolean)); + expect(this.choices.config.editItems).toEqual(jasmine.any(Boolean)); + expect(this.choices.config.duplicateItems).toEqual(jasmine.any(Boolean)); + expect(this.choices.config.delimiter).toEqual(jasmine.any(String)); + expect(this.choices.config.paste).toEqual(jasmine.any(Boolean)); + expect(this.choices.config.search).toEqual(jasmine.any(Boolean)); + expect(this.choices.config.regexFilter).toEqual(null); + expect(this.choices.config.placeholder).toEqual(jasmine.any(Boolean)); + expect(this.choices.config.placeholderValue).toEqual(null); + expect(this.choices.config.prependValue).toEqual(null); + expect(this.choices.config.appendValue).toEqual(null); + expect(this.choices.config.loadingText).toEqual(jasmine.any(String)); + expect(this.choices.config.classNames).toEqual(jasmine.any(Object)); + expect(this.choices.config.callbackOnInit).toEqual(jasmine.any(Function)); + expect(this.choices.config.callbackOnAddItem).toEqual(jasmine.any(Function)); + expect(this.choices.config.callbackOnRemoveItem).toEqual(jasmine.any(Function)); + expect(this.choices.config.callbackOnRender).toEqual(jasmine.any(Function)); + }); + + it('should expose public methods', function() { expect(this.choices.init).toEqual(jasmine.any(Function)); expect(this.choices.destroy).toEqual(jasmine.any(Function)); expect(this.choices.highlightItem).toEqual(jasmine.any(Function)); @@ -242,4 +274,43 @@ describe('Choices', function() { expect(this.choices.input.placeholder).toEqual('Placeholder text'); }); }); + + describe('should handle AJAX-populated choices', function() { + beforeEach(function() { + this.input = document.createElement('select'); + this.input.className = 'js-choices'; + this.input.multiple = false; + this.input.placeholder = 'Placeholder text'; + + for (let i = 1; i < 4; i++) { + const option = document.createElement('option'); + + option.value = `Value ${i}`; + option.innerHTML = `Value ${i}`; + + this.input.appendChild(option); + } + + document.body.appendChild(this.input); + + this.choices = new Choices(this.input); + spyOn(this.choices, 'ajax'); + + this.choices.ajax((callback) => { + fetch('https://restcountries.eu/rest/v1/all') + .then((response) => { + response.json().then((data) => { + callback(data, 'alpha2Code', 'name'); + }); + }) + .catch((error) => { + console.log(error); + }); + }); + }); + + it('should call ajax()', function() { + expect(this.choices.ajax).toHaveBeenCalledWith(jasmine.any(Function)); + }); + }); });