Skip to content

Commit

Permalink
Avoid duplication between test-module-for-component and test-module-f…
Browse files Browse the repository at this point in the history
…or-integration.
  • Loading branch information
Robert Jackson committed Aug 1, 2016
1 parent c1a362c commit 7394b86
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 296 deletions.
296 changes: 149 additions & 147 deletions lib/ember-test-helpers/test-module-for-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,153 +136,7 @@ export default TestModule.extend({
if (!hasEmberVersion(1,13)) {
return preGlimmerSetupIntegrationForComponent;
} else {
return function() {
var module = this;
var context = this.context;

this.actionHooks = context[ACTION_KEY] = {};
context.dispatcher = this.container.lookup('event_dispatcher:main') || Ember.EventDispatcher.create();
context.dispatcher.setup({}, '#ember-testing');

var hasRendered = false;
var OutletView = module.container.lookupFactory('view:-outlet');
var OutletTemplate = module.container.lookup('template:-outlet');
var toplevelView = module.component = OutletView.create();
var hasOutletTemplate = !!OutletTemplate;
var outletState = {
render: {
owner: getOwner ? getOwner(module.container) : undefined,
into: undefined,
outlet: 'main',
name: 'application',
controller: module.context,
ViewClass: undefined,
template: OutletTemplate
},

outlets: { }
};

var element = document.getElementById('ember-testing');
var templateId = 0;

if (hasOutletTemplate) {
Ember.run(() => {
toplevelView.setOutletState(outletState);
});
}

context.render = function(template) {
if (!template) {
throw new Error("in a component integration test you must pass a template to `render()`");
}
if (Ember.isArray(template)) {
template = template.join('');
}
if (typeof template === 'string') {
template = Ember.Handlebars.compile(template);
}

var templateFullName = 'template:-undertest-' + (++templateId);
this.registry.register(templateFullName, template);
var stateToRender = {
owner: getOwner ? getOwner(module.container) : undefined,
into: undefined,
outlet: 'main',
name: 'index',
controller: module.context,
ViewClass: undefined,
template: module.container.lookup(templateFullName),
outlets: { }
};

if (hasOutletTemplate) {
stateToRender.name = 'index';
outletState.outlets.main = { render: stateToRender, outlets: {} };
} else {
stateToRender.name = 'application';
outletState = { render: stateToRender, outlets: {} };
}

Ember.run(() => {
toplevelView.setOutletState(outletState);
});

if (!hasRendered) {
Ember.run(module.component, 'appendTo', '#ember-testing');
hasRendered = true;
}

// ensure the element is based on the wrapping toplevel view
// Ember still wraps the main application template with a
// normal tagged view
element = Ember.$('#ember-testing > .ember-view');
};

context.$ = function(selector) {
// emulates Ember internal behavor of `this.$` in a component
// https://github.com/emberjs/ember.js/blob/v2.5.1/packages/ember-views/lib/views/states/has_element.js#L18
return selector ? Ember.$(selector, element) : Ember.$(element);
};

context.set = function(key, value) {
var ret = Ember.run(function() {
return Ember.set(context, key, value);
});

if (hasEmberVersion(2,0)) {
return ret;
}
};

context.setProperties = function(hash) {
var ret = Ember.run(function() {
return Ember.setProperties(context, hash);
});

if (hasEmberVersion(2,0)) {
return ret;
}
};

context.get = function(key) {
return Ember.get(context, key);
};

context.getProperties = function() {
var args = Array.prototype.slice.call(arguments);
return Ember.getProperties(context, args);
};

context.on = function(actionName, handler) {
module.actionHooks[actionName] = handler;
};

context.send = function(actionName) {
var hook = module.actionHooks[actionName];
if (!hook) {
throw new Error("integration testing template received unexpected action " + actionName);
}
hook.apply(module.context, Array.prototype.slice.call(arguments, 1));
};

context.clearRender = function() {
Ember.run(function() {
toplevelView.setOutletState({
render: {
owner: module.container,
into: undefined,
outlet: 'main',
name: 'application',
controller: module.context,
ViewClass: undefined,
template: undefined
},
outlets: {}
});
});
};
};
return setupComponentIntegrationTest;
}
})(),

Expand All @@ -308,3 +162,151 @@ export default TestModule.extend({
}
}
});

export function setupComponentIntegrationTest() {
var module = this;
var context = this.context;

this.actionHooks = context[ACTION_KEY] = {};
context.dispatcher = this.container.lookup('event_dispatcher:main') || Ember.EventDispatcher.create();
context.dispatcher.setup({}, '#ember-testing');

var hasRendered = false;
var OutletView = module.container.lookupFactory('view:-outlet');
var OutletTemplate = module.container.lookup('template:-outlet');
var toplevelView = module.component = OutletView.create();
var hasOutletTemplate = !!OutletTemplate;
var outletState = {
render: {
owner: getOwner ? getOwner(module.container) : undefined,
into: undefined,
outlet: 'main',
name: 'application',
controller: module.context,
ViewClass: undefined,
template: OutletTemplate
},

outlets: { }
};

var element = document.getElementById('ember-testing');
var templateId = 0;

if (hasOutletTemplate) {
Ember.run(() => {
toplevelView.setOutletState(outletState);
});
}

context.render = function(template) {
if (!template) {
throw new Error("in a component integration test you must pass a template to `render()`");
}
if (Ember.isArray(template)) {
template = template.join('');
}
if (typeof template === 'string') {
template = Ember.Handlebars.compile(template);
}

var templateFullName = 'template:-undertest-' + (++templateId);
this.registry.register(templateFullName, template);
var stateToRender = {
owner: getOwner ? getOwner(module.container) : undefined,
into: undefined,
outlet: 'main',
name: 'index',
controller: module.context,
ViewClass: undefined,
template: module.container.lookup(templateFullName),
outlets: { }
};

if (hasOutletTemplate) {
stateToRender.name = 'index';
outletState.outlets.main = { render: stateToRender, outlets: {} };
} else {
stateToRender.name = 'application';
outletState = { render: stateToRender, outlets: {} };
}

Ember.run(() => {
toplevelView.setOutletState(outletState);
});

if (!hasRendered) {
Ember.run(module.component, 'appendTo', '#ember-testing');
hasRendered = true;
}

// ensure the element is based on the wrapping toplevel view
// Ember still wraps the main application template with a
// normal tagged view
element = Ember.$('#ember-testing > .ember-view');
};

context.$ = function(selector) {
// emulates Ember internal behavor of `this.$` in a component
// https://github.com/emberjs/ember.js/blob/v2.5.1/packages/ember-views/lib/views/states/has_element.js#L18
return selector ? Ember.$(selector, element) : Ember.$(element);
};

context.set = function(key, value) {
var ret = Ember.run(function() {
return Ember.set(context, key, value);
});

if (hasEmberVersion(2,0)) {
return ret;
}
};

context.setProperties = function(hash) {
var ret = Ember.run(function() {
return Ember.setProperties(context, hash);
});

if (hasEmberVersion(2,0)) {
return ret;
}
};

context.get = function(key) {
return Ember.get(context, key);
};

context.getProperties = function() {
var args = Array.prototype.slice.call(arguments);
return Ember.getProperties(context, args);
};

context.on = function(actionName, handler) {
module.actionHooks[actionName] = handler;
};

context.send = function(actionName) {
var hook = module.actionHooks[actionName];
if (!hook) {
throw new Error("integration testing template received unexpected action " + actionName);
}
hook.apply(module.context, Array.prototype.slice.call(arguments, 1));
};

context.clearRender = function() {
Ember.run(function() {
toplevelView.setOutletState({
render: {
owner: module.container,
into: undefined,
outlet: 'main',
name: 'application',
controller: module.context,
ViewClass: undefined,
template: undefined
},
outlets: {}
});
});
};
}
Loading

0 comments on commit 7394b86

Please sign in to comment.