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

[BREAKING] Fixes incompatibility with Ember Data Model Fragments 5.0.0-beta+ and Ember Data 3.13+ #449

Merged
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
32 changes: 32 additions & 0 deletions addon/converter/fixture-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { entries } from '../utils/helper-functions';
import { typeOf } from '@ember/utils';
import { getOwner } from '@ember/application';
import { camelize } from '@ember/string';
import Fragment from 'ember-data-model-fragments/fragment';
import FragmentArray from 'ember-data-model-fragments/array/fragment';

/**
Base class for converting the base fixture that factory guy creates to
Expand Down Expand Up @@ -185,6 +187,18 @@ export default class FixtureConverter {
let attributeKey = transformKeyFunction(attribute),
transformValueFunction = this.getTransformValueFunction(meta.type);

let attributeValueInFixture = fixture[attributeKey];

// If passed Fragments or FragmentArrays we must transform them to their serialized form before we can push them into the Store
if (
attributeValueInFixture instanceof Fragment ||
attributeValueInFixture instanceof FragmentArray
) {
fixture[attributeKey] = this.normalizeModelFragments(
attributeValueInFixture
);
}

if (Object.prototype.hasOwnProperty.call(fixture, attribute)) {
attributes[attributeKey] = transformValueFunction(
fixture[attribute],
Expand All @@ -203,6 +217,24 @@ export default class FixtureConverter {
return attributes;
}

normalizeModelFragments(attributeValueInFixture) {
if (attributeValueInFixture instanceof Fragment) {
return this.store.normalize(
attributeValueInFixture.constructor.modelName,
attributeValueInFixture.serialize()
).data.attributes;
}
if (attributeValueInFixture instanceof FragmentArray) {
return attributeValueInFixture
.serialize()
.map(
(item) =>
this.store.normalize(attributeValueInFixture.type, item).data
.attributes
);
}
}

/**
Extract relationships and descend into those looking for others

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
"ember-cli-inject-live-reload": "^2.1.0",
"ember-cli-sri": "^2.1.1",
"ember-cli-terser": "^4.0.2",
"ember-data": "~3.9",
"ember-data-model-fragments": "^4.0.0",
"ember-data": "~3.24",
"ember-data-model-fragments": "^5.0.0-beta.2",
"ember-disable-prototype-extensions": "^1.1.3",
"ember-django-adapter": "^2.1.1",
"ember-fetch": "^7.0.0",
Expand Down Expand Up @@ -81,7 +81,7 @@
"qunit-dom": "^1.6.0"
},
"resolutions": {
"ember-data": "3.9.0"
"ember-data": "3.24.2"
},
"keywords": [
"ember-addon",
Expand Down
9 changes: 8 additions & 1 deletion tests/helpers/utility-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { typeOf } from '@ember/utils';
import FactoryGuy, { manualSetup } from 'ember-data-factory-guy';
import DRFAdapter from 'ember-django-adapter/adapters/drf';
import DRFSerializer from 'ember-django-adapter/serializers/drf';
import RESTAdapter from '@ember-data/adapter/rest';
import ActiveModelAdapter, {
ActiveModelSerializer,
} from 'active-model-adapter';
Expand Down Expand Up @@ -129,7 +130,13 @@ export function containerSetup(application, serializerType) {
let store = application.lookup('service:store');

let adapterType = serializerType === '-json' ? '-rest' : serializerType;
let adapter = application.lookup('adapter:' + adapterType);
let adapter;
Copy link
Collaborator Author

@patocallaghan patocallaghan Oct 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are test-only changes but it appears that doing the following out-of-the-box no longer works and the adapter is undefined.

application.lookup('adapter:-rest');

I need to register the RESTAdapter similar to the other adapters a few lines above to be able to retrieve it using lookup.

if (serializerType === '-json' || serializerType === '-rest') {
application.register(`adapter:-rest`, RESTAdapter, {
singleton: false,
});
}
adapter = application.lookup('adapter:' + adapterType);
adapter = adapter.reopen(AdapterFetch);

serializerType = serializerType === '-json' ? '-default' : serializerType;
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/shared-factory-guy-behaviour.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,41 @@ SharedBehavior.makeTests = function () {
assert.ok(lastHat.get('outfit.hats.firstObject') === lastHat);
});

test('handles fragment relationships', function (assert) {
let name = make('name', {});

let employee = make('employee', { name });
assert.equal(
employee.name.firstName,
name.firstName,
'fragment name.firstName'
);
assert.equal(
employee.name.lastName,
name.lastName,
'fragment name.lastName'
);
});

test('handles fragmentArray relationships', function (assert) {
let departmentEmployments = make(
'employee',
'with_department_employments'
).departmentEmployments;

let employee = make('employee', { departmentEmployments });
assert.equal(
employee.departmentEmployments.firstObject.department.name,
departmentEmployments.firstObject.department.name,
'fragment array - first department name'
);
assert.equal(
employee.departmentEmployments.lastObject.department.name,
departmentEmployments.lastObject.department.name,
'fragment array - last department name'
);
});

test('using afterMake with transient attributes in definition', function (assert) {
run(function () {
let property = FactoryGuy.make('property');
Expand Down
Loading