Forked from code0100fun/tests_acceptance_foo-test.js
Created
September 7, 2016 03:12
-
-
Save plicjo/0e2ba40a41ea77fd965ce5d929faac67 to your computer and use it in GitHub Desktop.
Ember CLI QUnit text content helpers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// tests/acceptance/foo-test.js | |
// Assert that text should be found | |
assert.hasText('Not Found'); // Error: Could not find text "Not Found" on the page | |
// Provide custom message | |
assert.hasText('Not Found', 'Expected to find "Not Found"'); // Error: Expected to find "Not Found" | |
// Find any number of elements containing the query text | |
text('Found'); // [<div>Found</div>, <input value="Found">] | |
// Scope selector to type | |
text('Found', 'div'); // [<div>Found</div>] | |
// Find buttons | |
button('Sign In'); // [<input type="submit" value="Sign In">] | |
// Use with existing helpers | |
click(button('Sign In')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// tests/helpers/start-app.js | |
//... | |
import text from './text'; /* jshint unused:false */ | |
//... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import QUnit from 'qunit'; | |
var mergeUnique = function(){ | |
return $.unique($.merge.apply($, arguments)); | |
}; | |
var withValue = function(text, scope) { | |
return find(`${scope || ''}[value*="${text}"]`); | |
}; | |
var directlyContains = function(text, scope){ | |
var foundText = find(`${scope || ''}:contains(${text})`).filter(function() { | |
return ( | |
$(this).clone() //clone the element | |
.children() //select all the children | |
.remove() //remove all the children | |
.end() //again go back to selected element | |
.filter(`:contains(${text})`).length > 0); | |
}); | |
var foundValue = withValue(text, scope); | |
return mergeUnique(foundText, foundValue); | |
}; | |
var textHelpers = function(){ | |
Ember.Test.registerHelper('button', function (app, text) { | |
return mergeUnique( | |
withValue(text, 'input'), | |
directlyContains(text, 'button')); | |
}); | |
Ember.Test.registerHelper('text', function (app, text) { | |
return directlyContains(text); | |
}); | |
QUnit.assert.hasText = function(text, message) { | |
var element = directlyContains(text); | |
QUnit.assert.ok(element.length, message || `Could not find text "${text}" on the page`); | |
}; | |
}(); | |
export default textHelpers; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment