Skip to content

Instantly share code, notes, and snippets.

@robbiespeed
Created March 21, 2016 19:50
Show Gist options
  • Save robbiespeed/50df37b8c7c2283413b9 to your computer and use it in GitHub Desktop.
Save robbiespeed/50df37b8c7c2283413b9 to your computer and use it in GitHub Desktop.
Async Service Tests
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
import DS from 'ember-data';
export default DS.Model.extend({
text: DS.attr('string')
});
import Ember from 'ember';
const { RSVP } = Ember;
export default Ember.Service.extend({
store: Ember.inject.service(),
loggedIn: null,
name: null,
message: null,
signIn (data) {
return new RSVP.Promise((resolve) => {
this._parseSignIn(data);
resolve();
});
},
signOut (data) {
return new RSVP.Promise((resolve) => {
this._reset();
resolve();
});
},
_parseSignIn (data) {
this.set('loggedIn', true);
this.set('name', data.name);
this.set('message', this.get('store').createRecord('message', {
text: data.message,
}));
},
_reset () {
this.set('loggedIn', null);
this.set('name', null);
this.set('message', null);
this.get('store').unloadAll();
},
});
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 resolver from './helpers/resolver';
import {
setResolver
} from 'ember-qunit';
setResolver(resolver);
import { moduleFor, test } from 'ember-qunit';
import Ember from 'ember';
const { run, RSVP } = Ember;
moduleFor('service:user', 'Unit | Service | user', {
// Specify the other units that are required for this test.
needs: ['model:message']
});
test('exists', function(assert) {
const user = this.subject();
assert.ok(user);
});
test('can sign-out', function (assert) {
return new RSVP.Promise((resolve/*, reject*/) => {
const user = this.subject();
user.set('loggedIn', true);
user.signOut().finally(() => {
assert.ok(!user.get('loggedIn'));
resolve();
});
});
});
test('can sign-in', function(assert) {
return new RSVP.Promise((resolve/*, reject*/) => {
const user = this.subject();
user.signOut().finally(() => {
user.signIn({
name: 'Jim',
message: 'Hello World'
}).finally(() => {
assert.equal(user.get('name'), 'Jim');
resolve();
});
});
});
});
{
"version": "0.6.5",
"EmberENV": {
"FEATURES": {}
},
"options": {
"enable-testing": true
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.4.3",
"ember-data": "2.4.0",
"ember-template-compiler": "2.4.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment