From 2ade689e2e2cdcee53d49371acd3474ff1e1dc7b Mon Sep 17 00:00:00 2001 From: Scott Newcomer Date: Fri, 25 Sep 2020 14:20:23 -0700 Subject: [PATCH] [Debug]: improved debug msg instead of [object Object] update identifier as well Add tests for cache methods add moar tests cannot assert debug errors in prod tests address feedback --- .../integration/identifiers/cache-test.ts | 67 +++++++++++++++++++ .../store/addon/-private/identifiers/cache.ts | 6 +- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/packages/-ember-data/tests/integration/identifiers/cache-test.ts b/packages/-ember-data/tests/integration/identifiers/cache-test.ts index 9f45867bc75..7b56b4df183 100644 --- a/packages/-ember-data/tests/integration/identifiers/cache-test.ts +++ b/packages/-ember-data/tests/integration/identifiers/cache-test.ts @@ -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'); + }); + }); }); diff --git a/packages/store/addon/-private/identifiers/cache.ts b/packages/store/addon/-private/identifiers/cache.ts index a6b330850c3..f9e7af7db72 100644 --- a/packages/store/addon/-private/identifiers/cache.ts +++ b/packages/store/addon/-private/identifiers/cache.ts @@ -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;