Skip to content

Instantly share code, notes, and snippets.

@tim-evans
Last active January 29, 2016 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tim-evans/5982357f7df5c0c045dd to your computer and use it in GitHub Desktop.
Save tim-evans/5982357f7df5c0c045dd to your computer and use it in GitHub Desktop.
Module for Integration
import Ember from 'ember';
import { module } from 'qunit';
import startApp from '../helpers/start-app';
import destroyApp from '../helpers/destroy-app';
import MockFeatures from '../mocks/features';
export default function (name, options) {
module(name, {
beforeEach() {
let context = this;
this.application = startApp();
this.container = this.application.__container__;
this.registry = this.container._registry;
// Expose `register` as an outlet to register
// mock factories into the container
this.register = function (name, factory, options) {
this.registry.register(...arguments);
// Hack to return the injected factory rather than
// the one returned by the resolver.
this.registry._resolveCache[name] = factory;
return factory;
};
// Expose `inject.service` and `inject.controller`
// to the test context. Using these will set the
// property directly onto the test context, allowing
// for mutation of properties
let keys = Object.keys(Ember.inject);
this.inject = keys.reduce(function (E, typeName) {
E[typeName] = function (name, opts) {
let alias = (opts && opts.as) || name;
Ember.set(context, alias, context.container.lookup(typeName + ':' + name));
};
return E;
}, {});
let features = {};
this.register('service:features', MockFeatures(features));
this.enableFeature = function (featureName) {
features[featureName] = true;
};
this.disableFeature = function (featureName) {
delete features[featureName];
};
if (options.beforeEach) {
options.beforeEach.apply(this, arguments);
}
},
afterEach() {
destroyApp(this.application);
if (options.afterEach) {
options.afterEach.apply(this, arguments);
}
}
});
}
import Ember from 'ember';
import { moduleForComponent } from 'ember-qunit';
export default function (name, options={}) {
moduleForComponent(name, name, {
integration: true,
setup() {
this.select = function (from, text) {
let $el = this.$(from);
let $option = $el.find(`option:contains(${text})`);
Ember.run(function () {
$option.prop("selected", true);
$el.trigger('change');
});
};
this.text = function (selector) {
return this.$(selector).text();
};
this.click = function (selector) {
let $el = this.$(selector);
Ember.run($el, 'mousedown');
this.focus(selector);
Ember.run($el, 'mouseup');
Ember.run($el, 'click');
};
this.check = function (selector) {
let $el = this.$(selector);
let type = $el.prop('type');
Ember.assert(`To check '${selector}', the input must be a checkbox`,
type === 'checkbox');
if (!$el.prop('checked')) {
this.click(selector);
}
};
this.uncheck = function (selector) {
let $el = this.$(selector);
let type = $el.prop('type');
Ember.assert(`To check '${selector}', the input must be a checkbox`,
type === 'checkbox');
if ($el.prop('checked')) {
this.click(selector);
}
};
this.focus = function (selector) {
let $el = this.$(selector);
if (!document.hasFocus || document.hasFocus()) {
Ember.run($el, 'focus');
} else {
Ember.run($el, 'trigger', 'focusin');
}
};
this.blur = function (selector) {
let $el = this.$(selector);
if (!document.hasFocus || document.hasFocus()) {
Ember.run($el, 'blur');
} else {
Ember.run($el, 'trigger', 'focusout');
}
};
this.fillIn = function (selector, text) {
let $el = this.$(selector);
this.focus(selector);
Ember.run(function () {
$el.val(text);
$el.trigger('input');
$el.change();
});
};
this.keyEvent = function (selector, type, keyCode) {
this.triggerEvent(selector, type, {
keyCode,
which: keyCode
});
};
this.triggerEvent = function (selector, type, options) {
let $el = this.$(selector);
let evt = Ember.$.Event(type, options);
Ember.run($el, 'trigger', evt);
};
if (options.beforeEach) {
options.beforeEach.apply(this, arguments);
}
},
teardown() {
if (options.afterEach) {
options.afterEach.apply(this, arguments);
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment