Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ON_HOLD] feat: custom decorators #7477

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 125 additions & 31 deletions packages/-ember-data/tests/integration/adapter/build-url-mixin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import deepCopy from '@ember-data/unpublished-test-infra/test-support/deep-copy'
module('integration/adapter/build-url-mixin - BuildURLMixin with RESTAdapter', function(hooks) {
setupTest(hooks);

let store, adapter, Post, Comment, passedUrl;
let store, adapter, passedUrl;

function ajaxResponse(value) {
adapter.ajax = function(url, verb, hash) {
Expand All @@ -24,32 +24,31 @@ module('integration/adapter/build-url-mixin - BuildURLMixin with RESTAdapter', f
};
}

class Post extends Model {
@attr name;
}
class Comment extends Model {
@attr name;
}

hooks.beforeEach(function() {
let { owner } = this;
const PostModel = Model.extend({
name: attr('string'),
});
const CommentModel = Model.extend({
name: attr('string'),
});
const SuperUser = Model.extend({});

owner.register('adapter:application', RESTAdapter.extend());
owner.register('serializer:application', RESTSerializer.extend());
owner.register('model:comment', CommentModel);
owner.register('model:post', PostModel);
owner.register('model:super-user', SuperUser);

store = owner.lookup('service:store');
adapter = store.adapterFor('application');

Post = store.modelFor('post');
Comment = store.modelFor('comment');

passedUrl = null;
});

test('buildURL - with host and namespace', async function(assert) {
this.owner.register('model:comment', Comment);
this.owner.register('model:post', Post);

adapter.setProperties({
host: 'http://example.com',
namespace: 'api/v1',
Expand All @@ -68,8 +67,16 @@ module('integration/adapter/build-url-mixin - BuildURLMixin with RESTAdapter', f
namespace: 'api/v1',
});

Post.reopen({ comments: hasMany('comment', { async: true }) });
Comment.reopen({ post: belongsTo('post', { async: false }) });
class Post extends Model {
@attr name;
@hasMany('comment', { async: true }) comments;
}
class Comment extends Model {
@attr name;
@belongsTo('post', { async: false }) post;
}
this.owner.register('model:comment', Comment);
this.owner.register('model:post', Post);

ajaxResponse({ posts: [{ id: 1, links: { comments: 'comments' } }] });

Expand All @@ -86,8 +93,16 @@ module('integration/adapter/build-url-mixin - BuildURLMixin with RESTAdapter', f
namespace: 'api/v1',
});

Post.reopen({ comments: hasMany('comment', { async: true }) });
Comment.reopen({ post: belongsTo('post', { async: false }) });
class Post extends Model {
@attr name;
@hasMany('comment', { async: true }) comments;
}
class Comment extends Model {
@attr name;
@belongsTo('post', { async: false }) post;
}
this.owner.register('model:comment', Comment);
this.owner.register('model:post', Post);

ajaxResponse({ posts: [{ id: 1, links: { comments: '/api/v1/posts/1/comments' } }] });

Expand All @@ -103,8 +118,17 @@ module('integration/adapter/build-url-mixin - BuildURLMixin with RESTAdapter', f
host: '//example.com',
namespace: 'api/v1',
});
Post.reopen({ comments: hasMany('comment', { async: true }) });
Comment.reopen({ post: belongsTo('post', { async: false }) });

class Post extends Model {
@attr name;
@hasMany('comment', { async: true }) comments;
}
class Comment extends Model {
@attr name;
@belongsTo('post', { async: false }) post;
}
this.owner.register('model:comment', Comment);
this.owner.register('model:post', Post);

ajaxResponse({ posts: [{ id: 1, links: { comments: '/api/v1/posts/1/comments' } }] });

Expand All @@ -120,8 +144,17 @@ module('integration/adapter/build-url-mixin - BuildURLMixin with RESTAdapter', f
host: '/',
namespace: 'api/v1',
});
Post.reopen({ comments: hasMany('comment', { async: true }) });
Comment.reopen({ post: belongsTo('post', { async: false }) });

class Post extends Model {
@attr name;
@hasMany('comment', { async: true }) comments;
}
class Comment extends Model {
@attr name;
@belongsTo('post', { async: false }) post;
}
this.owner.register('model:comment', Comment);
this.owner.register('model:post', Post);

ajaxResponse({ posts: [{ id: 1, links: { comments: '/api/v1/posts/1/comments' } }] });

Expand All @@ -137,8 +170,16 @@ module('integration/adapter/build-url-mixin - BuildURLMixin with RESTAdapter', f
host: 'http://example.com',
namespace: 'api/v1',
});
Post.reopen({ comments: hasMany('comment', { async: true }) });
Comment.reopen({ post: belongsTo('post', { async: false }) });
class Post extends Model {
@attr name;
@hasMany('comment', { async: true }) comments;
}
class Comment extends Model {
@attr name;
@belongsTo('post', { async: false }) post;
}
this.owner.register('model:comment', Comment);
this.owner.register('model:post', Post);

ajaxResponse({
posts: [
Expand Down Expand Up @@ -171,7 +212,15 @@ module('integration/adapter/build-url-mixin - BuildURLMixin with RESTAdapter', f
});

test('buildURL - buildURL takes a record from find', async function(assert) {
Comment.reopen({ post: belongsTo('post', { async: false }) });
class Comment extends Model {
@attr name;
@belongsTo('post', { async: false }) post;
}
class Post extends Model {
@attr name;
}
this.owner.register('model:comment', Comment);
this.owner.register('model:post', Post);

adapter.buildURL = function(type, id, snapshot) {
return '/posts/' + snapshot.belongsTo('post', { id: true }) + '/comments/' + snapshot.id;
Expand All @@ -186,14 +235,22 @@ module('integration/adapter/build-url-mixin - BuildURLMixin with RESTAdapter', f
},
});

await store.findRecord('comment', 1, { preload: { post } });
await store.findRecord('comment', '1', { preload: { post } });

assert.equal(passedUrl, '/posts/2/comments/1');
});

test('buildURL - buildURL takes the records from findMany', async function(assert) {
Comment.reopen({ post: belongsTo('post', { async: false }) });
Post.reopen({ comments: hasMany('comment', { async: true }) });
class Comment extends Model {
@attr name;
@belongsTo('post', { async: false }) post;
}
class Post extends Model {
@attr name;
@hasMany('comment', { async: true }) comments;
}
this.owner.register('model:comment', Comment);
this.owner.register('model:post', Post);

adapter.buildURL = function(type, ids, snapshots) {
if (Array.isArray(snapshots)) {
Expand Down Expand Up @@ -225,7 +282,16 @@ module('integration/adapter/build-url-mixin - BuildURLMixin with RESTAdapter', f
});

test('buildURL - buildURL takes a record from create', async function(assert) {
Comment.reopen({ post: belongsTo('post', { async: false }) });
class Post extends Model {
@attr name;
@hasMany('comment', { async: true }) comments;
}
class Comment extends Model {
@attr name;
@belongsTo('post', { async: false }) post;
}
this.owner.register('model:comment', Comment);
this.owner.register('model:post', Post);
adapter.buildURL = function(type, id, snapshot) {
return '/posts/' + snapshot.belongsTo('post', { id: true }) + '/comments/';
};
Expand All @@ -245,7 +311,16 @@ module('integration/adapter/build-url-mixin - BuildURLMixin with RESTAdapter', f
});

test('buildURL - buildURL takes a record from create to query a resolved async belongsTo relationship', async function(assert) {
Comment.reopen({ post: belongsTo('post', { async: true }) });
class Comment extends Model {
@attr name;
@belongsTo('post', { async: true }) post;
}
class Post extends Model {
@attr name;
@hasMany('comment', { async: true }) comments;
}
this.owner.register('model:comment', Comment);
this.owner.register('model:post', Post);
adapter.buildURL = function(type, id, snapshot) {
return '/posts/' + snapshot.belongsTo('post', { id: true }) + '/comments/';
};
Expand All @@ -271,7 +346,16 @@ module('integration/adapter/build-url-mixin - BuildURLMixin with RESTAdapter', f
});

test('buildURL - buildURL takes a record from update', async function(assert) {
Comment.reopen({ post: belongsTo('post', { async: false }) });
class Comment extends Model {
@attr name;
@belongsTo('post', { async: false }) post;
}
class Post extends Model {
@attr name;
@hasMany('comment', { async: true }) comments;
}
this.owner.register('model:comment', Comment);
this.owner.register('model:post', Post);
adapter.buildURL = function(type, id, snapshot) {
return '/posts/' + snapshot.belongsTo('post', { id: true }) + '/comments/' + snapshot.id;
};
Expand All @@ -297,8 +381,17 @@ module('integration/adapter/build-url-mixin - BuildURLMixin with RESTAdapter', f
});

test('buildURL - buildURL takes a record from delete', async function(assert) {
Comment.reopen({ post: belongsTo('post', { async: false }) });
Post.reopen({ comments: hasMany('comment', { async: false }) });
class Post extends Model {
@attr name;
@hasMany('comment', { async: false }) comments;
}
class Comment extends Model {
@attr name;
@belongsTo('post', { async: false }) post;
}
this.owner.register('model:comment', Comment);
this.owner.register('model:post', Post);

adapter.buildURL = function(type, id, snapshot) {
return 'posts/' + snapshot.belongsTo('post', { id: true }) + '/comments/' + snapshot.id;
};
Expand Down Expand Up @@ -326,6 +419,7 @@ module('integration/adapter/build-url-mixin - BuildURLMixin with RESTAdapter', f
});

test('buildURL - with absolute namespace', async function(assert) {
this.owner.register('model:post', Post);
adapter.setProperties({
namespace: '/api/v1',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,43 +110,52 @@ module('integration/records/property-changes - Property changes', function(hooks
});
});

test('Saving a record trigger observers for locally changed attributes with the same canonical value', function(assert) {
assert.expect(1);
var person;
test('Saving a record trigger observers for locally changed attributes with the same canonical value', async function(assert) {
assert.expect(3);

let store = this.owner.lookup('service:store');
let adapter = store.adapterFor('application');
let observerCount = 0;

adapter.updateRecord = function(store, type, snapshot) {
return resolve({ data: { id: 'wat', type: 'person', attributes: { 'last-name': 'Katz' } } });
};

run(function() {
store.push({
return resolve({
data: {
type: 'person',
id: 'wat',
type: 'person',
attributes: {
firstName: 'Yehuda',
lastName: 'Katz',
'last-name': 'Katz',
},
},
});
person = store.peekRecord('person', 'wat');
person.set('lastName', 'Katz!');
};

let person = store.push({
data: {
type: 'person',
id: 'wat',
attributes: {
firstName: 'Yehuda',
lastName: 'Katz',
},
},
});

person.addObserver('firstName', function() {
assert.ok(false, 'firstName observer should not be triggered');
});

person.addObserver('lastName', function() {
assert.ok(true, 'lastName observer should be triggered');
observerCount++;
});

run(function() {
person.save();
});
person.set('lastName', 'Katz!');

assert.strictEqual(observerCount, 1, 'lastName observer should be triggered');

await person.save();

assert.strictEqual(observerCount, 2, 'lastName observer should be triggered');
assert.strictEqual(person.lastName, 'Katz', 'We changed back to canonical');
});

test('store.push should not override a modified attribute', function(assert) {
Expand Down
Loading