diff --git a/tests/integration/adapter/build-url-mixin-test.js b/tests/integration/adapter/build-url-mixin-test.js index 59a743e6824..aee911e6f6f 100644 --- a/tests/integration/adapter/build-url-mixin-test.js +++ b/tests/integration/adapter/build-url-mixin-test.js @@ -6,9 +6,9 @@ import {module, test} from 'qunit'; import DS from 'ember-data'; -var env, store, adapter, Post, Comment, SuperUser; -var passedUrl; -var run = Ember.run; +let env, store, adapter, Post, Comment, SuperUser; +let passedUrl; +const { run } = Ember; module("integration/adapter/build-url-mixin - BuildURLMixin with RESTAdapter", { beforeEach() { @@ -58,7 +58,7 @@ function ajaxResponse(value) { test('buildURL - with host and namespace', function(assert) { - run(function() { + run(() => { adapter.setProperties({ host: 'http://example.com', namespace: 'api/v1' @@ -67,33 +67,34 @@ test('buildURL - with host and namespace', function(assert) { ajaxResponse({ posts: [{ id: 1 }] }); - run(store, 'findRecord', 'post', 1).then(assert.wait(function(post) { + return run(store, 'findRecord', 'post', 1).then(post => { assert.equal(passedUrl, "http://example.com/api/v1/posts/1"); - })); + }); }); test('buildURL - with relative paths in links', function(assert) { - run(function() { + run(() => { adapter.setProperties({ host: 'http://example.com', namespace: 'api/v1' }); }); + Post.reopen({ comments: DS.hasMany('comment', { async: true }) }); Comment.reopen({ post: DS.belongsTo('post', { async: false }) }); ajaxResponse({ posts: [{ id: 1, links: { comments: 'comments' } }] }); - run(store, 'findRecord', 'post', '1').then(assert.wait(function(post) { + return run(store, 'findRecord', 'post', '1').then(post => { ajaxResponse({ comments: [{ id: 1 }] }); return post.get('comments'); - })).then(assert.wait(function (comments) { + }).then(comments => { assert.equal(passedUrl, "http://example.com/api/v1/posts/1/comments"); - })); + }); }); test('buildURL - with absolute paths in links', function(assert) { - run(function() { + run(() => { adapter.setProperties({ host: 'http://example.com', namespace: 'api/v1' @@ -104,17 +105,17 @@ test('buildURL - with absolute paths in links', function(assert) { ajaxResponse({ posts: [{ id: 1, links: { comments: '/api/v1/posts/1/comments' } }] }); - run(store, 'findRecord', 'post', 1).then(assert.wait(function(post) { + return run(store, 'findRecord', 'post', 1).then(post => { ajaxResponse({ comments: [{ id: 1 }] }); return post.get('comments'); - })).then(assert.wait(function (comments) { + }).then(comments => { assert.equal(passedUrl, "http://example.com/api/v1/posts/1/comments"); - })); + }); }); test('buildURL - with absolute paths in links and protocol relative host', function(assert) { - run(function() { + run(() => { adapter.setProperties({ host: '//example.com', namespace: 'api/v1' @@ -125,16 +126,16 @@ test('buildURL - with absolute paths in links and protocol relative host', funct ajaxResponse({ posts: [{ id: 1, links: { comments: '/api/v1/posts/1/comments' } }] }); - run(store, 'findRecord', 'post', 1).then(assert.wait(function(post) { + return run(store, 'findRecord', 'post', 1).then(post => { ajaxResponse({ comments: [{ id: 1 }] }); return post.get('comments'); - })).then(assert.wait(function (comments) { + }).then(comments => { assert.equal(passedUrl, "//example.com/api/v1/posts/1/comments"); - })); + }); }); test('buildURL - with absolute paths in links and host is /', function(assert) { - run(function() { + run(() => { adapter.setProperties({ host: '/', namespace: 'api/v1' @@ -145,12 +146,12 @@ test('buildURL - with absolute paths in links and host is /', function(assert) { ajaxResponse({ posts: [{ id: 1, links: { comments: '/api/v1/posts/1/comments' } }] }); - run(store, 'findRecord', 'post', 1).then(assert.wait(function(post) { + return run(store, 'findRecord', 'post', 1).then(post => { ajaxResponse({ comments: [{ id: 1 }] }); return post.get('comments'); - })).then(assert.wait(function (comments) { + }).then(comments => { assert.equal(passedUrl, '/api/v1/posts/1/comments', 'host stripped out properly'); - })); + }); }); test('buildURL - with full URLs in links', function(assert) { @@ -169,30 +170,30 @@ test('buildURL - with full URLs in links', function(assert) { ] }); - run(function() { - store.findRecord('post', 1).then(assert.wait(function(post) { + return run(() => { + return store.findRecord('post', 1).then(post => { ajaxResponse({ comments: [{ id: 1 }] }); return post.get('comments'); - })).then(assert.wait(function (comments) { + }).then(comments => { assert.equal(passedUrl, "http://example.com/api/v1/posts/1/comments"); - })); + }); }); }); test('buildURL - with camelized names', function(assert) { adapter.setProperties({ pathForType(type) { - var decamelized = Ember.String.decamelize(type); + let decamelized = Ember.String.decamelize(type); return Ember.String.underscore(Ember.String.pluralize(decamelized)); } }); ajaxResponse({ superUsers: [{ id: 1 }] }); - run(function() { - store.findRecord('super-user', 1).then(assert.wait(function(post) { + return run(() => { + return store.findRecord('super-user', 1).then(post => { assert.equal(passedUrl, "/super_users/1"); - })); + }); }); }); @@ -204,8 +205,8 @@ test('buildURL - buildURL takes a record from find', function(assert) { ajaxResponse({ comments: [{ id: 1 }] }); - var post; - run(function() { + let post; + run(() => { post = store.push({ data: { type: 'post', @@ -214,10 +215,10 @@ test('buildURL - buildURL takes a record from find', function(assert) { }); }); - run(function() { - store.findRecord('comment', 1, { preload: { post: post } }).then(assert.wait(function(post) { + return run(() => { + return store.findRecord('comment', 1, { preload: { post: post } }).then(post => { assert.equal(passedUrl, "/posts/2/comments/1"); - })); + }); }); }); @@ -234,9 +235,9 @@ test('buildURL - buildURL takes the records from findMany', function(assert) { adapter.coalesceFindRequests = true; ajaxResponse({ comments: [{ id: 1 }, { id: 2 }, { id: 3 }] }); - var post; + let post; - run(function() { + return run(() => { post = store.push({ data: { type: 'post', @@ -252,9 +253,10 @@ test('buildURL - buildURL takes the records from findMany', function(assert) { } } }); - post.get('comments').then(assert.wait(function(post) { + + return post.get('comments').then(post => { assert.equal(passedUrl, "/posts/2/comments/"); - })); + }); }); }); @@ -266,18 +268,18 @@ test('buildURL - buildURL takes a record from create', function(assert) { ajaxResponse({ comments: [{ id: 1 }] }); - run(function() { - var post = store.push({ + return run(() => { + let post = store.push({ data: { type: 'post', id: '2' } }); - var comment = store.createRecord('comment'); + let comment = store.createRecord('comment'); comment.set('post', post); - comment.save().then(assert.wait(function(post) { + return comment.save().then(post => { assert.equal(passedUrl, "/posts/2/comments/"); - })); + }); }); }); @@ -286,8 +288,8 @@ test('buildURL - buildURL takes a record from create to query a resolved async b ajaxResponse({ posts: [{ id: 2 }] }); - run(function() { - store.findRecord('post', 2).then(assert.wait(function(post) { + return run(() => { + store.findRecord('post', 2).then(post => { assert.equal(post.get('id'), 2); adapter.buildURL = function(type, id, snapshot) { @@ -296,13 +298,12 @@ test('buildURL - buildURL takes a record from create to query a resolved async b ajaxResponse({ comments: [{ id: 1 }] }); - var comment = store.createRecord('comment'); + let comment = store.createRecord('comment'); comment.set('post', post); - comment.save().then(assert.wait(function(post) { + return comment.save().then(post => { assert.equal(passedUrl, "/posts/2/comments/"); - })); - - })); + }); + }); }); }); @@ -314,8 +315,8 @@ test('buildURL - buildURL takes a record from update', function(assert) { ajaxResponse({ comments: [{ id: 1 }] }); - var post, comment; - run(function() { + let post, comment; + run(() => { post = store.push({ data: { type: 'post', @@ -330,10 +331,11 @@ test('buildURL - buildURL takes a record from update', function(assert) { }); comment.set('post', post); }); - run(function() { - comment.save().then(assert.wait(function(post) { + + return run(() => { + return comment.save().then(post => { assert.equal(passedUrl, "/posts/2/comments/1"); - })); + }); }); }); @@ -346,8 +348,9 @@ test('buildURL - buildURL takes a record from delete', function(assert) { ajaxResponse({ comments: [{ id: 1 }] }); - var post, comment; - run(function() { + let post, comment; + + run(() => { post = store.push({ data: { type: 'post', @@ -364,15 +367,16 @@ test('buildURL - buildURL takes a record from delete', function(assert) { comment.set('post', post); comment.deleteRecord(); }); - run(function() { - comment.save().then(assert.wait(function(post) { + + return run(() => { + return comment.save().then(post => { assert.equal(passedUrl, "posts/2/comments/1"); - })); + }); }); }); test('buildURL - with absolute namespace', function(assert) { - run(function() { + run(() => { adapter.setProperties({ namespace: '/api/v1' }); @@ -380,7 +384,7 @@ test('buildURL - with absolute namespace', function(assert) { ajaxResponse({ posts: [{ id: 1 }] }); - run(store, 'findRecord', 'post', 1).then(assert.wait(function(post) { + return run(store, 'findRecord', 'post', 1).then(post => { assert.equal(passedUrl, "/api/v1/posts/1"); - })); + }); }); diff --git a/tests/integration/adapter/find-all-test.js b/tests/integration/adapter/find-all-test.js index 914a4652e9d..b58f70ca5c9 100644 --- a/tests/integration/adapter/find-all-test.js +++ b/tests/integration/adapter/find-all-test.js @@ -56,12 +56,12 @@ test("When all records for a type are requested, the store should call the adapt })); return run(() => { - return store.findAll('person').then((all) => { + return store.findAll('person').then(all => { let allRecords = all; assert.equal(get(all, 'length'), 1, "the record array's length is 1 after a record is loaded into it"); assert.equal(all.objectAt(0).get('name'), "Braaaahm Dale", "the first item in the record array is Braaaahm Dale"); - return store.findAll('person').then((all) => { + return store.findAll('person').then(all => { // Only one record array per type should ever be created (identity map) assert.strictEqual(allRecords, all, "the same record array is returned every time all records of a type are requested"); }); @@ -92,11 +92,11 @@ test("When all records for a type are requested, a rejection should reject the p } })); - run(() => { - store.findAll('person').then(null, () => { + return run(() => { + return store.findAll('person').catch(() => { assert.ok(true, "The rejection should get here"); return store.findAll('person'); - }).then((all) => { + }).then(all => { assert.equal(get(all, 'length'), 1, "the record array's length is 1 after a record is loaded into it"); assert.equal(all.objectAt(0).get('name'), "Braaaahm Dale", "the first item in the record array is Braaaahm Dale"); }); @@ -163,8 +163,6 @@ testInDebug('When all records are requested, assert the payload is not blank', ( }); test("isUpdating is true while records are fetched", function(assert) { - let done = assert.async(); - let findAllDeferred = Ember.RSVP.defer(); env.registry.register('adapter:person', DS.Adapter.extend({ findAll() { @@ -174,7 +172,7 @@ test("isUpdating is true while records are fetched", function(assert) { shouldReloadAll: () => true })); - run(function() { + run(() => { store.push({ data: [{ type: 'person', @@ -186,34 +184,36 @@ test("isUpdating is true while records are fetched", function(assert) { let persons = store.peekAll('person'); assert.equal(persons.get("length"), 1); - run(function() { - store.findAll('person').then(function(persons) { + let wait = run(() => { + return store.findAll('person').then(persons => { assert.equal(persons.get("isUpdating"), false); assert.equal(persons.get("length"), 2); - - done(); }); }); assert.equal(persons.get("isUpdating"), true); findAllDeferred.resolve({ data: [{ id: 2, type: 'person' }] }); + + return wait; }); test("isUpdating is true while records are fetched in the background", function(assert) { - let done = assert.async(); - let findAllDeferred = Ember.RSVP.defer(); env.registry.register('adapter:person', DS.Adapter.extend({ findAll() { return findAllDeferred.promise; }, - shouldReloadAll: () => false, - shouldBackgroundReloadAll: () => true + shouldReloadAll() { + return false; + }, + shouldBackgroundReloadAll() { + return true; + } })); - run(function() { + run(() => { store.push({ data: [{ type: 'person', @@ -225,25 +225,23 @@ test("isUpdating is true while records are fetched in the background", function( let persons = store.peekAll('person'); assert.equal(persons.get("length"), 1); - run(function() { - store.findAll('person').then(function(persons) { + return run(() => { + return store.findAll('person').then(persons => { assert.equal(persons.get("isUpdating"), true); assert.equal(persons.get("length"), 1, "persons are updated in the background"); }); - }); + }).then(() => { + assert.equal(persons.get("isUpdating"), true); - assert.equal(persons.get("isUpdating"), true); - - run(function() { - findAllDeferred.resolve({ data: [{ id: 2, type: 'person' }] }); - }); - - run(function() { - findAllDeferred.promise.then(function() { - assert.equal(persons.get("isUpdating"), false); - assert.equal(persons.get("length"), 2); + run(() => { + findAllDeferred.resolve({ data: [{ id: 2, type: 'person' }] }); + }); - done(); + return run(() => { + return findAllDeferred.promise.then(() => { + assert.equal(persons.get("isUpdating"), false); + assert.equal(persons.get("length"), 2); + }); }); }); }); @@ -258,7 +256,7 @@ test("isUpdating is false if records are not fetched in the background", functio shouldBackgroundReloadAll: () => false })); - run(function() { + run(() => { store.push({ data: [{ type: 'person', @@ -270,11 +268,11 @@ test("isUpdating is false if records are not fetched in the background", functio let persons = store.peekAll('person'); assert.equal(persons.get("length"), 1); - run(function() { - store.findAll('person').then(function(persons) { + return run(() => { + return store.findAll('person').then(persons => { assert.equal(persons.get("isUpdating"), false); }); + }).then(() => { + assert.equal(persons.get("isUpdating"), false); }); - - assert.equal(persons.get("isUpdating"), false); }); diff --git a/tests/integration/adapter/find-test.js b/tests/integration/adapter/find-test.js index 2dd54dfb1ee..17b3225768f 100644 --- a/tests/integration/adapter/find-test.js +++ b/tests/integration/adapter/find-test.js @@ -6,7 +6,7 @@ import DS from 'ember-data'; const { run } = Ember; const { attr } = DS; -const { reject } = Ember.RSVP; +const { reject, Promise } = Ember.RSVP; let Person, store, env; @@ -30,7 +30,7 @@ module("integration/adapter/find - Finding Records", { } }); -testInDebug("It raises an assertion when `undefined` is passed as id (#1705)", (assert) => { +testInDebug("It raises an assertion when `undefined` is passed as id (#1705)", function(assert) { assert.expectAssertion(() => { store.find('person', undefined); }, `You cannot pass 'undefined' as id to the store's find method`); @@ -40,10 +40,10 @@ testInDebug("It raises an assertion when `undefined` is passed as id (#1705)", ( }, `You cannot pass 'null' as id to the store's find method`); }); -test("When a single record is requested, the adapter's find method should be called unless it's loaded.", (assert) => { +test("When a single record is requested, the adapter's find method should be called unless it's loaded.", function(assert) { assert.expect(2); - var count = 0; + let count = 0; env.registry.register('adapter:person', DS.Adapter.extend({ findRecord(_, type) { @@ -69,84 +69,79 @@ test("When a single record is requested, the adapter's find method should be cal }); }); -test("When a single record is requested multiple times, all .findRecord() calls are resolved after the promise is resolved", (assert) => { - var deferred = Ember.RSVP.defer(); +test("When a single record is requested multiple times, all .findRecord() calls are resolved after the promise is resolved", function(assert) { + let deferred = Ember.RSVP.defer(); env.registry.register('adapter:person', DS.Adapter.extend({ - findRecord: () => deferred.promise + findRecord() { + return deferred.promise; + } })); - run(() => { - store.findRecord('person', 1).then(assert.wait(function(person) { + let requestOne = run(() => { + return store.findRecord('person', 1).then(person => { assert.equal(person.get('id'), "1"); assert.equal(person.get('name'), "Braaaahm Dale"); - - let done = assert.async(); - deferred.promise.then(() => { - assert.ok(true, 'expected deferred.promise to fulfill'); - done(); - }, () => { - assert.ok(false, 'expected deferred.promise to fulfill, but rejected'); - done(); - }); - })); + }); }); - run(() => { - store.findRecord('person', 1).then(assert.wait((post) => { + let requestTwo = run(() => { + return store.findRecord('person', 1).then(post => { assert.equal(post.get('id'), "1"); assert.equal(post.get('name'), "Braaaahm Dale"); - - let done = assert.async(); - deferred.promise.then(() => { - assert.ok(true, 'expected deferred.promise to fulfill'); - done(); - }, () => { - assert.ok(false, 'expected deferred.promise to fulfill, but rejected'); - done(); - }); - - })); + }); }); - run(() => deferred.resolve({ - data: { - id: 1, - type: "person", - attributes: { - name: "Braaaahm Dale" + run(() => { + deferred.resolve({ + data: { + id: 1, + type: "person", + attributes: { + name: "Braaaahm Dale" + } } - }})); + }); + }); + + return Promise.all([ + requestOne, + requestTwo + ]) }); -test("When a single record is requested, and the promise is rejected, .findRecord() is rejected.", (assert) => { +test("When a single record is requested, and the promise is rejected, .findRecord() is rejected.", function(assert) { env.registry.register('adapter:person', DS.Adapter.extend({ - findRecord: () => reject() + findRecord() { + return reject(); + } })); - run(() => { - store.findRecord('person', 1).then(null, assert.wait(() => { - assert.ok(true, "The rejection handler was called"); - })); + return run(() => { + return store.findRecord('person', 1).catch(() => { + assert.ok(true, 'The rejection handler was called'); + }); }); }); -test("When a single record is requested, and the promise is rejected, the record should be unloaded.", (assert) => { +test("When a single record is requested, and the promise is rejected, the record should be unloaded.", function(assert) { assert.expect(2); env.registry.register('adapter:person', DS.Adapter.extend({ - findRecord: () => reject() + findRecord() { + return reject(); + } })); - run(() => { - store.findRecord('person', 1).then(null, assert.wait((reason) => { + return run(() => { + return store.findRecord('person', 1).catch(reason => { assert.ok(true, "The rejection handler was called"); assert.ok(!store.hasRecordForId('person', 1), "The record has been unloaded"); - })); + }); }); }); -testInDebug('When a single record is requested, and the payload is blank', (assert) => { +testInDebug('When a single record is requested, and the payload is blank', function(assert) { env.registry.register('adapter:person', DS.Adapter.extend({ findRecord: () => Ember.RSVP.resolve({}) })); @@ -156,7 +151,7 @@ testInDebug('When a single record is requested, and the payload is blank', (asse }, /You made a 'findRecord' request for a 'person' with id 'the-id', but the adapter's response did not have any data/); }); -testInDebug('When multiple records are requested, and the payload is blank', (assert) => { +testInDebug('When multiple records are requested, and the payload is blank', function(assert) { env.registry.register('adapter:person', DS.Adapter.extend({ coalesceFindRequests: true, findMany: () => Ember.RSVP.resolve({}) @@ -187,7 +182,5 @@ testInDebug("warns when returned record has different id", function(assert) { assert.expectWarning(/You requested a record of type 'person' with id 'me' but the adapter returned a payload with primary data having an id of '1'/); - run(function() { - env.store.findRecord('person', 'me'); - }); + return run(() => env.store.findRecord('person', 'me')); }); diff --git a/tests/integration/adapter/json-api-adapter-test.js b/tests/integration/adapter/json-api-adapter-test.js index dcf9ab82661..313fc0ee939 100644 --- a/tests/integration/adapter/json-api-adapter-test.js +++ b/tests/integration/adapter/json-api-adapter-test.js @@ -7,12 +7,12 @@ import testInDebug from 'dummy/tests/helpers/test-in-debug'; import DS from 'ember-data'; import { isEnabled } from 'ember-data/-private'; -var env, store, adapter; -var passedUrl, passedVerb, passedHash; +let env, store, adapter; +let passedUrl, passedVerb, passedHash; -var run = Ember.run; +const { run } = Ember; -var User, Post, Comment, Handle, GithubHandle, TwitterHandle, Company, DevelopmentShop, DesignStudio; +let User, Post, Comment, Handle, GithubHandle, TwitterHandle, Company, DevelopmentShop, DesignStudio; module('integration/adapter/json-api-adapter - JSONAPIAdapter', { beforeEach() { @@ -84,8 +84,8 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', { }); function ajaxResponse(responses) { - var counter = 0; - var index; + let counter = 0; + let index; passedUrl = []; passedVerb = []; @@ -127,8 +127,8 @@ test('find a single record', function(assert) { } }]); - run(function() { - store.findRecord('post', 1).then(function(post) { + return run(() => { + return store.findRecord('post', 1).then(post => { assert.equal(passedUrl[0], '/posts/1'); assert.equal(post.get('id'), '1'); @@ -192,8 +192,8 @@ test('find all records with sideloaded relationships', function(assert) { }] }]); - run(function() { - store.findAll('post').then(function(posts) { + return run(() => { + return store.findAll('post').then(posts => { assert.equal(passedUrl[0], '/posts'); assert.equal(posts.get('length'), '2'); @@ -224,8 +224,8 @@ test('find many records', function(assert) { }] }]); - run(function() { - store.query('post', { filter: { id: 1 } }).then(function(posts) { + return run(() => { + return store.query('post', { filter: { id: 1 } }).then(posts => { assert.equal(passedUrl[0], '/posts'); assert.deepEqual(passedHash[0], { data: { filter: { id: 1 } } }); @@ -246,8 +246,8 @@ test('queryRecord - primary data being a single record', function(assert) { } }]); - run(function() { - store.queryRecord('post', {}).then(function(post) { + return run(() => { + return store.queryRecord('post', {}).then(post => { assert.equal(passedUrl[0], '/posts'); assert.equal(post.get('title'), 'Ember.js rocks'); @@ -260,8 +260,8 @@ test('queryRecord - primary data being null', function(assert) { data: null }]); - run(function() { - store.queryRecord('post', {}).then(function(post) { + return run(() => { + return store.queryRecord('post', {}).then(post => { assert.equal(passedUrl[0], '/posts'); assert.strictEqual(post, null); @@ -277,10 +277,8 @@ testInDebug('queryRecord - primary data being an array throws an assertion', fun }] }]); - assert.expectAssertion(function() { - run(function() { - store.queryRecord('post', {}); - }); + assert.expectAssertion(() => { + run(() => store.queryRecord('post', {})); }, "Expected the primary data returned by the serializer for a `queryRecord` response to be a single object but instead it was an array."); }); @@ -313,14 +311,14 @@ test('find a single record with belongsTo link as object { related }', function( } }]); - run(function() { - store.findRecord('post', 1).then(function(post) { + return run(() => { + return store.findRecord('post', 1).then(post => { assert.equal(passedUrl[0], '/posts/1'); assert.equal(post.get('id'), '1'); assert.equal(post.get('title'), 'Ember.js rocks'); - post.get('author').then(function(author) { + return post.get('author').then(author => { assert.equal(passedUrl[1], 'http://example.com/user/2'); assert.equal(author.get('id'), '2'); @@ -358,14 +356,14 @@ test('find a single record with belongsTo link as object { data }', function(ass } }]); - run(function() { - store.findRecord('post', 1).then(function(post) { + return run(() => { + return store.findRecord('post', 1).then(post => { assert.equal(passedUrl[0], '/posts/1'); assert.equal(post.get('id'), '1'); assert.equal(post.get('title'), 'Ember.js rocks'); - post.get('author').then(function(author) { + return post.get('author').then(author => { assert.equal(passedUrl[1], '/users/2'); assert.equal(author.get('id'), '2'); @@ -404,15 +402,15 @@ test('find a single record with belongsTo link as object { data } (polymorphic)' } }]); - run(function() { - store.findRecord('user', 1).then(function(user) { + return run(() => { + return store.findRecord('user', 1).then(user => { assert.equal(passedUrl[0], '/users/1'); assert.equal(user.get('id'), '1'); assert.equal(user.get('firstName'), 'Yehuda'); assert.equal(user.get('lastName'), 'Katz'); - user.get('company').then(function(company) { + return user.get('company').then(company => { assert.equal(passedUrl[1], '/development-shops/2'); assert.equal(company.get('id'), '2'); @@ -449,15 +447,14 @@ test('find a single record with sideloaded belongsTo link as object { data }', f }] }]); - run(function() { - - store.findRecord('post', 1).then(function(post) { + return run(() => { + return store.findRecord('post', 1).then(post => { assert.equal(passedUrl[0], '/posts/1'); assert.equal(post.get('id'), '1'); assert.equal(post.get('title'), 'Ember.js rocks'); - post.get('author').then(function(author) { + return post.get('author').then(author => { assert.equal(passedUrl.length, 1); assert.equal(author.get('id'), '2'); @@ -502,14 +499,14 @@ test('find a single record with hasMany link as object { related }', function(as }] }]); - run(function() { - store.findRecord('post', 1).then(function(post) { + return run(() => { + return store.findRecord('post', 1).then(post => { assert.equal(passedUrl[0], '/posts/1'); assert.equal(post.get('id'), '1'); assert.equal(post.get('title'), 'Ember.js rocks'); - post.get('comments').then(function(comments) { + return post.get('comments').then(comments => { assert.equal(passedUrl[1], 'http://example.com/post/1/comments'); assert.equal(comments.get('length'), 2); @@ -557,14 +554,14 @@ test('find a single record with hasMany link as object { data }', function(asser } }]); - run(function() { - store.findRecord('post', 1).then(function(post) { + return run(() => { + return store.findRecord('post', 1).then(post => { assert.equal(passedUrl[0], '/posts/1'); assert.equal(post.get('id'), '1'); assert.equal(post.get('title'), 'Ember.js rocks'); - post.get('comments').then(function(comments) { + return post.get('comments').then(comments => { assert.equal(passedUrl[1], '/comments/2'); assert.equal(passedUrl[2], '/comments/3'); @@ -614,15 +611,15 @@ test('find a single record with hasMany link as object { data } (polymorphic)', } }]); - run(function() { - store.findRecord('user', 1).then(function(user) { + return run(() => { + return store.findRecord('user', 1).then(user => { assert.equal(passedUrl[0], '/users/1'); assert.equal(user.get('id'), '1'); assert.equal(user.get('firstName'), 'Yehuda'); assert.equal(user.get('lastName'), 'Katz'); - user.get('handles').then(function(handles) { + return user.get('handles').then(handles => { assert.equal(passedUrl[1], '/github-handles/2'); assert.equal(passedUrl[2], '/twitter-handles/3'); @@ -668,14 +665,14 @@ test('find a single record with sideloaded hasMany link as object { data }', fun }] }]); - run(function() { - store.findRecord('post', 1).then(function(post) { + return run(() => { + return store.findRecord('post', 1).then(post => { assert.equal(passedUrl[0], '/posts/1'); assert.equal(post.get('id'), '1'); assert.equal(post.get('title'), 'Ember.js rocks'); - post.get('comments').then(function(comments) { + return post.get('comments').then(comments => { assert.equal(passedUrl.length, 1); assert.equal(comments.get('length'), 2); @@ -721,15 +718,15 @@ test('find a single record with sideloaded hasMany link as object { data } (poly }] }]); - run(function() { - store.findRecord('user', 1).then(function(user) { + return run(() => { + return store.findRecord('user', 1).then(user => { assert.equal(passedUrl[0], '/users/1'); assert.equal(user.get('id'), '1'); assert.equal(user.get('firstName'), 'Yehuda'); assert.equal(user.get('lastName'), 'Katz'); - user.get('handles').then(function(handles) { + return user.get('handles').then(handles => { assert.equal(passedUrl.length, 1); assert.equal(handles.get('length'), 2); @@ -750,9 +747,9 @@ test('create record', function(assert) { } }]); - run(function() { + return run(() => { - var company = store.push({ data: { + let company = store.push({ data: { type: 'company', id: '1', attributes: { @@ -760,7 +757,7 @@ test('create record', function(assert) { } } }); - var githubHandle = store.push({ data: { + let githubHandle = store.push({ data: { type: 'github-handle', id: '2', attributes: { @@ -768,16 +765,16 @@ test('create record', function(assert) { } } }); - var user = store.createRecord('user', { + let user = store.createRecord('user', { firstName: 'Yehuda', lastName: 'Katz', company: company }); - user.get('handles').then(function(handles) { + return user.get('handles').then(handles => { handles.addObject(githubHandle); - user.save().then(function() { + return user.save().then(() => { assert.equal(passedUrl[0], '/users'); assert.equal(passedVerb[0], 'POST'); assert.deepEqual(passedHash[0], { @@ -811,8 +808,8 @@ test('update record', function(assert) { } }]); - run(function() { - var user = store.push({ data: { + return run(() => { + let user = store.push({ data: { type: 'user', id: '1', attributes: { @@ -821,7 +818,7 @@ test('update record', function(assert) { } } }); - var company = store.push({ data: { + let company = store.push({ data: { type: 'company', id: '2', attributes: { @@ -829,7 +826,7 @@ test('update record', function(assert) { } } }); - var githubHandle = store.push({ data: { + let githubHandle = store.push({ data: { type: 'github-handle', id: '3', attributes: { @@ -840,10 +837,10 @@ test('update record', function(assert) { user.set('firstName', 'Yehuda!'); user.set('company', company); - user.get('handles').then(function(handles) { + return user.get('handles').then(handles => { handles.addObject(githubHandle); - user.save().then(function() { + return user.save().then(() => { assert.equal(passedUrl[0], '/users/1'); assert.equal(passedVerb[0], 'PATCH'); assert.deepEqual(passedHash[0], { @@ -864,7 +861,6 @@ test('update record', function(assert) { } }); }); - }); }); }); @@ -885,8 +881,8 @@ test('update record - serialize hasMany', function(assert) { } })); - run(function() { - var user = store.push({ data: { + return run(() => { + let user = store.push({ data: { type: 'user', id: '1', attributes: { @@ -895,7 +891,7 @@ test('update record - serialize hasMany', function(assert) { } } }); - var githubHandle = store.push({ data: { + let githubHandle = store.push({ data: { type: 'github-handle', id: '2', attributes: { @@ -903,7 +899,7 @@ test('update record - serialize hasMany', function(assert) { } } }); - var twitterHandle = store.push({ data: { + let twitterHandle = store.push({ data: { type: 'twitter-handle', id: '3', attributes: { @@ -913,11 +909,11 @@ test('update record - serialize hasMany', function(assert) { user.set('firstName', 'Yehuda!'); - user.get('handles').then(function(handles) { + return user.get('handles').then(handles => { handles.addObject(githubHandle); handles.addObject(twitterHandle); - user.save().then(function() { + return user.save().then(() => { assert.equal(passedUrl[0], '/users/1'); assert.equal(passedVerb[0], 'PATCH'); assert.deepEqual(passedHash[0], { @@ -941,7 +937,6 @@ test('update record - serialize hasMany', function(assert) { } }); }); - }); }); }); @@ -968,12 +963,11 @@ test('fetching a belongsTo relationship link that returns null', function(assert data: null }]); - run(function() { - store.findRecord('post', 1).then(function(post) { + return run(() => { + return store.findRecord('post', 1).then(post => { assert.equal(passedUrl[0], '/posts/1'); return post.get('author'); - - }).then(function(author) { + }).then(author => { assert.equal(passedUrl[1], 'http://example.com/post/1/author'); assert.equal(author, null); }); diff --git a/tests/integration/adapter/queries-test.js b/tests/integration/adapter/queries-test.js index 7797127e98c..fcfed256fb0 100644 --- a/tests/integration/adapter/queries-test.js +++ b/tests/integration/adapter/queries-test.js @@ -6,9 +6,9 @@ import {module, test} from 'qunit'; import DS from 'ember-data'; -var get = Ember.get; -var Person, env, store, adapter; -var run = Ember.run; +const { get, run } = Ember; + +let Person, env, store, adapter; module("integration/adapter/queries - Queries", { beforeEach() { @@ -30,13 +30,13 @@ module("integration/adapter/queries - Queries", { }); testInDebug("It raises an assertion when no type is passed", function(assert) { - assert.expectAssertion(function() { + assert.expectAssertion(() => { store.query(); }, "You need to pass a model name to the store's query method"); }); testInDebug("It raises an assertion when no query hash is passed", function(assert) { - assert.expectAssertion(function() { + assert.expectAssertion(() => { store.query('person'); }, "You need to pass a query hash to the store's query method"); }); @@ -45,7 +45,7 @@ test("When a query is made, the adapter should receive a record array it can pop adapter.query = function(store, type, query, recordArray) { assert.equal(type, Person, "the query method is called with the correct type"); - return Ember.RSVP.resolve({ + return Ember.RSVP.Promise.resolve({ data: [ { id: 1, @@ -65,13 +65,13 @@ test("When a query is made, the adapter should receive a record array it can pop }); } - store.query('person', { page: 1 }).then(assert.wait(function(queryResults) { + return store.query('person', { page: 1 }).then(queryResults => { assert.equal(get(queryResults, 'length'), 2, "the record array has a length of 2 after the results are loaded"); assert.equal(get(queryResults, 'isLoaded'), true, "the record array's `isLoaded` property should be true"); assert.equal(queryResults.objectAt(0).get('name'), "Peter Wagenet", "the first record is 'Peter Wagenet'"); assert.equal(queryResults.objectAt(1).get('name'), "Brohuda Katz", "the second record is 'Brohuda Katz'"); - })); + }); }); test("a query can be updated via `update()`", function(assert) { @@ -79,8 +79,8 @@ test("a query can be updated via `update()`", function(assert) { return Ember.RSVP.resolve({ data: [{ id: 'first', type: 'person' }] }); }; - run(function() { - store.query('person', {}).then(function(query) { + return run(() => { + return store.query('person', {}).then(query => { assert.equal(query.get('length'), 1); assert.equal(query.get('firstObject.id'), 'first'); assert.equal(query.get('isUpdating'), false); @@ -96,7 +96,7 @@ test("a query can be updated via `update()`", function(assert) { return updateQuery; - }).then(function(query) { + }).then(query => { assert.equal(query.get('length'), 1); assert.equal(query.get('firstObject.id'), 'second'); @@ -116,9 +116,7 @@ testInDebug("The store asserts when query is made and the adapter responses with return Ember.RSVP.resolve({ data: [{ id: 1, type: 'person', attributes: { name: "Peter Wagenet" } }] }); }; - assert.expectAssertion(function() { - Ember.run(function() { - store.query('person', { page: 1 }); - }); + assert.expectAssertion(() => { + Ember.run(() => store.query('person', { page: 1 })); }, /The response to store.query is expected to be an array but it was a single record/); }); diff --git a/tests/integration/adapter/record-persistence-test.js b/tests/integration/adapter/record-persistence-test.js index 3a4091bd5e3..ac7047ef8c8 100644 --- a/tests/integration/adapter/record-persistence-test.js +++ b/tests/integration/adapter/record-persistence-test.js @@ -2,17 +2,13 @@ import setupStore from 'dummy/tests/helpers/store'; import Ember from 'ember'; import {module, test} from 'qunit'; - import DS from 'ember-data'; -var get = Ember.get; -var set = Ember.set; -var attr = DS.attr; -var Person, env, store; -var run = Ember.run; +const { get, set, run, RSVP } = Ember; +const { all, hash } = RSVP; +const { attr } = DS; -var all = Ember.RSVP.all; -var hash = Ember.RSVP.hash; +let Person, env, store; module("integration/adapter/record_persistence - Persisting Records", { beforeEach() { @@ -47,7 +43,7 @@ test("When a store is committed, the adapter's `commit` method should be called return run(Ember.RSVP, 'resolve'); }; - run(function() { + run(() => { env.store.push({ data: { type: 'person', @@ -59,20 +55,20 @@ test("When a store is committed, the adapter's `commit` method should be called }); }); - var tom; + let tom; - run(function() { - env.store.findRecord('person', 1).then(assert.wait(function(person) { + return run(() => { + return env.store.findRecord('person', 1).then(person => { tom = person; set(tom, "name", "Tom Dale"); - tom.save(); - })); + return tom.save(); + }); }); }); test("When a store is committed, the adapter's `commit` method should be called with records that have been created.", function(assert) { assert.expect(2); - var tom; + let tom; env.adapter.createRecord = function(store, type, snapshot) { assert.equal(type, Person, "the type is correct"); @@ -81,26 +77,28 @@ test("When a store is committed, the adapter's `commit` method should be called return Ember.RSVP.resolve({ data: { id: 1, type: "person", attributes: { name: "Tom Dale" } } }); }; - run(function() { + return run(() => { tom = env.store.createRecord('person', { name: "Tom Dale" }); - tom.save(); + return tom.save(); }); }); test("After a created record has been assigned an ID, finding a record by that ID returns the original record.", function(assert) { assert.expect(1); - var tom; + let tom; env.adapter.createRecord = function(store, type, snapshot) { return Ember.RSVP.resolve({ data: { id: 1, type: "person", attributes: { name: "Tom Dale" } } }); }; - run(function() { + return run(() => { tom = env.store.createRecord('person', { name: "Tom Dale" }); - tom.save(); + return tom.save(); + }).then(tom => { + return env.store.find('person', 1).then(nextTom => { + assert.equal(tom, nextTom, "the retrieved record is the same as the created record"); + }); }); - - assert.asyncEqual(tom, env.store.findRecord('person', 1), "the retrieved record is the same as the created record"); }); test("when a store is committed, the adapter's `commit` method should be called with records that have been deleted.", function(assert) { @@ -111,9 +109,9 @@ test("when a store is committed, the adapter's `commit` method should be called return run(Ember.RSVP, 'resolve'); }; - var tom; + let tom; - run(function() { + run(() => { env.store.push({ data: { type: 'person', @@ -124,25 +122,26 @@ test("when a store is committed, the adapter's `commit` method should be called } }); }); - env.store.findRecord('person', 1).then(assert.wait(function(person) { + + return env.store.findRecord('person', 1).then(person => { tom = person; tom.deleteRecord(); return tom.save(); - })).then(assert.wait(function(tom) { + }).then(tom => { assert.equal(get(tom, 'isDeleted'), true, "record is marked as deleted"); - })); + }); }); test("An adapter can notify the store that records were updated by calling `didSaveRecords`.", function(assert) { assert.expect(6); - var tom, yehuda; + let tom, yehuda; env.adapter.updateRecord = function(store, type, snapshot) { return Ember.RSVP.resolve(); }; - run(function() { + run(() => { env.store.push({ data: [{ type: 'person', @@ -154,8 +153,11 @@ test("An adapter can notify the store that records were updated by calling `didS }); }); - all([env.store.findRecord('person', 1), env.store.findRecord('person', 2)]) - .then(assert.wait(function(array) { + return all([ + env.store.findRecord('person', 1), + env.store.findRecord('person', 2) + ]) + .then(array => { tom = array[0]; yehuda = array[1]; @@ -165,14 +167,19 @@ test("An adapter can notify the store that records were updated by calling `didS assert.ok(tom.get('hasDirtyAttributes'), "tom is dirty"); assert.ok(yehuda.get('hasDirtyAttributes'), "yehuda is dirty"); - assert.assertClean(tom.save()).then(assert.wait(function(record) { + let savedTom = assert.assertClean(tom.save()).then(record => { assert.equal(record, tom, "The record is correct"); - })); + }); - assert.assertClean(yehuda.save()).then(assert.wait(function(record) { + let savedYehuda = assert.assertClean(yehuda.save()).then(record => { assert.equal(record, yehuda, "The record is correct"); - })); - })); + }); + + return all([ + savedTom, + savedYehuda + ]); + }); }); test("An adapter can notify the store that records were updated and provide new data by calling `didSaveRecords`.", function(assert) { @@ -184,7 +191,7 @@ test("An adapter can notify the store that records were updated and provide new } }; - run(function() { + run(() => { env.store.push({ data: [{ type: 'person', @@ -202,17 +209,23 @@ test("An adapter can notify the store that records were updated and provide new }); }); - hash({ tom: env.store.findRecord('person', 1), yehuda: env.store.findRecord('person', 2) }).then(assert.wait(function(people) { + return hash({ + tom: env.store.findRecord('person', 1), + yehuda: env.store.findRecord('person', 2) + }).then(people => { people.tom.set('name', "Draaaaaahm Dale"); people.yehuda.set('name', "Goy Katz"); - return hash({ tom: people.tom.save(), yehuda: people.yehuda.save() }); - })).then(assert.wait(function(people) { + return hash({ + tom: people.tom.save(), + yehuda: people.yehuda.save() + }); + }).then(people => { assert.equal(people.tom.get('name'), "Tom Dale", "name attribute should reflect value of hash passed to didSaveRecords"); assert.equal(people.tom.get('updatedAt'), "now", "updatedAt attribute should reflect value of hash passed to didSaveRecords"); assert.equal(people.yehuda.get('name'), "Yehuda Katz", "name attribute should reflect value of hash passed to didSaveRecords"); assert.equal(people.yehuda.get('updatedAt'), "now!", "updatedAt attribute should reflect value of hash passed to didSaveRecords"); - })); + }); }); test("An adapter can notify the store that a record was updated by calling `didSaveRecord`.", function(assert) { @@ -220,7 +233,7 @@ test("An adapter can notify the store that a record was updated by calling `didS return Ember.RSVP.resolve(); }; - run(function() { + run(() => { store.push({ data: [{ type: 'person', @@ -232,7 +245,10 @@ test("An adapter can notify the store that a record was updated by calling `didS }); }); - hash({ tom: store.findRecord('person', 1), yehuda: store.findRecord('person', 2) }).then(assert.wait(function(people) { + return hash({ + tom: store.findRecord('person', 1), + yehuda: store.findRecord('person', 2) + }).then(people => { people.tom.set('name', "Tom Dale"); people.yehuda.set('name', "Yehuda Katz"); @@ -241,8 +257,7 @@ test("An adapter can notify the store that a record was updated by calling `didS assert.assertClean(people.tom.save()); assert.assertClean(people.yehuda.save()); - })); - + }); }); test("An adapter can notify the store that a record was updated and provide new data by calling `didSaveRecord`.", function(assert) { @@ -255,7 +270,7 @@ test("An adapter can notify the store that a record was updated and provide new } }; - run(function() { + run(() => { env.store.push({ data: [{ type: 'person', @@ -273,18 +288,23 @@ test("An adapter can notify the store that a record was updated and provide new }); }); - hash({ tom: store.findRecord('person', 1), yehuda: store.findRecord('person', 2) }).then(assert.wait(function(people) { + return hash({ + tom: store.findRecord('person', 1), + yehuda: store.findRecord('person', 2) + }).then(people => { people.tom.set('name', "Draaaaaahm Dale"); people.yehuda.set('name', "Goy Katz"); - return hash({ tom: people.tom.save(), yehuda: people.yehuda.save() }); - })).then(assert.wait(function(people) { + return hash({ + tom: people.tom.save(), + yehuda: people.yehuda.save() + }); + }).then(people => { assert.equal(people.tom.get('name'), "Tom Dale", "name attribute should reflect value of hash passed to didSaveRecords"); assert.equal(people.tom.get('updatedAt'), "now", "updatedAt attribute should reflect value of hash passed to didSaveRecords"); assert.equal(people.yehuda.get('name'), "Yehuda Katz", "name attribute should reflect value of hash passed to didSaveRecords"); assert.equal(people.yehuda.get('updatedAt'), "now!", "updatedAt attribute should reflect value of hash passed to didSaveRecords"); - })); - + }); }); test("An adapter can notify the store that records were deleted by calling `didSaveRecords`.", function(assert) { @@ -292,7 +312,7 @@ test("An adapter can notify the store that records were deleted by calling `didS return Ember.RSVP.resolve(); }; - run(function() { + run(() => { env.store.push({ data: [{ type: 'person', @@ -310,11 +330,14 @@ test("An adapter can notify the store that records were deleted by calling `didS }); }); - hash({ tom: store.findRecord('person', 1), yehuda: store.findRecord('person', 2) }).then(assert.wait(function(people) { + return hash({ + tom: store.findRecord('person', 1), + yehuda: store.findRecord('person', 2) + }).then(people => { people.tom.deleteRecord(); people.yehuda.deleteRecord(); assert.assertClean(people.tom.save()); assert.assertClean(people.yehuda.save()); - })); + }); }); diff --git a/tests/integration/adapter/rest-adapter-test.js b/tests/integration/adapter/rest-adapter-test.js index ca66a32330c..403b14e4545 100644 --- a/tests/integration/adapter/rest-adapter-test.js +++ b/tests/integration/adapter/rest-adapter-test.js @@ -9,10 +9,11 @@ import Pretender from "pretender"; import DS from 'ember-data'; import { isEnabled } from 'ember-data/-private'; -var env, store, adapter, Post, Comment, SuperUser; -var passedUrl, passedVerb, passedHash; -var run = Ember.run; -var get = Ember.get; +let env, store, adapter, Post, Comment, SuperUser; +let passedUrl, passedVerb, passedHash; +const { run, get } = Ember; +let originalAjax = Ember.$.ajax; +let server; module("integration/adapter/rest_adapter - REST Adapter", { beforeEach() { @@ -37,6 +38,14 @@ module("integration/adapter/rest_adapter - REST Adapter", { adapter = env.adapter; passedUrl = passedVerb = passedHash = null; + }, + afterEach() { + Ember.$.ajax = originalAjax; + + if (server) { + server.shutdown(); + server = null; + } } }); @@ -63,14 +72,14 @@ function ajaxResponse(value) { test("findRecord - basic payload", function(assert) { ajaxResponse({ posts: [{ id: 1, name: "Rails is omakase" }] }); - run(store, 'findRecord', 'post', 1).then(assert.wait(function(post) { + return run(store, 'findRecord', 'post', 1).then(post => { assert.equal(passedUrl, "/posts/1"); assert.equal(passedVerb, "GET"); assert.deepEqual(passedHash.data, {}); assert.equal(post.get('id'), "1"); assert.equal(post.get('name'), "Rails is omakase"); - })); + }); }); @@ -82,22 +91,22 @@ test("findRecord - passes buildURL a requestType", function(assert) { ajaxResponse({ posts: [{ id: 1, name: "Rails is omakase" }] }); - run(store, 'findRecord', 'post', 1).then(assert.wait(function(post) { + return run(store, 'findRecord', 'post', 1).then(post => { assert.equal(passedUrl, "/findRecord/post/1"); - })); + }); }); test("findRecord - basic payload (with legacy singular name)", function(assert) { ajaxResponse({ post: { id: 1, name: "Rails is omakase" } }); - run(store, 'findRecord', 'post', 1).then(assert.wait(function(post) { + return run(store, 'findRecord', 'post', 1).then(post => { assert.equal(passedUrl, "/posts/1"); assert.equal(passedVerb, "GET"); assert.deepEqual(passedHash.data, {}); assert.equal(post.get('id'), "1"); assert.equal(post.get('name'), "Rails is omakase"); - })); + }); }); test("findRecord - payload with sideloaded records of the same type", function(assert) { @@ -108,7 +117,7 @@ test("findRecord - payload with sideloaded records of the same type", function(a ] }); - run(store, 'findRecord', 'post', 1).then(assert.wait(function(post) { + return run(store, 'findRecord', 'post', 1).then(post => { assert.equal(passedUrl, "/posts/1"); assert.equal(passedVerb, "GET"); assert.deepEqual(passedHash.data, {}); @@ -116,10 +125,10 @@ test("findRecord - payload with sideloaded records of the same type", function(a assert.equal(post.get('id'), "1"); assert.equal(post.get('name'), "Rails is omakase"); - var post2 = store.peekRecord('post', 2); + let post2 = store.peekRecord('post', 2); assert.equal(post2.get('id'), "2"); assert.equal(post2.get('name'), "The Parley Letter"); - })); + }); }); test("findRecord - payload with sideloaded records of a different type", function(assert) { @@ -128,7 +137,7 @@ test("findRecord - payload with sideloaded records of a different type", functio comments: [{ id: 1, name: "FIRST" }] }); - run(store, 'findRecord', 'post', 1).then(assert.wait(function(post) { + return run(store, 'findRecord', 'post', 1).then(post => { assert.equal(passedUrl, "/posts/1"); assert.equal(passedVerb, "GET"); assert.deepEqual(passedHash.data, {}); @@ -136,10 +145,10 @@ test("findRecord - payload with sideloaded records of a different type", functio assert.equal(post.get('id'), "1"); assert.equal(post.get('name'), "Rails is omakase"); - var comment = store.peekRecord('comment', 1); + let comment = store.peekRecord('comment', 1); assert.equal(comment.get('id'), "1"); assert.equal(comment.get('name'), "FIRST"); - })); + }); }); @@ -150,14 +159,14 @@ test("findRecord - payload with an serializer-specified primary key", function(a ajaxResponse({ posts: [{ "_ID_": 1, name: "Rails is omakase" }] }); - run(store, 'findRecord', 'post', 1).then(assert.wait(function(post) { + return run(store, 'findRecord', 'post', 1).then(post => { assert.equal(passedUrl, "/posts/1"); assert.equal(passedVerb, "GET"); assert.deepEqual(passedHash.data, {}); assert.equal(post.get('id'), "1"); assert.equal(post.get('name'), "Rails is omakase"); - })); + }); }); test("findRecord - payload with a serializer-specified attribute mapping", function(assert) { @@ -174,7 +183,7 @@ test("findRecord - payload with a serializer-specified attribute mapping", funct ajaxResponse({ posts: [{ id: 1, _NAME_: "Rails is omakase", _CREATED_AT_: 2013 }] }); - run(store, 'findRecord', 'post', 1).then(assert.wait(function(post) { + return run(store, 'findRecord', 'post', 1).then(post => { assert.equal(passedUrl, "/posts/1"); assert.equal(passedVerb, "GET"); assert.deepEqual(passedHash.data, {}); @@ -182,7 +191,7 @@ test("findRecord - payload with a serializer-specified attribute mapping", funct assert.equal(post.get('id'), "1"); assert.equal(post.get('name'), "Rails is omakase"); assert.equal(post.get('createdAt'), 2013); - })); + }); }); test("findRecord - passes `include` as a query parameter to ajax", function(assert) { @@ -190,25 +199,24 @@ test("findRecord - passes `include` as a query parameter to ajax", function(asse post: { id: 1, name: 'Rails is very expensive sushi' } }); - run(store, 'findRecord', 'post', 1, { include: 'comments' }).then(assert.wait(function() { + return run(store, 'findRecord', 'post', 1, { include: 'comments' }).then(() => { assert.deepEqual(passedHash.data, { include: 'comments' }, '`include` parameter sent to adapter.ajax'); - })); + }); }); test("createRecord - an empty payload is a basic success if an id was specified", function(assert) { ajaxResponse(); - var post; - run(function() { - post = store.createRecord('post', { id: "some-uuid", name: "The Parley Letter" }); - post.save().then(assert.wait(function(post) { + return run(() => { + let post = store.createRecord('post', { id: "some-uuid", name: "The Parley Letter" }); + return post.save().then(post => { assert.equal(passedUrl, "/posts"); assert.equal(passedVerb, "POST"); assert.deepEqual(passedHash.data, { post: { id: "some-uuid", name: "The Parley Letter" } }); assert.equal(post.get('hasDirtyAttributes'), false, "the post isn't dirty anymore"); assert.equal(post.get('name'), "The Parley Letter", "the post was updated"); - })); + }); }); }); @@ -218,22 +226,22 @@ test("createRecord - passes buildURL the requestType", function(assert) { }; ajaxResponse(); - var post; - run(function() { - post = store.createRecord('post', { id: "some-uuid", name: "The Parley Letter" }); - post.save().then(assert.wait(function(post) { + return run(() => { + let post = store.createRecord('post', { id: "some-uuid", name: "The Parley Letter" }); + return post.save().then(post => { assert.equal(passedUrl, "/post/createRecord"); - })); + }); }); }); test("createRecord - a payload with a new ID and data applies the updates", function(assert) { ajaxResponse({ posts: [{ id: "1", name: "Dat Parley Letter" }] }); - run(function() { - var post = store.createRecord('post', { name: "The Parley Letter" }); - post.save().then(assert.wait(function(post) { + return run(() => { + let post = store.createRecord('post', { name: "The Parley Letter" }); + + return post.save().then(post => { assert.equal(passedUrl, "/posts"); assert.equal(passedVerb, "POST"); assert.deepEqual(passedHash.data, { post: { name: "The Parley Letter" } }); @@ -241,18 +249,18 @@ test("createRecord - a payload with a new ID and data applies the updates", func assert.equal(post.get('id'), "1", "the post has the updated ID"); assert.equal(post.get('hasDirtyAttributes'), false, "the post isn't dirty anymore"); assert.equal(post.get('name'), "Dat Parley Letter", "the post was updated"); - })); + }); }); }); test("createRecord - a payload with a new ID and data applies the updates (with legacy singular name)", function(assert) { - var post; + let post; ajaxResponse({ post: { id: "1", name: "Dat Parley Letter" } }); run(function() { post = store.createRecord('post', { name: "The Parley Letter" }); }); - run(post, 'save').then(assert.wait(function(post) { + return run(post, 'save').then(post => { assert.equal(passedUrl, "/posts"); assert.equal(passedVerb, "POST"); assert.deepEqual(passedHash.data, { post: { name: "The Parley Letter" } }); @@ -260,17 +268,16 @@ test("createRecord - a payload with a new ID and data applies the updates (with assert.equal(post.get('id'), "1", "the post has the updated ID"); assert.equal(post.get('hasDirtyAttributes'), false, "the post isn't dirty anymore"); assert.equal(post.get('name'), "Dat Parley Letter", "the post was updated"); - })); + }); }); test("createRecord - findMany doesn't overwrite owner", function(assert) { ajaxResponse({ comment: { id: "1", name: "Dat Parley Letter", post: 1 } }); - var comment; Post.reopen({ comments: DS.hasMany('comment', { async: true }) }); Comment.reopen({ post: DS.belongsTo('post', { async: false }) }); - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -286,29 +293,27 @@ test("createRecord - findMany doesn't overwrite owner", function(assert) { } }); }); - var post = store.peekRecord('post', 1); + let post = store.peekRecord('post', 1); - run(function() { - comment = store.createRecord('comment', { name: "The Parley Letter" }); - }); + let comment = run(() => store.createRecord('comment', { name: "The Parley Letter" })); - run(function() { + run(() => { post.get('comments').pushObject(comment); assert.equal(comment.get('post'), post, "the post has been set correctly"); }); - run(function() { - comment.save().then(assert.wait(function(comment) { + return run(() => { + return comment.save().then(comment => { assert.equal(comment.get('hasDirtyAttributes'), false, "the post isn't dirty anymore"); assert.equal(comment.get('name'), "Dat Parley Letter", "the post was updated"); assert.equal(comment.get('post'), post, "the post is still set"); - })); + }); }); }); test("createRecord - a serializer's primary key and attributes are consulted when building the payload", function(assert) { - var post; + let post; env.registry.register('serializer:post', DS.RESTSerializer.extend({ primaryKey: '_id_', @@ -319,17 +324,17 @@ test("createRecord - a serializer's primary key and attributes are consulted whe ajaxResponse(); - run(function() { + run(() => { post = store.createRecord('post', { id: "some-uuid", name: "The Parley Letter" }); }); - run(post, 'save').then(assert.wait(function(post) { + return run(post, 'save').then(post => { assert.deepEqual(passedHash.data, { post: { _id_: 'some-uuid', '_name_': "The Parley Letter" } }); - })); + }); }); test("createRecord - a serializer's attributes are consulted when building the payload if no id is pre-defined", function(assert) { - var post; + let post; env.registry.register('serializer:post', DS.RESTSerializer.extend({ attrs: { name: '_name_' @@ -340,12 +345,12 @@ test("createRecord - a serializer's attributes are consulted when building the p post: { '_name_': "The Parley Letter", id: '1' } }); - run(function() { + return run(() => { post = store.createRecord('post', { name: "The Parley Letter" }); - post.save().then(assert.wait(function(post) { + return post.save().then(post => { assert.deepEqual(passedHash.data, { post: { '_name_': "The Parley Letter" } }); - })); + }); }); }); @@ -362,12 +367,12 @@ test("createRecord - a serializer's attribute mapping takes precedence over keyF ajaxResponse(); - run(function() { - var post = store.createRecord('post', { id: "some-uuid", name: "The Parley Letter" }); + return run(() => { + let post = store.createRecord('post', { id: "some-uuid", name: "The Parley Letter" }); - post.save().then(assert.wait(function(post) { + return post.save().then(post => { assert.deepEqual(passedHash.data, { post: { 'given_name': "The Parley Letter", id: "some-uuid" } }); - })); + }); }); }); @@ -386,13 +391,13 @@ test("createRecord - a serializer's attribute mapping takes precedence over keyF Comment.reopen({ post: DS.belongsTo('post', { async: false }) }); - run(function() { - var post = store.createRecord('post', { id: "a-post-id", name: "The Parley Letter" }); - var comment = store.createRecord('comment', { id: "some-uuid", name: "Letters are fun", post: post }); + return run(() => { + let post = store.createRecord('post', { id: "a-post-id", name: "The Parley Letter" }); + let comment = store.createRecord('comment', { id: "some-uuid", name: "Letters are fun", post: post }); - comment.save().then(assert.wait(function(post) { + return comment.save().then(post => { assert.deepEqual(passedHash.data, { comment: { article: "a-post-id", id: "some-uuid", name: "Letters are fun" } }); - })); + }); }); }); @@ -411,13 +416,13 @@ test("createRecord - a serializer's attribute mapping takes precedence over keyF Post.reopen({ comments: DS.hasMany('comment', { async: false }) }); - run(function() { - var comment = store.createRecord('comment', { id: "a-comment-id", name: "First!" }); - var post = store.createRecord('post', { id: "some-uuid", name: "The Parley Letter", comments: [comment] }); + return run(() => { + let comment = store.createRecord('comment', { id: "a-comment-id", name: "First!" }); + let post = store.createRecord('post', { id: "some-uuid", name: "The Parley Letter", comments: [comment] }); - post.save().then(assert.wait(function(post) { + return post.save().then(post => { assert.deepEqual(passedHash.data, { post: { opinions: ["a-comment-id"], id: "some-uuid", name: "The Parley Letter" } }); - })); + }); }); }); @@ -451,7 +456,7 @@ test("createRecord - a record on the many side of a hasMany relationship should Post.reopen({ comments: DS.hasMany('comment', { async: false }) }); Comment.reopen({ post: DS.belongsTo('post', { async: false }) }); - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -484,34 +489,31 @@ test("createRecord - a record on the many side of a hasMany relationship should }); }); - var post = store.peekRecord('post', 1); - var commentCount = run(function() { - return post.get('comments.length'); - }); + let post = store.peekRecord('post', 1); + let commentCount = run(() => post.get('comments.length')); assert.equal(commentCount, 1, "the post starts life with a comment"); - run(function() { - var comment = store.createRecord('comment', { name: "Another Comment", post: post }); + return run(() => { + let comment = store.createRecord('comment', { name: "Another Comment", post: post }); - comment.save().then(assert.wait(function(comment) { + return comment.save().then(comment => { assert.equal(comment.get('post'), post, "the comment is related to the post"); - })); - - post.reload().then(assert.wait(function(post) { - assert.equal(post.get('comments.length'), 2, "Post comment count has been updated"); - })); + return post.reload().then(post => { + assert.equal(post.get('comments.length'), 2, "Post comment count has been updated"); + }); + }); }); }); test("createRecord - sideloaded belongsTo relationships are both marked as loaded", function(assert) { assert.expect(4); - var post; + let post; Post.reopen({ comment: DS.belongsTo('comment', { async: false }) }); Comment.reopen({ post: DS.belongsTo('post', { async: false }) }); - run(function() { + run(() => { post = store.createRecord('post', { name: "man" }); }); @@ -520,13 +522,13 @@ test("createRecord - sideloaded belongsTo relationships are both marked as loade comments: [{ id: 1, post: 1, name: "Comcast is a bargain" }] }); - run(function() { - post.save().then(assert.wait(function(record) { + return run(() => { + return post.save().then(record => { assert.equal(store.peekRecord('post', 1).get('comment.isLoaded'), true, "post's comment isLoaded (via store)"); assert.equal(store.peekRecord('comment', 1).get('post.isLoaded'), true, "comment's post isLoaded (via store)"); assert.equal(record.get('comment.isLoaded'), true, "post's comment isLoaded (via record)"); assert.equal(record.get('comment.post.isLoaded'), true, "post's comment's post isLoaded (via record)"); - })); + }); }); }); @@ -549,38 +551,38 @@ test("createRecord - response can contain relationships the client doesn't yet k Post.reopen({ comments: DS.hasMany('comment', { async: false }) }); Comment.reopen({ post: DS.belongsTo('post', { async: false }) }); - var post; - run(function() { + let post; + run(() => { post = store.createRecord('post', { name: "Rails is omakase" }); }); - run(function() { - post.save().then(assert.wait(function(post) { + return run(() => { + return post.save().then(post => { assert.equal(post.get('comments.firstObject.post'), post, "the comments are related to the correct post model"); assert.equal(store._internalModelsFor('post').models.length, 1, "There should only be one post record in the store"); - var postRecords = store._internalModelsFor('post').models; + let postRecords = store._internalModelsFor('post').models; for (var i = 0; i < postRecords.length; i++) { assert.equal(post, postRecords[i].getRecord(), "The object in the identity map is the same"); } - })); + }); }); }); test("createRecord - relationships are not duplicated", function(assert) { - var post, comment; + let post, comment; Post.reopen({ comments: DS.hasMany('comment', { async: false }) }); Comment.reopen({ post: DS.belongsTo('post', { async: false }) }); - run(function() { + run(() => { post = store.createRecord('post', { name: "Tomtomhuda" }); comment = store.createRecord('comment', { id: 2, name: "Comment title" }); }); ajaxResponse({ post: [{ id: 1, name: "Rails is omakase", comments: [] }] }); - run(post, 'save').then(assert.wait(function(post) { + return run(post, 'save').then(post => { assert.equal(post.get('comments.length'), 0, "post has 0 comments"); post.get('comments').pushObject(comment); assert.equal(post.get('comments.length'), 1, "post has 1 comment"); @@ -591,13 +593,13 @@ test("createRecord - relationships are not duplicated", function(assert) { }); return post.save(); - })).then(assert.wait(function(post) { + }).then(post => { assert.equal(post.get('comments.length'), 1, "post has 1 comment"); - })); + }); }); test("updateRecord - an empty payload is a basic success", function(assert) { - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -609,19 +611,19 @@ test("updateRecord - an empty payload is a basic success", function(assert) { }); }); - run(function() { - var post = store.peekRecord('post', 1); + return run(() => { + let post = store.peekRecord('post', 1); ajaxResponse(); post.set('name', "The Parley Letter"); - post.save().then(assert.wait(function(post) { + return post.save().then(post => { assert.equal(passedUrl, "/posts/1"); assert.equal(passedVerb, "PUT"); assert.deepEqual(passedHash.data, { post: { name: "The Parley Letter" } }); assert.equal(post.get('hasDirtyAttributes'), false, "the post isn't dirty anymore"); assert.equal(post.get('name'), "The Parley Letter", "the post was updated"); - })); + }); }); }); @@ -631,7 +633,7 @@ test("updateRecord - passes the requestType to buildURL", function(assert) { }; adapter.shouldBackgroundReloadRecord = () => false; - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -643,21 +645,21 @@ test("updateRecord - passes the requestType to buildURL", function(assert) { }); }); - run(function() { - store.findRecord('post', 1).then(assert.wait(function(post) { + return run(() => { + return store.findRecord('post', 1).then(post => { ajaxResponse(); post.set('name', "The Parley Letter"); return post.save(); - })).then(assert.wait(function(post) { + }).then(post => { assert.equal(passedUrl, "/posts/1/updateRecord"); - })); + }); }); }); test("updateRecord - a payload with updates applies the updates", function(assert) { adapter.shouldBackgroundReloadRecord = () => false; - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -669,24 +671,24 @@ test("updateRecord - a payload with updates applies the updates", function(asser }); }); - store.findRecord('post', 1).then(assert.wait(function(post) { + return store.findRecord('post', 1).then(post => { ajaxResponse({ posts: [{ id: 1, name: "Dat Parley Letter" }] }); post.set('name', "The Parley Letter"); return post.save(); - })).then(assert.wait(function(post) { + }).then(post => { assert.equal(passedUrl, "/posts/1"); assert.equal(passedVerb, "PUT"); assert.deepEqual(passedHash.data, { post: { name: "The Parley Letter" } }); assert.equal(post.get('hasDirtyAttributes'), false, "the post isn't dirty anymore"); assert.equal(post.get('name'), "Dat Parley Letter", "the post was updated"); - })); + }); }); test("updateRecord - a payload with updates applies the updates (with legacy singular name)", function(assert) { adapter.shouldBackgroundReloadRecord = () => false; - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -698,30 +700,31 @@ test("updateRecord - a payload with updates applies the updates (with legacy sin }); }); - store.findRecord('post', 1).then(assert.wait(function(post) { + return store.findRecord('post', 1).then(post =>{ ajaxResponse({ post: { id: 1, name: "Dat Parley Letter" } }); post.set('name', "The Parley Letter"); return post.save(); - })).then(assert.wait(function(post) { + }).then(post => { assert.equal(passedUrl, "/posts/1"); assert.equal(passedVerb, "PUT"); assert.deepEqual(passedHash.data, { post: { name: "The Parley Letter" } }); assert.equal(post.get('hasDirtyAttributes'), false, "the post isn't dirty anymore"); assert.equal(post.get('name'), "Dat Parley Letter", "the post was updated"); - })); + }); }); test("updateRecord - a payload with sideloaded updates pushes the updates", function(assert) { - var post; + let post; ajaxResponse({ posts: [{ id: 1, name: "Dat Parley Letter" }], comments: [{ id: 1, name: "FIRST" }] }); - run(function() { + + return run(() => { post = store.createRecord('post', { name: "The Parley Letter" }); - post.save().then(assert.wait(function(post) { + return post.save().then(post => { assert.equal(passedUrl, "/posts"); assert.equal(passedVerb, "POST"); assert.deepEqual(passedHash.data, { post: { name: "The Parley Letter" } }); @@ -730,15 +733,15 @@ test("updateRecord - a payload with sideloaded updates pushes the updates", func assert.equal(post.get('hasDirtyAttributes'), false, "the post isn't dirty anymore"); assert.equal(post.get('name'), "Dat Parley Letter", "the post was updated"); - var comment = store.peekRecord('comment', 1); + let comment = store.peekRecord('comment', 1); assert.equal(comment.get('name'), "FIRST", "The comment was sideloaded"); - })); + }); }); }); test("updateRecord - a payload with sideloaded updates pushes the updates", function(assert) { adapter.shouldBackgroundReloadRecord = () => false; - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -750,7 +753,7 @@ test("updateRecord - a payload with sideloaded updates pushes the updates", func }); }); - store.findRecord('post', 1).then(assert.wait(function(post) { + return store.findRecord('post', 1).then(post => { ajaxResponse({ posts: [{ id: 1, name: "Dat Parley Letter" }], comments: [{ id: 1, name: "FIRST" }] @@ -758,7 +761,7 @@ test("updateRecord - a payload with sideloaded updates pushes the updates", func post.set('name', "The Parley Letter"); return post.save(); - })).then(assert.wait(function(post) { + }).then(post => { assert.equal(passedUrl, "/posts/1"); assert.equal(passedVerb, "PUT"); assert.deepEqual(passedHash.data, { post: { name: "The Parley Letter" } }); @@ -766,9 +769,9 @@ test("updateRecord - a payload with sideloaded updates pushes the updates", func assert.equal(post.get('hasDirtyAttributes'), false, "the post isn't dirty anymore"); assert.equal(post.get('name'), "Dat Parley Letter", "the post was updated"); - var comment = store.peekRecord('comment', 1); + let comment = store.peekRecord('comment', 1); assert.equal(comment.get('name'), "FIRST", "The comment was sideloaded"); - })); + }); }); test("updateRecord - a serializer's primary key and attributes are consulted when building the payload", function(assert) { @@ -781,7 +784,7 @@ test("updateRecord - a serializer's primary key and attributes are consulted whe } })); - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -790,14 +793,15 @@ test("updateRecord - a serializer's primary key and attributes are consulted whe } }); }); + ajaxResponse(); - store.findRecord('post', 1).then(assert.wait(function(post) { + return store.findRecord('post', 1).then(post => { post.set('name', "The Parley Letter"); return post.save(); - })).then(assert.wait(function(post) { + }).then(post => { assert.deepEqual(passedHash.data, { post: { '_name_': "The Parley Letter" } }); - })); + }); }); test("updateRecord - hasMany relationships faithfully reflect simultaneous adds and removes", function(assert) { @@ -805,7 +809,7 @@ test("updateRecord - hasMany relationships faithfully reflect simultaneous adds Comment.reopen({ post: DS.belongsTo('post', { async: false }) }); adapter.shouldBackgroundReloadRecord = () => false; - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -841,26 +845,26 @@ test("updateRecord - hasMany relationships faithfully reflect simultaneous adds posts: { id: 1, name: "Not everyone uses Rails", comments: [2] } }); - store.findRecord('comment', 2).then(assert.wait(function() { + return store.findRecord('comment', 2).then(() => { return store.findRecord('post', 1); - })).then(assert.wait(function(post) { - var newComment = store.peekRecord('comment', 2); - var comments = post.get('comments'); + }).then(post => { + let newComment = store.peekRecord('comment', 2); + let comments = post.get('comments'); // Replace the comment with a new one comments.popObject(); comments.pushObject(newComment); return post.save(); - })).then(assert.wait(function(post) { + }).then(post => { assert.equal(post.get('comments.length'), 1, "the post has the correct number of comments"); assert.equal(post.get('comments.firstObject.name'), "Yes. Yes it is.", "the post has the correct comment"); - })); + }); }); test("deleteRecord - an empty payload is a basic success", function(assert) { adapter.shouldBackgroundReloadRecord = () => false; - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -872,19 +876,19 @@ test("deleteRecord - an empty payload is a basic success", function(assert) { }); }); - store.findRecord('post', 1).then(assert.wait(function(post) { + return store.findRecord('post', 1).then(post => { ajaxResponse(); post.deleteRecord(); return post.save(); - })).then(assert.wait(function(post) { + }).then(post => { assert.equal(passedUrl, "/posts/1"); assert.equal(passedVerb, "DELETE"); assert.equal(passedHash, undefined); assert.equal(post.get('hasDirtyAttributes'), false, "the post isn't dirty anymore"); assert.equal(post.get('isDeleted'), true, "the post is now deleted"); - })); + }); }); test("deleteRecord - passes the requestType to buildURL", function(assert) { @@ -893,7 +897,7 @@ test("deleteRecord - passes the requestType to buildURL", function(assert) { return "/posts/" + id + "/" + requestType; }; - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -905,19 +909,19 @@ test("deleteRecord - passes the requestType to buildURL", function(assert) { }); }); - store.findRecord('post', 1).then(assert.wait(function(post) { + return store.findRecord('post', 1).then(post => { ajaxResponse(); post.deleteRecord(); return post.save(); - })).then(assert.wait(function(post) { + }).then(post => { assert.equal(passedUrl, "/posts/1/deleteRecord"); - })); + }); }); test("deleteRecord - a payload with sideloaded updates pushes the updates", function(assert) { adapter.shouldBackgroundReloadRecord = () => false; - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -929,12 +933,12 @@ test("deleteRecord - a payload with sideloaded updates pushes the updates", func }); }); - store.findRecord('post', 1).then(assert.wait(function(post) { + return store.findRecord('post', 1).then(post => { ajaxResponse({ comments: [{ id: 1, name: "FIRST" }] }); post.deleteRecord(); return post.save(); - })).then(assert.wait(function(post) { + }).then(post => { assert.equal(passedUrl, "/posts/1"); assert.equal(passedVerb, "DELETE"); assert.equal(passedHash, undefined); @@ -942,14 +946,14 @@ test("deleteRecord - a payload with sideloaded updates pushes the updates", func assert.equal(post.get('hasDirtyAttributes'), false, "the post isn't dirty anymore"); assert.equal(post.get('isDeleted'), true, "the post is now deleted"); - var comment = store.peekRecord('comment', 1); + let comment = store.peekRecord('comment', 1); assert.equal(comment.get('name'), "FIRST", "The comment was sideloaded"); - })); + }); }); test("deleteRecord - a payload with sidloaded updates pushes the updates when the original record is omitted", function(assert) { adapter.shouldBackgroundReloadRecord = () => false; - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -961,12 +965,12 @@ test("deleteRecord - a payload with sidloaded updates pushes the updates when th }); }); - store.findRecord('post', 1).then(assert.wait(function(post) { + return store.findRecord('post', 1).then(post => { ajaxResponse({ posts: [{ id: 2, name: "The Parley Letter" }] }); post.deleteRecord(); return post.save(); - })).then(assert.wait(function(post) { + }).then(post => { assert.equal(passedUrl, "/posts/1"); assert.equal(passedVerb, "DELETE"); assert.equal(passedHash, undefined); @@ -974,27 +978,24 @@ test("deleteRecord - a payload with sidloaded updates pushes the updates when th assert.equal(post.get('hasDirtyAttributes'), false, "the original post isn't dirty anymore"); assert.equal(post.get('isDeleted'), true, "the original post is now deleted"); - var newPost = store.peekRecord('post', 2); + let newPost = store.peekRecord('post', 2); assert.equal(newPost.get('name'), "The Parley Letter", "The new post was added to the store"); - })); + }); }); test("deleteRecord - deleting a newly created record should not throw an error", function(assert) { - var post; - run(function() { - post = store.createRecord('post'); - }); + let post = run(() => store.createRecord('post')); - run(function() { + return run(() => { post.deleteRecord(); - post.save().then(assert.wait(function(post) { + return post.save().then(post => { assert.equal(passedUrl, null, "There is no ajax call to delete a record that has never been saved."); assert.equal(passedVerb, null, "There is no ajax call to delete a record that has never been saved."); assert.equal(passedHash, null, "There is no ajax call to delete a record that has never been saved."); assert.equal(post.get('isDeleted'), true, "the post is now deleted"); assert.equal(post.get('isError'), false, "the post is not an error"); - })); + }); }); }); @@ -1006,13 +1007,13 @@ test("findAll - returning an array populates the array", function(assert) { ] }); - store.findAll('post').then(assert.wait(function(posts) { + return store.findAll('post').then(posts => { assert.equal(passedUrl, "/posts"); assert.equal(passedVerb, "GET"); assert.deepEqual(passedHash.data, {}); - var post1 = store.peekRecord('post', 1); - var post2 = store.peekRecord('post', 2); + let post1 = store.peekRecord('post', 1); + let post2 = store.peekRecord('post', 2); assert.deepEqual( post1.getProperties('id', 'name'), @@ -1033,7 +1034,7 @@ test("findAll - returning an array populates the array", function(assert) { [post1, post2], "The correct records are in the array" ); - })); + }); }); @@ -1052,9 +1053,9 @@ test("findAll - passes buildURL the requestType and snapshot", function(assert) ] }); - store.findAll('post', { adapterOptions: adapterOptionsStub }).then(assert.wait(function(posts) { + return store.findAll('post', { adapterOptions: adapterOptionsStub }).then(posts => { assert.equal(passedUrl, "/findAll/posts"); - })); + }); }); test("findAll - passed `include` as a query parameter to ajax", function(assert) { @@ -1062,9 +1063,9 @@ test("findAll - passed `include` as a query parameter to ajax", function(assert) posts: [{ id: 1, name: 'Rails is very expensive sushi' }] }); - run(store, 'findAll', 'post', { include: 'comments' }).then(assert.wait(function() { + return run(store, 'findAll', 'post', { include: 'comments' }).then(() => { assert.deepEqual(passedHash.data, { include: 'comments' }, '`include` params sent to adapter.ajax'); - })); + }); }); test("findAll - returning sideloaded data loads the data", function(assert) { @@ -1075,11 +1076,11 @@ test("findAll - returning sideloaded data loads the data", function(assert) { ], comments: [{ id: 1, name: "FIRST" }] }); - store.findAll('post').then(assert.wait(function(posts) { - var comment = store.peekRecord('comment', 1); + return store.findAll('post').then(posts => { + let comment = store.peekRecord('comment', 1); assert.deepEqual(comment.getProperties('id', 'name'), { id: "1", name: "FIRST" }); - })); + }); }); test("findAll - data is normalized through custom serializers", function(assert) { @@ -1095,9 +1096,9 @@ test("findAll - data is normalized through custom serializers", function(assert) ] }); - store.findAll('post').then(assert.wait(function(posts) { - var post1 = store.peekRecord('post', 1); - var post2 = store.peekRecord('post', 2); + return store.findAll('post').then(posts => { + let post1 = store.peekRecord('post', 1); + let post2 = store.peekRecord('post', 2); assert.deepEqual( post1.getProperties('id', 'name'), @@ -1117,7 +1118,7 @@ test("findAll - data is normalized through custom serializers", function(assert) [post1, post2], "The correct records are in the array" ); - })); + }); }); test("query - if `sortQueryParams` option is not provided, query params are sorted alphabetically", function(assert) { @@ -1125,9 +1126,9 @@ test("query - if `sortQueryParams` option is not provided, query params are sort posts: [{ id: 1, name: "Rails is very expensive sushi" }] }); - store.query('post', { "params": 1, "in": 2, "wrong": 3, "order": 4 }).then(assert.wait(function() { + return store.query('post', { "params": 1, "in": 2, "wrong": 3, "order": 4 }).then(() => { assert.deepEqual(Object.keys(passedHash.data), ["in", "order", "params", "wrong"], 'query params are received in alphabetical order'); - })); + }); }); test("query - passes buildURL the requestType", function(assert) { @@ -1139,9 +1140,9 @@ test("query - passes buildURL the requestType", function(assert) { posts: [{ id: 1, name: "Rails is very expensive sushi" }] }); - store.query('post', { "params": 1, "in": 2, "wrong": 3, "order": 4 }).then(assert.wait(function() { + return store.query('post', { "params": 1, "in": 2, "wrong": 3, "order": 4 }).then(() => { assert.equal(passedUrl, '/query/posts'); - })); + }); }); test("query - if `sortQueryParams` is falsey, query params are not sorted at all", function(assert) { @@ -1151,9 +1152,9 @@ test("query - if `sortQueryParams` is falsey, query params are not sorted at all adapter.sortQueryParams = null; - store.query('post', { "params": 1, "in": 2, "wrong": 3, "order": 4 }).then(assert.wait(function() { + return store.query('post', { "params": 1, "in": 2, "wrong": 3, "order": 4 }).then(() => { assert.deepEqual(Object.keys(passedHash.data), ["params", "in", "wrong", "order"], 'query params are received in their original order'); - })); + }); }); test("query - if `sortQueryParams` is a custom function, query params passed through that function", function(assert) { @@ -1162,9 +1163,9 @@ test("query - if `sortQueryParams` is a custom function, query params passed thr }); adapter.sortQueryParams = function(obj) { - var sortedKeys = Object.keys(obj).sort().reverse(); - var len = sortedKeys.length; - var newQueryParams = {}; + let sortedKeys = Object.keys(obj).sort().reverse(); + let len = sortedKeys.length; + let newQueryParams = {}; for (var i = 0; i < len; i++) { newQueryParams[sortedKeys[i]] = obj[sortedKeys[i]]; @@ -1172,9 +1173,9 @@ test("query - if `sortQueryParams` is a custom function, query params passed thr return newQueryParams; }; - store.query('post', { "params": 1, "in": 2, "wrong": 3, "order": 4 }).then(assert.wait(function() { + return store.query('post', { "params": 1, "in": 2, "wrong": 3, "order": 4 }).then(() => { assert.deepEqual(Object.keys(passedHash.data), ["wrong", "params", "order", "in"], 'query params are received in reverse alphabetical order'); - })); + }); }); test("query - payload 'meta' is accessible on the record array", function(assert) { @@ -1183,13 +1184,13 @@ test("query - payload 'meta' is accessible on the record array", function(assert posts: [{ id: 1, name: "Rails is very expensive sushi" }] }); - store.query('post', { page: 2 }).then(assert.wait(function(posts) { + return store.query('post', { page: 2 }).then(posts => { assert.equal( posts.get('meta.offset'), 5, "Reponse metadata can be accessed with recordArray.meta" ); - })); + }); }); test("query - each record array can have it's own meta object", function(assert) { @@ -1198,7 +1199,7 @@ test("query - each record array can have it's own meta object", function(assert) posts: [{ id: 1, name: "Rails is very expensive sushi" }] }); - store.query('post', { page: 2 }).then(assert.wait(function(posts) { + return store.query('post', { page: 2 }).then(posts => { assert.equal( posts.get('meta.offset'), 5, @@ -1208,11 +1209,12 @@ test("query - each record array can have it's own meta object", function(assert) meta: { offset: 1 }, posts: [{ id: 1, name: "Rails is very expensive sushi" }] }); - store.query('post', { page: 1 }).then(assert.wait(function(newPosts) { + + return store.query('post', { page: 1 }).then(newPosts => { assert.equal(newPosts.get('meta.offset'), 1, 'new array has correct metadata'); assert.equal(posts.get('meta.offset'), 5, 'metadata on the old array hasnt been clobbered'); - })); - })); + }); + }); }); @@ -1223,13 +1225,13 @@ test("query - returning an array populates the array", function(assert) { { id: 2, name: "The Parley Letter" }] }); - store.query('post', { page: 1 }).then(assert.wait(function(posts) { + return store.query('post', { page: 1 }).then(posts => { assert.equal(passedUrl, '/posts'); assert.equal(passedVerb, 'GET'); assert.deepEqual(passedHash.data, { page: 1 }); - var post1 = store.peekRecord('post', 1); - var post2 = store.peekRecord('post', 2); + let post1 = store.peekRecord('post', 1); + let post2 = store.peekRecord('post', 2); assert.deepEqual( post1.getProperties('id', 'name'), @@ -1249,7 +1251,7 @@ test("query - returning an array populates the array", function(assert) { [post1, post2], "The correct records are in the array" ); - })); + }); }); test("query - returning sideloaded data loads the data", function(assert) { @@ -1261,11 +1263,11 @@ test("query - returning sideloaded data loads the data", function(assert) { comments: [{ id: 1, name: "FIRST" }] }); - store.query('post', { page: 1 }).then(assert.wait(function(posts) { - var comment = store.peekRecord('comment', 1); + return store.query('post', { page: 1 }).then(posts => { + let comment = store.peekRecord('comment', 1); assert.deepEqual(comment.getProperties('id', 'name'), { id: "1", name: "FIRST" }); - })); + }); }); test("query - data is normalized through custom serializers", function(assert) { @@ -1279,9 +1281,9 @@ test("query - data is normalized through custom serializers", function(assert) { { _ID_: 2, _NAME_: "The Parley Letter" }] }); - store.query('post', { page: 1 }).then(assert.wait(function(posts) { - var post1 = store.peekRecord('post', 1); - var post2 = store.peekRecord('post', 2); + return store.query('post', { page: 1 }).then(posts => { + let post1 = store.peekRecord('post', 1); + let post2 = store.peekRecord('post', 2); assert.deepEqual( post1.getProperties('id', 'name'), @@ -1302,15 +1304,15 @@ test("query - data is normalized through custom serializers", function(assert) { [post1, post2], "The correct records are in the array" ); - })); + }); }); test("queryRecord - empty response", function(assert) { ajaxResponse({}); - store.queryRecord('post', { slug: 'ember-js-rocks' }).then(assert.wait(function(post) { + return store.queryRecord('post', { slug: 'ember-js-rocks' }).then(post => { assert.strictEqual(post, null); - })); + }); }); test("queryRecord - primary data being null", function(assert) { @@ -1318,9 +1320,9 @@ test("queryRecord - primary data being null", function(assert) { post: null }); - store.queryRecord('post', { slug: 'ember-js-rocks' }).then(assert.wait(function(post) { + return store.queryRecord('post', { slug: 'ember-js-rocks' }).then(post => { assert.strictEqual(post, null); - })); + }); }); test("queryRecord - primary data being a single object", function(assert) { @@ -1331,9 +1333,9 @@ test("queryRecord - primary data being a single object", function(assert) { } }); - store.queryRecord('post', { slug: 'ember-js-rocks' }).then(assert.wait(function(post) { + return store.queryRecord('post', { slug: 'ember-js-rocks' }).then(post => { assert.deepEqual(post.get('name'), "Ember.js rocks"); - })); + }); }); test("queryRecord - returning sideloaded data loads the data", function(assert) { @@ -1342,11 +1344,11 @@ test("queryRecord - returning sideloaded data loads the data", function(assert) comments: [{ id: 1, name: "FIRST" }] }); - store.queryRecord('post', { slug: 'rails-is-omakaze' }).then(assert.wait(function(post) { - var comment = store.peekRecord('comment', 1); + return store.queryRecord('post', { slug: 'rails-is-omakaze' }).then(post => { + let comment = store.peekRecord('comment', 1); assert.deepEqual(comment.getProperties('id', 'name'), { id: "1", name: "FIRST" }); - })); + }); }); testInDebug("queryRecord - returning an array picks the first one but saves all records to the store", function(assert) { @@ -1356,46 +1358,34 @@ testInDebug("queryRecord - returning an array picks the first one but saves all assert.expectDeprecation('The adapter returned an array for the primary data of a `queryRecord` response. This is deprecated as `queryRecord` should return a single record.'); - run(function() { - store.queryRecord('post', { slug: 'rails-is-omakaze' }).then(assert.wait(function(post) { - var post2 = store.peekRecord('post', 2); + return run(() => { + return store.queryRecord('post', { slug: 'rails-is-omakaze' }).then(post => { + let post2 = store.peekRecord('post', 2); assert.deepEqual(post.getProperties('id', 'name'), { id: "1", name: "Rails is omakase" }); assert.deepEqual(post2.getProperties('id', 'name'), { id: "2", name: "Ember is js" }); - })); + }); }); }); testInDebug("queryRecord - returning an array is deprecated", function(assert) { - let done = assert.async(); - ajaxResponse({ post: [{ id: 1, name: "Rails is omakase" }, { id: 2, name: "Ember is js" }] }); assert.expectDeprecation('The adapter returned an array for the primary data of a `queryRecord` response. This is deprecated as `queryRecord` should return a single record.'); - run(function() { - store.queryRecord('post', { slug: 'rails-is-omakaze' }).then(function() { - done(); - }); - }); + return run(() => store.queryRecord('post', { slug: 'rails-is-omakaze' })); }); testInDebug("queryRecord - returning an single object doesn't throw a deprecation", function(assert) { - let done = assert.async(); - ajaxResponse({ post: { id: 1, name: "Rails is omakase" } }); assert.expectNoDeprecation(); - run(function() { - store.queryRecord('post', { slug: 'rails-is-omakaze' }).then(function() { - done(); - }); - }); + return run(() => store.queryRecord('post', { slug: 'rails-is-omakaze' })); }); test("queryRecord - data is normalized through custom serializers", function(assert) { @@ -1408,21 +1398,20 @@ test("queryRecord - data is normalized through custom serializers", function(ass post: { _ID_: 1, _NAME_: "Rails is omakase" } }); - store.queryRecord('post', { slug: 'rails-is-omakaze' }).then(assert.wait(function(post) { - + return store.queryRecord('post', { slug: 'rails-is-omakaze' }).then(post => { assert.deepEqual( post.getProperties('id', 'name'), { id: "1", name: "Rails is omakase" }, "Post 1 is loaded with correct data" ); - })); + }); }); test("findMany - findMany uses a correct URL to access the records", function(assert) { Post.reopen({ comments: DS.hasMany('comment', { async: true }) }); adapter.coalesceFindRequests = true; - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -1443,7 +1432,7 @@ test("findMany - findMany uses a correct URL to access the records", function(as }); }); - var post = store.peekRecord('post', 1); + let post = store.peekRecord('post', 1); ajaxResponse({ comments: [ { id: 1, name: "FIRST" }, @@ -1451,10 +1440,11 @@ test("findMany - findMany uses a correct URL to access the records", function(as { id: 3, name: "What is omakase?" } ] }); - run(post, 'get', 'comments').then(assert.wait(function(comments) { + + return run(post, 'get', 'comments').then(comments => { assert.equal(passedUrl, "/comments"); assert.deepEqual(passedHash, { data: { ids: ["1", "2", "3"] } }); - })); + }); }); test("findMany - passes buildURL the requestType", function(assert) { @@ -1465,7 +1455,7 @@ test("findMany - passes buildURL the requestType", function(assert) { Post.reopen({ comments: DS.hasMany('comment', { async: true }) }); adapter.coalesceFindRequests = true; - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -1486,7 +1476,7 @@ test("findMany - passes buildURL the requestType", function(assert) { }); }); - var post = store.peekRecord('post', 1); + let post = store.peekRecord('post', 1); ajaxResponse({ comments: [ { id: 1, name: "FIRST" }, @@ -1494,15 +1484,16 @@ test("findMany - passes buildURL the requestType", function(assert) { { id: 3, name: "What is omakase?" } ] }); - run(post, 'get', 'comments').then(assert.wait(function(comments) { + + return run(post, 'get', 'comments').then(comments => { assert.equal(passedUrl, "/findMany/comment"); - })); + }); }); test("findMany - findMany does not coalesce by default", function(assert) { Post.reopen({ comments: DS.hasMany('comment', { async: true }) }); - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -1523,7 +1514,7 @@ test("findMany - findMany does not coalesce by default", function(assert) { }); }); - var post = store.peekRecord('post', 1); + let post = store.peekRecord('post', 1); //It's still ok to return this even without coalescing because RESTSerializer supports sideloading ajaxResponse({ comments: [ @@ -1532,10 +1523,11 @@ test("findMany - findMany does not coalesce by default", function(assert) { { id: 3, name: "What is omakase?" } ] }); - run(post, 'get', 'comments').then(assert.wait(function(comments) { + + return run(post, 'get', 'comments').then(comments => { assert.equal(passedUrl, "/comments/3"); assert.deepEqual(passedHash.data, {}); - })); + }); }); test("findMany - returning an array populates the array", function(assert) { @@ -1543,7 +1535,7 @@ test("findMany - returning an array populates the array", function(assert) { Post.reopen({ comments: DS.hasMany('comment', { async: true }) }); adapter.coalesceFindRequests = true; - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -1564,7 +1556,7 @@ test("findMany - returning an array populates the array", function(assert) { }); }); - store.findRecord('post', 1).then(assert.wait(function(post) { + return store.findRecord('post', 1).then(post => { ajaxResponse({ comments: [ { id: 1, name: "FIRST" }, @@ -1574,10 +1566,10 @@ test("findMany - returning an array populates the array", function(assert) { }); return post.get('comments'); - })).then(assert.wait(function(comments) { - var comment1 = store.peekRecord('comment', 1); - var comment2 = store.peekRecord('comment', 2); - var comment3 = store.peekRecord('comment', 3); + }).then(comments => { + let comment1 = store.peekRecord('comment', 1); + let comment2 = store.peekRecord('comment', 2); + let comment3 = store.peekRecord('comment', 3); assert.deepEqual(comment1.getProperties('id', 'name'), { id: "1", name: "FIRST" }); assert.deepEqual(comment2.getProperties('id', 'name'), { id: "2", name: "Rails is unagi" }); @@ -1588,7 +1580,7 @@ test("findMany - returning an array populates the array", function(assert) { [comment1, comment2, comment3], "The correct records are in the array" ); - })); + }); }); test("findMany - returning sideloaded data loads the data", function(assert) { @@ -1596,7 +1588,7 @@ test("findMany - returning sideloaded data loads the data", function(assert) { Post.reopen({ comments: DS.hasMany('comment', { async: true }) }); adapter.coalesceFindRequests = true; - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -1617,7 +1609,7 @@ test("findMany - returning sideloaded data loads the data", function(assert) { }); }); - store.findRecord('post', 1).then(assert.wait(function(post) { + return store.findRecord('post', 1).then(post => { ajaxResponse({ comments: [ { id: 1, name: "FIRST" }, @@ -1629,12 +1621,12 @@ test("findMany - returning sideloaded data loads the data", function(assert) { }); return post.get('comments'); - })).then(assert.wait(function(comments) { - var comment1 = store.peekRecord('comment', 1); - var comment2 = store.peekRecord('comment', 2); - var comment3 = store.peekRecord('comment', 3); - var comment4 = store.peekRecord('comment', 4); - var post2 = store.peekRecord('post', 2); + }).then(comments => { + let comment1 = store.peekRecord('comment', 1); + let comment2 = store.peekRecord('comment', 2); + let comment3 = store.peekRecord('comment', 3); + let comment4 = store.peekRecord('comment', 4); + let post2 = store.peekRecord('post', 2); assert.deepEqual( comments.toArray(), @@ -1644,7 +1636,7 @@ test("findMany - returning sideloaded data loads the data", function(assert) { assert.deepEqual(comment4.getProperties('id', 'name'), { id: "4", name: "Unrelated comment" }); assert.deepEqual(post2.getProperties('id', 'name'), { id: "2", name: "The Parley Letter" }); - })); + }); }); test("findMany - a custom serializer is used if present", function(assert) { @@ -1662,7 +1654,7 @@ test("findMany - a custom serializer is used if present", function(assert) { adapter.coalesceFindRequests = true; Post.reopen({ comments: DS.hasMany('comment', { async: true }) }); - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -1683,7 +1675,7 @@ test("findMany - a custom serializer is used if present", function(assert) { }); }); - store.findRecord('post', 1).then(assert.wait(function(post) { + return store.findRecord('post', 1).then(post => { ajaxResponse({ comments: [ { _ID_: 1, _NAME_: "FIRST" }, @@ -1692,24 +1684,24 @@ test("findMany - a custom serializer is used if present", function(assert) { }); return post.get('comments'); - })).then(assert.wait(function(comments) { - var comment1 = store.peekRecord('comment', 1); - var comment2 = store.peekRecord('comment', 2); - var comment3 = store.peekRecord('comment', 3); + }).then(comments => { + let comment1 = store.peekRecord('comment', 1); + let comment2 = store.peekRecord('comment', 2); + let comment3 = store.peekRecord('comment', 3); assert.deepEqual(comment1.getProperties('id', 'name'), { id: "1", name: "FIRST" }); assert.deepEqual(comment2.getProperties('id', 'name'), { id: "2", name: "Rails is unagi" }); assert.deepEqual(comment3.getProperties('id', 'name'), { id: "3", name: "What is omakase?" }); assert.deepEqual(comments.toArray(), [comment1, comment2, comment3], "The correct records are in the array"); - })); + }); }); test("findHasMany - returning an array populates the array", function(assert) { adapter.shouldBackgroundReloadRecord = () => false; Post.reopen({ comments: DS.hasMany('comment', { async: true }) }); - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -1728,7 +1720,7 @@ test("findHasMany - returning an array populates the array", function(assert) { }); }); - run(store, 'findRecord', 'post', '1').then(assert.wait(function(post) { + return run(store, 'findRecord', 'post', '1').then(post => { ajaxResponse({ comments: [ { id: 1, name: "FIRST" }, @@ -1738,21 +1730,21 @@ test("findHasMany - returning an array populates the array", function(assert) { }); return post.get('comments'); - })).then(assert.wait(function(comments) { + }).then(comments => { assert.equal(passedUrl, '/posts/1/comments'); assert.equal(passedVerb, 'GET'); assert.equal(passedHash, undefined); - var comment1 = store.peekRecord('comment', 1); - var comment2 = store.peekRecord('comment', 2); - var comment3 = store.peekRecord('comment', 3); + let comment1 = store.peekRecord('comment', 1); + let comment2 = store.peekRecord('comment', 2); + let comment3 = store.peekRecord('comment', 3); assert.deepEqual(comment1.getProperties('id', 'name'), { id: "1", name: "FIRST" }); assert.deepEqual(comment2.getProperties('id', 'name'), { id: "2", name: "Rails is unagi" }); assert.deepEqual(comment3.getProperties('id', 'name'), { id: "3", name: "What is omakase?" }); assert.deepEqual(comments.toArray(), [comment1, comment2, comment3], "The correct records are in the array"); - })); + }); }); test("findHasMany - passes buildURL the requestType", function(assert) { @@ -1765,7 +1757,7 @@ test("findHasMany - passes buildURL the requestType", function(assert) { Post.reopen({ comments: DS.hasMany('comment', { async: true }) }); - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -1784,7 +1776,7 @@ test("findHasMany - passes buildURL the requestType", function(assert) { }); }); - run(store, 'findRecord', 'post', '1').then(assert.wait(function(post) { + return run(store, 'findRecord', 'post', '1').then(post => { ajaxResponse({ comments: [ { id: 1, name: "FIRST" }, @@ -1794,19 +1786,15 @@ test("findHasMany - passes buildURL the requestType", function(assert) { }); return post.get('comments'); - })).then(assert.wait(function(comments) { - // NOOP - })); + }); }); - - test("findMany - returning sideloaded data loads the data (with JSONApi Links)", function(assert) { adapter.shouldBackgroundReloadRecord = () => false; Post.reopen({ comments: DS.hasMany('comment', { async: true }) }); adapter.coalesceFindRequests = true; - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -1825,7 +1813,7 @@ test("findMany - returning sideloaded data loads the data (with JSONApi Links)", }); }); - store.findRecord('post', 1).then(assert.wait(function(post) { + return store.findRecord('post', 1).then(post => { ajaxResponse({ comments: [ { id: 1, name: "FIRST" }, @@ -1836,16 +1824,16 @@ test("findMany - returning sideloaded data loads the data (with JSONApi Links)", }); return post.get('comments'); - })).then(assert.wait(function(comments) { - var comment1 = store.peekRecord('comment', 1); - var comment2 = store.peekRecord('comment', 2); - var comment3 = store.peekRecord('comment', 3); - var post2 = store.peekRecord('post', 2); + }).then(comments => { + let comment1 = store.peekRecord('comment', 1); + let comment2 = store.peekRecord('comment', 2); + let comment3 = store.peekRecord('comment', 3); + let post2 = store.peekRecord('post', 2); assert.deepEqual(comments.toArray(), [comment1, comment2, comment3], "The correct records are in the array"); assert.deepEqual(post2.getProperties('id', 'name'), { id: "2", name: "The Parley Letter" }); - })); + }); }); test("findMany - a custom serializer is used if present", function(assert) { @@ -1862,7 +1850,7 @@ test("findMany - a custom serializer is used if present", function(assert) { Post.reopen({ comments: DS.hasMany('comment', { async: true }) }); - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -1881,7 +1869,7 @@ test("findMany - a custom serializer is used if present", function(assert) { }); }); - store.findRecord('post', 1).then(assert.wait(function(post) { + return store.findRecord('post', 1).then(post => { ajaxResponse({ comments: [ { _ID_: 1, _NAME_: "FIRST" }, @@ -1890,17 +1878,17 @@ test("findMany - a custom serializer is used if present", function(assert) { ] }); return post.get('comments'); - })).then(assert.wait(function(comments) { - var comment1 = store.peekRecord('comment', 1); - var comment2 = store.peekRecord('comment', 2); - var comment3 = store.peekRecord('comment', 3); + }).then(comments => { + let comment1 = store.peekRecord('comment', 1); + let comment2 = store.peekRecord('comment', 2); + let comment3 = store.peekRecord('comment', 3); assert.deepEqual(comment1.getProperties('id', 'name'), { id: "1", name: "FIRST" }); assert.deepEqual(comment2.getProperties('id', 'name'), { id: "2", name: "Rails is unagi" }); assert.deepEqual(comment3.getProperties('id', 'name'), { id: "3", name: "What is omakase?" }); assert.deepEqual(comments.toArray(), [comment1, comment2, comment3], "The correct records are in the array"); - })); + }); }); test('findBelongsTo - passes buildURL the requestType', function(assert) { @@ -1913,7 +1901,7 @@ test('findBelongsTo - passes buildURL the requestType', function(assert) { Comment.reopen({ post: DS.belongsTo('post', { async: true }) }); - run(function() { + run(() => { store.push({ data: { type: 'comment', @@ -1932,12 +1920,10 @@ test('findBelongsTo - passes buildURL the requestType', function(assert) { }); }); - run(store, 'findRecord', 'comment', 1).then(assert.wait(function(comment) { + return run(store, 'findRecord', 'comment', 1).then(comment => { ajaxResponse({ post: { id: 1, name: 'Rails is omakase' } }); return comment.get('post'); - })).then(assert.wait(function(post) { - // NOOP - })); + }); }); testInDebug('coalesceFindRequests assert.warns if the expected records are not returned in the coalesced request', function(assert) { @@ -2000,8 +1986,8 @@ test('groupRecordsForFindMany groups records based on their url', function(asser return Ember.RSVP.resolve({ comments: [{ id: 2 }, { id: 3 }] }); }; - var post; - run(function() { + let post; + run(() => { store.push({ data: { type: 'post', @@ -2020,9 +2006,7 @@ test('groupRecordsForFindMany groups records based on their url', function(asser post = store.peekRecord('post', 2); }); - run(function() { - post.get('comments'); - }); + run(() => post.get('comments')); }); test('groupRecordsForFindMany groups records correctly when singular URLs are encoded as query params', function(assert) { @@ -2047,9 +2031,9 @@ test('groupRecordsForFindMany groups records correctly when singular URLs are en assert.deepEqual(ids, ['2', '3']); return Ember.RSVP.resolve({ comments: [{ id: 2 }, { id: 3 }] }); }; - var post; + let post; - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -2068,9 +2052,7 @@ test('groupRecordsForFindMany groups records correctly when singular URLs are en post = store.peekRecord('post', 2); }); - run(function() { - post.get('comments'); - }); + run(() => post.get('comments')); }); test('normalizeKey - to set up _ids and _id', function(assert) { @@ -2084,10 +2066,10 @@ test('normalizeKey - to set up _ids and _id', function(assert) { keyForRelationship(rel, kind) { if (kind === 'belongsTo') { - var underscored = Ember.String.underscore(rel); + let underscored = Ember.String.underscore(rel); return underscored + '_id'; } else { - var singular = Ember.String.singularize(rel); + let singular = Ember.String.singularize(rel); return Ember.String.underscore(singular) + '_ids'; } } @@ -2132,12 +2114,12 @@ test('normalizeKey - to set up _ids and _id', function(assert) { }] }); - run(function() { - store.findRecord('post', 1).then(assert.wait(function(post) { + return run(() => { + return store.findRecord('post', 1).then(post => { assert.equal(post.get('authorName'), "@d2h"); assert.equal(post.get('author.name'), "D2H"); assert.deepEqual(post.get('comments').mapBy('body'), ["Rails is unagi", "What is omakase?"]); - })); + }); }); }); @@ -2151,10 +2133,11 @@ test('groupRecordsForFindMany splits up calls for large ids', function(assert) { return new Array(n+1).join(character); } - var a2000 = repeatChar('a', 2000); - var b2000 = repeatChar('b', 2000); - var post; - run(function() { + let a2000 = repeatChar('a', 2000); + let b2000 = repeatChar('b', 2000); + let post; + + run(() => { store.push({ data: { type: 'post', @@ -2187,9 +2170,7 @@ test('groupRecordsForFindMany splits up calls for large ids', function(assert) { return Ember.RSVP.reject(); }; - run(function() { - post.get('comments'); - }); + run(() => post.get('comments')); }); test('groupRecordsForFindMany groups calls for small ids', function(assert) { @@ -2202,11 +2183,11 @@ test('groupRecordsForFindMany groups calls for small ids', function(assert) { return new Array(n+1).join(character); } - var a100 = repeatChar('a', 100); - var b100 = repeatChar('b', 100); - var post; + let a100 = repeatChar('a', 100); + let b100 = repeatChar('b', 100); + let post; - run(function() { + run(() => { store.push({ data: { type: 'post', @@ -2236,20 +2217,17 @@ test('groupRecordsForFindMany groups calls for small ids', function(assert) { return Ember.RSVP.resolve({ comments: [{ id: a100 }, { id: b100 }] }); }; - run(function() { - post.get('comments'); - }); + run(() => post.get('comments')); }); test("calls adapter.handleResponse with the jqXHR and json", function(assert) { assert.expect(2); - var originalAjax = Ember.$.ajax; - var jqXHR = { + let jqXHR = { status: 200, getAllResponseHeaders() { return ''; } }; - var data = { + let data = { post: { id: "1", name: "Docker is amazing" @@ -2266,25 +2244,18 @@ test("calls adapter.handleResponse with the jqXHR and json", function(assert) { return json; }; - try { - run(function() { - store.findRecord('post', '1'); - }); - } finally { - Ember.$.ajax = originalAjax; - } + run(() => store.findRecord('post', '1')); }); test('calls handleResponse with jqXHR, jqXHR.responseText, and requestData', function(assert) { assert.expect(4); - var originalAjax = Ember.$.ajax; - var jqXHR = { + let jqXHR = { status: 400, responseText: 'Nope lol', getAllResponseHeaders() { return ''; } }; - var expectedRequestData = { + let expectedRequestData = { method: "GET", url: "/posts/1" }; @@ -2300,24 +2271,17 @@ test('calls handleResponse with jqXHR, jqXHR.responseText, and requestData', fun return new DS.AdapterError('nope!'); }; - try { - run(function() { - store.findRecord('post', '1').catch(function(err) { - assert.ok(err, 'promise rejected'); - }); - }); - } finally { - Ember.$.ajax = originalAjax; - } + return run(() => { + return store.findRecord('post', '1').catch(err => assert.ok(err, 'promise rejected')); + }); }); test("rejects promise if DS.AdapterError is returned from adapter.handleResponse", function(assert) { assert.expect(3); - var originalAjax = Ember.$.ajax; - var jqXHR = { + let jqXHR = { getAllResponseHeaders() { return ''; } }; - var data = { + let data = { something: 'is invalid' }; @@ -2330,20 +2294,17 @@ test("rejects promise if DS.AdapterError is returned from adapter.handleResponse return new DS.AdapterError(json); }; - Ember.run(function() { - store.findRecord('post', '1').then(null, function(reason) { + return Ember.run(() => { + return store.findRecord('post', '1').catch(reason => { assert.ok(true, 'promise should be rejected'); assert.ok(reason instanceof DS.AdapterError, 'reason should be an instance of DS.AdapterError'); }); }); - - Ember.$.ajax = originalAjax; }); test("gracefully handles exceptions in handleResponse", function(assert) { assert.expect(1); - var originalAjax = Ember.$.ajax; - var jqXHR = { + let jqXHR = { status: 200, getAllResponseHeaders() { return ''; } }; @@ -2356,50 +2317,39 @@ test("gracefully handles exceptions in handleResponse", function(assert) { throw new Error('Unexpected error'); }; - try { - return run(function() { - return store.findRecord('post', '1').catch(function(error) { - assert.ok(true, 'Unexpected error is captured by the promise chain'); - }); - + return run(() => { + return store.findRecord('post', '1').catch(error => { + assert.ok(true, 'Unexpected error is captured by the promise chain'); }); - } finally { - Ember.$.ajax = originalAjax; - } + }); }); test("gracefully handles exceptions in handleResponse where the ajax request errors", function(assert) { assert.expect(1); - var originalAjax = Ember.$.ajax; - var jqXHR = { + let jqXHR = { status: 500, getAllResponseHeaders() { return ''; } }; Ember.$.ajax = function(hash) { - setTimeout(function() { hash.error({}, 'Internal Server Error', jqXHR); }, 1) + setTimeout(() => hash.error({}, 'Internal Server Error', jqXHR) , 1); }; adapter.handleResponse = function(status, headers, json) { throw new Error('Unexpected error'); }; - try { - return run(function() { - return store.findRecord('post', '1').catch(function(error) { - assert.ok(true, 'Unexpected error is captured by the promise chain'); - }); + return run(() => { + return store.findRecord('post', '1').catch(error => { + assert.ok(true, 'Unexpected error is captured by the promise chain'); }); - } finally { - Ember.$.ajax = originalAjax; - } + }); }); test('treats status code 0 as an abort', function(assert) { assert.expect(1); - var originalAjax = Ember.$.ajax; - var jqXHR = { + let jqXHR = { status: 0, getAllResponseHeaders() { return ''; } }; @@ -2412,27 +2362,22 @@ test('treats status code 0 as an abort', function(assert) { assert.ok(false); }; - try { - run(function() { - store.findRecord('post', '1').catch(function(err) { - assert.ok(err instanceof DS.AbortError, 'reason should be an instance of DS.AbortError'); - }); + return run(() => { + return store.findRecord('post', '1').catch(err => { + assert.ok(err instanceof DS.AbortError, 'reason should be an instance of DS.AbortError'); }); - } finally { - Ember.$.ajax = originalAjax; - } + }); }); test('on error appends errorThrown for sanity', function(assert) { assert.expect(2); - var originalAjax = Ember.$.ajax; - var jqXHR = { + let jqXHR = { responseText: 'Nope lol', getAllResponseHeaders() { return ''; } }; - var errorThrown = new Error('nope!'); + let errorThrown = new Error('nope!'); Ember.$.ajax = function(hash) { hash.error(jqXHR, jqXHR.responseText, errorThrown); @@ -2442,25 +2387,20 @@ test('on error appends errorThrown for sanity', function(assert) { assert.ok(false); }; - try { - run(function() { - store.findRecord('post', '1').catch(function(err) { - assert.equal(err, errorThrown); - assert.ok(err, 'promise rejected'); - }); + return run(() => { + return store.findRecord('post', '1').catch(err => { + assert.equal(err, errorThrown); + assert.ok(err, 'promise rejected'); }); - } finally { - Ember.$.ajax = originalAjax; - } + }); }); if (isEnabled('ds-extended-errors')) { test("rejects promise with a specialized subclass of DS.AdapterError if ajax responds with http error codes", function(assert) { assert.expect(10); - var originalAjax = Ember.$.ajax; - var jqXHR = { - getAllResponseHeaders: function() { return ''; } + let jqXHR = { + getAllResponseHeaders() { return ''; } }; Ember.$.ajax = function(hash) { @@ -2468,8 +2408,8 @@ if (isEnabled('ds-extended-errors')) { hash.error(jqXHR, 'error'); }; - Ember.run(function() { - store.find('post', '1').then(null, function(reason) { + Ember.run(() => { + store.find('post', '1').catch(reason => { assert.ok(true, 'promise should be rejected'); assert.ok(reason instanceof DS.UnauthorizedError, 'reason should be an instance of DS.UnauthorizedError'); }); @@ -2480,8 +2420,8 @@ if (isEnabled('ds-extended-errors')) { hash.error(jqXHR, 'error'); }; - Ember.run(function() { - store.find('post', '1').then(null, function(reason) { + Ember.run(() => { + store.find('post', '1').catch(reason => { assert.ok(true, 'promise should be rejected'); assert.ok(reason instanceof DS.ForbiddenError, 'reason should be an instance of DS.ForbiddenError'); }); @@ -2492,8 +2432,8 @@ if (isEnabled('ds-extended-errors')) { hash.error(jqXHR, 'error'); }; - Ember.run(function() { - store.find('post', '1').then(null, function(reason) { + Ember.run(() => { + store.find('post', '1').catch(reason => { assert.ok(true, 'promise should be rejected'); assert.ok(reason instanceof DS.NotFoundError, 'reason should be an instance of DS.NotFoundError'); }); @@ -2504,8 +2444,8 @@ if (isEnabled('ds-extended-errors')) { hash.error(jqXHR, 'error'); }; - Ember.run(function() { - store.find('post', '1').then(null, function(reason) { + Ember.run(() => { + store.find('post', '1').catch(reason => { assert.ok(true, 'promise should be rejected'); assert.ok(reason instanceof DS.ConflictError, 'reason should be an instance of DS.ConflictError'); }); @@ -2516,96 +2456,77 @@ if (isEnabled('ds-extended-errors')) { hash.error(jqXHR, 'error'); }; - Ember.run(function() { - store.find('post', '1').then(null, function(reason) { + Ember.run(() => { + store.find('post', '1').catch(reason => { assert.ok(true, 'promise should be rejected'); assert.ok(reason instanceof DS.ServerError, 'reason should be an instance of DS.ServerError'); }); }); - - Ember.$.ajax = originalAjax; }); } test('on error wraps the error string in an DS.AdapterError object', function(assert) { assert.expect(2); - var originalAjax = Ember.$.ajax; - var jqXHR = { + let jqXHR = { responseText: '', getAllResponseHeaders() { return ''; } }; - var errorThrown = 'nope!'; + let errorThrown = 'nope!'; Ember.$.ajax = function(hash) { hash.error(jqXHR, 'error', errorThrown); }; - try { - run(function() { - store.findRecord('post', '1').catch(function(err) { - assert.equal(err.errors[0].detail, errorThrown); - assert.ok(err, 'promise rejected'); - }); + run(() => { + store.findRecord('post', '1').catch(err => { + assert.equal(err.errors[0].detail, errorThrown); + assert.ok(err, 'promise rejected'); }); - } finally { - Ember.$.ajax = originalAjax; - } + }); }); test('error handling includes a detailed message from the server', (assert) => { assert.expect(2); - - let originalAjax = Ember.$.ajax; let jqXHR = { status: 500, responseText: 'An error message, perhaps generated from a backend server!', - getAllResponseHeaders: function() { return 'Content-Type: text/plain'; } + getAllResponseHeaders() { return 'Content-Type: text/plain'; } }; Ember.$.ajax = function(hash) { hash.error(jqXHR, 'error'); }; - try { - run(function() { - store.findRecord('post', '1').catch(function(err) { - assert.equal(err.message, "Ember Data Request GET /posts/1 returned a 500\nPayload (text/plain)\nAn error message, perhaps generated from a backend server!"); - assert.ok(err, 'promise rejected'); - }); + run(() => { + store.findRecord('post', '1').catch(err => { + assert.equal(err.message, "Ember Data Request GET /posts/1 returned a 500\nPayload (text/plain)\nAn error message, perhaps generated from a backend server!"); + assert.ok(err, 'promise rejected'); }); - } finally { - Ember.$.ajax = originalAjax; - } + }); }); test('error handling with a very long HTML-formatted payload truncates the friendly message', (assert) => { assert.expect(2); - let originalAjax = Ember.$.ajax; let jqXHR = { status: 500, responseText: new Array(100).join(""), - getAllResponseHeaders: function() { return 'Content-Type: text/html'; } + getAllResponseHeaders() { return 'Content-Type: text/html'; } }; Ember.$.ajax = function(hash) { hash.error(jqXHR, 'error'); }; - try { - run(function() { - store.findRecord('post', '1').catch(function(err) { - assert.equal(err.message, "Ember Data Request GET /posts/1 returned a 500\nPayload (text/html)\n[Omitted Lengthy HTML]"); - assert.ok(err, 'promise rejected'); - }); + run(() => { + store.findRecord('post', '1').catch(err => { + assert.equal(err.message, "Ember Data Request GET /posts/1 returned a 500\nPayload (text/html)\n[Omitted Lengthy HTML]"); + assert.ok(err, 'promise rejected'); }); - } finally { - Ember.$.ajax = originalAjax; - } - + }); }); test('findAll resolves with a collection of DS.Models, not DS.InternalModels', (assert) => { @@ -2628,13 +2549,12 @@ test('findAll resolves with a collection of DS.Models, not DS.InternalModels', ( ] }); - run(() => { - store.findAll('post').then(assert.wait((posts) => { + return run(() => { + return store.findAll('post').then(posts => { assert.equal(get(posts, 'length'), 3); posts.forEach((post) => assert.ok(post instanceof DS.Model)); - })); + }); }); - }); test("createRecord - sideloaded records are pushed to the store", function(assert) { @@ -2656,12 +2576,13 @@ test("createRecord - sideloaded records are pushed to the store", function(asser name: 'Second comment' }] }); - var post; + let post; - run(function() { + return run(() => { post = store.createRecord('post', { name: 'The Parley Letter' }); - post.save().then(function(post) { - var comments = store.peekAll('comment'); + + return post.save().then(post => { + let comments = store.peekAll('comment'); assert.equal(get(comments, 'length'), 2, 'comments.length is correct'); assert.equal(get(comments, 'firstObject.name'), 'First comment', 'comments.firstObject.name is correct'); @@ -2671,27 +2592,21 @@ test("createRecord - sideloaded records are pushed to the store", function(asser }); testInDebug("warns when an empty response is returned, though a valid stringified JSON is expected", function(assert) { - let done = assert.async(); let server = new Pretender(); server.post('/posts', function() { return [201, { "Content-Type": "application/json" }, ""]; }); - run(function() { - let post = store.createRecord('post'); - let save = post.save(); - - save.then(null, function() { - server.shutdown(); - done(); - }); + return run(() => { + return store.createRecord('post').save(); + }).then(() => { + assert.equal(true, false, 'should not have fulfilled'); + }, reason => { + assert.ok(/JSON/.test(reason.message)); }); - - assert.expectWarning("The server returned an empty string for POST /posts, which cannot be parsed into a valid JSON. Return either null or {}."); }); - if (isEnabled('ds-improved-ajax')) { testInDebug("The RESTAdapter should use `ajax` with a deprecation message when it is overridden by the user.", function(assert) { assert.expect(2) @@ -2701,10 +2616,8 @@ if (isEnabled('ds-improved-ajax')) { return { posts: { id: 1, name: "Rails is omakase" } }; }; - assert.expectDeprecation(function() { - run(function() { - store.findRecord('post', 1); - }); + assert.expectDeprecation(() => { + run(() => store.findRecord('post', 1)); }, /RESTAdapter#ajax has been deprecated/) }); @@ -2713,29 +2626,26 @@ if (isEnabled('ds-improved-ajax')) { assert.expect(2) adapter._ajaxRequest = function(hash) { - var jqXHR = { + let jqXHR = { status: 200, getAllResponseHeaders() { return ''; } }; hash.success({ posts: { id: 1, name: "Rails is omakase" } }, 'OK', jqXHR); } - var oldAjaxOptions = adapter.ajaxOptions; + let oldAjaxOptions = adapter.ajaxOptions; adapter.ajaxOptions = function() { assert.ok(true, 'The ajaxOptions method should be called when it is overridden'); return oldAjaxOptions.apply(this, arguments); }; - assert.expectDeprecation(function() { - run(function() { - store.findRecord('post', 1); - }); + assert.expectDeprecation(() => { + run(() => store.findRecord('post', 1)); }, /RESTAdapter#ajaxOptions has been deprecated/) }); test("_requestToJQueryAjaxHash works correctly for GET requests - GH-4445", function(assert) { - let done = assert.async(); - let server = new Pretender(); + server = new Pretender(); server.get('/posts/1', function(request) { assert.equal(request.url, "/posts/1", "no query param is added to the GET request"); @@ -2743,14 +2653,7 @@ if (isEnabled('ds-improved-ajax')) { return [201, { "Content-Type": "application/json" }, JSON.stringify({ post: { id: 1 } })]; }); - run(function() { - let post = store.findRecord('post', 1); - - post.then(function() { - server.shutdown(); - done(); - }); - }); + return run(() => store.findRecord('post', 1)); }); } diff --git a/tests/integration/adapter/serialize-test.js b/tests/integration/adapter/serialize-test.js index de40aa3182d..fdf961fe740 100644 --- a/tests/integration/adapter/serialize-test.js +++ b/tests/integration/adapter/serialize-test.js @@ -5,12 +5,13 @@ import {module, test} from 'qunit'; import DS from 'ember-data'; -var run = Ember.run; -var env, store, adapter, serializer; +const { run } = Ember; + +let env, store, adapter, serializer; module("integration/adapter/serialize - DS.Adapter integration test", { beforeEach() { - var Person = DS.Model.extend({ + const Person = DS.Model.extend({ name: DS.attr('string') }); @@ -32,8 +33,8 @@ test("serialize() is delegated to the serializer", function(assert) { assert.deepEqual(options, { foo: 'bar' }); }; - run(function() { - var person = store.createRecord('person'); + run(() => { + let person = store.createRecord('person'); adapter.serialize(person._createSnapshot(), { foo: 'bar' }); }); }); diff --git a/tests/integration/adapter/store-adapter-test.js b/tests/integration/adapter/store-adapter-test.js index fe114a08d69..85326d8947a 100644 --- a/tests/integration/adapter/store-adapter-test.js +++ b/tests/integration/adapter/store-adapter-test.js @@ -19,13 +19,11 @@ import DS from 'ember-data'; the given record or record array changes state appropriately. */ -var get = Ember.get; -var set = Ember.set; -var run = Ember.run; -var Person, Dog, env, store, adapter; +const { get, set, run } = Ember; +let Person, Dog, env, store, adapter; function moveRecordOutOfInFlight(record) { - run(function() { + run(() => { // move record out of the inflight state so the tests can clean up // correctly let { store, _internalModel } = record; @@ -77,11 +75,11 @@ test("Records loaded multiple times and retrieved in recordArray are ready to se }); }; - run(store, 'query', 'person', { q: 'bla' }).then(assert.wait(function(people) { - var people2 = store.query('person', { q: 'bla2' }); + return run(store, 'query', 'person', { q: 'bla' }).then(people => { + let people2 = store.query('person', { q: 'bla2' }); return Ember.RSVP.hash({ people: people, people2: people2 }); - })).then(assert.wait(function(results) { + }).then(results => { assert.equal(results.people2.get('length'), 2, 'return the elements'); assert.ok(results.people2.get('isLoaded'), 'array is loaded'); @@ -90,12 +88,11 @@ test("Records loaded multiple times and retrieved in recordArray are ready to se // delete record will not throw exception person.deleteRecord(); - })); - + }); }); test("by default, createRecords calls createRecord once per record", function(assert) { - var count = 1; + let count = 1; adapter.shouldBackgroundReloadRecord = () => false; adapter.createRecord = function(store, type, snapshot) { assert.equal(type, Person, "the type is correct"); @@ -108,8 +105,8 @@ test("by default, createRecords calls createRecord once per record", function(as assert.ok(false, "should not have invoked more than 2 times"); } - var hash = snapshot.attributes(); - var recordId = count; + let hash = snapshot.attributes(); + let recordId = count; hash['updated-at'] = "now"; count++; @@ -121,21 +118,22 @@ test("by default, createRecords calls createRecord once per record", function(as } }); }; - var tom, yehuda; - run(function() { + let tom, yehuda; + + run(() => { tom = store.createRecord('person', { name: "Tom Dale" }); yehuda = store.createRecord('person', { name: "Yehuda Katz" }); }); - var promise = run(function() { + let promise = run(() => { return Ember.RSVP.hash({ tom: tom.save(), yehuda: yehuda.save() }); }); - promise.then(assert.wait(function(records) { + return promise.then(records => { tom = records.tom; yehuda = records.yehuda; @@ -143,12 +141,11 @@ test("by default, createRecords calls createRecord once per record", function(as assert.asyncEqual(yehuda, store.findRecord('person', 2), "Once an ID is in, findRecord returns the same object"); assert.equal(get(tom, 'updatedAt'), "now", "The new information is received"); assert.equal(get(yehuda, 'updatedAt'), "now", "The new information is received"); - - })); + }); }); test("by default, updateRecords calls updateRecord once per record", function(assert) { - var count = 0; + let count = 0; adapter.shouldBackgroundReloadRecord = () => false; adapter.updateRecord = function(store, type, snapshot) { assert.equal(type, Person, "the type is correct"); @@ -168,7 +165,7 @@ test("by default, updateRecords calls updateRecord once per record", function(as return Ember.RSVP.resolve(); }; - run(function() { + run(() => { env.store.push({ data: [{ type: 'person', @@ -186,35 +183,38 @@ test("by default, updateRecords calls updateRecord once per record", function(as }); }); - var promise = run(function() { + let promise = run(() => { return Ember.RSVP.hash({ tom: store.findRecord('person', 1), yehuda: store.findRecord('person', 2) }); }); - promise.then(assert.wait(function(records) { - var tom = records.tom; - var yehuda = records.yehuda; + return promise.then(records => { + let tom = records.tom; + let yehuda = records.yehuda; set(tom, "name", "Tom Dale"); set(yehuda, "name", "Yehuda Katz"); - return Ember.RSVP.hash({ tom: tom.save(), yehuda: yehuda.save() }); - })).then(assert.wait(function(records) { - var tom = records.tom; - var yehuda = records.yehuda; + return Ember.RSVP.hash({ + tom: tom.save(), + yehuda: yehuda.save() + }); + }).then(records => { + let tom = records.tom; + let yehuda = records.yehuda; assert.equal(tom.get('isSaving'), false, "record is no longer saving"); assert.equal(tom.get('isLoaded'), true, "record is loaded"); assert.equal(yehuda.get('isSaving'), false, "record is no longer saving"); assert.equal(yehuda.get('isLoaded'), true, "record is loaded"); - })); + }); }); test("calling store.didSaveRecord can provide an optional hash", function(assert) { - var count = 0; + let count = 0; adapter.shouldBackgroundReloadRecord = () => false; adapter.updateRecord = function(store, type, snapshot) { assert.equal(type, Person, "the type is correct"); @@ -231,7 +231,7 @@ test("calling store.didSaveRecord can provide an optional hash", function(assert } }; - run(function() { + run(() => { env.store.push({ data: [{ type: 'person', @@ -249,36 +249,40 @@ test("calling store.didSaveRecord can provide an optional hash", function(assert }); }); - var promise = run(function() { + let promise = run(() => { return Ember.RSVP.hash({ tom: store.findRecord('person', 1), yehuda: store.findRecord('person', 2) }); }); - promise.then(assert.wait(function(records) { - var tom = records.tom; - var yehuda = records.yehuda; + + return promise.then(records => { + let tom = records.tom; + let yehuda = records.yehuda; set(tom, "name", "Tom Dale"); set(yehuda, "name", "Yehuda Katz"); - return Ember.RSVP.hash({ tom: tom.save(), yehuda: yehuda.save() }); - })).then(assert.wait(function(records) { - var tom = records.tom; - var yehuda = records.yehuda; + return Ember.RSVP.hash({ + tom: tom.save(), + yehuda: yehuda.save() + }); + }).then(records => { + let tom = records.tom; + let yehuda = records.yehuda; assert.equal(get(tom, 'hasDirtyAttributes'), false, "the record should not be dirty"); assert.equal(get(tom, 'updatedAt'), "now", "the hash was updated"); assert.equal(get(yehuda, 'hasDirtyAttributes'), false, "the record should not be dirty"); assert.equal(get(yehuda, 'updatedAt'), "now!", "the hash was updated"); - })); + }); }); test("by default, deleteRecord calls deleteRecord once per record", function(assert) { assert.expect(4); - var count = 0; + let count = 0; adapter.shouldBackgroundReloadRecord = () => false; adapter.deleteRecord = function(store, type, snapshot) { assert.equal(type, Person, "the type is correct"); @@ -296,7 +300,7 @@ test("by default, deleteRecord calls deleteRecord once per record", function(ass return Ember.RSVP.resolve(); }; - run(function() { + run(() => { env.store.push({ data: [{ type: 'person', @@ -314,29 +318,32 @@ test("by default, deleteRecord calls deleteRecord once per record", function(ass }); }); - var promise = run(function() { + let promise = run(() => { return Ember.RSVP.hash({ tom: store.findRecord('person', 1), yehuda: store.findRecord('person', 2) }); }); - promise.then(assert.wait(function(records) { - var tom = records.tom; - var yehuda = records.yehuda; + return promise.then(records => { + let tom = records.tom; + let yehuda = records.yehuda; tom.deleteRecord(); yehuda.deleteRecord(); - tom.save(); - yehuda.save(); - })); + return Ember.RSVP.Promise.all([ + tom.save(), + yehuda.save() + ]); + }); }); test("by default, destroyRecord calls deleteRecord once per record without requiring .save", function(assert) { assert.expect(4); - var count = 0; + let count = 0; + adapter.shouldBackgroundReloadRecord = () => false; adapter.deleteRecord = function(store, type, snapshot) { assert.equal(type, Person, "the type is correct"); @@ -354,7 +361,7 @@ test("by default, destroyRecord calls deleteRecord once per record without requi return Ember.RSVP.resolve(); }; - run(function() { + run(() => { env.store.push({ data: [{ type: 'person', @@ -372,26 +379,28 @@ test("by default, destroyRecord calls deleteRecord once per record without requi }); }); - var promise = run(function() { + let promise = run(() => { return Ember.RSVP.hash({ tom: store.findRecord('person', 1), yehuda: store.findRecord('person', 2) }); }); - promise.then(assert.wait(function(records) { - var tom = records.tom; - var yehuda = records.yehuda; + return promise.then(records => { + let tom = records.tom; + let yehuda = records.yehuda; - tom.destroyRecord(); - yehuda.destroyRecord(); - })); + return Ember.RSVP.Promise.all([ + tom.destroyRecord(), + yehuda.destroyRecord() + ]); + }); }); test("if an existing model is edited then deleted, deleteRecord is called on the adapter", function(assert) { assert.expect(5); - var count = 0; + let count = 0; adapter.shouldBackgroundReloadRecord = () => false; adapter.deleteRecord = function(store, type, snapshot) { count++; @@ -406,7 +415,7 @@ test("if an existing model is edited then deleted, deleteRecord is called on the }; // Load data for a record into the store. - run(function() { + run(() => { env.store.push({ data: { type: 'person', @@ -419,22 +428,22 @@ test("if an existing model is edited then deleted, deleteRecord is called on the }); // Retrieve that loaded record and edit it so it becomes dirty - run(store, 'findRecord', 'person', 'deleted-record').then(assert.wait(function(tom) { + return run(store, 'findRecord', 'person', 'deleted-record').then(tom => { tom.set('name', "Tom Mothereffin' Dale"); assert.equal(get(tom, 'hasDirtyAttributes'), true, "precond - record should be dirty after editing"); tom.deleteRecord(); return tom.save(); - })).then(assert.wait(function(tom) { + }).then(tom => { assert.equal(get(tom, 'hasDirtyAttributes'), false, "record should not be dirty"); assert.equal(get(tom, 'isDeleted'), true, "record should be considered deleted"); - })); + }); }); test("if a deleted record errors, it enters the error state", function(assert) { - var count = 0; - var error = new DS.AdapterError(); + let count = 0; + let error = new DS.AdapterError(); adapter.shouldBackgroundReloadRecord = () => false; adapter.deleteRecord = function(store, type, snapshot) { @@ -445,7 +454,7 @@ test("if a deleted record errors, it enters the error state", function(assert) { } }; - run(function() { + run(() => { env.store.push({ data: { type: 'person', @@ -457,23 +466,22 @@ test("if a deleted record errors, it enters the error state", function(assert) { }); }); - var tom; - - run(function() { - store.findRecord('person', 'deleted-record').then(assert.wait(function(person) { + return run(() => { + let tom; + store.findRecord('person', 'deleted-record').then(person => { tom = person; person.deleteRecord(); return person.save(); - })).then(null, assert.wait(function() { + }).catch(() => { assert.equal(tom.get('isError'), true, "Tom is now errored"); assert.equal(tom.get('adapterError'), error, "error object is exposed"); // this time it succeeds return tom.save(); - })).then(assert.wait(function() { + }).then(() => { assert.equal(tom.get('isError'), false, "Tom is not errored anymore"); assert.equal(tom.get('adapterError'), null, "error object is discarded"); - })); + }); }); }); @@ -496,13 +504,13 @@ test("if a created record is marked as invalid by the server, it enters an error } }; - var yehuda = run(function() { + let yehuda = run(() => { return store.createRecord('person', { id: 1, name: "Yehuda Katz" }); }); // Wrap this in an Ember.run so that all chained async behavior is set up // before flushing any scheduled behavior. - Ember.run(function() { - yehuda.save().then(null, assert.wait(function(error) { + return Ember.run(function() { + return yehuda.save().catch(error => { assert.equal(get(yehuda, 'isValid'), false, "the record is invalid"); assert.ok(get(yehuda, 'errors.name'), "The errors.name property exists"); @@ -517,12 +525,12 @@ test("if a created record is marked as invalid by the server, it enters an error assert.equal(get(yehuda, 'isNew'), true, "precond - record is still new"); return yehuda.save(); - })).then(assert.wait(function(person) { + }).then(person => { assert.strictEqual(person, yehuda, "The promise resolves with the saved record"); assert.equal(get(yehuda, 'isValid'), true, "record remains valid after committing"); assert.equal(get(yehuda, 'isNew'), false, "record is no longer new"); - })); + }); }); }); @@ -543,14 +551,14 @@ test("allows errors on arbitrary properties on create", function(assert) { } }; - var yehuda = run(function () { + let yehuda = run(() => { return store.createRecord('person', { id: 1, name: "Yehuda Katz" }); }); // Wrap this in an Ember.run so that all chained async behavior is set up // before flushing any scheduled behavior. - run(function() { - yehuda.save().then(null, assert.wait(function(error) { + return run(() => { + return yehuda.save().catch(error => { assert.equal(get(yehuda, 'isValid'), false, "the record is invalid"); assert.ok(get(yehuda, 'errors.base'), "The errors.base property exists"); assert.deepEqual(get(yehuda, 'errors').errorsFor('base'), [{ attribute: 'base', message: "is a generally unsavoury character" }]); @@ -566,18 +574,18 @@ test("allows errors on arbitrary properties on create", function(assert) { assert.equal(get(yehuda, 'isNew'), true, "precond - record is still new"); return yehuda.save(); - })).then(assert.wait(function(person) { + }).then(person => { assert.strictEqual(person, yehuda, "The promise resolves with the saved record"); assert.ok(!get(yehuda, 'errors.base'), "The errors.base property does not exist"); assert.deepEqual(get(yehuda, 'errors').errorsFor('base'), []); assert.equal(get(yehuda, 'isValid'), true, "record remains valid after committing"); assert.equal(get(yehuda, 'isNew'), false, "record is no longer new"); - })); + }); }); }); test("if a created record is marked as invalid by the server, you can attempt the save again", function(assert) { - var saveCount = 0; + let saveCount = 0; adapter.createRecord = function(store, type, snapshot) { assert.equal(type, Person, "the type is correct"); saveCount++; @@ -597,14 +605,14 @@ test("if a created record is marked as invalid by the server, you can attempt th } }; - var yehuda = run(function() { + let yehuda = run(() => { return store.createRecord('person', { id: 1, name: "Yehuda Katz" }); }); // Wrap this in an Ember.run so that all chained async behavior is set up // before flushing any scheduled behavior. - Ember.run(function() { - yehuda.save().then(null, assert.wait(function(reason) { + return Ember.run(() => { + return yehuda.save().catch(reason => { assert.equal(saveCount, 1, "The record has been saved once"); assert.ok(reason.message.match("The adapter rejected the commit because it was invalid"), "It should fail due to being invalid"); assert.equal(get(yehuda, 'isValid'), false, "the record is invalid"); @@ -612,7 +620,7 @@ test("if a created record is marked as invalid by the server, you can attempt th assert.ok(get(yehuda, 'errors.name'), "The errors.name property exists"); assert.equal(get(yehuda, 'isNew'), true, "precond - record is still new"); return yehuda.save(); - })).then(null, assert.wait(function(reason) { + }).catch(reason => { assert.equal(saveCount, 2, "The record has been saved twice"); assert.ok(reason.message.match("The adapter rejected the commit because it was invalid"), "It should fail due to being invalid"); assert.equal(get(yehuda, 'isValid'), false, "the record is still invalid"); @@ -621,29 +629,29 @@ test("if a created record is marked as invalid by the server, you can attempt th assert.equal(get(yehuda, 'isNew'), true, "precond - record is still new"); set(yehuda, 'name', 'Brohuda Brokatz'); return yehuda.save(); - })).then(assert.wait(function(person) { + }).then(person => { assert.equal(saveCount, 3, "The record has been saved thrice"); assert.equal(get(yehuda, 'isValid'), true, "record is valid"); assert.equal(get(yehuda, 'hasDirtyAttributes'), false, "record is not dirty"); assert.equal(get(yehuda, 'errors.isEmpty'), true, "record has no errors"); - })); + }); }); }); test("if a created record is marked as erred by the server, it enters an error state", function(assert) { - var error = new DS.AdapterError(); + let error = new DS.AdapterError(); adapter.createRecord = function(store, type, snapshot) { return Ember.RSVP.reject(error); }; - Ember.run(function() { - var person = store.createRecord('person', { id: 1, name: "John Doe" }); + return Ember.run(() => { + let person = store.createRecord('person', { id: 1, name: "John Doe" }); - person.save().then(null, assert.wait(function() { + return person.save().catch(() => { assert.ok(get(person, 'isError'), "the record is in the error state"); assert.equal(get(person, 'adapterError'), error, "error object is exposed"); - })); + }); }); }); @@ -667,7 +675,7 @@ test("if an updated record is marked as invalid by the server, it enters an erro } }; - var yehuda = run(function() { + let yehuda = run(() => { env.store.push({ data: { type: 'person', @@ -677,11 +685,12 @@ test("if an updated record is marked as invalid by the server, it enters an erro } } }); + return store.peekRecord('person', 1); }); - return Ember.run(function() { - return store.findRecord('person', 1).then(assert.wait(function(person) { + return Ember.run(() => { + return store.findRecord('person', 1).then(person => { assert.equal(person, yehuda, "The same object is passed through"); assert.equal(get(yehuda, 'isValid'), true, "precond - the record is valid"); @@ -691,7 +700,7 @@ test("if an updated record is marked as invalid by the server, it enters an erro assert.equal(get(yehuda, 'hasDirtyAttributes'), true, "the record is dirty"); return yehuda.save(); - })).then(null, assert.wait(function(reason) { + }).catch(reason => { assert.equal(get(yehuda, 'hasDirtyAttributes'), true, "the record is still dirty"); assert.equal(get(yehuda, 'isValid'), false, "the record is invalid"); @@ -703,14 +712,13 @@ test("if an updated record is marked as invalid by the server, it enters an erro assert.equal(get(yehuda, 'hasDirtyAttributes'), true, "the record has outstanding changes"); return yehuda.save(); - })).then(assert.wait(function(yehuda) { + }).then(yehuda => { assert.equal(get(yehuda, 'isValid'), true, "record remains valid after committing"); assert.equal(get(yehuda, 'hasDirtyAttributes'), false, "record is no longer new"); - })); + }); }); }); - test("records can have errors on arbitrary properties after update", function(assert) { adapter.shouldBackgroundReloadRecord = () => false; adapter.updateRecord = function(store, type, snapshot) { @@ -729,7 +737,7 @@ test("records can have errors on arbitrary properties after update", function(as } }; - var yehuda = run(function() { + let yehuda = run(() => { env.store.push({ data: { type: 'person', @@ -742,8 +750,8 @@ test("records can have errors on arbitrary properties after update", function(as return store.peekRecord('person', 1); }); - run(function() { - store.findRecord('person', 1).then(assert.wait(function(person) { + return run(() => { + return store.findRecord('person', 1).then(person => { assert.equal(person, yehuda, "The same object is passed through"); assert.equal(get(yehuda, 'isValid'), true, "precond - the record is valid"); @@ -753,7 +761,7 @@ test("records can have errors on arbitrary properties after update", function(as assert.equal(get(yehuda, 'hasDirtyAttributes'), true, "the record is dirty"); return yehuda.save(); - })).then(null, assert.wait(function(reason) { + }).catch(reason => { assert.equal(get(yehuda, 'hasDirtyAttributes'), true, "the record is still dirty"); assert.equal(get(yehuda, 'isValid'), false, "the record is invalid"); assert.ok(get(yehuda, 'errors.base'), "The errors.base property exists"); @@ -767,19 +775,17 @@ test("records can have errors on arbitrary properties after update", function(as assert.equal(get(yehuda, 'hasDirtyAttributes'), true, "the record has outstanding changes"); return yehuda.save(); - })).then(assert.wait(function(yehuda) { + }).then(yehuda => { assert.equal(get(yehuda, 'isValid'), true, "record remains valid after committing"); assert.equal(get(yehuda, 'hasDirtyAttributes'), false, "record is no longer new"); assert.ok(!get(yehuda, 'errors.base'), "The errors.base property does not exist"); assert.deepEqual(get(yehuda, 'errors').errorsFor('base'), []); - })); + }); }); }); - - test("if an updated record is marked as invalid by the server, you can attempt the save again", function(assert) { - var saveCount = 0; + let saveCount = 0; adapter.shouldBackgroundReloadRecord = () => false; adapter.updateRecord = function(store, type, snapshot) { assert.equal(type, Person, "the type is correct"); @@ -799,7 +805,7 @@ test("if an updated record is marked as invalid by the server, you can attempt t } }; - var yehuda = run(function() { + let yehuda = run(() => { env.store.push({ data: { type: 'person', @@ -812,8 +818,8 @@ test("if an updated record is marked as invalid by the server, you can attempt t return store.peekRecord('person', 1); }); - Ember.run(function() { - store.findRecord('person', 1).then(assert.wait(function(person) { + return Ember.run(() => { + return store.findRecord('person', 1).then(person => { assert.equal(person, yehuda, "The same object is passed through"); assert.equal(get(yehuda, 'isValid'), true, "precond - the record is valid"); @@ -823,38 +829,37 @@ test("if an updated record is marked as invalid by the server, you can attempt t assert.equal(get(yehuda, 'hasDirtyAttributes'), true, "the record is dirty"); return yehuda.save(); - })).then(null, assert.wait(function(reason) { + }).catch(reason => { assert.equal(saveCount, 1, "The record has been saved once"); assert.ok(reason.message.match("The adapter rejected the commit because it was invalid"), "It should fail due to being invalid"); assert.equal(get(yehuda, 'hasDirtyAttributes'), true, "the record is still dirty"); assert.equal(get(yehuda, 'isValid'), false, "the record is invalid"); return yehuda.save(); - })).then(null, assert.wait(function(reason) { + }).catch(reason => { assert.equal(saveCount, 2, "The record has been saved twice"); assert.ok(reason.message.match("The adapter rejected the commit because it was invalid"), "It should fail due to being invalid"); assert.equal(get(yehuda, 'isValid'), false, "record is still invalid"); assert.equal(get(yehuda, 'hasDirtyAttributes'), true, "record is still dirty"); set(yehuda, 'name', 'Brohuda Brokatz'); return yehuda.save(); - })).then(assert.wait(function(person) { + }).then(person => { assert.equal(saveCount, 3, "The record has been saved thrice"); assert.equal(get(yehuda, 'isValid'), true, "record is valid"); assert.equal(get(yehuda, 'hasDirtyAttributes'), false, "record is not dirty"); assert.equal(get(yehuda, 'errors.isEmpty'), true, "record has no errors"); - })); + }); }); }); - test("if a updated record is marked as erred by the server, it enters an error state", function(assert) { - var error = new DS.AdapterError(); + let error = new DS.AdapterError(); adapter.shouldBackgroundReloadRecord = () => false; adapter.updateRecord = function(store, type, snapshot) { return Ember.RSVP.reject(error); }; - var person = run(function() { + let person = run(() => { env.store.push({ data: { type: 'person', @@ -867,14 +872,14 @@ test("if a updated record is marked as erred by the server, it enters an error s return store.peekRecord('person', 1); }); - run(store, 'findRecord', 'person', 1).then(assert.wait(function(record) { + return run(store, 'findRecord', 'person', 1).then(record => { assert.equal(record, person, "The person was resolved"); person.set('name', "Jonathan Doe"); return person.save(); - })).then(null, assert.wait(function(reason) { + }).catch(reason => { assert.ok(get(person, 'isError'), "the record is in the error state"); assert.equal(get(person, 'adapterError'), error, "error object is exposed"); - })); + }); }); test("can be created after the DS.Store", function(assert) { @@ -885,9 +890,7 @@ test("can be created after the DS.Store", function(assert) { return Ember.RSVP.resolve({ data: { id: 1, type: "person" } }); }; - run(function() { - store.findRecord('person', 1); - }); + run(() => store.findRecord('person', 1)); }); test("the filter method can optionally take a server query as well", function(assert) { @@ -913,19 +916,19 @@ test("the filter method can optionally take a server query as well", function(as }); }; - var asyncFilter = store.filter('person', { page: 1 }, function(data) { + let asyncFilter = store.filter('person', { page: 1 }, data => { return data.get('name') === "Tom Dale"; }); - var loadedFilter; + let loadedFilter; - asyncFilter.then(assert.wait(function(filter) { + return asyncFilter.then(filter => { loadedFilter = filter; return store.findRecord('person', 2); - })).then(assert.wait(function(tom) { + }).then(tom => { assert.equal(get(loadedFilter, 'length'), 1, "The filter has an item in it"); assert.deepEqual(loadedFilter.toArray(), [tom], "The filter has a single entry in it"); - })); + }); }); test("relationships returned via `commit` do not trigger additional findManys", function(assert) { @@ -933,7 +936,7 @@ test("relationships returned via `commit` do not trigger additional findManys", dogs: DS.hasMany('dog', { async: false }) }); - run(function() { + run(() => { env.store.push({ data: { type: 'dog', @@ -961,7 +964,7 @@ test("relationships returned via `commit` do not trigger additional findManys", }; adapter.updateRecord = function(store, type, snapshot) { - return new Ember.RSVP.Promise(function(resolve, reject) { + return new Ember.RSVP.Promise((resolve, reject) => { env.store.push({ data: { type: 'person', @@ -995,15 +998,15 @@ test("relationships returned via `commit` do not trigger additional findManys", assert.ok(false, "Should not get here"); }; - run(function() { - store.findRecord('person', 1).then(assert.wait(function(person) { + return run(() => { + store.findRecord('person', 1).then(person => { return Ember.RSVP.hash({ tom: person, dog: store.findRecord('dog', 1) }); - })).then(assert.wait(function(records) { + }).then(records => { records.tom.get('dogs'); return records.dog.save(); - })).then(assert.wait(function(tom) { + }).then(tom => { assert.ok(true, "Tom was saved"); - })); + }); }); }); @@ -1013,7 +1016,7 @@ test("relationships don't get reset if the links is the same", function(assert) dogs: DS.hasMany({ async: true }) }); - var count = 0; + let count = 0; adapter.findHasMany = function(store, snapshot, link, relationship) { assert.ok(count++ === 0, "findHasMany is only called once"); @@ -1021,7 +1024,7 @@ test("relationships don't get reset if the links is the same", function(assert) return Ember.RSVP.resolve({ data: [{ id: 1, type: "dog", attributes: { name: "Scruffy" } }] }); }; - run(function() { + run(() => { store.push({ data: { type: 'person', @@ -1040,13 +1043,13 @@ test("relationships don't get reset if the links is the same", function(assert) }); }); - var tom, dogs; + let tom, dogs; - run(store, 'findRecord', 'person', 1).then(assert.wait(function(person) { + return run(store, 'findRecord', 'person', 1).then(person => { tom = person; dogs = tom.get('dogs'); return dogs; - })).then(assert.wait(function(dogs) { + }).then(dogs => { assert.equal(dogs.get('length'), 1, "The dogs are loaded"); store.push({ data: { @@ -1066,9 +1069,9 @@ test("relationships don't get reset if the links is the same", function(assert) }); assert.ok(tom.get('dogs') instanceof DS.PromiseArray, 'dogs is a promise'); return tom.get('dogs'); - })).then(assert.wait(function(dogs) { + }).then(dogs => { assert.equal(dogs.get('length'), 1, "The same dogs are loaded"); - })); + }); }); test("async hasMany always returns a promise", function(assert) { @@ -1090,20 +1093,17 @@ test("async hasMany always returns a promise", function(assert) { } }); }; - var tom; - run(function() { - tom = store.createRecord('person', { name: "Tom Dale" }); - }); + let tom = run(() => store.createRecord('person', { name: "Tom Dale" })); - run(function() { + run(() => { assert.ok(tom.get('dogs') instanceof DS.PromiseArray, "dogs is a promise before save"); }); - run(function() { - tom.save().then(assert.wait(function() { + return run(() => { + return tom.save().then(() => { assert.ok(tom.get('dogs') instanceof DS.PromiseArray, "dogs is a promise after save"); - })); + }); }); }); @@ -1117,7 +1117,7 @@ test("createRecord receives a snapshot", function(assert) { var person; - run(function() { + run(() => { person = store.createRecord('person', { name: "Tom Dale", id: 1 }); person.save(); }); @@ -1131,9 +1131,9 @@ test("updateRecord receives a snapshot", function(assert) { return Ember.RSVP.resolve(); }; - var person; + let person; - run(function() { + run(() => { store.push({ data: { type: 'person', @@ -1146,7 +1146,7 @@ test("updateRecord receives a snapshot", function(assert) { person = store.peekRecord('person', 1); }); - run(function() { + run(() => { set(person, "name", "Tomster"); person.save(); }); @@ -1160,9 +1160,9 @@ test("deleteRecord receives a snapshot", function(assert) { return Ember.RSVP.resolve(); }; - var person; + let person; - run(function() { + run(() => { store.push({ data: { type: 'person', @@ -1175,9 +1175,9 @@ test("deleteRecord receives a snapshot", function(assert) { person = store.peekRecord('person', 1); }); - run(function() { + return run(() => { person.deleteRecord(); - person.save(); + return person.save(); }); }); @@ -1189,9 +1189,7 @@ test("findRecord receives a snapshot", function(assert) { return Ember.RSVP.resolve({ data: { id: 1, type: "person" } }); }; - run(function() { - store.findRecord('person', 1); - }); + return run(() => store.findRecord('person', 1)); }); test("findMany receives an array of snapshots", function(assert) { @@ -1208,9 +1206,9 @@ test("findMany receives an array of snapshots", function(assert) { return Ember.RSVP.resolve({ data: [{ id: 2, type: "dog" }, { id: 3, type: "dog" }] }); }; - var person; + let person; - run(function() { + run(() => { store.push({ data: { type: 'person', @@ -1228,9 +1226,7 @@ test("findMany receives an array of snapshots", function(assert) { person = store.peekRecord('person', 1); }); - run(function() { - person.get('dogs'); - }); + run(() => person.get('dogs')); }); test("findHasMany receives a snapshot", function(assert) { @@ -1245,9 +1241,9 @@ test("findHasMany receives a snapshot", function(assert) { return Ember.RSVP.resolve({ data: [{ id: 2, type: "dog" }, { id: 3, type: "dog" }] }); }; - var person; + let person; - run(function() { + run(() => { store.push({ data: { type: 'person', @@ -1264,9 +1260,7 @@ test("findHasMany receives a snapshot", function(assert) { person = store.peekRecord('person', 1); }); - run(function() { - person.get('dogs'); - }); + return run(() => person.get('dogs')); }); test("findBelongsTo receives a snapshot", function(assert) { @@ -1276,14 +1270,14 @@ test("findBelongsTo receives a snapshot", function(assert) { dog: DS.belongsTo({ async: true }) }); - env.adapter.findBelongsTo = assert.wait(function(store, snapshot, link, relationship) { + env.adapter.findBelongsTo = function(store, snapshot, link, relationship) { assert.ok(snapshot instanceof DS.Snapshot, "snapshot is an instance of DS.Snapshot"); return Ember.RSVP.resolve({ data: { id: 2, type: "dog" } }); - }); + }; - var person; + let person; - run(function() { + run(() => { store.push({ data: { type: 'person', @@ -1300,20 +1294,18 @@ test("findBelongsTo receives a snapshot", function(assert) { person = store.peekRecord('person', 1); }); - run(function() { - person.get('dog'); - }); + return run(() => person.get('dog')); }); test("record.save should pass adapterOptions to the updateRecord method", function(assert) { assert.expect(1); - env.adapter.updateRecord = assert.wait(function(store, type, snapshot) { + env.adapter.updateRecord = function(store, type, snapshot) { assert.deepEqual(snapshot.adapterOptions, { subscribe: true }); return Ember.RSVP.resolve({ data: { id: 1, type: "person" } }); - }); + }; - run(function() { + return run(() => { store.push({ data: { type: 'person', @@ -1323,34 +1315,34 @@ test("record.save should pass adapterOptions to the updateRecord method", functi } } }); - var person = store.peekRecord('person', 1); - person.save({ adapterOptions: { subscribe: true } }); + let person = store.peekRecord('person', 1); + return person.save({ adapterOptions: { subscribe: true } }); }); }); test("record.save should pass adapterOptions to the createRecord method", function(assert) { assert.expect(1); - env.adapter.createRecord = assert.wait(function(store, type, snapshot) { + env.adapter.createRecord = function(store, type, snapshot) { assert.deepEqual(snapshot.adapterOptions, { subscribe: true }); return Ember.RSVP.resolve({ data: { id: 1, type: "person" } }); - }); + }; - run(function() { - var person = store.createRecord('person', { name: 'Tom' }); - person.save({ adapterOptions: { subscribe: true } }); + return run(() => { + let person = store.createRecord('person', { name: 'Tom' }); + return person.save({ adapterOptions: { subscribe: true } }); }); }); test("record.save should pass adapterOptions to the deleteRecord method", function(assert) { assert.expect(1); - env.adapter.deleteRecord = assert.wait(function(store, type, snapshot) { + env.adapter.deleteRecord = function(store, type, snapshot) { assert.deepEqual(snapshot.adapterOptions, { subscribe: true }); return Ember.RSVP.resolve({ data: { id: 1, type: "person" } }); - }); + }; - run(function() { + run(() => { store.push({ data: { type: 'person', @@ -1360,32 +1352,31 @@ test("record.save should pass adapterOptions to the deleteRecord method", functi } } }); - var person = store.peekRecord('person', 1); + let person = store.peekRecord('person', 1); person.destroyRecord({ adapterOptions: { subscribe: true } }); }); }); - test("store.findRecord should pass adapterOptions to adapter.findRecord", function(assert) { assert.expect(1); - env.adapter.findRecord = assert.wait(function(store, type, id, snapshot) { + env.adapter.findRecord = function(store, type, id, snapshot) { assert.deepEqual(snapshot.adapterOptions, { query: { embed: true } }); return Ember.RSVP.resolve({ data: { id: 1, type: "person" } }); - }); + }; - run(function() { - store.findRecord('person', 1, { adapterOptions: { query: { embed: true } } }); + return run(() => { + return store.findRecord('person', 1, { adapterOptions: { query: { embed: true } } }); }); }); test("store.findRecord should pass 'include' to adapter.findRecord", function(assert) { assert.expect(1); - env.adapter.findRecord = assert.wait((store, type, id, snapshot) => { + env.adapter.findRecord = (store, type, id, snapshot) => { assert.equal(snapshot.include, 'books', 'include passed to adapter.findRecord'); return Ember.RSVP.resolve({ data: { id: 1, type: "person" } }); - }); + }; run(() => store.findRecord('person', 1, { include: 'books' })); }); @@ -1393,35 +1384,35 @@ test("store.findRecord should pass 'include' to adapter.findRecord", function(as test("store.findAll should pass adapterOptions to the adapter.findAll method", function(assert) { assert.expect(1); - env.adapter.findAll = assert.wait(function(store, type, sinceToken, arraySnapshot) { - var adapterOptions = arraySnapshot.adapterOptions; + env.adapter.findAll = function(store, type, sinceToken, arraySnapshot) { + let adapterOptions = arraySnapshot.adapterOptions; assert.deepEqual(adapterOptions, { query: { embed: true } }); return Ember.RSVP.resolve({ data: [{ id: 1, type: "person" }] }); - }); + }; - run(function() { - store.findAll('person', { adapterOptions: { query: { embed: true } } }); + return run(() => { + return store.findAll('person', { adapterOptions: { query: { embed: true } } }); }); }); test("store.findAll should pass 'include' to adapter.findAll", function(assert) { assert.expect(1); - env.adapter.findAll = assert.wait((store, type, sinceToken, arraySnapshot) => { + env.adapter.findAll = function(store, type, sinceToken, arraySnapshot) { assert.equal(arraySnapshot.include, 'books', 'include passed to adapter.findAll'); return Ember.RSVP.resolve({ data: [{ id: 1, type: "person" }] }); - }); + }; run(() => store.findAll('person', { include: 'books' })); }); test("An async hasMany relationship with links should not trigger shouldBackgroundReloadRecord", function(assert) { - var Post = DS.Model.extend({ + const Post = DS.Model.extend({ name: DS.attr("string"), comments: DS.hasMany('comment', { async: true }) }); - var Comment = DS.Model.extend({ + const Comment = DS.Model.extend({ name: DS.attr("string") }); @@ -1455,19 +1446,19 @@ test("An async hasMany relationship with links should not trigger shouldBackgrou store = env.store; - run(store, 'findRecord', 'post', '1').then(assert.wait(function(post) { + return run(store, 'findRecord', 'post', '1').then(post => { return post.get('comments'); - })).then(assert.wait(function(comments) { + }).then(comments => { assert.equal(comments.get('length'), 3); - })); + }); }); testInDebug("There should be a friendly error for if the adapter does not implement createRecord", function(assert) { adapter.createRecord = null; let tom; - assert.expectAssertion(function() { - run(function() { + assert.expectAssertion(() => { + run(() => { tom = store.createRecord('person', { name: "Tom Dale" }); tom.save(); }); @@ -1480,8 +1471,8 @@ testInDebug("There should be a friendly error for if the adapter does not implem adapter.updateRecord = null; let tom; - assert.expectAssertion(function() { - run(function() { + assert.expectAssertion(() => { + run(() => { tom = store.push({ data: { type: 'person', id: 1 } }); tom.save(); }); @@ -1494,8 +1485,8 @@ testInDebug("There should be a friendly error for if the adapter does not implem adapter.deleteRecord = null; let tom; - assert.expectAssertion(function() { - run(function() { + assert.expectAssertion(() => { + run(() => { tom = store.push({ data: { type: 'person', id: 1 } }); tom.deleteRecord(); tom.save(); diff --git a/tests/integration/records/collection-save-test.js b/tests/integration/records/collection-save-test.js index 2ea23b0b647..801275a7321 100644 --- a/tests/integration/records/collection-save-test.js +++ b/tests/integration/records/collection-save-test.js @@ -5,8 +5,9 @@ import {module, test} from 'qunit'; import DS from 'ember-data'; -var Post, env; -var run = Ember.run; +const { run } = Ember; + +let Post, env; module("integration/records/collection_save - Save Collection of Records", { beforeEach() { @@ -25,52 +26,52 @@ module("integration/records/collection_save - Save Collection of Records", { test("Collection will resolve save on success", function(assert) { assert.expect(1); let id = 1; - run(function() { + run(() => { env.store.createRecord('post', { title: 'Hello' }); env.store.createRecord('post', { title: 'World' }); }); - var posts = env.store.peekAll('post'); + let posts = env.store.peekAll('post'); env.adapter.createRecord = function(store, type, snapshot) { return Ember.RSVP.resolve({ data: { id: id++ , type: 'post' } }); }; - run(function() { - posts.save().then(assert.wait(function() { + return run(() => { + return posts.save().then(() => { assert.ok(true, 'save operation was resolved'); - })); + }); }); }); test("Collection will reject save on error", function(assert) { - run(function() { + run(() => { env.store.createRecord('post', { title: 'Hello' }); env.store.createRecord('post', { title: 'World' }); }); - var posts = env.store.peekAll('post'); + let posts = env.store.peekAll('post'); env.adapter.createRecord = function(store, type, snapshot) { return Ember.RSVP.reject(); }; - run(function() { - posts.save().then(function() {}, assert.wait(function() { + return run(() => { + return posts.save().catch(() => { assert.ok(true, 'save operation was rejected'); - })); + }); }); }); test("Retry is allowed in a failure handler", function(assert) { - run(function() { + run(() => { env.store.createRecord('post', { title: 'Hello' }); env.store.createRecord('post', { title: 'World' }); }); - var posts = env.store.peekAll('post'); + let posts = env.store.peekAll('post'); - var count = 0; + let count = 0; let id = 1; env.adapter.createRecord = function(store, type, snapshot) { @@ -85,35 +86,33 @@ test("Retry is allowed in a failure handler", function(assert) { return Ember.RSVP.resolve({ data: { id: snapshot.id, type: 'post' } }); }; - run(function() { - posts.save() - .then( - function() {}, - assert.wait(function() { return posts.save(); })) - .then( - assert.wait(function(post) { - // the ID here is '2' because the second post saves on the first attempt, - // while the first post saves on the second attempt - assert.equal(posts.get('firstObject.id'), '2', "The post ID made it through"); - })); + return run(() => { + return posts.save() + .catch(() => posts.save()) + .then(post => { + // the ID here is '2' because the second post saves on the first attempt, + // while the first post saves on the second attempt + assert.equal(posts.get('firstObject.id'), '2', "The post ID made it through"); + }); }); }); test("Collection will reject save on invalid", function(assert) { assert.expect(1); - run(function() { + + run(() => { env.store.createRecord('post', { title: 'Hello' }); env.store.createRecord('post', { title: 'World' }); }); - var posts = env.store.peekAll('post'); + let posts = env.store.peekAll('post'); env.adapter.createRecord = function(store, type, snapshot) { return Ember.RSVP.reject({ title: 'invalid' }); }; - Ember.run(function() { - posts.save().then(function() {}, function() { + return Ember.run(() => { + return posts.save().catch(() => { assert.ok(true, 'save operation was rejected'); }); }); diff --git a/tests/integration/records/load-test.js b/tests/integration/records/load-test.js index 02e070c0c1c..eea2c0f7ae4 100644 --- a/tests/integration/records/load-test.js +++ b/tests/integration/records/load-test.js @@ -5,9 +5,10 @@ import {module, test} from 'qunit'; import DS from 'ember-data'; -var hasMany = DS.hasMany; -var Post, Comment, env; -var run = Ember.run; +const { hasMany } = DS; +const { run } = Ember; + +let Post, Comment, env; module("integration/load - Loading Records", { beforeEach() { @@ -30,9 +31,9 @@ test("When loading a record fails, the record is not left behind", function(asse return Ember.RSVP.reject(); }; - run(function() { - env.store.findRecord('post', 1).then(null, assert.wait(function() { + return run(() => { + return env.store.findRecord('post', 1).catch(() => { assert.equal(env.store.hasRecordForId('post', 1), false); - })); + }); }); }); diff --git a/tests/integration/relationships/belongs-to-test.js b/tests/integration/relationships/belongs-to-test.js index 7cc2b0c9fbf..6bad183895c 100644 --- a/tests/integration/relationships/belongs-to-test.js +++ b/tests/integration/relationships/belongs-to-test.js @@ -6,14 +6,12 @@ import {module, test} from 'qunit'; import DS from 'ember-data'; -var env, store, User, Message, Post, Comment, Book, Chapter, Author, NewMessage; -var get = Ember.get; -var run = Ember.run; +const { get, run, RSVP } = Ember; +const { attr, hasMany, belongsTo } = DS; +const { hash } = RSVP; -var attr = DS.attr; -var hasMany = DS.hasMany; -var belongsTo = DS.belongsTo; -var hash = Ember.RSVP.hash; +let env, store, User, Message, Post, Comment, Book, Chapter, Author, NewMessage; +const injectionValue = Ember.MODEL_FACTORY_INJECTIONS; module("integration/relationship/belongs_to Belongs-To Relationships", { beforeEach() { @@ -85,6 +83,7 @@ module("integration/relationship/belongs_to Belongs-To Relationships", { }, afterEach() { + Ember.MODEL_FACTORY_INJECTIONS = injectionValue; run(env.container, 'destroy'); } }); @@ -109,7 +108,7 @@ test("The store can materialize a non loaded monomorphic belongsTo association", }); }; - run(function() { + run(() => { env.store.push({ data: { id: '1', @@ -126,8 +125,8 @@ test("The store can materialize a non loaded monomorphic belongsTo association", }); }); - run(function() { - env.store.findRecord('post', 1).then(function(post) { + return run(() => { + return env.store.findRecord('post', 1).then(post => { post.get('user'); }); }); @@ -136,7 +135,7 @@ test("The store can materialize a non loaded monomorphic belongsTo association", testInDebug("Only a record of the same modelClass can be used with a monomorphic belongsTo relationship", function(assert) { assert.expect(1); env.adapter.shouldBackgroundReloadRecord = () => false; - run(function() { + run(() => { store.push({ data: { id: '1', @@ -152,12 +151,12 @@ testInDebug("Only a record of the same modelClass can be used with a monomorphic }); - run(function() { - hash({ + return run(() => { + return hash({ post: store.findRecord('post', 1), comment: store.findRecord('comment', 2) - }).then(function(records) { - assert.expectAssertion(function() { + }).then(records => { + assert.expectAssertion(() => { records.post.set('user', records.comment); }, /You cannot add a record of modelClass 'comment' to the 'post.user' relationship/); }); @@ -167,7 +166,7 @@ testInDebug("Only a record of the same modelClass can be used with a monomorphic testInDebug("Only a record of the same base modelClass can be used with a polymorphic belongsTo relationship", function(assert) { env.adapter.shouldBackgroundReloadRecord = () => false; assert.expect(1); - run(function() { + run(() => { store.push({ data: [{ id: '1', @@ -193,22 +192,22 @@ testInDebug("Only a record of the same base modelClass can be used with a polymo }); - run(function() { - var asyncRecords = hash({ + return run(() => { + let asyncRecords = hash({ user: store.findRecord('user', 3), post: store.findRecord('post', 1), comment: store.findRecord('comment', 1), anotherComment: store.findRecord('comment', 2) }); - asyncRecords.then(function(records) { - var comment = records.comment; + return asyncRecords.then(records => { + let comment = records.comment; comment.set('message', records.anotherComment); comment.set('message', records.post); comment.set('message', null); - assert.expectAssertion(function() { + assert.expectAssertion(() => { comment.set('message', records.user); }, /You cannot add a record of modelClass 'user' to the 'comment.message' relationship \(only 'message' allowed\)/); }); @@ -217,7 +216,7 @@ testInDebug("Only a record of the same base modelClass can be used with a polymo test("The store can load a polymorphic belongsTo association", function(assert) { env.adapter.shouldBackgroundReloadRecord = () => false; - run(function() { + run(() => { env.store.push({ data: { id: '1', @@ -241,11 +240,11 @@ test("The store can load a polymorphic belongsTo association", function(assert) }); }); - run(function() { - hash({ + return run(() => { + return hash({ message: store.findRecord('post', 1), comment: store.findRecord('comment', 2) - }).then(function(records) { + }).then(records => { assert.equal(records.comment.get('message'), records.message); }); }); @@ -253,19 +252,21 @@ test("The store can load a polymorphic belongsTo association", function(assert) test("The store can serialize a polymorphic belongsTo association", function(assert) { env.adapter.shouldBackgroundReloadRecord = () => false; - var serializerInstance = store.serializerFor('comment'); + let serializerInstance = store.serializerFor('comment'); serializerInstance.serializePolymorphicType = function(record, json, relationship) { assert.ok(true, "The serializer's serializePolymorphicType method should be called"); json["message_type"] = "post"; }; - run(function() { + + return run(() => { env.store.push({ data: { id: '1', type: 'post' } }); + env.store.push({ data: { id: '2', @@ -281,8 +282,8 @@ test("The store can serialize a polymorphic belongsTo association", function(ass } }); - store.findRecord('comment', 2).then(function(comment) { - var serialized = comment.serialize({ includeId: true }); + return store.findRecord('comment', 2).then(comment => { + let serialized = comment.serialize({ includeId: true }); assert.equal(serialized.data.relationships.message.data.id, 1); assert.equal(serialized.data.relationships.message.data.type, 'posts'); }); @@ -290,20 +291,19 @@ test("The store can serialize a polymorphic belongsTo association", function(ass }); test("A serializer can materialize a belongsTo as a link that gets sent back to findBelongsTo", function(assert) { - let done = assert.async(); env.adapter.shouldBackgroundReloadRecord = () => false; - var Group = DS.Model.extend({ + let Group = DS.Model.extend({ people: DS.hasMany('person', { async: false }) }); - var Person = DS.Model.extend({ + let Person = DS.Model.extend({ group: DS.belongsTo({ async: true }) }); env.registry.register('model:group', Group); env.registry.register('model:person', Person); - run(function() { + run(() => { store.push({ data: { id: '1', @@ -323,7 +323,7 @@ test("A serializer can materialize a belongsTo as a link that gets sent back to throw new Error("Adapter's find method should not be called"); }; - env.adapter.findBelongsTo = assert.wait(function(store, snapshot, link, relationship) { + env.adapter.findBelongsTo = function(store, snapshot, link, relationship) { assert.equal(relationship.type, 'group'); assert.equal(relationship.key, 'group'); assert.equal(link, "/people/1/group"); @@ -339,34 +339,32 @@ test("A serializer can materialize a belongsTo as a link that gets sent back to } } }); - }); + }; - run(function() { - env.store.findRecord('person', 1).then(function(person) { + return run(() => { + return env.store.findRecord('person', 1).then(person => { return person.get('group'); - }).then(function(group) { + }).then(group => { assert.ok(group instanceof Group, "A group object is loaded"); assert.ok(group.get('id') === '1', 'It is the group we are expecting'); - done(); }); }); }); test('A record with an async belongsTo relationship always returns a promise for that relationship', function(assert) { - let done = assert.async(); env.adapter.shouldBackgroundReloadRecord = () => false; - var Seat = DS.Model.extend({ + let Seat = DS.Model.extend({ person: DS.belongsTo('person', { async: false }) }); - var Person = DS.Model.extend({ + let Person = DS.Model.extend({ seat: DS.belongsTo('seat', { async: true }) }); env.registry.register('model:seat', Seat); env.registry.register('model:person', Person); - run(function() { + run(() => { store.push({ data: { id: '1', @@ -386,18 +384,17 @@ test('A record with an async belongsTo relationship always returns a promise for throw new Error("Adapter's find method should not be called"); }; - env.adapter.findBelongsTo = assert.wait(function(store, snapshot, link, relationship) { + env.adapter.findBelongsTo = function(store, snapshot, link, relationship) { return Ember.RSVP.resolve({ data: { id: 1, type: 'seat' } }); - }); + }; - run(function() { - env.store.findRecord('person', 1).then(function(person) { - person.get('seat').then(function(seat) { + return run(() => { + return env.store.findRecord('person', 1).then(person => { + return person.get('seat').then(seat => { // this assertion fails too // ok(seat.get('person') === person, 'parent relationship should be populated'); seat.set('person', person); assert.ok(person.get('seat').then, 'seat should be a PromiseObject'); - done(); }); }); }); @@ -407,18 +404,18 @@ test("A record with an async belongsTo relationship returning null should resolv assert.expect(1); env.adapter.shouldBackgroundReloadRecord = () => false; - var Group = DS.Model.extend({ + let Group = DS.Model.extend({ people: DS.hasMany('person', { async: false }) }); - var Person = DS.Model.extend({ + let Person = DS.Model.extend({ group: DS.belongsTo({ async: true }) }); env.registry.register('model:group', Group); env.registry.register('model:person', Person); - run(function() { + run(() => { store.push({ data: { id: '1', @@ -438,33 +435,33 @@ test("A record with an async belongsTo relationship returning null should resolv throw new Error("Adapter's find method should not be called"); }; - env.adapter.findBelongsTo = assert.wait(function(store, snapshot, link, relationship) { + env.adapter.findBelongsTo = function(store, snapshot, link, relationship) { return Ember.RSVP.resolve({ data: null }); - }); + }; - env.store.findRecord('person', '1').then(assert.wait(function(person) { + return env.store.findRecord('person', '1').then(person => { return person.get('group'); - })).then(assert.wait(function(group) { + }).then(group => { assert.ok(group === null, "group should be null"); - })); + }); }); test("A record can be created with a resolved belongsTo promise", function(assert) { assert.expect(1); env.adapter.shouldBackgroundReloadRecord = () => false; - var Group = DS.Model.extend({ + let Group = DS.Model.extend({ people: DS.hasMany('person', { async: false }) }); - var Person = DS.Model.extend({ + let Person = DS.Model.extend({ group: DS.belongsTo({ async: true }) }); env.registry.register('model:group', Group); env.registry.register('model:person', Person); - run(function() { + run(() => { store.push({ data: { id: 1, @@ -473,58 +470,42 @@ test("A record can be created with a resolved belongsTo promise", function(asser }); }); - var groupPromise = store.findRecord('group', 1); - groupPromise.then(assert.wait(function(group) { - var person = env.store.createRecord('person', { + let groupPromise = store.findRecord('group', 1); + return groupPromise.then(group => { + let person = env.store.createRecord('person', { group: groupPromise }); assert.equal(person.get('group.content'), group); - })); + }); }); test("polymorphic belongsTo class-checks check the superclass when MODEL_FACTORY_INJECTIONS is enabled", function(assert) { assert.expect(1); - var injectionValue = Ember.MODEL_FACTORY_INJECTIONS; - Ember.MODEL_FACTORY_INJECTIONS = true; + run(() => { + let igor = env.store.createRecord('user', { name: 'Igor' }); + let post = env.store.createRecord('post', { title: "Igor's unimaginative blog post" }); - try { - run(function () { - var igor = env.store.createRecord('user', { name: 'Igor' }); - var post = env.store.createRecord('post', { title: "Igor's unimaginative blog post" }); - - igor.set('favouriteMessage', post); + igor.set('favouriteMessage', post); - assert.equal(igor.get('favouriteMessage.title'), "Igor's unimaginative blog post"); - }); - } finally { - Ember.MODEL_FACTORY_INJECTIONS = injectionValue; - } + assert.equal(igor.get('favouriteMessage.title'), "Igor's unimaginative blog post"); + }); }); test("the subclass in a polymorphic belongsTo relationship is an instanceof its superclass", function(assert) { assert.expect(1); - - var injectionValue = Ember.MODEL_FACTORY_INJECTIONS; - Ember.MODEL_FACTORY_INJECTIONS = true; - - try { - run(function () { - var message = env.store.createRecord('message', { id: 1 }); - var comment = env.store.createRecord('comment', { id: 2, message: message }); - assert.ok(comment instanceof Message, 'a comment is an instance of a message'); - }); - - } finally { - Ember.MODEL_FACTORY_INJECTIONS = injectionValue; - } + run(() => { + let message = env.store.createRecord('message', { id: 1 }); + let comment = env.store.createRecord('comment', { id: 2, message: message }); + assert.ok(comment instanceof Message, 'a comment is an instance of a message'); + }); }); test("relationshipsByName does not cache a factory", function(assert) { // The model is loaded up via a container. It has relationshipsByName // called on it. - var modelViaFirstFactory = store.modelFor('user'); + let modelViaFirstFactory = store.modelFor('user'); get(modelViaFirstFactory, 'relationshipsByName'); // An app is reset, or the container otherwise destroyed. @@ -542,26 +523,26 @@ test("relationshipsByName does not cache a factory", function(assert) { store = env.store; // relationshipsByName is called again. - var modelViaSecondFactory = store.modelFor('user'); - var relationshipsByName = get(modelViaSecondFactory, 'relationshipsByName'); - var messageType = relationshipsByName.get('messages').type; + let modelViaSecondFactory = store.modelFor('user'); + let relationshipsByName = get(modelViaSecondFactory, 'relationshipsByName'); + let messageType = relationshipsByName.get('messages').type; // A model is looked up in the store based on a string, via user input - var messageModelFromStore = store.modelFor('message'); + let messageModelFromStore = store.modelFor('message'); // And the model is lookup up internally via the relationship type - var messageModelFromRelationType = store.modelFor(messageType); + let messageModelFromRelationType = store.modelFor(messageType); assert.equal(messageModelFromRelationType, messageModelFromStore, "model factory based on relationship type matches the model based on store.modelFor"); }); test("relationshipsByName is cached in production", function(assert) { - var model = store.modelFor('user'); - var oldTesting = Ember.testing; + let model = store.modelFor('user'); + let oldTesting = Ember.testing; //We set the cacheable to true because that is the default state for any CP and then assert that it //did not get dynamically changed when accessed - var relationshipsByName = model.relationshipsByName; - var oldCacheable = relationshipsByName._cacheable; + let relationshipsByName = model.relationshipsByName; + let oldCacheable = relationshipsByName._cacheable; relationshipsByName._cacheable = true; Ember.testing = false; try { @@ -574,12 +555,12 @@ test("relationshipsByName is cached in production", function(assert) { }); test("relatedTypes is cached in production", function(assert) { - var model = store.modelFor('user'); - var oldTesting = Ember.testing; + let model = store.modelFor('user'); + let oldTesting = Ember.testing; //We set the cacheable to true because that is the default state for any CP and then assert that it //did not get dynamically changed when accessed - var relatedTypes = model.relatedTypes; - var oldCacheable = relatedTypes._cacheable; + let relatedTypes = model.relatedTypes; + let oldCacheable = relatedTypes._cacheable; relatedTypes._cacheable = true; Ember.testing = false; try { @@ -592,12 +573,12 @@ test("relatedTypes is cached in production", function(assert) { }); test("relationships is cached in production", function(assert) { - var model = store.modelFor('user'); - var oldTesting = Ember.testing; + let model = store.modelFor('user'); + let oldTesting = Ember.testing; //We set the cacheable to true because that is the default state for any CP and then assert that it //did not get dynamically changed when accessed - var relationships = model.relationships; - var oldCacheable = relationships._cacheable; + let relationships = model.relationships; + let oldCacheable = relationships._cacheable; relationships._cacheable = true; Ember.testing = false; try { @@ -635,8 +616,8 @@ test("relationship changes shouldn’t cause async fetches", function(assert) { env.store.modelFor('comment').reopen({ post: DS.belongsTo('post', { async: false }) }); - var comment; - run(function() { + let comment; + run(() => { env.store.push({ data: { id: '1', @@ -689,7 +670,7 @@ test("relationship changes shouldn’t cause async fetches", function(assert) { test("Destroying a record with an unloaded aync belongsTo association does not fetch the record", function(assert) { assert.expect(2); - var post; + let post; env.store.modelFor('message').reopen({ user: DS.hasMany('user', { @@ -704,7 +685,7 @@ test("Destroying a record with an unloaded aync belongsTo association does not f }) }); - run(function() { + run(() => { post = env.store.push({ data: { id: '1', @@ -752,8 +733,8 @@ test("Destroying a record with an unloaded aync belongsTo association does not f }); testInDebug("A sync belongsTo errors out if the record is unlaoded", function(assert) { - var message; - run(function() { + let message; + run(() => { message = env.store.push({ data: { id: '1', @@ -771,7 +752,7 @@ testInDebug("A sync belongsTo errors out if the record is unlaoded", function(as }); - assert.expectAssertion(function() { + assert.expectAssertion(() => { message.get('user'); }, /You looked up the 'user' relationship on a 'message' with id 1 but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async \(`DS.belongsTo\({ async: true }\)`\)/); }); @@ -780,8 +761,8 @@ test("Rollbacking attributes for a deleted record restores implicit relationship Book.reopen({ author: DS.belongsTo('author', { async: true }) }); - var book, author; - run(function() { + let book, author; + run(() => { book = env.store.push({ data: { id: '1', @@ -810,18 +791,20 @@ test("Rollbacking attributes for a deleted record restores implicit relationship }); }); - run(function() { + return run(() => { author.deleteRecord(); author.rollbackAttributes(); - book.get('author').then(function(fetchedAuthor) { + + return book.get('author').then(fetchedAuthor => { assert.equal(fetchedAuthor, author, 'Book has an author after rollback attributes'); }); }); }); test("Rollbacking attributes for a deleted record restores implicit relationship - sync", function(assert) { - var book, author; - run(function() { + let book, author; + + run(() => { book = env.store.push({ data: { id: '1', @@ -839,6 +822,7 @@ test("Rollbacking attributes for a deleted record restores implicit relationship } } }); + author = env.store.push({ data: { id: '2', @@ -848,19 +832,20 @@ test("Rollbacking attributes for a deleted record restores implicit relationship } } }); - }); - run(function() { + + run(() =>{ author.deleteRecord(); author.rollbackAttributes(); }); + assert.equal(book.get('author'), author, 'Book has an author after rollback attributes'); }); testInDebug("Passing a model as type to belongsTo should not work", function(assert) { assert.expect(1); - assert.expectAssertion(function() { + assert.expectAssertion(() => { User = DS.Model.extend(); DS.Model.extend({ @@ -889,9 +874,9 @@ test("belongsTo hasData async loaded", function(assert) { }); }; - run(function() { - store.findRecord('book', 1).then(function(book) { - var relationship = book._internalModel._relationships.get('author'); + return run(() => { + return store.findRecord('book', 1).then(book => { + let relationship = book._internalModel._relationships.get('author'); assert.equal(relationship.hasData, true, 'relationship has data'); }); }); @@ -913,9 +898,9 @@ test("belongsTo hasData sync loaded", function(assert) { }); }; - run(function() { - store.findRecord('book', 1).then(function(book) { - var relationship = book._internalModel._relationships.get('author'); + return run(() => { + return store.findRecord('book', 1).then(book => { + let relationship = book._internalModel._relationships.get('author'); assert.equal(relationship.hasData, true, 'relationship has data'); }); }); @@ -941,9 +926,9 @@ test("belongsTo hasData async not loaded", function(assert) { }); }; - run(function() { - store.findRecord('book', 1).then(function(book) { - var relationship = book._internalModel._relationships.get('author'); + return run(() => { + return store.findRecord('book', 1).then(book => { + let relationship = book._internalModel._relationships.get('author'); assert.equal(relationship.hasData, false, 'relationship does not have data'); }); }); @@ -962,9 +947,9 @@ test("belongsTo hasData sync not loaded", function(assert) { }); } - run(function() { - store.findRecord('book', 1).then(function(book) { - var relationship = book._internalModel._relationships.get('author'); + return run(() => { + return store.findRecord('book', 1).then(book => { + let relationship = book._internalModel._relationships.get('author'); assert.equal(relationship.hasData, false, 'relationship does not have data'); }); }); @@ -977,9 +962,9 @@ test("belongsTo hasData async created", function(assert) { author: belongsTo('author', { async: true }) }); - run(function() { - var book = store.createRecord('book', { name: 'The Greatest Book' }); - var relationship = book._internalModel._relationships.get('author'); + run(() => { + let book = store.createRecord('book', { name: 'The Greatest Book' }); + let relationship = book._internalModel._relationships.get('author'); assert.equal(relationship.hasData, true, 'relationship has data'); }); }); @@ -987,16 +972,17 @@ test("belongsTo hasData async created", function(assert) { test("belongsTo hasData sync created", function(assert) { assert.expect(1); - run(function() { - var book = store.createRecord('book', { name: 'The Greatest Book' }); - var relationship = book._internalModel._relationships.get('author'); + run(() => { + let book = store.createRecord('book', { name: 'The Greatest Book' }); + let relationship = book._internalModel._relationships.get('author'); assert.equal(relationship.hasData, true, 'relationship has data'); }); }); test("Model's belongsTo relationship should not be created during model creation", function(assert) { - var user; - run(function () { + let user; + + run(() => { user = env.store.push({ data: { id: '1', @@ -1009,8 +995,9 @@ test("Model's belongsTo relationship should not be created during model creation }); test("Model's belongsTo relationship should be created during model creation if relationship passed in constructor", function(assert) { - var user, message; - run(function () { + let user, message; + + run(() => { message = env.store.createRecord('message'); user = env.store.createRecord('user', { name: 'John Doe', @@ -1021,8 +1008,9 @@ test("Model's belongsTo relationship should be created during model creation if }); test("Model's belongsTo relationship should be created during 'set' method", function(assert) { - var user, message; - run(function () { + let user, message; + + run(() => { message = env.store.createRecord('message'); user = env.store.createRecord('user'); user.set('favouriteMessage', message); @@ -1031,8 +1019,9 @@ test("Model's belongsTo relationship should be created during 'set' method", fun }); test("Model's belongsTo relationship should be created during 'get' method", function(assert) { - var user; - run(function () { + let user; + + run(() => { user = env.store.createRecord('user'); user.get('favouriteMessage'); assert.ok(user._internalModel._relationships.has('favouriteMessage'), "Newly created record with relationships in params passed in its constructor should have relationships"); @@ -1058,7 +1047,7 @@ test("Related link should be fetched when no local data is present", function(as }); }; - run(function() { + return run(() => { let book = env.store.push({ data: { type: 'book', @@ -1072,7 +1061,8 @@ test("Related link should be fetched when no local data is present", function(as } } }); - book.get('author').then((author) => { + + return book.get('author').then(author => { assert.equal(author.get('name'), 'This is author', 'author name is correct'); }); }); @@ -1099,7 +1089,7 @@ test("Local data should take precedence over related link", function(assert) { }); }; - run(function() { + return run(() => { let book = env.store.push({ data: { type: 'book', @@ -1114,7 +1104,8 @@ test("Local data should take precedence over related link", function(assert) { } } }); - book.get('author').then((author) => { + + return book.get('author').then(author => { assert.equal(author.get('name'), 'This is author', 'author name is correct'); }); }); @@ -1143,7 +1134,7 @@ test("New related link should take precedence over local data", function(assert) assert.ok(false, "The adapter's findRecord method should not be called"); }; - run(function() { + return run(() => { let book = env.store.push({ data: { type: 'book', @@ -1202,7 +1193,7 @@ test("Updated related link should take precedence over local data", function(ass assert.ok(false, "The adapter's findRecord method should not be called"); }; - run(function() { + return run(() => { let book = env.store.push({ data: { type: 'book', @@ -1225,26 +1216,26 @@ test("Updated related link should take precedence over local data", function(ass }] }); - book.get('author').then((author) => { + return book.get('author').then((author) => { assert.equal(author.get('name'), 'This is author', 'author name is correct'); - }); - - env.store.push({ - data: { - type: 'book', - id: '1', - relationships: { - author: { - links: { - related: 'author-updated-link' + }).then(() => { + env.store.push({ + data: { + type: 'book', + id: '1', + relationships: { + author: { + links: { + related: 'author-updated-link' + } } } } - } - }); + }); - book.get('author').then((author) => { - assert.equal(author.get('name'), 'This is updated author', 'author name is correct'); + return book.get('author').then((author) => { + assert.equal(author.get('name'), 'This is updated author', 'author name is correct'); + }); }); }); }); @@ -1264,7 +1255,7 @@ test("Updated identical related link should not take precedence over local data" assert.ok(false, "The adapter's findRecord method should not be called"); }; - run(function() { + return run(() => { let book = env.store.push({ data: { type: 'book', @@ -1287,33 +1278,32 @@ test("Updated identical related link should not take precedence over local data" }] }); - book.get('author').then((author) => { + return book.get('author').then((author) => { assert.equal(author.get('name'), 'This is author', 'author name is correct'); - }); + }).then(() => { - env.store.push({ - data: { - type: 'book', - id: '1', - relationships: { - author: { - links: { - related: 'author' + env.store.push({ + data: { + type: 'book', + id: '1', + relationships: { + author: { + links: { + related: 'author' + } } } } - } - }); + }); - book.get('author').then((author) => { - assert.equal(author.get('name'), 'This is author', 'author name is correct'); + return book.get('author').then((author) => { + assert.equal(author.get('name'), 'This is author', 'author name is correct'); + }); }); }); }); test("A belongsTo relationship can be reloaded using the reference if it was fetched via link", function(assert) { - var done = assert.async(); - Chapter.reopen({ book: DS.belongsTo({ async: true }) }); @@ -1342,13 +1332,14 @@ test("A belongsTo relationship can be reloaded using the reference if it was fet }); }; - run(function() { - var chapter; - store.findRecord('chapter', 1).then(function(_chapter) { + return run(() => { + let chapter; + + return store.findRecord('chapter', 1).then(_chapter => { chapter = _chapter; return chapter.get('book'); - }).then(function(book) { + }).then(book => { assert.equal(book.get('name'), "book title"); env.adapter.findBelongsTo = function() { @@ -1362,23 +1353,19 @@ test("A belongsTo relationship can be reloaded using the reference if it was fet }; return chapter.belongsTo('book').reload(); - }).then(function(book) { + }).then(book => { assert.equal(book.get('name'), "updated book title"); - - done(); }); }); }); test("A sync belongsTo relationship can be reloaded using a reference if it was fetched via id", function(assert) { - var done = assert.async(); - Chapter.reopen({ book: DS.belongsTo() }); - var chapter; - run(function() { + let chapter; + run(() => { chapter = env.store.push({ data: { type: 'chapter', @@ -1411,27 +1398,23 @@ test("A sync belongsTo relationship can be reloaded using a reference if it was }); }; - run(function() { - var book = chapter.get('book'); + return run(() => { + let book = chapter.get('book'); assert.equal(book.get('name'), "book title"); - chapter.belongsTo('book').reload().then(function(book) { + return chapter.belongsTo('book').reload().then(function(book) { assert.equal(book.get('name'), "updated book title"); - - done(); }); }); }); test("A belongsTo relationship can be reloaded using a reference if it was fetched via id", function(assert) { - var done = assert.async(); - Chapter.reopen({ book: DS.belongsTo({ async: true }) }); - var chapter; - run(function() { + let chapter; + run(() => { chapter = env.store.push({ data: { type: 'chapter', @@ -1455,8 +1438,8 @@ test("A belongsTo relationship can be reloaded using a reference if it was fetch }); }; - run(function() { - chapter.get('book').then(function(book) { + return run(() => { + return chapter.get('book').then(book => { assert.equal(book.get('name'), "book title"); env.adapter.findRecord = function() { @@ -1470,17 +1453,15 @@ test("A belongsTo relationship can be reloaded using a reference if it was fetch }; return chapter.belongsTo('book').reload(); - }).then(function(book) { + }).then(book => { assert.equal(book.get('name'), "updated book title"); - - done(); }); }); }); testInDebug("A belongsTo relationship warns if malformatted data is pushed into the store", function(assert) { - assert.expectAssertion(function() { - run(function() { + assert.expectAssertion(() => { + run(() => { let chapter = env.store.push({ data: { type: 'chapter', diff --git a/tests/integration/relationships/has-many-test.js b/tests/integration/relationships/has-many-test.js index b7534447b16..e2915ce39f7 100644 --- a/tests/integration/relationships/has-many-test.js +++ b/tests/integration/relationships/has-many-test.js @@ -8,15 +8,12 @@ import {module, test} from 'qunit'; import DS from 'ember-data'; -var env, store, User, Contact, Email, Phone, Message, Post, Comment; -var Book, Chapter, Page; -var get = Ember.get; -var resolve = Ember.RSVP.resolve; -var run = Ember.run; +let env, store, User, Contact, Email, Phone, Message, Post, Comment; +let Book, Chapter, Page; -var attr = DS.attr; -var hasMany = DS.hasMany; -var belongsTo = DS.belongsTo; +const { get, run } = Ember; +const { resolve } = Ember.RSVP; +const { attr, hasMany, belongsTo } = DS; module("integration/relationships/has_many - Has-Many Relationships", { beforeEach() { @@ -122,7 +119,7 @@ test("When a hasMany relationship is accessed, the adapter's findMany method sho return { data: postData }; }; - run(function() { + return run(() => { env.store.push({ data: postData, included: [{ @@ -130,7 +127,8 @@ test("When a hasMany relationship is accessed, the adapter's findMany method sho id: '1' }] }); - env.store.findRecord('post', 1).then(function(post) { + + return env.store.findRecord('post', 1).then(post => { return post.get('comments'); }); }); @@ -169,11 +167,12 @@ test("adapter.findMany only gets unique IDs even if duplicate IDs are present in return { data: bookData }; }; - run(function() { + return run(() => { env.store.push({ data: bookData }); - env.store.findRecord('book', 1).then(function(book) { + + return env.store.findRecord('book', 1).then(book => { return book.get('chapters'); }); }); @@ -226,21 +225,20 @@ test("A serializer can materialize a hasMany as an opaque token that can be lazi }); }; - run(function() { - env.store.findRecord('post', 1).then(assert.wait(function(post) { + return run(() => { + return env.store.findRecord('post', 1).then(post => { return post.get('comments'); - })).then(assert.wait(function(comments) { + }).then(comments => { assert.equal(comments.get('isLoaded'), true, "comments are loaded"); assert.equal(comments.get('length'), 2, "comments have 2 length"); assert.equal(comments.objectAt(0).get('body'), 'First', "comment loaded successfully"); - })); + }); }); }); test("Accessing a hasMany backed by a link multiple times triggers only one request", function(assert) { assert.expect(2); - let done = assert.async(); - var count = 0; + let count = 0; Post.reopen({ comments: DS.hasMany('comment', { async: true }) }); @@ -248,9 +246,9 @@ test("Accessing a hasMany backed by a link multiple times triggers only one requ Comment.reopen({ message: DS.belongsTo('post', { async: true }) }); - var post; + let post; - run(function() { + run(() => { env.store.push({ data: { type: 'post', @@ -270,9 +268,9 @@ test("Accessing a hasMany backed by a link multiple times triggers only one requ env.adapter.findHasMany = function(store, snapshot, link, relationship) { count++; assert.equal(count, 1, "findHasMany has only been called once"); - return new Ember.RSVP.Promise(function(resolve, reject) { - setTimeout(function() { - var value = { + return new Ember.RSVP.Promise((resolve, reject) => { + setTimeout(() => { + let value = { data: [ { id: 1, type: 'comment', attributes: { body: "First" } }, { id: 2, type: 'comment', attributes: { body: "Second" } } @@ -283,8 +281,9 @@ test("Accessing a hasMany backed by a link multiple times triggers only one requ }); }; - var promise1, promise2; - run(function() { + let promise1, promise2; + + run(() => { promise1 = post.get('comments'); //Invalidate the post.comments CP env.store.push({ @@ -300,9 +299,12 @@ test("Accessing a hasMany backed by a link multiple times triggers only one requ }); promise2 = post.get('comments'); }); - Ember.RSVP.all([promise1, promise2]).then(function() { + + return Ember.RSVP.all([ + promise1, + promise2 + ]).then(() => { assert.equal(promise1.promise, promise2.promise, "Same promise is returned both times"); - done(); }); }); @@ -324,8 +326,8 @@ test("A hasMany backed by a link remains a promise after a record has been added ] }); }; - var post; - run(function() { + let post; + run(() => { env.store.push({ data: { type: 'post', @@ -342,8 +344,8 @@ test("A hasMany backed by a link remains a promise after a record has been added post = env.store.peekRecord('post', 1); }); - run(function() { - post.get('comments').then(function() { + return run(() => { + return post.get('comments').then(() => { env.store.push({ data: { type: 'comment', @@ -355,7 +357,8 @@ test("A hasMany backed by a link remains a promise after a record has been added } } }); - post.get('comments').then(function() { + + return post.get('comments').then(() => { assert.ok(true, 'Promise was called'); }); }); @@ -389,20 +392,18 @@ test("A hasMany updated link should not remove new children", function(assert) { }); }; - run(function() { - var post = env.store.createRecord('post', {}); + return run(() => { + let post = env.store.createRecord('post', {}); env.store.createRecord('comment', { message: post }); - post.get('comments') - .then(function(comments) { + return post.get('comments') + .then(comments => { assert.equal(comments.get('length'), 1); return post.save(); }) - .then(function() { - return post.get('comments'); - }) - .then(function(comments) { + .then(() => post.get('comments')) + .then(comments => { assert.equal(comments.get('length'), 1); }); }); @@ -437,20 +438,17 @@ test("A hasMany updated link should not remove new children when the parent reco }); }; - run(function() { - var post = env.store.createRecord('post', {}); + return run(() => { + let post = env.store.createRecord('post', {}); env.store.createRecord('comment', { message: post }); - post.get('comments') - .then(function(comments) { + return post.get('comments') + .then(comments => { assert.equal(comments.get('length'), 1); - return post.save(); }) - .then(function() { - return post.get('comments'); - }) - .then(function(comments) { + .then(() =>post.get('comments')) + .then(comments => { assert.equal(comments.get('length'), 2); }); }); @@ -470,21 +468,21 @@ test("A hasMany relationship doesn't contain duplicate children, after the canon return Ember.RSVP.resolve({ data: { id: 1, type: 'post' } }); }; - run(function() { - var post = env.store.createRecord('post', {}); + return run(() => { + let post = env.store.createRecord('post', {}); // create a new comment with id 'local', which is in the 'comments' // relationship of post - var localComment = env.store.createRecord('comment', { id: 'local', message: post }); + let localComment = env.store.createRecord('comment', { id: 'local', message: post }); - post.get('comments') - .then(function(comments) { + return post.get('comments') + .then(comments => { assert.equal(comments.get('length'), 1); assert.equal(localComment.get('isNew'), true); return post.save(); }) - .then(function() { + .then(() => { // Now the post is saved but the locally created comment with the id // 'local' is still in the created state since it hasn't been saved @@ -513,10 +511,8 @@ test("A hasMany relationship doesn't contain duplicate children, after the canon }); }) - .then(function() { - return post.get('comments'); - }) - .then(function(comments) { + .then(() => post.get('comments')) + .then(comments => { assert.equal(comments.get('length'), 1); assert.equal(localComment.get('isNew'), true); }); @@ -626,7 +622,7 @@ test("A sync hasMany relationship can be reloaded if it was fetched via ids", fu }); env.store.findRecord('post', '1').then(function(post) { - var comments = post.get('comments'); + let comments = post.get('comments'); assert.equal(comments.get('isLoaded'), true, "comments are loaded"); assert.equal(comments.get('length'), 2, "comments have a length of 2"); @@ -715,7 +711,7 @@ test("A hasMany relationship can be reloaded even if it failed at the first time }); }; - var loadingCount = -1; + let loadingCount = -1; env.adapter.findHasMany = function(store, record, link, relationship) { loadingCount++; if (loadingCount % 2 === 0) { @@ -729,7 +725,7 @@ test("A hasMany relationship can be reloaded even if it failed at the first time }; run(function() { env.store.findRecord('post', 1).then(function(post) { - var comments = post.get('comments'); + let comments = post.get('comments'); return comments.catch(function() { return comments.reload(); }).then(function(manyArray) { @@ -927,7 +923,7 @@ test("PromiseArray proxies createRecord to its ManyArray once the hasMany is loa { id: 2, type: 'comment', attributes: { body: "Second" } } ]}); }; - var post; + let post; run(function() { env.store.push({ @@ -951,7 +947,7 @@ test("PromiseArray proxies createRecord to its ManyArray once the hasMany is loa assert.equal(comments.get('isLoaded'), true, "comments are loaded"); assert.equal(comments.get('length'), 2, "comments have 2 length"); - var newComment = post.get('comments').createRecord({ body: 'Third' }); + let newComment = post.get('comments').createRecord({ body: 'Third' }); assert.equal(newComment.get('body'), 'Third', "new comment is returned"); assert.equal(comments.get('length'), 3, "comments have 3 length, including new record"); }); @@ -971,7 +967,7 @@ test("PromiseArray proxies evented methods to its ManyArray", function(assert) { { id: 2, type: 'comment', attributes: { body: "Second" } } ]}); }; - var post, comments; + let post, comments; run(function() { env.store.push({ @@ -1045,7 +1041,7 @@ test("An updated `links` value should invalidate a relationship cache", function ]}); } }; - var post; + let post; run(function() { env.store.push({ @@ -1130,7 +1126,7 @@ test("When a polymorphic hasMany relationship is accessed, the adapter's findMan run(function() { env.store.findRecord('user', 1).then(function(user) { - var messages = user.get('messages'); + let messages = user.get('messages'); assert.equal(messages.get('length'), 2, "The messages are correctly loaded"); }); }); @@ -1178,13 +1174,13 @@ test("When a polymorphic hasMany relationship is accessed, the store can call mu test("polymorphic hasMany type-checks check the superclass when MODEL_FACTORY_INJECTIONS is enabled", function(assert) { assert.expect(1); - var injectionValue = Ember.MODEL_FACTORY_INJECTIONS; + let injectionValue = Ember.MODEL_FACTORY_INJECTIONS; Ember.MODEL_FACTORY_INJECTIONS = true; try { run(function () { - var igor = env.store.createRecord('user', { name: 'Igor' }); - var comment = env.store.createRecord('comment', { body: "Well I thought the title was fine" }); + let igor = env.store.createRecord('user', { name: 'Igor' }); + let comment = env.store.createRecord('comment', { body: "Well I thought the title was fine" }); igor.get('messages').addObject(comment); @@ -1343,7 +1339,7 @@ test("Polymorphic relationships with a hasMany is set up correctly on both sides Post.reopen({ contact: DS.belongsTo('contact', { polymorphic: true, async: false }) }); - var email, post; + let email, post; run(function () { email = env.store.createRecord('email'); @@ -1451,7 +1447,7 @@ testInDebug("Only records of the same base modelClass can be added to a polymorp }] }); }); - var asyncRecords; + let asyncRecords; run(function() { asyncRecords = Ember.RSVP.hash({ @@ -1498,7 +1494,7 @@ test("A record can be removed from a polymorphic association", function(assert) }] }); }); - var asyncRecords; + let asyncRecords; run(function() { asyncRecords = Ember.RSVP.hash({ @@ -1512,7 +1508,7 @@ test("A record can be removed from a polymorphic association", function(assert) }).then(function(records) { assert.equal(records.messages.get('length'), 1, "The user has 1 message"); - var removedObject = records.messages.popObject(); + let removedObject = records.messages.popObject(); assert.equal(removedObject, records.comment, "The message is correctly removed"); assert.equal(records.messages.get('length'), 0, "The user does not have any messages"); @@ -1524,14 +1520,14 @@ test("A record can be removed from a polymorphic association", function(assert) test("When a record is created on the client, its hasMany arrays should be in a loaded state", function(assert) { assert.expect(3); - var post; + let post; run(function() { post = env.store.createRecord('post'); }); assert.ok(get(post, 'isLoaded'), "The post should have isLoaded flag"); - var comments; + let comments; run(function() { comments = get(post, 'comments'); }); @@ -1548,7 +1544,7 @@ test("When a record is created on the client, its async hasMany arrays should be comments: DS.hasMany('comment', { async: true }) }); - var post = run(function() { + let post = run(function() { return env.store.createRecord('post'); }); @@ -1565,7 +1561,7 @@ test("When a record is created on the client, its async hasMany arrays should be test("we can set records SYNC HM relationship", function(assert) { assert.expect(1); - var post = run(function() { + let post = run(function() { return env.store.createRecord('post'); }); run(function() { @@ -1596,7 +1592,7 @@ test("We can set records ASYNC HM relationship", function(assert) { comments: DS.hasMany('comment', { async: true }) }); - var post = run(function() { + let post = run(function() { return env.store.createRecord('post'); }); run(function() { @@ -1618,28 +1614,28 @@ test("We can set records ASYNC HM relationship", function(assert) { post.set('comments', env.store.peekAll('comment')); }); - post.get('comments').then(assert.wait(function(comments) { + return post.get('comments').then(comments => { assert.equal(comments.get('length') , 2, "we can set async HM relationship"); - })); + }); }); test("When a record is saved, its unsaved hasMany records should be kept", function(assert) { assert.expect(1); - var post, comment; + let post, comment; env.adapter.createRecord = function(store, type, snapshot) { return Ember.RSVP.resolve({ data: { id: 1, type: snapshot.modelName } }); }; - run(function () { + return run(() => { post = env.store.createRecord('post'); comment = env.store.createRecord('comment'); post.get('comments').pushObject(comment); - post.save(); + return post.save(); + }).then(() => { + assert.equal(get(post, 'comments.length'), 1, "The unsaved comment should be in the post's comments array"); }); - - assert.equal(get(post, 'comments.length'), 1, "The unsaved comment should be in the post's comments array"); }); test("dual non-async HM <-> BT", function(assert) { @@ -1652,11 +1648,11 @@ test("dual non-async HM <-> BT", function(assert) { }); env.adapter.createRecord = function(store, type, snapshot) { - var serialized = snapshot.record.serialize(); + let serialized = snapshot.record.serialize(); serialized.data.id = 2; return Ember.RSVP.resolve(serialized); }; - var post, firstComment; + let post, firstComment; run(function() { env.store.push({ @@ -1689,9 +1685,9 @@ test("dual non-async HM <-> BT", function(assert) { env.store.createRecord('comment', { post: post }).save().then(function(comment) { - var commentPost = comment.get('post'); - var postComments = comment.get('post.comments'); - var postCommentsLength = comment.get('post.comments.length'); + let commentPost = comment.get('post'); + let postComments = comment.get('post.comments'); + let postCommentsLength = comment.get('post.comments.length'); assert.deepEqual(post, commentPost, 'expect the new comments post, to be the correct post'); assert.ok(postComments, "comments should exist"); @@ -1717,9 +1713,9 @@ test("When an unloaded record is added to the hasMany, it gets fetched once the env.adapter.findRecord = function(store, type, id, snapshot) { return resolve({ data: { id: 3, type: 'comment', attributes: { body: 'third' } } }); }; - var post; + let post; - run(function() { + run(() => { env.store.push({ data: { type: 'post', @@ -1737,8 +1733,8 @@ test("When an unloaded record is added to the hasMany, it gets fetched once the post = env.store.peekRecord('post', 1); }); - run(function() { - post.get('comments').then(assert.wait(function(fetchedComments) { + return run(() => { + return post.get('comments').then(fetchedComments => { assert.equal(fetchedComments.get('length'), 2, 'comments fetched successfully'); assert.equal(fetchedComments.objectAt(0).get('body'), 'first', 'first comment loaded successfully'); env.store.push({ @@ -1756,11 +1752,12 @@ test("When an unloaded record is added to the hasMany, it gets fetched once the } } }); - post.get('comments').then(assert.wait(function(newlyFetchedComments) { + + return post.get('comments').then(newlyFetchedComments => { assert.equal(newlyFetchedComments.get('length'), 3, 'all three comments fetched successfully'); assert.equal(newlyFetchedComments.objectAt(2).get('body'), 'third', 'third comment loaded successfully'); - })); - })); + }); + }); }); }); @@ -1789,9 +1786,10 @@ testInDebug('A sync hasMany errors out if there are unlaoded records in it', fun }); test("If reordered hasMany data has been pushed to the store, the many array reflects the ordering change - sync", function(assert) { - var comment1, comment2, comment3, comment4; - var post; - run(function() { + let comment1, comment2, comment3, comment4; + let post; + + run(() => { env.store.push({ data: [{ type: 'comment', @@ -1814,7 +1812,7 @@ test("If reordered hasMany data has been pushed to the store, the many array ref comment4 = env.store.peekRecord('comment', 4); }); - run(function() { + run(() => { env.store.push({ data: { type: 'post', @@ -1834,7 +1832,7 @@ test("If reordered hasMany data has been pushed to the store, the many array ref assert.deepEqual(post.get('comments').toArray(), [comment1, comment2], 'Initial ordering is correct'); }); - run(function() { + run(() => { env.store.push({ data: { type: 'post', @@ -1852,7 +1850,7 @@ test("If reordered hasMany data has been pushed to the store, the many array ref }); assert.deepEqual(post.get('comments').toArray(), [comment2, comment1], 'Updated ordering is correct'); - run(function() { + run(() => { env.store.push({ data: { type: 'post', @@ -1869,7 +1867,7 @@ test("If reordered hasMany data has been pushed to the store, the many array ref }); assert.deepEqual(post.get('comments').toArray(), [comment2], 'Updated ordering is correct'); - run(function() { + run(() => { env.store.push({ data: { type: 'post', @@ -1889,7 +1887,7 @@ test("If reordered hasMany data has been pushed to the store, the many array ref }); assert.deepEqual(post.get('comments').toArray(), [comment1, comment2, comment3, comment4], 'Updated ordering is correct'); - run(function() { + run(() => { env.store.push({ data: { type: 'post', @@ -1907,7 +1905,7 @@ test("If reordered hasMany data has been pushed to the store, the many array ref }); assert.deepEqual(post.get('comments').toArray(), [comment4, comment3], 'Updated ordering is correct'); - run(function() { + run(() => { env.store.push({ data: { type: 'post', @@ -1925,12 +1923,14 @@ test("If reordered hasMany data has been pushed to the store, the many array ref } }); }); + assert.deepEqual(post.get('comments').toArray(), [comment4, comment2, comment3, comment1], 'Updated ordering is correct'); }); test("Rollbacking attributes for deleted record restores implicit relationship correctly when the hasMany side has been deleted - async", function(assert) { - var book, chapter; - run(function() { + let book, chapter; + + run(() => { env.store.push({ data: { type: 'book', @@ -1957,20 +1957,23 @@ test("Rollbacking attributes for deleted record restores implicit relationship c book = env.store.peekRecord('book', 1); chapter = env.store.peekRecord('chapter', 2); }); - run(function() { + + run(() => { chapter.deleteRecord(); chapter.rollbackAttributes(); }); - run(function() { - book.get('chapters').then(function(fetchedChapters) { + + return run(() => { + return book.get('chapters').then(fetchedChapters => { assert.equal(fetchedChapters.objectAt(0), chapter, 'Book has a chapter after rollback attributes'); }); }); }); test("Rollbacking attributes for deleted record restores implicit relationship correctly when the hasMany side has been deleted - sync", function(assert) { - var book, chapter; - run(function() { + let book, chapter; + + run(() => { env.store.push({ data: { type: 'book', @@ -1997,11 +2000,13 @@ test("Rollbacking attributes for deleted record restores implicit relationship c book = env.store.peekRecord('book', 1); chapter = env.store.peekRecord('chapter', 2); }); - run(function() { + + run(() => { chapter.deleteRecord(); chapter.rollbackAttributes(); }); - run(function() { + + run(() => { assert.equal(book.get('chapters.firstObject'), chapter, "Book has a chapter after rollback attributes"); }); }); @@ -2010,8 +2015,10 @@ test("Rollbacking attributes for deleted record restores implicit relationship c Page.reopen({ chapter: DS.belongsTo('chapter', { async: true }) }); - var chapter, page; - run(function() { + + let chapter, page; + + run(() => { env.store.push({ data: { type: 'chapter', @@ -2036,20 +2043,22 @@ test("Rollbacking attributes for deleted record restores implicit relationship c chapter = env.store.peekRecord('chapter', 2); page = env.store.peekRecord('page', 3); }); - run(function() { + + run(() => { chapter.deleteRecord(); chapter.rollbackAttributes(); }); - run(function() { - page.get('chapter').then(function(fetchedChapter) { + + return run(() => { + return page.get('chapter').then(fetchedChapter => { assert.equal(fetchedChapter, chapter, 'Page has a chapter after rollback attributes'); }); }); }); test("Rollbacking attributes for deleted record restores implicit relationship correctly when the belongsTo side has been deleted - sync", function(assert) { - var chapter, page; - run(function() { + let chapter, page; + run(() => { env.store.push({ data: { type: 'chapter', @@ -2074,21 +2083,23 @@ test("Rollbacking attributes for deleted record restores implicit relationship c chapter = env.store.peekRecord('chapter', 2); page = env.store.peekRecord('page', 3); }); - run(function() { + + run(() => { chapter.deleteRecord(); chapter.rollbackAttributes(); }); - run(function() { + + run(() => { assert.equal(page.get('chapter'), chapter, "Page has a chapter after rollback attributes"); }); }); test("ManyArray notifies the array observers and flushes bindings when removing", function(assert) { assert.expect(2); - var chapter, page, page2; - var observe = false; + let chapter, page, page2; + let observe = false; - run(function() { + run(() => { env.store.push({ data: [{ type: 'page', @@ -2135,7 +2146,8 @@ test("ManyArray notifies the array observers and flushes bindings when removing" } }); }); - run(function() { + + run(() => { observe = true; page2.set('chapter', null); observe = false; @@ -2144,10 +2156,10 @@ test("ManyArray notifies the array observers and flushes bindings when removing" test("ManyArray notifies the array observers and flushes bindings when adding", function(assert) { assert.expect(2); - var chapter, page, page2; - var observe = false; + let chapter, page, page2; + let observe = false; - run(function() { + run(() => { env.store.push({ data: [{ type: 'page', @@ -2193,7 +2205,8 @@ test("ManyArray notifies the array observers and flushes bindings when adding", } }); }); - run(function() { + + run(() => { observe = true; page2.set('chapter', chapter); observe = false; @@ -2203,7 +2216,7 @@ test("ManyArray notifies the array observers and flushes bindings when adding", testInDebug("Passing a model as type to hasMany should not work", function(assert) { assert.expect(1); - assert.expectAssertion(function() { + assert.expectAssertion(() => { User = DS.Model.extend(); Contact = DS.Model.extend({ @@ -2213,7 +2226,7 @@ testInDebug("Passing a model as type to hasMany should not work", function(asser }); test("Relationship.clear removes all records correctly", function(assert) { - var post; + let post; Comment.reopen({ post: DS.belongsTo('post', { async: false }) @@ -2223,7 +2236,7 @@ test("Relationship.clear removes all records correctly", function(assert) { comments: DS.hasMany('comment', { inverse: 'post', async: false }) }); - run(function() { + run(() => { env.store.push({ data: [{ type: 'post', @@ -2268,13 +2281,13 @@ test("Relationship.clear removes all records correctly", function(assert) { post = env.store.peekRecord('post', 2); }); - run(function() { + run(() => { // unclear what the semantics of clearing a yet to be created relationship // ought to be. env.store.peekAll('comment').mapBy('post'); post._internalModel._relationships.get('comments').clear(); - var comments = Ember.A(env.store.peekAll('comment')); + let comments = Ember.A(env.store.peekAll('comment')); assert.deepEqual(comments.mapBy('post'), [null, null, null]); }); @@ -2282,7 +2295,7 @@ test("Relationship.clear removes all records correctly", function(assert) { test('unloading a record with associated records does not prevent the store from tearing down', function(assert) { - var post; + let post; Comment.reopen({ post: DS.belongsTo('post', { async: false }) @@ -2292,7 +2305,7 @@ test('unloading a record with associated records does not prevent the store from comments: DS.hasMany('comment', { inverse: 'post', async: false }) }); - run(function() { + run(() => { env.store.push({ data: [{ type: 'post', @@ -2336,8 +2349,9 @@ test('unloading a record with associated records does not prevent the store from // because records are being removed) env.store.unloadRecord(post); }); + try { - run(function() { + run(() => { env.store.destroy(); }); assert.ok(true, "store destroyed correctly"); @@ -2348,14 +2362,13 @@ test('unloading a record with associated records does not prevent the store from test("adding and removing records from hasMany relationship #2666", function(assert) { assert.expect(4); - let done = assert.async(); - var Post = DS.Model.extend({ + let Post = DS.Model.extend({ comments: DS.hasMany('comment', { async: true }) }); Post.reopenClass({ toString: () => 'Post' }); - var Comment = DS.Model.extend({ + let Comment = DS.Model.extend({ post: DS.belongsTo('post', { async: false }) }); Comment.reopenClass({ toString: () => 'Comment' }); @@ -2368,7 +2381,7 @@ test("adding and removing records from hasMany relationship #2666", function(ass }) }); - var commentId = 4; + let commentId = 4; env.registry.register('adapter:comment', DS.RESTAdapter.extend({ deleteRecord(record) { return Ember.RSVP.resolve(); @@ -2381,7 +2394,7 @@ test("adding and removing records from hasMany relationship #2666", function(ass } })); - run(function() { + run(() => { env.store.push({ data: [{ type: 'post', @@ -2408,32 +2421,32 @@ test("adding and removing records from hasMany relationship #2666", function(ass }); }); - run(function() { - env.store.findRecord('post', 1).then(function (post) { - var comments = post.get('comments'); + return run(() => { + return env.store.findRecord('post', 1).then(post => { + let comments = post.get('comments'); assert.equal(comments.get('length'), 3, "Initial comments count"); // Add comment #4 - var comment = env.store.createRecord('comment'); + let comment = env.store.createRecord('comment'); comments.addObject(comment); - return comment.save().then(function() { - var comments = post.get('comments'); + + return comment.save().then(() => { + let comments = post.get('comments'); assert.equal(comments.get('length'), 4, "Comments count after first add"); // Delete comment #4 return comments.get('lastObject').destroyRecord(); - }).then(function() { - var comments = post.get('comments'); + }).then(() => { + let comments = post.get('comments'); assert.equal(comments.get('length'), 3, "Comments count after destroy"); // Add another comment #4 - var comment = env.store.createRecord('comment'); + let comment = env.store.createRecord('comment'); comments.addObject(comment); return comment.save(); - }).then(function() { - var comments = post.get('comments'); + }).then(() => { + let comments = post.get('comments'); assert.equal(comments.get('length'), 4, "Comments count after second add"); - done(); }); }); }); @@ -2461,9 +2474,9 @@ test("hasMany hasData async loaded", function(assert) { }); }; - run(function() { - store.findRecord('chapter', 1).then(function(chapter) { - var relationship = chapter._internalModel._relationships.get('pages'); + return run(() => { + return store.findRecord('chapter', 1).then(chapter => { + let relationship = chapter._internalModel._relationships.get('pages'); assert.equal(relationship.hasData, true, 'relationship has data'); }); }); @@ -2487,9 +2500,9 @@ test("hasMany hasData sync loaded", function(assert) { }); }; - run(function() { - store.findRecord('chapter', 1).then(function(chapter) { - var relationship = chapter._internalModel._relationships.get('pages'); + return run(() => { + return store.findRecord('chapter', 1).then(chapter => { + let relationship = chapter._internalModel._relationships.get('pages'); assert.equal(relationship.hasData, true, 'relationship has data'); }); }); @@ -2517,9 +2530,9 @@ test("hasMany hasData async not loaded", function(assert) { }); }; - run(function() { - store.findRecord('chapter', 1).then(function(chapter) { - var relationship = chapter._internalModel._relationships.get('pages'); + return run(() => { + return store.findRecord('chapter', 1).then(chapter => { + let relationship = chapter._internalModel._relationships.get('pages'); assert.equal(relationship.hasData, false, 'relationship does not have data'); }); }); @@ -2538,9 +2551,9 @@ test("hasMany hasData sync not loaded", function(assert) { }); }; - run(function() { - store.findRecord('chapter', 1).then(function(chapter) { - var relationship = chapter._internalModel._relationships.get('pages'); + return run(() => { + return store.findRecord('chapter', 1).then(chapter => { + let relationship = chapter._internalModel._relationships.get('pages'); assert.equal(relationship.hasData, false, 'relationship does not have data'); }); }); @@ -2553,9 +2566,9 @@ test("hasMany hasData async created", function(assert) { pages: hasMany('pages', { async: true }) }); - run(function() { - var chapter = store.createRecord('chapter', { title: 'The Story Begins' }); - var relationship = chapter._internalModel._relationships.get('pages'); + run(() => { + let chapter = store.createRecord('chapter', { title: 'The Story Begins' }); + let relationship = chapter._internalModel._relationships.get('pages'); assert.equal(relationship.hasData, true, 'relationship has data'); }); }); @@ -2563,16 +2576,16 @@ test("hasMany hasData async created", function(assert) { test("hasMany hasData sync created", function(assert) { assert.expect(1); - run(function() { - var chapter = store.createRecord('chapter', { title: 'The Story Begins' }); - var relationship = chapter._internalModel._relationships.get('pages'); + run(() => { + let chapter = store.createRecord('chapter', { title: 'The Story Begins' }); + let relationship = chapter._internalModel._relationships.get('pages'); assert.equal(relationship.hasData, true, 'relationship has data'); }); }); test("Model's hasMany relationship should not be created during model creation", function(assert) { - var user; - run(function () { + let user; + run(() => { env.store.push({ data: { type: 'user', @@ -2585,8 +2598,8 @@ test("Model's hasMany relationship should not be created during model creation", }); test("Model's belongsTo relationship should be created during 'get' method", function(assert) { - var user; - run(function () { + let user; + run(() => { user = env.store.createRecord('user'); user.get('messages'); assert.ok(user._internalModel._relationships.has('messages'), "Newly created record with relationships in params passed in its constructor should have relationships"); @@ -2595,12 +2608,12 @@ test("Model's belongsTo relationship should be created during 'get' method", fun test("metadata is accessible when pushed as a meta property for a relationship", function(assert) { assert.expect(1); - var book; + let book; env.adapter.findHasMany = function() { return resolve({}); }; - run(function() { + run(() => { env.store.push({ data: { type: 'book', @@ -2623,7 +2636,7 @@ test("metadata is accessible when pushed as a meta property for a relationship", book = env.store.peekRecord('book', 1); }); - run(function() { + run(() => { assert.equal(book._internalModel._relationships.get('chapters').meta.where, 'the lefkada sea', 'meta is there'); }); }); @@ -2644,9 +2657,9 @@ test("metadata is accessible when return from a fetchLink", function(assert) { }); }; - var book; + let book; - run(function() { + run(() => { env.store.push({ data: { type: 'book', @@ -2666,20 +2679,20 @@ test("metadata is accessible when return from a fetchLink", function(assert) { book = env.store.peekRecord('book', 1); }); - run(function() { - book.get('chapters').then(function(chapters) { - var meta = chapters.get('meta'); + return run(() => { + return book.get('chapters').then(chapters => { + let meta = chapters.get('meta'); assert.equal(get(meta, 'foo'), 'bar', 'metadata is available'); }); }); }); test("metadata should be reset between requests", function(assert) { - var counter = 0; + let counter = 0; env.registry.register('serializer:application', DS.RESTSerializer); env.adapter.findHasMany = function() { - var data = { + let data = { meta: { foo: 'bar' }, @@ -2700,9 +2713,9 @@ test("metadata should be reset between requests", function(assert) { return resolve(data); }; - var book1, book2; + let book1, book2; - run(function() { + run(() => { env.store.push({ data: [{ type: 'book', @@ -2736,13 +2749,13 @@ test("metadata should be reset between requests", function(assert) { book2 = env.store.peekRecord('book', 2); }); - run(function() { - book1.get('chapters').then(function(chapters) { - var meta = chapters.get('meta'); + return run(() => { + return book1.get('chapters').then(chapters => { + let meta = chapters.get('meta'); assert.equal(get(meta, 'foo'), 'bar', 'metadata should available'); - book2.get('chapters').then(function(chapters) { - var meta = chapters.get('meta'); + return book2.get('chapters').then(chapters => { + let meta = chapters.get('meta'); assert.equal(meta, undefined, 'metadata should not be available'); }); }); @@ -2764,7 +2777,7 @@ test("Related link should be fetched when no local data is present", function(as ]}); }; - run(function() { + return run(() => { let post = env.store.push({ data: { type: 'post', @@ -2778,7 +2791,8 @@ test("Related link should be fetched when no local data is present", function(as } } }); - post.get('comments').then((comments) => { + + return post.get('comments').then(comments => { assert.equal(comments.get('firstObject.body'), 'This is comment', 'comment body is correct'); }); }); @@ -2799,7 +2813,7 @@ test("Local data should take precedence over related link", function(assert) { return Ember.RSVP.resolve({ data: { id: 1, type: 'comment', attributes: { body: 'This is comment' } } }); }; - run(function() { + return run(() => { let post = env.store.push({ data: { type: 'post', @@ -2816,7 +2830,8 @@ test("Local data should take precedence over related link", function(assert) { } } }); - post.get('comments').then((comments) => { + + return post.get('comments').then(comments => { assert.equal(comments.get('firstObject.body'), 'This is comment', 'comment body is correct'); }); }); @@ -2841,7 +2856,7 @@ test("Updated related link should take precedence over local data", function(ass assert.ok(false, "The adapter's findRecord method should not be called"); }; - run(function() { + return run(() => { let post = env.store.push({ data: { type: 'post', @@ -2873,7 +2888,7 @@ test("Updated related link should take precedence over local data", function(ass } }); - post.get('comments').then((comments) => { + return post.get('comments').then(comments => { assert.equal(comments.get('firstObject.body'), 'This is comment', 'comment body is correct'); }); }); @@ -2893,8 +2908,8 @@ test("PromiseArray proxies createRecord to its ManyArray before the hasMany is l ]}); }; - run(function() { - var post = env.store.push({ + return run(() => { + let post = env.store.push({ data: { type: 'post', id: 1, @@ -2908,19 +2923,19 @@ test("PromiseArray proxies createRecord to its ManyArray before the hasMany is l } }); - var comments = post.get('comments'); + let comments = post.get('comments'); comments.createRecord(); - comments.then(function(comments) { + return comments.then(comments => { assert.equal(comments.get('length'), 3, "comments have 3 length, including new record"); }); }); }); test("unloading and reloading a record with hasMany relationship - #3084", function(assert) { - var user; - var message; + let user; + let message; - run(function() { + run(() => { env.store.push({ data: [{ type: 'user', @@ -2948,11 +2963,11 @@ test("unloading and reloading a record with hasMany relationship - #3084", funct assert.equal(get(message, 'user.id'), 'user-1'); }); - run(function() { + run(() => { env.store.unloadRecord(user); }); - run(function() { + run(() => { // The record is resurrected for some reason. env.store.push({ data: [{ @@ -2979,14 +2994,14 @@ test("unloading and reloading a record with hasMany relationship - #3084", funct }); test("deleted records should stay deleted", function(assert) { - var user; - var message; + let user; + let message; env.adapter.deleteRecord = function(store, type, id) { return null; }; - run(function() { + run(() => { env.store.push({ data: [{ type: 'user', @@ -3017,11 +3032,9 @@ test("deleted records should stay deleted", function(assert) { assert.equal(get(user, 'messages.length'), 2); }); - run(function() { - message.destroyRecord() - }); + run(() => message.destroyRecord()); - run(function() { + run(() => { // a new message is added to the user should not resurrected the // deleted message env.store.push({ diff --git a/tests/integration/relationships/many-to-many-test.js b/tests/integration/relationships/many-to-many-test.js index f2549882e8a..ae3016fe73f 100644 --- a/tests/integration/relationships/many-to-many-test.js +++ b/tests/integration/relationships/many-to-many-test.js @@ -7,11 +7,10 @@ import {module, test} from 'qunit'; import DS from 'ember-data'; -var Account, Topic, User, store, env; -var run = Ember.run; +const { run } = Ember; +const { attr, hasMany } = DS; -var attr = DS.attr; -var hasMany = DS.hasMany; +let Account, Topic, User, store, env; module('integration/relationships/many_to_many_test - ManyToMany relationships', { beforeEach() { @@ -44,9 +43,7 @@ module('integration/relationships/many_to_many_test - ManyToMany relationships', }, afterEach() { - run(function() { - env.container.destroy(); - }); + run(() => env.container.destroy()); } }); @@ -55,7 +52,7 @@ module('integration/relationships/many_to_many_test - ManyToMany relationships', */ test("Loading from one hasMany side reflects on the other hasMany side - async", function(assert) { - run(function() { + run(() => { store.push({ data: { id: '1', @@ -78,7 +75,7 @@ test("Loading from one hasMany side reflects on the other hasMany side - async", }); }); - var topic = run(function() { + let topic = run(() => { return store.push({ data: { id: '2', @@ -90,17 +87,16 @@ test("Loading from one hasMany side reflects on the other hasMany side - async", }); }); - run(function() { - topic.get('users').then(assert.wait(function(fetchedUsers) { + return run(() => { + return topic.get('users').then(fetchedUsers => { assert.equal(fetchedUsers.get('length'), 1, 'User relationship was set up correctly'); - })); + }); }); }); - test("Relationship is available from one hasMany side even if only loaded from the other hasMany side - sync", function(assert) { var account; - run(function() { + run(() => { account = store.push({ data: { id: '2', @@ -129,14 +125,15 @@ test("Relationship is available from one hasMany side even if only loaded from t }); }); - run(function() { + run(() => { assert.equal(account.get('users.length'), 1, 'User relationship was set up correctly'); }); }); test("Fetching a hasMany where a record was removed reflects on the other hasMany side - async", function(assert) { - var user, topic; - run(function() { + let user, topic; + + run(() => { user = store.push({ data: { id: '1', @@ -169,21 +166,22 @@ test("Fetching a hasMany where a record was removed reflects on the other hasMan } }); }); - run(function() { - user.get('topics').then(assert.wait(function(fetchedTopics) { + + return run(() => { + return user.get('topics').then(fetchedTopics => { assert.equal(fetchedTopics.get('length'), 0, 'Topics were removed correctly'); assert.equal(fetchedTopics.objectAt(0), null, "Topics can't be fetched"); - topic.get('users').then(assert.wait(function(fetchedUsers) { + return topic.get('users').then(fetchedUsers => { assert.equal(fetchedUsers.get('length'), 0, 'Users were removed correctly'); assert.equal(fetchedUsers.objectAt(0), null, "User can't be fetched"); - })); - })); + }); + }); }); }); test("Fetching a hasMany where a record was removed reflects on the other hasMany side - sync", function(assert) { - var account, user; - run(function() { + let account, user; + run(() => { account = store.push({ data: { id: '2', @@ -226,7 +224,7 @@ test("Fetching a hasMany where a record was removed reflects on the other hasMan }); }); - run(function() { + run(() => { assert.equal(user.get('accounts.length'), 0, 'Accounts were removed correctly'); assert.equal(account.get('users.length'), 0, 'Users were removed correctly'); }); @@ -238,9 +236,9 @@ test("Fetching a hasMany where a record was removed reflects on the other hasMan test("Pushing to a hasMany reflects on the other hasMany side - async", function(assert) { assert.expect(1); - var user, topic; + let user, topic; - run(function() { + run(() => { user = store.push({ data: { id: '1', @@ -266,19 +264,19 @@ test("Pushing to a hasMany reflects on the other hasMany side - async", function }); }); - run(function() { - topic.get('users').then(assert.wait(function(fetchedUsers) { + return run(() => { + return topic.get('users').then(fetchedUsers => { fetchedUsers.pushObject(user); - user.get('topics').then(assert.wait(function(fetchedTopics) { + return user.get('topics').then(fetchedTopics => { assert.equal(fetchedTopics.get('length'), 1, 'User relationship was set up correctly'); - })); - })); + }); + }); }); }); test("Pushing to a hasMany reflects on the other hasMany side - sync", function(assert) { - var account, stanley; - run(function() { + let account, stanley; + run(() => { account = store.push({ data: { id: '2', @@ -300,14 +298,14 @@ test("Pushing to a hasMany reflects on the other hasMany side - sync", function( stanley.get('accounts').pushObject(account); }); - run(function() { + run(() => { assert.equal(account.get('users.length'), 1, 'User relationship was set up correctly'); }); }); test("Removing a record from a hasMany reflects on the other hasMany side - async", function(assert) { - var user, topic; - run(function() { + let user, topic; + run(() => { user = store.push({ data: { id: '1', @@ -336,21 +334,21 @@ test("Removing a record from a hasMany reflects on the other hasMany side - asyn }); }); - run(function() { - user.get('topics').then(assert.wait(function(fetchedTopics) { + return run(() => { + return user.get('topics').then(fetchedTopics => { assert.equal(fetchedTopics.get('length'), 1, 'Topics were setup correctly'); fetchedTopics.removeObject(topic); - topic.get('users').then(assert.wait(function(fetchedUsers) { + return topic.get('users').then(fetchedUsers => { assert.equal(fetchedUsers.get('length'), 0, 'Users were removed correctly'); assert.equal(fetchedUsers.objectAt(0), null, "User can't be fetched"); - })); - })); + }); + }); }); }); test("Removing a record from a hasMany reflects on the other hasMany side - sync", function(assert) { - var account, user; - run(function() { + let account, user; + run(() => { account = store.push({ data: { id: '2', @@ -379,7 +377,7 @@ test("Removing a record from a hasMany reflects on the other hasMany side - sync }); }); - run(function() { + run(() => { assert.equal(account.get('users.length'), 1, 'Users were setup correctly'); account.get('users').removeObject(user); assert.equal(user.get('accounts.length'), 0, 'Accounts were removed correctly'); @@ -392,8 +390,8 @@ test("Removing a record from a hasMany reflects on the other hasMany side - sync */ test("Rollbacking attributes for a deleted record that has a ManyToMany relationship works correctly - async", function(assert) { - var user, topic; - run(function() { + let user, topic; + run(() => { user = store.push({ data: { id: '1', @@ -422,23 +420,30 @@ test("Rollbacking attributes for a deleted record that has a ManyToMany relation }); }); - run(function() { + run(() => { topic.deleteRecord(); topic.rollbackAttributes(); }); - run(function() { - topic.get('users').then(assert.wait(function(fetchedUsers) { + + return run(() => { + let users = topic.get('users').then(fetchedUsers => { assert.equal(fetchedUsers.get('length'), 1, 'Users are still there'); - })); - user.get('topics').then(assert.wait(function(fetchedTopics) { + }); + + let topics = user.get('topics').then(fetchedTopics => { assert.equal(fetchedTopics.get('length'), 1, 'Topic got rollbacked into the user'); - })); + }); + + return Ember.RSVP.Promise.all([ + users, + topics + ]); }); }); test("Deleting a record that has a hasMany relationship removes it from the otherMany array but does not remove the other record from itself - sync", function(assert) { - var account, user; - run(function() { + let account, user; + run(() => { account = store.push({ data: { id: '2', @@ -467,7 +472,7 @@ test("Deleting a record that has a hasMany relationship removes it from the othe }); }); - run(function() { + run(() => { account.deleteRecord(); account.rollbackAttributes(); assert.equal(account.get('users.length'), 1, 'Users are still there'); @@ -476,8 +481,8 @@ test("Deleting a record that has a hasMany relationship removes it from the othe }); test("Rollbacking attributes for a created record that has a ManyToMany relationship works correctly - async", function(assert) { - var user, topic; - run(function() { + let user, topic; + run(() => { user = store.push({ data: { id: '1', @@ -490,25 +495,33 @@ test("Rollbacking attributes for a created record that has a ManyToMany relation topic = store.createRecord('topic'); }); - run(function() { - user.get('topics').then(assert.wait(function(fetchedTopics) { + + return run(() => { + return user.get('topics').then(fetchedTopics => { fetchedTopics.pushObject(topic); topic.rollbackAttributes(); - topic.get('users').then(assert.wait(function(fetchedUsers) { + + let users = topic.get('users').then(fetchedUsers => { assert.equal(fetchedUsers.get('length'), 0, 'Users got removed'); assert.equal(fetchedUsers.objectAt(0), null, "User can't be fetched"); - })); - user.get('topics').then(assert.wait(function(fetchedTopics) { + }); + + let topics = user.get('topics').then(fetchedTopics => { assert.equal(fetchedTopics.get('length'), 0, 'Topics got removed'); assert.equal(fetchedTopics.objectAt(0), null, "Topic can't be fetched"); - })); - })); + }); + + return Ember.RSVP.Promise.all([ + users, + topics + ]); + }); }); }); test("Deleting a record that has a hasMany relationship removes it from the otherMany array but does not remove the other record from itself - sync", function(assert) { - var account, user; - run(function() { + let account, user; + run(() => { account = store.push({ data: { id: '2', @@ -521,18 +534,20 @@ test("Deleting a record that has a hasMany relationship removes it from the othe user = store.createRecord('user'); }); - run(function() { + + run(() => { account.get('users').pushObject(user); user.rollbackAttributes(); }); + assert.equal(account.get('users.length'), 0, 'Users got removed'); assert.equal(user.get('accounts.length'), 0, 'Accounts got rolledback correctly'); }); - test("Re-loading a removed record should re add it to the relationship when the removed record is the last one in the relationship", function(assert) { - var account, ada, byron; - run(function() { + let account, ada, byron; + + run(() => { account = store.push({ data: { id: '2', @@ -599,6 +614,5 @@ test("Re-loading a removed record should re add it to the relationship when the }); }); - assert.equal(account.get('users.length'), 2, 'Accounts were updated correctly'); });