diff --git a/__tests__/serializers/serializeResourceObject.test.ts b/__tests__/serializers/serializeResourceObject.test.ts index d30c09c..06a4f26 100644 --- a/__tests__/serializers/serializeResourceObject.test.ts +++ b/__tests__/serializers/serializeResourceObject.test.ts @@ -9,6 +9,9 @@ import { serializeResourceObject } from '../../src/serializers/serializeResource import { collect } from '../../src/serializers/utils/collect'; import { getMetadataBySymbol } from '../../src/serializers/utils/getMetadataBySymbol'; +import type { JSONAPIResourceIdentifierObject } from '../../src'; +import type { JSONAPIResourceLinkage } from '../../src/types/resourceLinkage'; + jest.mock('../../src/serializers/utils/getMetadataBySymbol'); const getMetadataBySymbolMocked = jest.mocked(getMetadataBySymbol); @@ -47,7 +50,7 @@ describe('`serializeResourceObject`', () => { }; expect(() => serializeResourceObject(classInstance)).toThrow( - `Failed to serialize relationship object for ${key} becuase not all elements in the array are objects.`, + 'Failed to serialize resource object because the provided class instance is not a resource.', ); }); @@ -90,21 +93,31 @@ describe('`serializeResourceObject`', () => { const b = chance.string(); - // TODO: serialize resource linkage mock + serializeResourceLinkageMocked.mockImplementation((classInstance_s) => { + return (classInstance_s as object[]).map( + (classInstance) => + ({ + ...classInstance, + b, + }) as unknown as JSONAPIResourceIdentifierObject, + ); + }); const result = serializeResourceObject(classInstance); expect(result.relationships).toEqual({ - [key]: [ - { - ...relatedClassInstance, - b, - }, - { - ...secondRelatedClassInstance, - b, - }, - ], + [key]: { + data: [ + { + ...relatedClassInstance, + b, + }, + { + ...secondRelatedClassInstance, + b, + }, + ], + }, }); }); }); @@ -132,7 +145,7 @@ describe('`serializeResourceObject`', () => { ); }); - it('should serialize the relationship object and return it', () => { + it('should serialize the relationships object and return it', () => { const key = chance.string(); getMetadataBySymbolMocked.mockImplementation( @@ -167,14 +180,22 @@ describe('`serializeResourceObject`', () => { const b = chance.string(); - // TODO: serialize resource linkage mock + serializeResourceLinkageMocked.mockImplementation( + (classInstance) => + ({ + ...classInstance, + b, + }) as unknown as Exclude, + ); const result = serializeResourceObject(classInstance); expect(result.relationships).toEqual({ [key]: { - ...relatedClassInstance, - b, + data: { + ...relatedClassInstance, + b, + }, }, }); }); @@ -234,7 +255,7 @@ describe('`serializeResourceObject`', () => { [key]: null, }); - expect(serializeResourceLinkage).not.toHaveBeenCalled(); + expect(serializeResourceLinkageMocked).not.toHaveBeenCalled(); expect(result.relationships).toBeUndefined(); }); diff --git a/src/serializers/serializeResourceLinkage.ts b/src/serializers/serializeResourceLinkage.ts index 2324572..d56fb51 100644 --- a/src/serializers/serializeResourceLinkage.ts +++ b/src/serializers/serializeResourceLinkage.ts @@ -6,12 +6,12 @@ import type { JSONAPIResourceLinkage } from '../types/resourceLinkage'; import { isObject } from './utils'; export const serializeResourceLinkage = ( - classInstance_s: I, + classInstance_s: I | I[], ): Exclude => { if (Array.isArray(classInstance_s)) { if (!classInstance_s.every(isObject)) { throw new Error( - 'Failed to serialize resource linkage becuase not all elements in the array are objects.', + 'Failed to serialize resource linkage because not all elements in the array are objects.', ); }