diff --git a/lib/assets/javascripts/spec/ariatiseFormSpec.js b/lib/assets/javascripts/spec/ariatiseFormSpec.js index 439753d..da91c0d 100644 --- a/lib/assets/javascripts/spec/ariatiseFormSpec.js +++ b/lib/assets/javascripts/spec/ariatiseFormSpec.js @@ -5,7 +5,7 @@ beforeEach(() => { this.submitButton = '[type="submit"]'; - this.form = fixture.load('form.html'); + $('body').html(fixture.load('form.html')); ariatiseForm({ selector: `#${$('form').attr('id')}` }); // Override the form submission, we are just going to validate the ariatisation of the form $('form').submit((e) => { e.preventDefault(); }); @@ -13,8 +13,8 @@ afterEach(() => { fixture.cleanup(); + $('body').html(''); }); - it('adds an error message section to fields with `aria-required="true"` or `data-validation="[type]"`', () => { $.each($('input'), (idx, el) => { const block = $(`#${$(el).attr('aria-describedby')}`); @@ -27,14 +27,11 @@ } }); }); - it('invalid data causes errors to appear', () => { - // const spyEvent = spyOnEvent(this.submitButton, 'click'); - $(this.submitButton).click(); - - // TODO: For some reason the Spy does not catch the button click :/ - // expect('click').toHaveBeenPreventedOn(this.submitButton); - // expect(spyEvent).toHaveBeenPrevented(); + const spyEvent = spyOnEvent(this.submitButton, 'click'); + $(this.submitButton).trigger('click'); + expect('click').toHaveBeenPreventedOn(this.submitButton); + expect(spyEvent).toHaveBeenPrevented(); // Only the aria-required="true" errors should be visible $.each($('input[aria-required="true"]'), (idx, el) => { @@ -50,7 +47,6 @@ expect($(block)).not.toBeVisible(); }); }); - it('text validation is working', () => { ['#text-required'].forEach((selector) => { const block = $(`#${$(selector).attr('aria-describedby')}`); diff --git a/lib/assets/javascripts/spec/fixtures/form.html b/lib/assets/javascripts/spec/fixtures/form.html index bf06e68..728a244 100644 --- a/lib/assets/javascripts/spec/fixtures/form.html +++ b/lib/assets/javascripts/spec/fixtures/form.html @@ -25,23 +25,23 @@

Fields that are required

- +
- +
- +
diff --git a/lib/assets/javascripts/spec/tinymceSpec.js b/lib/assets/javascripts/spec/tinymceSpec.js new file mode 100644 index 0000000..06266be --- /dev/null +++ b/lib/assets/javascripts/spec/tinymceSpec.js @@ -0,0 +1,50 @@ +import { Tinymce } from '../utils/tinymce'; + +beforeEach(() => { + $('body').append(''); + $('body').append(''); + Tinymce.init({ selector: '.test' }); +}); + +describe('findEditorsByClassName test suite', () => { + it('expect two editors with class name test', () => expect(Tinymce.findEditorsByClassName('test').length).toBe(2)); + it('expect zero editors with class name whatever', () => expect(Tinymce.findEditorsByClassName('whatever').length).toBe(0)); +}); + +describe('findEditorById test suite', () => { + it('expect editor with id test1 to be defined', () => expect(Tinymce.findEditorById('test1')).toBeDefined()); + it('expect editor with id test2 to be defined', () => expect(Tinymce.findEditorById('test2')).toBeDefined()); + it('expect editor with id whatever to be undefined', () => expect(Tinymce.findEditorById('whatever')).not.toBeDefined()); +}); + +describe('destroyEditorsByClassName test suite', () => { + it('expect remaining two editors', () => { + Tinymce.destroyEditorsByClassName('whatever'); + expect(Tinymce.findEditorsByClassName('test').length).toBe(2); + }); + it('expect remaining zero editors', () => { + Tinymce.destroyEditorsByClassName('test'); + expect(Tinymce.findEditorsByClassName('test').length).toBe(0); + }); +}); + +describe('destroyEditorsById test suite', () => { + it('expect remaining two editors', () => { + Tinymce.destroyEditorById('test3'); + expect(Tinymce.findEditorsByClassName('test').length).toBe(2); + }); + it('expect remaining one editor', () => { + Tinymce.destroyEditorById('test1'); + expect(Tinymce.findEditorsByClassName('test').length).toBe(1); + }); + it('expect remaining zero editors', () => { + Tinymce.destroyEditorById('test1'); + Tinymce.destroyEditorById('test2'); + expect(Tinymce.findEditorsByClassName('test').length).toBe(0); + }); +}); + +afterEach(() => { + $('body').html(''); +}); + diff --git a/lib/assets/javascripts/utils/ariatiseForm.js b/lib/assets/javascripts/utils/ariatiseForm.js index 2471742..fdd417a 100644 --- a/lib/assets/javascripts/utils/ariatiseForm.js +++ b/lib/assets/javascripts/utils/ariatiseForm.js @@ -149,6 +149,7 @@ // Bind validations to the form's submit button $(`${options.selector} [type="submit"]`).click((e) => { + let anyInvalid = false; validatable.each((i, el) => { const type = getValidationTypeForElement(el); const required = ($(el).attr('aria-required') && $(el).attr('aria-required') === 'true'); @@ -158,11 +159,14 @@ if (isValid(type, getValue(type, el))) { valid(el); } else { - e.preventDefault(); + anyInvalid = true; invalid(el, getValidationMessage(type)); } } }); + if (anyInvalid) { + e.preventDefault(); + } }); } }; diff --git a/lib/assets/javascripts/utils/tinymceSpec.js b/lib/assets/javascripts/utils/tinymceSpec.js deleted file mode 100644 index 2c6439f..0000000 --- a/lib/assets/javascripts/utils/tinymceSpec.js +++ /dev/null @@ -1,50 +0,0 @@ -import { Tinymce } from './tinymce'; - -beforeEach(() => { - $('body').append(''); - $('body').append(''); - Tinymce.init({ selector: '.test' }); -}); - -describe('findEditorsByClassName test suite', () => { - it('expect two editors with class name test', () => expect(Tinymce.findEditorsByClassName('test').length).toBe(2)); - it('expect zero editors with class name whatever', () => expect(Tinymce.findEditorsByClassName('whatever').length).toBe(0)); -}); - -describe('findEditorById test suite', () => { - it('expect editor with id test1 to be defined', () => expect(Tinymce.findEditorById('test1')).toBeDefined()); - it('expect editor with id test2 to be defined', () => expect(Tinymce.findEditorById('test2')).toBeDefined()); - it('expect editor with id whatever to be undefined', () => expect(Tinymce.findEditorById('whatever')).not.toBeDefined()); -}); - -describe('destroyEditorsByClassName test suite', () => { - it('expect remaining two editors', () => { - Tinymce.destroyEditorsByClassName('whatever'); - expect(Tinymce.findEditorsByClassName('test').length).toBe(2); - }); - it('expect remaining zero editors', () => { - Tinymce.destroyEditorsByClassName('test'); - expect(Tinymce.findEditorsByClassName('test').length).toBe(0); - }); -}); - -describe('destroyEditorsById test suite', () => { - it('expect remaining two editors', () => { - Tinymce.destroyEditorById('test3'); - expect(Tinymce.findEditorsByClassName('test').length).toBe(2); - }); - it('expect remaining one editor', () => { - Tinymce.destroyEditorById('test1'); - expect(Tinymce.findEditorsByClassName('test').length).toBe(1); - }); - it('expect remaining zero editors', () => { - Tinymce.destroyEditorById('test1'); - Tinymce.destroyEditorById('test2'); - expect(Tinymce.findEditorsByClassName('test').length).toBe(0); - }); -}); - -afterEach(() => { - $('body').html(''); -}); - diff --git a/lib/assets/karma.conf.js b/lib/assets/karma.conf.js index a48ab6e..63ead47 100644 --- a/lib/assets/karma.conf.js +++ b/lib/assets/karma.conf.js @@ -9,11 +9,12 @@ // frameworks to use // available frameworks: https://npmjs.org/browse/keyword/karma-adapter - frameworks: ['fixture', 'jquery-3.2.1', 'jasmine-jquery', 'jasmine'], + frameworks: ['fixture', 'jquery-3.2.1', 'jasmine'], // list of files / patterns to load in the browser files: [ './node_modules/phantomjs-polyfill/bind-polyfill.js', + './node_modules/jasmine-jquery/lib/jasmine-jquery.js', './javascripts/**/*Spec.js', 'javascripts/spec/fixtures/**/*', ], diff --git a/lib/assets/package.json b/lib/assets/package.json index 2d0a2b5..21e1786 100644 --- a/lib/assets/package.json +++ b/lib/assets/package.json @@ -43,12 +43,12 @@ "extract-text-webpack-plugin": "^3.0.0", "file-loader": "^0.11.2", "jasmine-core": "^2.7.0", + "jasmine-jquery": "^2.1.1", "karma": "^1.7.0", "karma-chrome-launcher": "^2.2.0", "karma-fixture": "^0.2.6", "karma-html2js-preprocessor": "^1.1.0", "karma-jasmine": "^1.1.0", - "karma-jasmine-jquery": "^0.1.1", "karma-jquery": "^0.2.2", "karma-json-fixtures-preprocessor": "0.0.6", "karma-webpack": "^2.0.4",