testInDebug("push(record) works with polymorphic modelClass", function(assert) { var done = assert.async(); var person, mafiaFamily; if (isEnabled('ds-overhaul-references')) { assert.expectDeprecation('BelongsToReference#push(DS.Model) is deprecated. Update relationship via `model.set(\'relationshipName\', value)` instead.') } env.registry.register('model:mafia-family', Family.extend()); run(function() { person = env.store.push({ data: { type: 'person', id: 1 } }); mafiaFamily = env.store.push({ data: { type: 'mafia-family', id: 1 } }); }); var familyReference = person.belongsTo('family'); run(function() { familyReference.push(mafiaFamily).then(function(family) { assert.equal(family, mafiaFamily); done(); }); }); });
testInDebug("push(array) asserts polymorphic type", function(assert) { var family; run(function() { family = env.store.push({ data: { type: 'family', id: 1 } }); }); var personsReference = family.hasMany('persons'); if (isEnabled('ds-overhaul-references')) { assert.expectDeprecation("HasManyReference#push(array) is deprecated. Push a JSON-API document instead."); } else { assert.expectNoDeprecation(); } assert.expectAssertion(() => { run(() => { var data = [ { data: { type: 'family', id: 1 } } ]; personsReference.push(data); }); }, "You cannot add a record of modelClass 'family' to the 'family.persons' relationship (only 'person' allowed)"); });
testInDebug("push(record) asserts for invalid modelClass", function(assert) { var person, anotherPerson; if (isEnabled('ds-overhaul-references')) { assert.expectDeprecation('BelongsToReference#push(DS.Model) is deprecated. Update relationship via `model.set(\'relationshipName\', value)` instead.') } run(function() { person = env.store.push({ data: { type: 'person', id: 1, relationships: { family: { data: { type: 'family', id: 1 } } } } }); anotherPerson = env.store.push({ data: { type: 'person', id: 2 } }); }); var familyReference = person.belongsTo('family'); assert.expectAssertion(function() { run(function() { familyReference.push(anotherPerson); }); }, "You cannot add a record of modelClass 'person' to the 'person.family' relationship (only 'family' allowed)"); });
function ajaxResponse(value) { if (isEnabled('ds-improved-ajax')) { env.adapter._makeRequest = function() { return run(Ember.RSVP, 'resolve', Ember.copy(value, true)); }; } else { env.adapter.ajax = function(url, verb, hash) { return run(Ember.RSVP, 'resolve', Ember.copy(value, true)); }; } }
run(function() { if (isEnabled('ds-overhaul-references')) { assert.expectDeprecation("BelongsToReference#push(DS.Model) is deprecated. Update relationship via `model.set('relationshipName', value)` instead."); } familyReference.push(family).then(function(record) { assert.ok(Family.detectInstance(record), "push resolves with the referenced record"); assert.equal(get(record, 'name'), "Coreleone", "name is set"); assert.equal(record, family); done(); }); });
function ajaxResponse(value) { if (isEnabled('ds-improved-ajax')) { adapter._makeRequest = function(request) { passedUrl = request.url; return run(Ember.RSVP, 'resolve', Ember.copy(value, true)); }; } else { adapter.ajax = function(url, verb, hash) { passedUrl = url; return run(Ember.RSVP, 'resolve', Ember.copy(value, true)); }; } }
run(function() { var payload = { data: [ { data: { type: 'person', id: 1, attributes: { name: "Vito" } } }, { data: { type: 'person', id: 2, attributes: { name: "Michael" } } } ] }; if (isEnabled('ds-overhaul-references')) { payload = { data: [ { type: 'person', id: 1, attributes: { name: "Vito" } }, { type: 'person', id: 2, attributes: { name: "Michael" } } ] }; } deferred.resolve(payload); });
run(() => { var data = [ { data: { type: 'mafia-boss', id: 1, attributes: { name: "Vito" } } } ]; if (isEnabled('ds-overhaul-references')) { assert.expectDeprecation("HasManyReference#push(array) is deprecated. Push a JSON-API document instead."); } else { assert.expectNoDeprecation(); } personsReference.push(data).then(function(records) { assert.ok(records instanceof DS.ManyArray, "push resolves with the referenced records"); assert.equal(get(records, 'length'), 1); assert.equal(records.objectAt(0).get('name'), "Vito"); done(); }); });
testInDebug("push(array)", function(assert) { var done = assert.async(); var family; run(function() { family = env.store.push({ data: { type: 'family', id: 1, relationships: { persons: { data: [ { type: 'person', id: 1 }, { type: 'person', id: 2 } ] } } } }); }); var personsReference = family.hasMany('persons'); if (isEnabled('ds-overhaul-references')) { assert.expectDeprecation("HasManyReference#push(array) is deprecated. Push a JSON-API document instead."); } run(function() { var data = [ { data: { type: 'person', id: 1, attributes: { name: "Vito" } } }, { data: { type: 'person', id: 2, attributes: { name: "Michael" } } } ]; personsReference.push(data).then(function(records) { assert.ok(records instanceof DS.ManyArray, "push resolves with the referenced records"); assert.equal(get(records, 'length'), 2); assert.equal(records.objectAt(0).get('name'), "Vito"); assert.equal(records.objectAt(1).get('name'), "Michael"); done(); }); }); });
run(function() { var payload = { data: [ { data: { type: 'person', id: 1, attributes: { name: "Vito" } } }, { data: { type: 'person', id: 2, attributes: { name: "Michael" } } } ] }; if (isEnabled('ds-overhaul-references')) { assert.expectDeprecation("HasManyReference#push() expects a valid JSON-API document."); } else { assert.expectNoDeprecation(); } personsReference.push(payload).then(function(records) { assert.ok(records instanceof DS.ManyArray, "push resolves with the referenced records"); assert.equal(get(records, 'length'), 2); assert.equal(records.objectAt(0).get('name'), "Vito"); assert.equal(records.objectAt(1).get('name'), "Michael"); done(); }); });
test("serialize includes id when includeId is true", function(assert) { run(() => { post = env.store.createRecord('post', { title: 'Rails is omakase', comments: [] }); post.set('id', 'test'); }); let json = serializer.serialize(post._createSnapshot(), { includeId: true }); assert.deepEqual(json, { id: 'test', title: 'Rails is omakase', comments: [] }); }); if (isEnabled("ds-serialize-id")) { test("serializeId", function(assert) { run(() => { post = env.store.createRecord('post'); post.set('id', 'test'); }); let json = {}; serializer.serializeId(post._createSnapshot(), json, 'id'); assert.deepEqual(json, { id: 'test' }); }); test("modified serializeId is called from serialize", function(assert) {
data: { type: 'users', id: '1', relationships: { 'reportsTo': { data: null } } } }; assert.expectAssertion(function() { env.store.serializerFor("user").normalizeResponse(env.store, User, jsonHash, '1', 'findRecord'); }, /Your payload for 'user' contains 'reportsTo', but your serializer is setup to look for 'reports-to'/); }); if (isEnabled("ds-payload-type-hooks")) { test('mapping of payload type can be customized via modelNameFromPayloadType', function(assert) { env.registry.register('serializer:user', DS.JSONAPISerializer.extend({ modelNameFromPayloadType: function(payloadType) { return payloadType.replace("api::v1::", ""); } })); let jsonHash = { data: { id: "1", type: "api::v1::user", relationships: { company: { data: { id: "1",
store.push({ data: { type: 'person', id: '1', attributes: { firstName: 'Tomster', emailAddress: '*****@*****.**', isMascot: true } } }); }); }, /The payload for 'person' contains these unknown .*: .* Make sure they've been defined in your model./); }); if (isEnabled('ds-pushpayload-return')) { test("Calling pushPayload returns records", function(assert) { env.registry.register('serializer:person', DS.RESTSerializer); let people = run(() => { return store.pushPayload('person', { people: [{ id: '1', firstName: 'Robert', lastName: 'Jackson' }, { id: '2', firstName: 'Matthew', lastName: 'Beale' }] });
} else { assert.expectNoDeprecation(); } personsReference.push(payload).then(function(records) { assert.ok(records instanceof DS.ManyArray, "push resolves with the referenced records"); assert.equal(get(records, 'length'), 2); assert.equal(records.objectAt(0).get('name'), "Vito"); assert.equal(records.objectAt(1).get('name'), "Michael"); done(); }); }); }); if (isEnabled('ds-overhaul-references')) { test("push(object) supports JSON-API payload", function(assert) { var done = assert.async(); var family; run(function() { family = env.store.push({ data: { type: 'family', id: 1, relationships: { persons: { data: [ { type: 'person', id: 1 }, { type: 'person', id: 2 } ]
module('unit/adapters/rest_adapter/group_records_for_find_many_test - DS.RESTAdapter#groupRecordsForFindMany', { beforeEach() { maxLength = -1; requests = []; lengths = []; GroupsAdapter = DS.RESTAdapter.extend({ coalesceFindRequests: true, findRecord(store, type, id, snapshot) { return { id }; } }); if (isEnabled('ds-improved-ajax')) { GroupsAdapter.reopen({ _makeRequest(request) { requests.push({ url: request.url, ids: request.data.ids }); let queryString = request.data.ids.map(i => { return encodeURIComponent('ids[]') + '=' + encodeURIComponent(i); }).join('&'); let fullUrl = request.url + '?' + queryString; maxLength = this.get('maxURLLength'); lengths.push(fullUrl.length);