Skip to content

Instantly share code, notes, and snippets.

@pangratz
Forked from Gaurav0/components.my-component.js
Last active July 6, 2017 08:14
Show Gist options
  • Save pangratz/8f3f95f3485bec5be5acaa21452e71f7 to your computer and use it in GitHub Desktop.
Save pangratz/8f3f95f3485bec5be5acaa21452e71f7 to your computer and use it in GitHub Desktop.
Mirage Test
import Ember from 'ember';
export default Ember.Component.extend({
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
export default function() {
this.get('/users', 'users');
};
import Mirage, { trait } from 'ember-cli-mirage';
export default Mirage.Factory.extend({
name: function(i) {
return 'User ' + i;
},
noName: trait({
name: null
})
});
export default function(server) {
// added this line because global wasn't created otherwise.
window.server = server;
server.createList('user', 10);
}
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string')
});
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('user', { path: '/user/:id' });
});
export default Router;
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return this.store.findAll('user');
}
});
import Ember from 'ember';
export default Ember.Route.extend({
model(params) {
return this.store.findRecord('user', params.id);
}
});
import DS from 'ember-data';
export default DS.RESTSerializer.extend();
<h1>Welcome to {{appName}}</h1>
<br>
<br>
<ul>
{{#each model as |user|}}
<li class="user-name">{{user.name}}</li>
{{/each}}
</ul>
<br>
<br>
{{#each model as |user|}}
{{#link-to "user" user.id tagName="li" class="user-name"}}{{user.name}}{{/link-to}}
{{/each}}
<p class="name">{{model.name}}</p>
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
moduleForAcceptance('Visiting application');
test('visiting /', function(assert) {
visit('/');
andThen(function() {
assert.equal(currentURL(), '/');
// #root is required because application is also running under test
// any way to avoid this?
assert.equal($('#root li.user-name').length, 10);
});
});
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
moduleForAcceptance('Visiting /user');
test('visiting /user', function(assert) {
let { id } = server.create('user', 'noName');
visit(`/user/${id}`);
andThen(function() {
assert.equal(currentURL(), `/user/${id}`);
assert.equal($('.name').text().trim(), 'User 1');
});
});
import Ember from 'ember';
export default function destroyApp(application) {
Ember.run(application, 'destroy');
}
import { module } from 'qunit';
import startApp from '../helpers/start-app';
import destroyApp from '../helpers/destroy-app';
export default function(name, options = {}) {
module(name, {
beforeEach() {
this.application = startApp();
if (options.beforeEach) {
options.beforeEach.apply(this, arguments);
}
},
afterEach() {
if (options.afterEach) {
options.afterEach.apply(this, arguments);
}
destroyApp(this.application);
}
});
}
import Resolver from '../../resolver';
import config from '../../config/environment';
const resolver = Resolver.create();
resolver.namespace = {
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix
};
export default resolver;
import Ember from 'ember';
import Application from '../../app';
import config from '../../config/environment';
export default function startApp(attrs) {
let application;
let attributes = Ember.merge({rootElement: "#test-root"}, config.APP);
attributes = Ember.merge(attributes, attrs); // use defaults, but you can override;
Ember.run(() => {
application = Application.create(attributes);
application.setupForTesting();
application.injectTestHelpers();
});
return application;
}
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
moduleForComponent('my-component', 'My component test', {
integration: true
});
test('it renders', function(assert) {
// Set any properties with this.set('myProperty', 'value');
// Handle any actions with this.on('myAction', function(val) { ... });
this.render(hbs`{{my-component}}`);
assert.equal(this.$().text().trim(), '');
// Template block usage:
this.render(hbs`
{{#my-component}}
template block text
{{/my-component}}
`);
assert.equal(this.$().text().trim(), 'template block text');
});
import resolver from './helpers/resolver';
import {
setResolver
} from 'ember-qunit';
setResolver(resolver);
{
"version": "0.7.2",
"ENV": {
"ember-cli-mirage": {
"enabled": true
}
},
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": true
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.10.2",
"ember-data": "2.10.0",
"ember-template-compiler": "2.10.2"
},
"addons": {
"ember-cli-mirage": "latest"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment