Skip to content

Instantly share code, notes, and snippets.

@Gaurav0
Last active March 6, 2017 21:50
Show Gist options
  • Save Gaurav0/8016c3e0d2524ff88a2893f39f33e6c7 to your computer and use it in GitHub Desktop.
Save Gaurav0/8016c3e0d2524ff88a2893f39f33e6c7 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', (schema) => {
return schema.db.users;
});
this.get('/users/:id', (schema, request) => {
return schema.db.users.find(request.params.id);
});
};
import Mirage from 'ember-cli-mirage';
export default Mirage.Factory.extend({
name: function(i) {
return 'User ' + i;
}
});
export default function(server) {
window.server = server;
// added this line because global wasn't created otherwise.
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.JSONSerializer.extend();
<h1>Welcome to {{appName}}</h1>
<br>
<br>
<ul>
{{outlet}}
</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(), '/');
assert.equal($('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) {
visit('/user/2');
andThen(function() {
assert.equal(currentURL(), '/user/2');
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,
"directory": "app/mirage"
}
},
"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.1",
"ember-data": "2.10.0",
"ember-template-compiler": "2.10.1",
"ember-testing": "2.10.1"
},
"addons": {
"ember-cli-mirage": "0.2.4"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment