From 0792568c98b9a8b28cfb82a91fbc91d9b32051e1 Mon Sep 17 00:00:00 2001 From: Robdel12 Date: Tue, 6 Oct 2015 16:16:52 -0500 Subject: [PATCH] Introduce `select` test helper --- FEATURES.md | 26 ++++++ features.json | 1 + packages/ember-testing/lib/helpers.js | 129 ++++++++++++++++++++++++++ 3 files changed, 156 insertions(+) diff --git a/FEATURES.md b/FEATURES.md index c262888c3f5..44a0ccc7dc1 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -59,3 +59,29 @@ for a detailed explanation. When the proper API's are implemented by the resolver in use this feature allows `{{x-foo}}` in a given routes template (say the `post` route) to lookup a component nested under `post`. + +* `ember-testing-select-helper` + + Add a `select` test helper. + + Say your handlebars file looks like this: + + ```handlebars + + ``` + + You could select the first option like this: + + ```javascript + select('.my-drop-down', 'My Option'); + ``` + + And for multiselecting: + + ```javascript + select('.my-drop-down', 'My Option', 'My Option Two', 'My Option Three'); + ``` diff --git a/features.json b/features.json index 6f8832e0c4a..7aecdc8a367 100644 --- a/features.json +++ b/features.json @@ -10,6 +10,7 @@ "ember-metal-ember-assign": null, "ember-contextual-components": true, "ember-container-inject-owner": true, + "ember-testing-select-helper": null, "ember-htmlbars-local-lookup": null } } diff --git a/packages/ember-testing/lib/helpers.js b/packages/ember-testing/lib/helpers.js index ee1766845ca..69f4b2ca0c3 100644 --- a/packages/ember-testing/lib/helpers.js +++ b/packages/ember-testing/lib/helpers.js @@ -89,6 +89,60 @@ function click(app, selector, context) { return app.testHelpers.wait(); } + +function check(app, selector, context) { + var $el = app.testHelpers.findWithAssert(selector, context); + var type = $el.prop('type'); + + assert( + `To check '${selector}', the input must be a checkbox`, + type === 'checkbox' + ); + + if (!$el.prop('checked')) { + app.testHelpers.click(selector, context); + } + + return app.testHelpers.wait(); +} + +function uncheck(app, selector, context) { + var $el = app.testHelpers.findWithAssert(selector, context); + var type = $el.prop('type'); + + assert( + `To uncheck '${selector}', the input must be a checkbox`, + type === 'checkbox' + ); + + if ($el.prop('checked')) { + app.testHelpers.click(selector, context); + } + + return app.testHelpers.wait(); +} + +function select(app, selector, ...texts) { + let $options = app.testHelpers.findWithAssert(`${selector} option`); + let type = $options.prop('type'); + + assert( + `To select '${selector}', the elment must be a select box`, + type === 'select-one' || type === 'select-multiple' + ); + + $options.each(function() { + let $option = Ember.$(this); + + Ember.run(() => { + this.selected = texts.some(text => $option.is(`:contains('${text}')`)); + $option.trigger('change'); + }); + }); + + return app.testHelpers.wait(); +} + function triggerEvent(app, selector, contextOrType, typeOrOptions, possibleOptions) { var arity = arguments.length; var context, type, options; @@ -250,6 +304,81 @@ asyncHelper('visit', visit); */ asyncHelper('click', click); + +if (isEnabled('ember-testing-checkbox-helpers')) { + /** + Checks a checkbox. Ensures the presence of the `checked` attribute + + Example: + + ```javascript + check('#remember-me').then(function() { + // assert something + }); + ``` + + @method check + @param {String} selector jQuery selector finding an `input[type="checkbox"]` + element on the DOM to check + @return {RSVP.Promise} + @private + */ + asyncHelper('check', check); + + /** + Unchecks a checkbox. Ensures the absence of the `checked` attribute + + Example: + + ```javascript + uncheck('#remember-me').then(function() { + // assert something + }); + ``` + + @method check + @param {String} selector jQuery selector finding an `input[type="checkbox"]` + element on the DOM to uncheck + @return {RSVP.Promise} + @private + */ + asyncHelper('uncheck', uncheck); +} + +if (Ember.FEATURES.isEnabled("ember-testing-select-helper")) { + /** + Selects options in a select box. + + Say you your handlebars file looks like this: + + ```handlebars + + ``` + + You could select the first option like this: + + ```javascript + select('.my-drop-down', 'My Option'); + ``` + + And for multiselecting: + + ```javascript + select('.my-drop-down', 'My Option', 'My Option Two', 'My Option Three'); + ``` + + @method select + @param {String} selector jQuery selector finding a `