Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1364 from samselikoff/fix-rest-serializer
Add clearer test around normalizeIds
- Loading branch information
Showing
3 changed files
with
154 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
4 changes: 2 additions & 2 deletions
4
...tegration/server/shorthand-sanity-test.js → ...ds/active-model-serializer-sanity-test.js
This file contains 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
152 changes: 152 additions & 0 deletions
152
tests/integration/server/shorthands/rest-serializer-sanity-test.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import { module, test } from 'qunit'; | ||
import { Model, hasMany, belongsTo, RestSerializer } from 'ember-cli-mirage'; | ||
import Server from 'ember-cli-mirage/server'; | ||
import promiseAjax from '../../../helpers/promise-ajax'; | ||
|
||
module('Integration | Server | Shorthands | REST Serializer Sanity check', function(hooks) { | ||
hooks.beforeEach(function() { | ||
this.server = new Server({ | ||
environment: 'test', | ||
models: { | ||
contact: Model.extend({ | ||
addresses: hasMany() | ||
}), | ||
address: Model.extend({ | ||
contact: belongsTo() | ||
}) | ||
}, | ||
serializers: { | ||
application: RestSerializer.extend({ | ||
normalizeIds: true | ||
}) | ||
} | ||
}); | ||
this.server.timing = 0; | ||
this.server.logging = false; | ||
}); | ||
|
||
hooks.afterEach(function() { | ||
this.server.shutdown(); | ||
}); | ||
|
||
test('a get shorthand works', async function(assert) { | ||
assert.expect(2); | ||
|
||
this.server.db.loadData({ | ||
contacts: [ | ||
{ id: 1, name: 'Link' } | ||
] | ||
}); | ||
|
||
this.server.get('/contacts'); | ||
|
||
let { xhr, data } = await promiseAjax({ | ||
method: 'GET', | ||
url: '/contacts' | ||
}); | ||
|
||
assert.equal(xhr.status, 200); | ||
assert.deepEqual(data, { contacts: [{ id: '1', name: 'Link', addresses: [] }] }); | ||
}); | ||
|
||
test('a post shorthand works', async function(assert) { | ||
let { server } = this; | ||
assert.expect(3); | ||
|
||
this.server.db.loadData({ | ||
contacts: [ | ||
{ id: 1, name: 'Link' } | ||
] | ||
}); | ||
|
||
server.post('/addresses'); | ||
|
||
let { xhr } = await promiseAjax({ | ||
method: 'POST', | ||
url: '/addresses', | ||
data: JSON.stringify({ | ||
address: { | ||
street: '5th ave', | ||
contact: 1 | ||
} | ||
}) | ||
}); | ||
|
||
assert.equal(xhr.status, 201); | ||
assert.equal(server.db.addresses.length, 1); | ||
assert.equal(server.db.addresses[0].contactId, 1); | ||
}); | ||
|
||
test('a put shorthand works', async function(assert) { | ||
let { server } = this; | ||
assert.expect(2); | ||
|
||
this.server.db.loadData({ | ||
contacts: [ | ||
{ id: 1, name: 'Link' } | ||
] | ||
}); | ||
|
||
server.put('/contacts/:id'); | ||
|
||
let { xhr } = await promiseAjax({ | ||
method: 'PUT', | ||
url: '/contacts/1', | ||
data: JSON.stringify({ | ||
contact: { | ||
name: 'Zelda' | ||
} | ||
}) | ||
}); | ||
|
||
assert.equal(xhr.status, 200); | ||
assert.equal(server.db.contacts[0].name, 'Zelda'); | ||
}); | ||
|
||
test('a patch shorthand works', async function(assert) { | ||
let { server } = this; | ||
assert.expect(2); | ||
|
||
this.server.db.loadData({ | ||
contacts: [ | ||
{ id: 1, name: 'Link' } | ||
] | ||
}); | ||
|
||
server.patch('/contacts/:id'); | ||
|
||
let { xhr } = await promiseAjax({ | ||
method: 'PATCH', | ||
url: '/contacts/1', | ||
data: JSON.stringify({ | ||
contact: { | ||
name: 'Zelda' | ||
} | ||
}) | ||
}); | ||
|
||
assert.equal(xhr.status, 200); | ||
assert.equal(server.db.contacts[0].name, 'Zelda'); | ||
}); | ||
|
||
test('a delete shorthand works', async function(assert) { | ||
let { server } = this; | ||
assert.expect(2); | ||
|
||
this.server.db.loadData({ | ||
contacts: [ | ||
{ id: 1, name: 'Link' } | ||
] | ||
}); | ||
|
||
server.del('/contacts/:id'); | ||
|
||
let { xhr } = await promiseAjax({ | ||
method: 'DELETE', | ||
url: '/contacts/1' | ||
}); | ||
|
||
assert.equal(xhr.status, 204); | ||
assert.equal(server.db.contacts.length, 0); | ||
}); | ||
}); |