Last active
September 18, 2016 23:59
-
-
Save benoror/7a107278c1dc4c4fe7d05b31b36e2786 to your computer and use it in GitHub Desktop.
testing-serializers-david-tang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Ember from 'ember'; | |
| export default Ember.Controller.extend({ | |
| appName: 'Ember Twiddle' | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Model from "ember-data/model"; | |
| import attr from "ember-data/attr"; | |
| import { belongsTo, hasMany } from "ember-data/relationships"; | |
| export default Model.extend({ | |
| name: attr('string') | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export default DS.RESTSerializer.extend({ | |
| serialize(snapshot) { | |
| return { | |
| cats: [ | |
| { name: snapshot.attr('name') } | |
| ] | |
| }; | |
| }, | |
| serializeIntoHash(data, type, record, options) { | |
| delete data.cat; | |
| Ember.merge(data, this.serialize(record, options)); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Ember from 'ember'; | |
| export default function destroyApp(application) { | |
| Ember.run(application, 'destroy'); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Resolver from '../../resolver'; | |
| import config from '../../config/environment'; | |
| const resolver = Resolver.create(); | |
| resolver.namespace = { | |
| modulePrefix: config.modulePrefix, | |
| podModulePrefix: config.podModulePrefix | |
| }; | |
| export default resolver; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Ember from 'ember'; | |
| import Application from '../../app'; | |
| import config from '../../config/environment'; | |
| const { run } = Ember; | |
| const assign = Ember.assign || Ember.merge; | |
| export default function startApp(attrs) { | |
| let application; | |
| let attributes = assign({rootElement: "#test-root"}, config.APP); | |
| attributes = assign(attributes, attrs); // use defaults, but you can override; | |
| run(() => { | |
| application = Application.create(attributes); | |
| application.setupForTesting(); | |
| application.injectTestHelpers(); | |
| }); | |
| return application; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import resolver from './helpers/resolver'; | |
| import { | |
| setResolver | |
| } from 'ember-qunit'; | |
| setResolver(resolver); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { moduleForModel, test } from 'ember-qunit'; | |
| //import Pretender from 'pretender'; | |
| moduleForModel('cat', 'Unit | Serializer | cat', { | |
| needs: ['serializer:cat', 'model:cat'] | |
| // beforeEach() { | |
| // this.server = new Pretender(function() { | |
| // this.post('/cats', function() { | |
| // // default RESTSerializer response format | |
| // // so that the promise from save() resolves | |
| // let response = { | |
| // cat: { id: 1, name: 'Frisky' } | |
| // }; | |
| // | |
| // return [ 200, {}, JSON.stringify(response) ]; | |
| // }); | |
| // }); | |
| // }, | |
| // afterEach() { | |
| // this.server.shutdown(); | |
| // } | |
| }); | |
| test('save() sends the data formatted correctly', function(assert) { | |
| assert.expect(1); | |
| let store = this.store(); | |
| Ember.run(() => { | |
| let cat = store.createRecord('cat', { | |
| name: 'Frisky' | |
| }); | |
| cat.save().then(() => { | |
| assert.equal({},{}); | |
| // let [ request ] = this.server.handledRequests; | |
| // let requestPayload = JSON.parse(request.requestBody); | |
| // assert.deepEqual(requestPayload, { | |
| // cats: [ | |
| // { name: 'Frisky' } | |
| // ] | |
| // }); | |
| }); | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "version": "0.10.5", | |
| "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.7.3", | |
| "ember-data": "2.7.0", | |
| "ember-template-compiler": "2.7.3", | |
| "ember-testing": "2.7.3" | |
| }, | |
| "addons": {} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment