diff --git a/__tests__/serializers/serializeResourceLinkage.test.ts b/__tests__/serializers/serializeResourceLinkage.test.ts index e86ecec..efe6f47 100644 --- a/__tests__/serializers/serializeResourceLinkage.test.ts +++ b/__tests__/serializers/serializeResourceLinkage.test.ts @@ -18,6 +18,19 @@ describe('`serializeResourceLinkage`', () => { chance = new Chance(); }); + describe('when given a resource linkage', () => { + it('should return the resource linkage as is', () => { + const resourceLinkage = { + type: chance.string(), + id: chance.string(), + }; + + expect(serializeResourceLinkage(resourceLinkage)).toEqual( + resourceLinkage, + ); + }); + }); + describe('when given an array of class instances', () => { it('should throw an error if some element in the array is not an object', () => { const classInstances = [1]; diff --git a/__tests__/serializers/serializeResourceObject.test.ts b/__tests__/serializers/serializeResourceObject.test.ts index 2eb49cd..1fb7444 100644 --- a/__tests__/serializers/serializeResourceObject.test.ts +++ b/__tests__/serializers/serializeResourceObject.test.ts @@ -122,8 +122,8 @@ describe('`serializeResourceObject`', () => { }); }); - describe('when the related class instance is not an object', () => { - it('should throw an error', () => { + describe('when the related class instance is a single object', () => { + it('should throw an error if the related class instance is not an object', () => { const key = chance.string(); getMetadataBySymbolMocked.mockImplementation( @@ -144,9 +144,7 @@ describe('`serializeResourceObject`', () => { `Failed to serialize relationship object for ${key} because the value is not an object.`, ); }); - }); - describe('when the related class instance is an object', () => { it('should serialize the relationships object and return it', () => { const key = chance.string(); @@ -203,49 +201,6 @@ describe('`serializeResourceObject`', () => { }); }); - describe('when the related class instance is already a relationship object', () => { - it('should return without serializing the relationship object', () => { - const key = chance.string(); - - getMetadataBySymbolMocked.mockImplementation( - (_: object, symbol: symbol) => { - if (symbol === resourceSymbol) { - return 'type'; - } - - if (symbol === relationshipsSymbol) { - return [[key, '']]; - } - - return undefined; - }, - ); - - collectMocked.mockImplementation((_: object, symbol: symbol) => { - if (symbol === idSymbol) { - return 'id'; - } - - return undefined; - }); - - const relatedClassInstance = { - id: 'some id', - type: 'some type', - }; - - const classInstance = { - [key]: { - data: relatedClassInstance, - }, - }; - - const result = serializeResourceObject(classInstance); - - expect(result.relationships).toBeUndefined(); - }); - }); - it('should return without the `relationships` field if no relationships are found', () => { getMetadataBySymbolMocked.mockImplementation( (_: object, symbol: symbol) => { diff --git a/__tests__/serializers/utils/isRelationshipObject.test.ts b/__tests__/serializers/utils/isRelationshipObject.test.ts deleted file mode 100644 index ce0864a..0000000 --- a/__tests__/serializers/utils/isRelationshipObject.test.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { isRelationshipObject } from '../../../src/serializers/utils/isRelationshipObject'; -import { isResourceLinkage } from '../../../src/serializers/utils/isResourceLinkage'; - -jest.mock('../../../src/serializers/utils/isResourceLinkage'); -const isResourceLinkageMocked = jest.mocked(isResourceLinkage); - -describe('`isRelationshipObject`', () => { - it('should return false if `relationshipObjectCandidate` is `null`', () => { - const relationshipObjectCandidate = null; - - expect(isRelationshipObject(relationshipObjectCandidate)).toBe(false); - }); - - it('should return false if `relationshipObjectCandidate` is not an object', () => { - const relationshipObjectCandidate = 'not an object'; - - expect(isRelationshipObject(relationshipObjectCandidate)).toBe(false); - }); - - it('should return false if `relationshipObjectCandidate` does not have a `data` property', () => { - const relationshipObjectCandidate = {}; - - expect(isRelationshipObject(relationshipObjectCandidate)).toBe(false); - }); - - it('should return true if `relationshipObjectCandidate.data` is a `JSONAPIResourceLinkage`', () => { - isResourceLinkageMocked.mockReturnValue(true); - - const relationshipObjectCandidate = { - data: 'foo', - }; - - expect(isRelationshipObject(relationshipObjectCandidate)).toBe(true); - }); -}); diff --git a/src/serializers/serializeResourceLinkage.ts b/src/serializers/serializeResourceLinkage.ts index 671b6f6..9776233 100644 --- a/src/serializers/serializeResourceLinkage.ts +++ b/src/serializers/serializeResourceLinkage.ts @@ -3,13 +3,16 @@ import { resourceSymbol } from '../decorators/resource'; import { collect } from './utils/collect'; import { getMetadataBySymbol } from './utils/getMetadataBySymbol'; import { isObject } from './utils/isObject'; +import { isResourceLinkage } from './utils/isResourceLinkage'; import type { JSONAPIResourceLinkage } from '../types/resourceLinkage'; export const serializeResourceLinkage = ( - classInstanceCandidate_s: I | JSONAPIResourceLinkage, + classInstance_s: I | JSONAPIResourceLinkage, ): JSONAPIResourceLinkage => { - const classInstance_s = classInstanceCandidate_s as I; + if (isResourceLinkage(classInstance_s)) { + return classInstance_s; + } if (Array.isArray(classInstance_s)) { if (!classInstance_s.every(isObject)) { diff --git a/src/serializers/serializeResourceObject.ts b/src/serializers/serializeResourceObject.ts index 48bf413..ed034d2 100644 --- a/src/serializers/serializeResourceObject.ts +++ b/src/serializers/serializeResourceObject.ts @@ -17,7 +17,6 @@ import type { JSONAPIResourceObject, JSONObject, } from '../types'; -import { isRelationshipObject } from './utils/isRelationshipObject'; export const serializeResourceObject = ( classInstance: I, @@ -44,10 +43,6 @@ export const serializeResourceObject = ( ); } - if (isRelationshipObject(relatedClassInstance_s)) { - return acc; - } - acc[key.toString()] = { data: serializeResourceLinkage(relatedClassInstance_s), }; diff --git a/src/serializers/utils/index.ts b/src/serializers/utils/index.ts index 57072d2..67ffb17 100644 --- a/src/serializers/utils/index.ts +++ b/src/serializers/utils/index.ts @@ -3,6 +3,5 @@ export * from './getMetadataBySymbol'; export * from './collect'; export * from './clean'; export * from './isObject'; -export * from './isRelationshipObject'; export * from './isResourceIdentifierObject'; export * from './isResourceLinkage'; diff --git a/src/serializers/utils/isRelationshipObject.ts b/src/serializers/utils/isRelationshipObject.ts deleted file mode 100644 index 89b78f8..0000000 --- a/src/serializers/utils/isRelationshipObject.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { isResourceLinkage } from './isResourceLinkage'; - -import type { JSONAPIRelationshipObject } from '../../types/relationshipObject'; - -export const isRelationshipObject = ( - relationshipObjectCandidate: unknown, -): relationshipObjectCandidate is JSONAPIRelationshipObject => { - if (relationshipObjectCandidate === null) { - return false; - } - - if (typeof relationshipObjectCandidate !== 'object') { - return false; - } - - if (!('data' in relationshipObjectCandidate)) { - return false; - } - - const { data } = relationshipObjectCandidate; - - return isResourceLinkage(data); -};