From 897b6bac58e6025f2e0687719d6f6c79437cecd6 Mon Sep 17 00:00:00 2001 From: Philip Dudley Date: Fri, 18 Mar 2016 01:23:01 -0400 Subject: [PATCH] Add test helpers - Adds test helpers for use in acceptance tests and integration tests - Fixes #8 --- README.md | 27 ++++++++++++++++++++ test-support/helpers/ember-test-selectors.js | 9 +++++++ tests/unit/test-support/helpers-test.js | 12 +++++++++ 3 files changed, 48 insertions(+) create mode 100644 test-support/helpers/ember-test-selectors.js create mode 100644 tests/unit/test-support/helpers-test.js diff --git a/README.md b/README.md index 10d90fbc..7ba723d7 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,33 @@ delivered__: ``` +## Testing + +Ember Test Selectors comes with a test helper that can be used in acceptance & integration tests: + +* `testSelector('post-title')`: Returns a selector `[data-test-post-title]` +* `testSelector('resource-id', '2')`: Returns a selector `[data-test-resource-id="2"]` + +The test helpers can be imported from the helpers/ember-test-selectors module in the application's namespace: + +```javascript +import testSelector from '/tests/helpers/ember-test-selectors'; +``` + +### Acceptance Test Usage + +```javascript +find(testSelector('post-title')) // => find('[data-test-post-title]') +find(testSelector('selector', 'post-title')) // => find('[data-test-selector="post-title"]') +``` + +### Integration Test Usage + +```javascript +this.$(testSelector('post-title')).click() // => this.$('[data-test-post-title]').click() +this.$(testSelector('selector', 'post-title')).click() // => this.$('[data-test-selector="post-title"]').click() +``` + ## License ember-test-selectors is developed by and © diff --git a/test-support/helpers/ember-test-selectors.js b/test-support/helpers/ember-test-selectors.js new file mode 100644 index 00000000..0c1ad997 --- /dev/null +++ b/test-support/helpers/ember-test-selectors.js @@ -0,0 +1,9 @@ +export default function testSelector(key, value) { + let selector; + if (value) { + selector = `[data-test-${key}="${value}"]`; + } else { + selector = `[data-test-${key}]`; + } + return selector; +}; diff --git a/tests/unit/test-support/helpers-test.js b/tests/unit/test-support/helpers-test.js new file mode 100644 index 00000000..4f8d1558 --- /dev/null +++ b/tests/unit/test-support/helpers-test.js @@ -0,0 +1,12 @@ +import testSelector from 'dummy/tests/helpers/ember-test-selectors'; +import { module, test } from 'qunit'; + +module('test-support helpers'); + +test('testSelector with a value', function(assert) { + assert.equal(testSelector('selector', 'welcome-text'), '[data-test-selector="welcome-text"]'); +}); + +test('testSelector without a value', function(assert) { + assert.equal(testSelector('selector'), '[data-test-selector]'); +});