From 90e518df78d6f05217700dc8d1f3a2e2082dd05a Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Sat, 20 Aug 2016 11:16:52 +0100 Subject: [PATCH] Text input tests --- tests/spec/choices_spec.js | 52 +++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/tests/spec/choices_spec.js b/tests/spec/choices_spec.js index e7ebab8..ca35af4 100644 --- a/tests/spec/choices_spec.js +++ b/tests/spec/choices_spec.js @@ -133,10 +133,11 @@ describe('Choices', function() { document.body.appendChild(this.input); - this.choices = new Choices(this.input); }); it('should accept a user inputted value', function() { + this.choices = new Choices(this.input); + this.choices.input.focus(); this.choices.input.value = 'test'; @@ -150,8 +151,57 @@ describe('Choices', function() { }); it('should copy the passed placeholder to the cloned input', function() { + this.choices = new Choices(this.input); + expect(this.choices.input.placeholder).toEqual(this.input.placeholder); }); + + it('should not allow duplicates if duplicateItems is false', function() { + this.choices = new Choices(this.input, { + duplicateItems: false, + items: ['test 1'], + }); + + this.choices.input.focus(); + this.choices.input.value = 'test 1'; + + this.choices._onKeyDown({ + target: this.choices.input, + keyCode: 13, + ctrlKey: false + }); + + expect(this.choices.currentState.items[this.choices.currentState.items.length - 1]).not.toContain(this.choices.input.value); + }); + + it('should filter input if regexFilter is passed', function() { + this.choices = new Choices(this.input, { + 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,}))$/, + }); + + this.choices.input.focus(); + this.choices.input.value = 'josh@joshuajohnson.co.uk'; + + this.choices._onKeyDown({ + target: this.choices.input, + keyCode: 13, + ctrlKey: false + }); + + this.choices.input.focus(); + this.choices.input.value = 'not an email address'; + + this.choices._onKeyDown({ + target: this.choices.input, + keyCode: 13, + ctrlKey: false + }); + + const lastItem = this.choices.currentState.items[this.choices.currentState.items.length - 1]; + + expect(lastItem.value).toEqual('josh@joshuajohnson.co.uk'); + expect(lastItem.value).not.toEqual('not an email address'); + }); }); describe('should accept single select inputs', function() {