Skip to content

Commit

Permalink
fix: serialize resource linkage and serialize resource object tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhaticus committed Nov 23, 2024
1 parent cdd7144 commit ac6874d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
55 changes: 38 additions & 17 deletions __tests__/serializers/serializeResourceObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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.',
);
});

Expand Down Expand Up @@ -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,
},
],
},
});
});
});
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -167,14 +180,22 @@ describe('`serializeResourceObject`', () => {

const b = chance.string();

// TODO: serialize resource linkage mock
serializeResourceLinkageMocked.mockImplementation(
(classInstance) =>
({
...classInstance,
b,
}) as unknown as Exclude<JSONAPIResourceLinkage, null>,
);

const result = serializeResourceObject(classInstance);

expect(result.relationships).toEqual({
[key]: {
...relatedClassInstance,
b,
data: {
...relatedClassInstance,
b,
},
},
});
});
Expand Down Expand Up @@ -234,7 +255,7 @@ describe('`serializeResourceObject`', () => {
[key]: null,
});

expect(serializeResourceLinkage).not.toHaveBeenCalled();
expect(serializeResourceLinkageMocked).not.toHaveBeenCalled();

expect(result.relationships).toBeUndefined();
});
Expand Down
4 changes: 2 additions & 2 deletions src/serializers/serializeResourceLinkage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import type { JSONAPIResourceLinkage } from '../types/resourceLinkage';
import { isObject } from './utils';

export const serializeResourceLinkage = <I extends object>(
classInstance_s: I,
classInstance_s: I | I[],
): Exclude<JSONAPIResourceLinkage, null> => {
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.',
);
}

Expand Down

0 comments on commit ac6874d

Please sign in to comment.