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

[CHORE]: Modernize json-api-adapter-test #7628

Merged
merged 1 commit into from
Apr 15, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,59 @@ import { resolve } from 'rsvp';
import DS from 'ember-data';
import { setupTest } from 'ember-qunit';

import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
import testInDebug from '@ember-data/unpublished-test-infra/test-support/test-in-debug';

let store, adapter;
let passedUrl, passedVerb, passedHash;

let User, Post, Comment, Handle, GithubHandle, TwitterHandle, Company, DevelopmentShop, DesignStudio;

module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks) {
setupTest(hooks);

hooks.beforeEach(function () {
User = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
posts: DS.hasMany('post', { async: true }),
handles: DS.hasMany('handle', { async: true, polymorphic: true }),
company: DS.belongsTo('company', { async: true, polymorphic: true }),
});

Post = DS.Model.extend({
title: DS.attr('string'),
author: DS.belongsTo('user', { async: true }),
comments: DS.hasMany('comment', { async: true }),
});

Comment = DS.Model.extend({
text: DS.attr('string'),
post: DS.belongsTo('post', { async: true }),
});

Handle = DS.Model.extend({
user: DS.belongsTo('user', { async: true }),
});

GithubHandle = Handle.extend({
username: DS.attr('string'),
});

TwitterHandle = Handle.extend({
nickname: DS.attr('string'),
});

Company = DS.Model.extend({
name: DS.attr('string'),
employees: DS.hasMany('user', { async: true }),
});

DevelopmentShop = Company.extend({
coffee: DS.attr('boolean'),
});

DesignStudio = Company.extend({
hipsters: DS.attr('number'),
});
class User extends Model {
@attr('string') firstName;
@attr('string') lastName;
@hasMany('post', { async: true }) posts;
@hasMany('handle', { async: true, polymorphic: true }) handles;
@belongsTo('company', { async: true, polymorphic: true }) company;
}

class Post extends Model {
@attr('string') title;
@belongsTo('user', { async: true }) author;
@hasMany('comment', { async: true }) comments;
}

class Comment extends Model {
@attr('string') text;
@belongsTo('post', { async: true }) post;
}

class Handle extends Model {
@belongsTo('user', { async: true }) user;
}

class GithubHandle extends Handle {
@attr('string') username;
}

class TwitterHandle extends Handle {
@attr('string') nickname;
}

class Company extends Model {
@attr('string') name;
@hasMany('user', { async: true }) employees;
}

class DevelopmentShop extends Company {
@attr('boolean') coffee;
}

class DesignStudio extends Company {
@attr('number') hipsters;
}

this.owner.register('adapter:application', DS.JSONAPIAdapter.extend());
this.owner.register('serializer:application', DS.JSONAPISerializer.extend());
Expand Down Expand Up @@ -113,8 +112,8 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
let post = await store.findRecord('post', '1');

assert.strictEqual(passedUrl[0], '/posts/1', 'Builds URL correctly');
assert.strictEqual(post.get('id'), '1', 'Stores record with correct id');
assert.strictEqual(post.get('title'), 'Ember.js rocks', 'Title for record is correct');
assert.strictEqual(post.id, '1', 'Stores record with correct id');
assert.strictEqual(post.title, 'Ember.js rocks', 'Title for record is correct');
});

test('find all records with sideloaded relationships', async function (assert) {
Expand Down Expand Up @@ -185,30 +184,30 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)

assert.strictEqual(passedUrl[0], '/posts');

assert.strictEqual(posts.get('length'), 2, 'Returns two post records');
assert.strictEqual(posts.get('firstObject.title'), 'Ember.js rocks', 'The title for the first post is correct');
assert.strictEqual(posts.get('lastObject.title'), 'Tomster rules', 'The title for the second post is correct');
assert.strictEqual(posts.length, 2, 'Returns two post records');
assert.strictEqual(posts.firstObject.title, 'Ember.js rocks', 'The title for the first post is correct');
assert.strictEqual(posts.lastObject.title, 'Tomster rules', 'The title for the second post is correct');

assert.strictEqual(
posts.get('firstObject.author.firstName'),
posts.firstObject.author.get('firstName'),
'Yehuda',
'The author for the first post is loaded and has the correct first name'
);
assert.strictEqual(
posts.get('lastObject.author.lastName'),
posts.lastObject.author.get('lastName'),
'Katz',
'The author for the last post is loaded and has the correct last name'
);

assert.strictEqual(posts.get('firstObject.comments.length'), 0, 'First post doesnt have comments');
assert.strictEqual(posts.firstObject.comments.length, 0, 'First post doesnt have comments');

assert.strictEqual(
posts.get('lastObject.comments.firstObject.text'),
posts.lastObject.comments.firstObject.text,
'This is the first comment',
'Loads first comment for second post'
);
assert.strictEqual(
posts.get('lastObject.comments.lastObject.text'),
posts.lastObject.comments.lastObject.text,
'This is the second comment',
'Loads second comment for second post'
);
Expand All @@ -231,13 +230,13 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
},
]);

let posts = await store.query('post', { filter: { id: 1 } });
let posts = await store.query('post', { filter: { id: '1' } });

assert.strictEqual(passedUrl[0], '/posts', 'Builds correct URL');
assert.deepEqual(passedHash[0], { data: { filter: { id: 1 } } }, 'Sends correct params to adapter');
assert.deepEqual(passedHash[0], { data: { filter: { id: '1' } } }, 'Sends correct params to adapter');

assert.strictEqual(posts.get('length'), 1, 'Returns the correct number of records');
assert.strictEqual(posts.get('firstObject.title'), 'Ember.js rocks', 'Sets correct title to record');
assert.strictEqual(posts.length, 1, 'Returns the correct number of records');
assert.strictEqual(posts.firstObject.title, 'Ember.js rocks', 'Sets correct title to record');
});

test('queryRecord - primary data being a single record', async function (assert) {
Expand All @@ -256,7 +255,7 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
let post = await store.queryRecord('post', {});

assert.strictEqual(passedUrl[0], '/posts', 'Builds correc URL');
assert.strictEqual(post.get('title'), 'Ember.js rocks', 'Sets correct title to record');
assert.strictEqual(post.title, 'Ember.js rocks', 'Sets correct title to record');
});

test('queryRecord - primary data being null', async function (assert) {
Expand Down Expand Up @@ -328,7 +327,7 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
assert.strictEqual(post.id, '1', 'Stores record using the correct id');
assert.strictEqual(post.title, 'Ember.js rocks', 'Sets correct title to record');

let author = await post.get('author');
let author = await post.author;

assert.strictEqual(
passedUrl[1],
Expand Down Expand Up @@ -378,7 +377,7 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
assert.strictEqual(post.id, '1', 'Stores record using the correct id');
assert.strictEqual(post.title, 'Ember.js rocks', 'Sets correct title to record');

let author = await post.get('author');
let author = await post.author;

assert.strictEqual(passedUrl[1], '/users/2', 'The relationship user:2 was fetched by the correct url');
assert.strictEqual(author.id, '2', 'Record has correct id');
Expand Down Expand Up @@ -425,7 +424,7 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
assert.strictEqual(user.firstName, 'Yehuda', 'Sets correct firstName to record');
assert.strictEqual(user.lastName, 'Katz', 'Sets correct lastName to record');

let company = await user.get('company');
let company = await user.company;

assert.strictEqual(
passedUrl[1],
Expand Down Expand Up @@ -475,7 +474,7 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
assert.strictEqual(post.id, '1', 'Record has correct id');
assert.strictEqual(post.title, 'Ember.js rocks', 'Title is set correctly');

let author = await post.get('author');
let author = await post.author;

assert.strictEqual(passedUrl.length, 1);

Expand Down Expand Up @@ -530,16 +529,16 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
assert.strictEqual(post.id, '1');
assert.strictEqual(post.title, 'Ember.js rocks');

let comments = await post.get('comments');
let comments = await post.comments;

assert.strictEqual(
passedUrl[1],
'http://example.com/post/1/comments',
'The related records comments using correct url'
);
assert.strictEqual(comments.length, 2, 'Loads the correct number of comments from response');
assert.strictEqual(comments.get('firstObject.text'), 'This is the first comment', 'First comment text is correct');
assert.strictEqual(comments.get('lastObject.text'), 'This is the second comment', 'Second comment text is correct');
assert.strictEqual(comments.firstObject.text, 'This is the first comment', 'First comment text is correct');
assert.strictEqual(comments.lastObject.text, 'This is the second comment', 'Second comment text is correct');
});

test('find a single record with hasMany link as object { data }', async function (assert) {
Expand Down Expand Up @@ -589,13 +588,13 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
assert.strictEqual(post.id, '1', 'Record id is correct');
assert.strictEqual(post.title, 'Ember.js rocks', 'Record title is correct');

let comments = await post.get('comments');
let comments = await post.comments;

assert.strictEqual(passedUrl[1], '/comments/2', 'Builds correct URL to fetch related record');
assert.strictEqual(passedUrl[2], '/comments/3', 'Builds correct URL to fetch related record');
assert.strictEqual(comments.length, 2);
assert.strictEqual(comments.get('firstObject.text'), 'This is the first comment', 'First comment text is correct');
assert.strictEqual(comments.get('lastObject.text'), 'This is the second comment', 'Second comment text is correct');
assert.strictEqual(comments.firstObject.text, 'This is the first comment', 'First comment text is correct');
assert.strictEqual(comments.lastObject.text, 'This is the second comment', 'Second comment text is correct');
});

test('find a single record with hasMany link as object { data } (polymorphic)', async function (assert) {
Expand Down Expand Up @@ -648,14 +647,14 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
assert.strictEqual(user.firstName, 'Yehuda', 'Record firstName is loaded');
assert.strictEqual(user.lastName, 'Katz', 'Record lastName is loaded');

let handles = await user.get('handles');
let handles = await user.handles;

assert.strictEqual(passedUrl[1], '/github-handles/2', 'Builds correct URL to fetch related record');
assert.strictEqual(passedUrl[2], '/twitter-handles/3', 'Builds correct URL to fetch related record');

assert.strictEqual(handles.get('length'), 2);
assert.strictEqual(handles.get('firstObject.username'), 'wycats', 'First handle username is correct');
assert.strictEqual(handles.get('lastObject.nickname'), '@wycats', 'Second handle nickname is correct');
assert.strictEqual(handles.length, 2);
assert.strictEqual(handles.firstObject.username, 'wycats', 'First handle username is correct');
assert.strictEqual(handles.lastObject.nickname, '@wycats', 'Second handle nickname is correct');
});

test('find a single record with sideloaded hasMany link as object { data }', async function (assert) {
Expand Down Expand Up @@ -703,13 +702,13 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
assert.strictEqual(post.id, '1', 'Record id is loaded');
assert.strictEqual(post.title, 'Ember.js rocks', 'Record title is loaded');

let comments = await post.get('comments');
let comments = await post.comments;

assert.strictEqual(passedUrl.length, 1, 'Do not call extra end points because related records are included');

assert.strictEqual(comments.get('length'), 2, 'Loads related records');
assert.strictEqual(comments.get('firstObject.text'), 'This is the first comment', 'First comment text is correct');
assert.strictEqual(comments.get('lastObject.text'), 'This is the second comment', 'Second comment text is correct');
assert.strictEqual(comments.length, 2, 'Loads related records');
assert.strictEqual(comments.firstObject.text, 'This is the first comment', 'First comment text is correct');
assert.strictEqual(comments.lastObject.text, 'This is the second comment', 'Second comment text is correct');
});

test('find a single record with sideloaded hasMany link as object { data } (polymorphic)', async function (assert) {
Expand Down Expand Up @@ -756,17 +755,17 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)

assert.strictEqual(passedUrl[0], '/users/1');

assert.strictEqual(user.get('id'), '1');
assert.strictEqual(user.get('firstName'), 'Yehuda');
assert.strictEqual(user.get('lastName'), 'Katz');
assert.strictEqual(user.id, '1');
assert.strictEqual(user.firstName, 'Yehuda');
assert.strictEqual(user.lastName, 'Katz');

let handles = await user.get('handles');
let handles = await user.handles;

assert.strictEqual(passedUrl.length, 1, 'Do not call extra end points because related records are included');

assert.strictEqual(handles.get('length'), 2);
assert.strictEqual(handles.get('firstObject.username'), 'wycats');
assert.strictEqual(handles.get('lastObject.nickname'), '@wycats');
assert.strictEqual(handles.length, 2);
assert.strictEqual(handles.firstObject.username, 'wycats');
assert.strictEqual(handles.lastObject.nickname, '@wycats');
});

test('create record', async function (assert) {
Expand Down Expand Up @@ -807,7 +806,7 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
company: company,
});

let handles = await user.get('handles');
let handles = await user.handles;

handles.addObject(githubHandle);

Expand Down Expand Up @@ -881,7 +880,7 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
user.set('firstName', 'Yehuda!');
user.set('company', company);

let handles = await user.get('handles');
let handles = await user.handles;

handles.addObject(githubHandle);

Expand Down Expand Up @@ -963,7 +962,7 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)

user.set('firstName', 'Yehuda!');

let handles = await user.get('handles');
let handles = await user.handles;

handles.addObject(githubHandle);
handles.addObject(twitterHandle);
Expand Down Expand Up @@ -1024,7 +1023,7 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)

assert.strictEqual(passedUrl[0], '/posts/1');

let author = await post.get('author');
let author = await post.author;

assert.strictEqual(passedUrl[1], 'http://example.com/post/1/author');
assert.strictEqual(author, null);
Expand Down