-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CHORE] pushPayload test for serializer encapsulation side quest (#6554)
* [CHORE] pushPayload test for serializer encapsulation side quest * do not duplicate test in debug
- Loading branch information
Showing
4 changed files
with
122 additions
and
11 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
packages/-serializer-encapsulation-test-app/tests/helpers/test-in-debug.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,10 @@ | ||
import { DEBUG } from '@glimmer/env'; | ||
import { test, skip } from 'qunit'; | ||
|
||
export default function testInDebug() { | ||
if (DEBUG) { | ||
test(...arguments); | ||
} else { | ||
skip(...arguments); | ||
} | ||
} |
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
106 changes: 106 additions & 0 deletions
106
packages/-serializer-encapsulation-test-app/tests/integration/push-payload-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,106 @@ | ||
import Model, { attr } from '@ember-data/model'; | ||
import Store from 'serializer-encapsulation-test-app/services/store'; | ||
import EmberObject from '@ember/object'; | ||
import { setupTest } from 'ember-qunit'; | ||
import { module, test } from 'qunit'; | ||
import testInDebug from 'serializer-encapsulation-test-app/tests/helpers/test-in-debug'; | ||
|
||
class Person extends Model { | ||
@attr | ||
firstName; | ||
|
||
@attr | ||
lastName; | ||
|
||
// override to not call serialize() | ||
toJSON() { | ||
const { id, firstName, lastName } = this; | ||
return { | ||
id, | ||
type: this.constructor.modelName, | ||
attributes: { | ||
firstName, | ||
lastName, | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
module('integration/push-payload - pushPayload method forwards to Serializer#pushPayload', function(hooks) { | ||
setupTest(hooks); | ||
|
||
hooks.beforeEach(function(assert) { | ||
this.owner.register('service:store', Store); | ||
this.owner.register('model:person', Person); | ||
}); | ||
|
||
test('Store#pushPayload calls Serializer#pushPayload', async function(assert) { | ||
let pushPayloadCalled = 0; | ||
|
||
class TestMinimumSerializer extends EmberObject { | ||
pushPayload(store, rawPayload) { | ||
pushPayloadCalled++; | ||
|
||
assert.deepEqual(rawPayload, { | ||
id: '1', | ||
type: 'person', | ||
attributes: { | ||
firstName: 'John', | ||
lastName: 'Smith', | ||
}, | ||
}); | ||
|
||
store.push({ | ||
data: rawPayload, | ||
}); | ||
} | ||
} | ||
this.owner.register('serializer:application', TestMinimumSerializer); | ||
|
||
const store = this.owner.lookup('service:store'); | ||
|
||
store.pushPayload('person', { | ||
id: '1', | ||
type: 'person', | ||
attributes: { | ||
firstName: 'John', | ||
lastName: 'Smith', | ||
}, | ||
}); | ||
let person = store.peekRecord('person', '1'); | ||
|
||
assert.equal(pushPayloadCalled, 1, 'pushPayload called once'); | ||
assert.deepEqual( | ||
person.toJSON(), | ||
{ | ||
id: '1', | ||
type: 'person', | ||
attributes: { | ||
firstName: 'John', | ||
lastName: 'Smith', | ||
}, | ||
}, | ||
'normalized payload is correct' | ||
); | ||
}); | ||
|
||
testInDebug('Store#pushPayload throws an error if Serializer#pushPayload is not implemented', async function(assert) { | ||
class TestMinimumSerializer extends EmberObject {} | ||
this.owner.register('serializer:application', TestMinimumSerializer); | ||
|
||
const store = this.owner.lookup('service:store'); | ||
|
||
assert.expectAssertion(() => { | ||
store.pushPayload('person', { | ||
data: { | ||
id: '1', | ||
type: 'person', | ||
attributes: { | ||
firstName: 'John', | ||
lastName: 'Smith', | ||
}, | ||
}, | ||
}); | ||
}, /You must define a pushPayload method in your serializer in order to call store.pushPayload/); | ||
}); | ||
}); |
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