Skip to content

Commit

Permalink
fix more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Aug 30, 2022
1 parent 0e07474 commit c920915
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,26 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
@belongsTo('post', { async: true, inverse: 'comments' }) post;
}

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

class GithubHandle extends Handle {
class GithubHandle extends Model {
@attr('string') username;
@belongsTo('user', { async: true, inverse: 'handles', as: 'handle' }) user;
}

class TwitterHandle extends Handle {
class TwitterHandle extends Model {
@attr('string') nickname;
@belongsTo('user', { async: true, inverse: 'handles', as: 'handle' }) user;
}

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

class DevelopmentShop extends Company {
class DevelopmentShop extends Model {
@attr('boolean') coffee;
@attr('string') name;
@hasMany('user', { async: true, inverse: 'company', as: 'company' }) employees;
}

class DesignStudio extends Company {
class DesignStudio extends Model {
@attr('number') hipsters;
@attr('string') name;
@hasMany('user', { async: true, inverse: 'company', as: 'company' }) employees;
}

this.owner.register('adapter:application', class extends JSONAPIAdapter {});
Expand All @@ -65,10 +62,8 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
this.owner.register('model:user', User);
this.owner.register('model:post', Post);
this.owner.register('model:comment', Comment);
this.owner.register('model:handle', Handle);
this.owner.register('model:github-handle', GithubHandle);
this.owner.register('model:twitter-handle', TwitterHandle);
this.owner.register('model:company', Company);
this.owner.register('model:development-shop', DevelopmentShop);
this.owner.register('model:design-studio', DesignStudio);

Expand Down Expand Up @@ -770,6 +765,14 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
test('create record', async function (assert) {
assert.expect(3);

this.owner.register(
'model:company',
class Company extends Model {
@attr('string') name;
@hasMany('user', { async: true, inverse: 'company', as: 'company' }) employees;
}
);

ajaxResponse([
{
data: {
Expand Down Expand Up @@ -798,6 +801,15 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
},
},
});
let twitterHandle = store.push({
data: {
type: 'twitter-handle',
id: '2',
attributes: {
nickname: 'wycats',
},
},
});

let user = store.createRecord('user', {
firstName: 'Yehuda',
Expand All @@ -808,6 +820,7 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
let handles = await user.handles;

handles.push(githubHandle);
handles.push(twitterHandle);

await user.save();

Expand All @@ -827,6 +840,12 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
company: {
data: { type: 'companies', id: '1' },
},
handles: {
data: [
{ type: 'github-handles', id: '2' },
{ type: 'twitter-handles', id: '2' },
],
},
},
},
},
Expand All @@ -836,6 +855,14 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
test('update record', async function (assert) {
assert.expect(3);

this.owner.register(
'model:company',
class Company extends Model {
@attr('string') name;
@hasMany('user', { async: true, inverse: 'company', as: 'company' }) employees;
}
);

ajaxResponse([
{
data: {
Expand Down Expand Up @@ -901,6 +928,9 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function (hooks)
company: {
data: { type: 'companies', id: '2' },
},
handles: {
data: [{ type: 'github-handles', id: '3' }],
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ module('integration/relationship/belongs_to Belongs-To Relationships', function
}

class Message extends Model {
@belongsTo('user', { inverse: 'messages', async: false }) user;
@belongsTo('user', { inverse: 'messages', async: false, as: 'message' }) user;
@attr('date') created_at;
}

Expand All @@ -287,6 +287,7 @@ module('integration/relationship/belongs_to Belongs-To Relationships', function
}
class Post extends Message {
@attr('string') title;
@belongsTo('user', { inverse: 'messages', async: false, as: 'message' }) user;
@hasMany('comment', { async: false, inverse: null }) comments;
}

Expand Down Expand Up @@ -428,16 +429,12 @@ module('integration/relationship/belongs_to Belongs-To Relationships', function

test('The store can materialize a non loaded monomorphic belongsTo association', function (assert) {
assert.expect(1);
class Message extends Model {
@belongsTo('user', { inverse: 'messages', async: false }) user;
@attr('date') created_at;
}
class Post extends Message {

class Post extends Model {
@attr('string') title;
@hasMany('comment', { async: false, inverse: null }) comments;
@belongsTo('user', { async: true, inverse: 'messages' }) user;
@belongsTo('user', { async: true, inverse: 'messages', as: 'message' }) user;
}
this.owner.register('model:message', Message);
this.owner.register('model:post', Post);

let store = this.owner.lookup('service:store');
Expand Down
26 changes: 25 additions & 1 deletion packages/model/addon/-private/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { HAS_DEBUG_PACKAGE } from '@ember-data/private-build-infra';
import {
DEPRECATE_EARLY_STATIC,
DEPRECATE_MODEL_REOPEN,
DEPRECATE_NON_EXPLICIT_POLYMORPHISM,
DEPRECATE_RELATIONSHIPS_WITHOUT_INVERSE,
DEPRECATE_SAVE_PROMISE_ACCESS,
} from '@ember-data/private-build-infra/deprecations';
Expand Down Expand Up @@ -1490,7 +1491,30 @@ class Model extends EmberObject {

if (inverseOptions?.polymorphic) {
// validate
assert(`these should match`, !!options.as && inverse.type === options.as);
if (DEPRECATE_NON_EXPLICIT_POLYMORPHISM) {
if (!options.as) {
debugger;
deprecate(
`Relationships that satisfy polymorphic relationships MUST define which psuedo-type they are satisfying using 'as'.`,
false,
{
id: 'ember-data:non-explicit-relationships',
since: { enabled: '4.8', available: '4.8' },
until: '5.0',
for: 'ember-data',
}
);
}
} else {
assert(
`Relationships that satisfy polymorphic relationships MUST define which psuedo-type they are satisfying using 'as'.`,
options.as
);
assert(
`options.as should match the expected type of the polymorphic relationship`,
!!options.as && inverse.type === options.as
);
}
} else {
assert(
`The ${inverseType.modelName}:${inverseName} relationship declares 'inverse: null', but it was resolved as the inverse for ${this.modelName}:${name}.`,
Expand Down
1 change: 1 addition & 0 deletions packages/private-build-infra/addon/current-deprecations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ export default {
DEPRECATE_PROMISE_PROXIES: '4.8',
DEPRECATE_ARRAY_LIKE: '4.8',
DEPRECATE_COMPUTED_CHAINS: '4.8',
DEPRECATE_NON_EXPLICIT_POLYMORPHISM: '4.8',
};
1 change: 1 addition & 0 deletions packages/private-build-infra/addon/deprecations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ export const DEPRECATE_A_USAGE = deprecationState('DEPRECATE_A_USAGE');
export const DEPRECATE_PROMISE_PROXIES = deprecationState('DEPRECATE_PROMISE_PROXIES');
export const DEPRECATE_ARRAY_LIKE = deprecationState('DEPRECATE_ARRAY_LIKE');
export const DEPRECATE_COMPUTED_CHAINS = deprecationState('DEPRECATE_COMPUTED_CHAINS');
export const DEPRECATE_NON_EXPLICIT_POLYMORPHISM = deprecationState('DEPRECATE_NON_EXPLICIT_POLYMORPHISM');
46 changes: 34 additions & 12 deletions packages/store/addon/-debug/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { assert } from '@ember/debug';
import { DEBUG } from '@glimmer/env';

import { DEPRECATE_NON_EXPLICIT_POLYMORPHISM } from '@ember-data/private-build-infra/deprecations';

/*
Assert that `addedRecord` has a valid type so it can be added to the
relationship of the `record`.
Expand Down Expand Up @@ -29,18 +31,38 @@ if (DEBUG) {
};

assertPolymorphicType = function assertPolymorphicType(parentIdentifier, parentDefinition, addedIdentifier, store) {
store = store._store ? store._store : store; // allow usage with storeWrapper
let addedModelName = addedIdentifier.type;
let parentModelName = parentIdentifier.type;
let key = parentDefinition.key;
let relationshipModelName = parentDefinition.type;
let relationshipClass = store.modelFor(relationshipModelName);
let addedClass = store.modelFor(addedModelName);

let assertionMessage = `The '${addedModelName}' type does not implement '${relationshipModelName}' and thus cannot be assigned to the '${key}' relationship in '${parentModelName}'. Make it a descendant of '${relationshipModelName}' or use a mixin of the same name.`;
let isPolymorphic = checkPolymorphic(relationshipClass, addedClass);

assert(assertionMessage, isPolymorphic);
let asserted = false;

if (parentDefinition.inverseIsImplicit) {
return;
}
if (parentDefinition.isPolymorphic) {
let meta = store.getSchemaDefinitionService().relationshipsDefinitionFor(addedIdentifier)[
parentDefinition.inverseKey
];
if (meta?.options?.as) {
asserted = true;
assert(
`The schema for the relationship '${parentDefinition.inverseKey}' on '${addedIdentifier.type}' type does not implement '${parentDefinition.type}' and thus cannot be assigned to the '${parentDefinition.key}' relationship in '${parentIdentifier.type}'. The definition should specify 'as: "${parentDefinition.type}"' in options.`,
meta.options.as === parentDefinition.type
);
}
}

if (DEPRECATE_NON_EXPLICIT_POLYMORPHISM && !asserted) {
store = store._store ? store._store : store; // allow usage with storeWrapper
let addedModelName = addedIdentifier.type;
let parentModelName = parentIdentifier.type;
let key = parentDefinition.key;
let relationshipModelName = parentDefinition.type;
let relationshipClass = store.modelFor(relationshipModelName);
let addedClass = store.modelFor(addedModelName);

let assertionMessage = `The '${addedModelName}' type does not implement '${relationshipModelName}' and thus cannot be assigned to the '${key}' relationship in '${parentModelName}'. Make it a descendant of '${relationshipModelName}' or use a mixin of the same name.`;
let isPolymorphic = checkPolymorphic(relationshipClass, addedClass);

assert(assertionMessage, isPolymorphic);
}
};
}

Expand Down

0 comments on commit c920915

Please sign in to comment.