Skip to content

Commit

Permalink
Test for removing type check that prevents polymorphic return types i…
Browse files Browse the repository at this point in the history
…n API responses
  • Loading branch information
ryandoherty committed Feb 9, 2021
1 parent 8382187 commit 9e86443
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,48 @@ module('integration/adapter/json-api-adapter - JSONAPIAdapter', function(hooks)
assert.equal(posts.get('firstObject.title'), 'Ember.js rocks', 'Sets correct title to record');
});

test('find many polymorphic records', async function(assert) {
assert.expect(4);

ajaxResponse([
{
data: [
{
type: 'github-handles',
id: '1',
attributes: {
username: 'ykatz',
},
},
{
type: 'twitter-handles',
id: '2',
attributes: {
nickname: 'ykatz',
},
},
],
included: [],
},
]);

let handles = await store.findAll('handle');

let twitter_handles = store.peekAll('twitter-handles');
let github_handles = store.peekAll('github-handles');

//This should work b/c we loaded these types from the API, but it doesn't
assert.equal(twitter_handles.get('length'), 1);
assert.equal(github_handles.get('length'), 1);

assert.equal(passedUrl[0], '/handles', 'Builds correct URL');

//These won't work because we didn't load anything of the base type 'handles'
assert.equal(handles.get('length'), 2, 'Returns the correct number of records');
assert.equal(handles.get('firstObject.username'), 'ykatz', 'Sets correct username to record');
assert.equal(handles.get('secondObject.nickname'), 'ykatz', 'Sets correct username to record');
});

test('queryRecord - primary data being a single record', async function(assert) {
ajaxResponse([
{
Expand Down
8 changes: 0 additions & 8 deletions packages/store/addon/-private/identifiers/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,6 @@ function performRecordIdentifierUpdate(
updateFn: UpdateMethod
) {
let { id, lid } = data;
let type = data.type && normalizeModelName(data.type);

if (DEBUG) {
// get the mutable instance behind our proxy wrapper
Expand Down Expand Up @@ -526,13 +525,6 @@ function performRecordIdentifierUpdate(
}
}

// TODO consider just ignoring here to allow flexible polymorphic support
if (type && type !== identifier.type) {
throw new Error(
`The 'type' for a RecordIdentifier cannot be updated once it has been set. Attempted to set type for '${wrapper}' to '${type}'.`
);
}

updateFn(wrapper, data, 'record');
} else {
updateFn(identifier, data, 'record');
Expand Down

0 comments on commit 9e86443

Please sign in to comment.