-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from jkusa/test-support
Test support helpers
- Loading branch information
Showing
6 changed files
with
289 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import Test from 'ember-test'; | ||
import Ember from 'ember'; | ||
|
||
const { run } = Ember; | ||
|
||
/* === Integration Test Helpers === */ | ||
|
||
/** | ||
* Fires `success` action for an instance of a copy-button component | ||
* @function triggerSuccess | ||
* @param {Object} context - integration test’s this context | ||
* @param {String|Element} selector - selector of the copy-button instance | ||
* @returns {Void} | ||
*/ | ||
export function triggerSuccess(context, selector) { | ||
fireComponentAction(context, selector, 'success'); | ||
} | ||
|
||
/** | ||
* Fires `error` action for an instance of a copy-button component | ||
* @function triggerError | ||
* @param {Object} context - integration test’s this context | ||
* @param {String|Element} selector - selector of the copy-button instance | ||
* @returns {Void} | ||
*/ | ||
export function triggerError(context, selector) { | ||
fireComponentAction(context, selector, 'error'); | ||
} | ||
|
||
/* === Acceptance Test Helpers === */ | ||
|
||
/** | ||
* Default export is a function that registers acceptance test helpers | ||
*/ | ||
export default function() { | ||
Test.registerAsyncHelper('triggerCopySuccess', function(app, selector='.copy-btn') { | ||
fireComponentActionFromApp(app, selector, 'success'); | ||
}); | ||
|
||
Test.registerAsyncHelper('triggerCopyError', function(app, selector='.copy-btn') { | ||
fireComponentActionFromApp(app, selector, 'error'); | ||
}); | ||
} | ||
|
||
/* === Private Functions === */ | ||
|
||
/** | ||
* Fires named action for an instance of a copy-button component in an app | ||
* @function fireComponentActionFromApp | ||
* @param {Object} app - Ember application | ||
* @param {String|Element} selector - selector of the copy-button instance | ||
* @param {String} actionName - name of action | ||
* @returns {Void} | ||
*/ | ||
function fireComponentActionFromApp(app, selector, actionName) { | ||
fireComponentAction({ | ||
container: app.__container__, | ||
$: app.$ | ||
}, selector, actionName); | ||
} | ||
|
||
/** | ||
* Fires named action for an instance of a copy-button component | ||
* @function fireComponentAction | ||
* @param {Object} context - test context | ||
* @param {String|Element} selector - selector of the copy-button instance | ||
* @param {String} actionName - name of action | ||
* @returns {Void} | ||
*/ | ||
function fireComponentAction(context, selector, actionName) { | ||
let component = getComponentBySelector(context, selector); | ||
fireActionByName(component, actionName); | ||
} | ||
|
||
/** | ||
* Fetches component reference for a given context and selector | ||
* @function getComponentBySelector | ||
* @param {Object} context - test context | ||
* @param {String|Element} selector - selector of the copy-button instance | ||
* @returns {Object} component object | ||
*/ | ||
function getComponentBySelector(context, selector='.copy-btn') { | ||
let emberId = context.$(selector).attr('id'); | ||
return context.container.lookup('-view-registry:main')[emberId]; | ||
} | ||
|
||
/** | ||
* Fires a component's action given an action name | ||
* @function fireActionByName | ||
* @param {Ember.Component} component - component to fire action from | ||
* @param {String} actionName - name of action | ||
* @returns {Void} | ||
*/ | ||
function fireActionByName(component, actionName) { | ||
let action = component[actionName]; | ||
|
||
run(() => { | ||
if (typeof action === 'string') { | ||
component.sendAction(action); | ||
} else { | ||
action(); | ||
} | ||
}); | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { test } from 'qunit'; | ||
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance'; | ||
|
||
moduleForAcceptance('Acceptance | test helpers'); | ||
|
||
test('test-helpers', function(assert) { | ||
assert.expect(3); | ||
|
||
visit('/'); | ||
andThen(() => { | ||
assert.notOk(!!find('.alert').length, | ||
'no alert message is initially present'); | ||
}); | ||
|
||
triggerCopySuccess(); | ||
|
||
andThen(() => { | ||
assert.ok(!!find('.alert.alert-success').length, | ||
'a success message is displayed when a copy is successful'); | ||
}); | ||
|
||
triggerCopyError(); | ||
|
||
andThen(() => { | ||
assert.ok(!!find('.alert.alert-info').length, | ||
'an error message is displayed when a copy is unsuccessful'); | ||
}); | ||
}); |
This file contains 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
This file contains 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