Skip to content

Commit

Permalink
Merge pull request #7337 from emberjs/sn/better-debug-msg
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue authored Oct 7, 2020
2 parents 4bd2b32 + 2ade689 commit ff4f911
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
67 changes: 67 additions & 0 deletions packages/-ember-data/tests/integration/identifiers/cache-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,71 @@ module('Integration | Identifiers - cache', function(hooks) {
);
});
});

module('createIdentifierForNewRecord()', function() {
test('returns new identifier', async function(assert) {
const runspiredHash = {
type: 'person',
id: '1',
attributes: {
name: 'runspired',
},
};
const identifier = cache.createIdentifierForNewRecord(runspiredHash);

assert.equal(identifier.id, '1', 'identifier has id');
assert.equal(identifier.type, 'person', 'identifier has type');
assert.ok(identifier.lid, 'identifier has lid');
});
});

module('updateRecordIdentifier()', function() {
test('returns same identifier', async function(assert) {
const runspiredHash = {
type: 'person',
id: '1',
attributes: {
name: 'runspired',
},
};
let identifier = cache.createIdentifierForNewRecord(runspiredHash);

let mergedIdentifier = cache.updateRecordIdentifier(identifier, { type: 'person', id: '1' });

assert.equal(mergedIdentifier.id, identifier.id, 'merged identifier has same id');
assert.equal(mergedIdentifier.type, identifier.type, 'merged identifier has same type');
});

test('returns new identifier with different id', async function(assert) {
const runspiredHash = {
type: 'person',
id: '1',
attributes: {
name: 'runspired',
},
};
let identifier = cache.createIdentifierForNewRecord(runspiredHash);

let mergedIdentifier = cache.updateRecordIdentifier(identifier, { type: 'person', id: '2' });

assert.equal(mergedIdentifier.id, '2', 'merged identifier has new id');
assert.equal(mergedIdentifier.type, 'person', 'merged identifier has same type');
});

test('id is null', async function(assert) {
const runspiredHash = {
type: 'person',
id: '1',
attributes: {
name: 'runspired',
},
};
let identifier = cache.createIdentifierForNewRecord(runspiredHash);

let mergedIdentifier = cache.updateRecordIdentifier(identifier, { type: 'person', id: null });

assert.equal(mergedIdentifier.id, null, 'merged identifier has null id');
assert.equal(mergedIdentifier.type, identifier.type, 'merged identifier has same type');
});
});
});
6 changes: 2 additions & 4 deletions packages/store/addon/-private/identifiers/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,17 +340,15 @@ export class IdentifierCache {
): StableRecordIdentifier {
let identifier = this.getOrCreateRecordIdentifier(identifierObject);

let id = identifier.id;
let newId = coerceId(data.id);

const keyOptions = getTypeIndex(this._cache.types, identifier.type);
let existingIdentifier = detectMerge(this._cache.types, identifier, data, newId, this._cache.lids);

if (existingIdentifier) {
let keyOptions = getTypeIndex(this._cache.types, identifier.type);
identifier = this._mergeRecordIdentifiers(keyOptions, identifier, existingIdentifier, data, newId as string);
}

id = identifier.id;
let id = identifier.id;
performRecordIdentifierUpdate(identifier, data, this._update);
newId = identifier.id;

Expand Down

0 comments on commit ff4f911

Please sign in to comment.