Skip to content

Commit

Permalink
refactor: renamed serialize resource relationship object to serialize…
Browse files Browse the repository at this point in the history
… relationship object
  • Loading branch information
ryanhaticus committed Nov 15, 2024
1 parent 2241523 commit 4c6dd5b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
22 changes: 11 additions & 11 deletions __tests__/serializers/serializeResourceObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { idSymbol } from '../../src/decorators/id';
import { relationshipsSymbol } from '../../src/decorators/relationship';
import { resourceSymbol } from '../../src/decorators/resource';
import { serializeResourceObject } from '../../src/serializers/serializeResourceObject';
import { serializeResourceRelationshipObject } from '../../src/serializers/serializeResourceRelationshipObject';
import { serializeRelationshipObject } from '../../src/serializers/serializeRelationshipObject';
import { collect } from '../../src/serializers/utils/collect';
import { getMetadataBySymbol } from '../../src/serializers/utils/getMetadataBySymbol';

Expand All @@ -15,9 +15,9 @@ const getMetadataBySymbolMocked = jest.mocked(getMetadataBySymbol);
jest.mock('../../src/serializers/utils/collect');
const collectMocked = jest.mocked(collect);

jest.mock('../../src/serializers/serializeResourceRelationshipObject');
const serializeResourceRelationshipObjectMocked = jest.mocked(
serializeResourceRelationshipObject,
jest.mock('../../src/serializers/serializeRelationshipObject');
const serializeRelationshipObjectMocked = jest.mocked(
serializeRelationshipObject,
);

jest.mock('../../src/serializers/utils/assertMetadataIsPresent');
Expand Down Expand Up @@ -49,11 +49,11 @@ describe('`serializeResourceObject`', () => {
};

expect(() => serializeResourceObject(classInstance)).toThrow(
`Failed to serialize resource relationship object for ${key} becuase not all elements in the array are objects.`,
`Failed to serialize relationship object for ${key} becuase not all elements in the array are objects.`,
);
});

it('should serialize defined resource relationship objects and return them', () => {
it('should serialize defined relationship objects and return them', () => {
const key = chance.string();

getMetadataBySymbolMocked.mockImplementation(
Expand Down Expand Up @@ -92,7 +92,7 @@ describe('`serializeResourceObject`', () => {

const b = chance.string();

serializeResourceRelationshipObjectMocked.mockImplementation(
serializeRelationshipObjectMocked.mockImplementation(
(classInstance) =>
({
...classInstance,
Expand Down Expand Up @@ -137,11 +137,11 @@ describe('`serializeResourceObject`', () => {
};

expect(() => serializeResourceObject(classInstance)).toThrow(
`Failed to serialize resource relationship object for ${key} because the value is not an object.`,
`Failed to serialize relationship object for ${key} because the value is not an object.`,
);
});

it('should serialize the resource relationship object and return it', () => {
it('should serialize the relationship object and return it', () => {
const key = chance.string();

getMetadataBySymbolMocked.mockImplementation(
Expand Down Expand Up @@ -176,7 +176,7 @@ describe('`serializeResourceObject`', () => {

const b = chance.string();

serializeResourceRelationshipObjectMocked.mockImplementation(
serializeRelationshipObjectMocked.mockImplementation(
(classInstance) =>
({
...classInstance,
Expand Down Expand Up @@ -250,7 +250,7 @@ describe('`serializeResourceObject`', () => {
[key]: null,
});

expect(serializeResourceRelationshipObjectMocked).not.toHaveBeenCalled();
expect(serializeRelationshipObjectMocked).not.toHaveBeenCalled();

expect(result.relationships).toBeUndefined();
});
Expand Down
20 changes: 10 additions & 10 deletions __tests__/serializers/serializeResourceRelationshipObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { collect } from "../../src/serializers/utils/collect";
import { getMetadataBySymbol } from "../../src/serializers/utils/getMetadataBySymbol";
import { idSymbol } from '../../src/decorators/id';
import { resourceSymbol } from '../../src/decorators/resource';
import { serializeResourceRelationshipObject } from "../../src/serializers/serializeResourceRelationshipObject";
import { serializeRelationshipObject } from "../../src/serializers/serializeRelationshipObject";
import { linksSymbol } from "../../src/decorators/links";
import { metaSymbol } from "../../src/decorators/meta";

Expand All @@ -13,7 +13,7 @@ const getMetadataBySymbolMocked = jest.mocked(getMetadataBySymbol);
jest.mock('../../src/serializers/utils/collect');
const collectMocked = jest.mocked(collect);

describe('`serializeResourceRelationshipObject`', () => {
describe('`serializeRelationshipObject`', () => {
let chance: Chance.Chance;

beforeEach(() => {
Expand Down Expand Up @@ -45,7 +45,7 @@ describe('`serializeResourceRelationshipObject`', () => {
}
);

const result = serializeResourceRelationshipObject({});
const result = serializeRelationshipObject({});

expect(result.data).toHaveProperty('type', type);
});
Expand All @@ -71,8 +71,8 @@ describe('`serializeResourceRelationshipObject`', () => {
}
);

expect(() => serializeResourceRelationshipObject({})).toThrow(
'Failed to serialize resource relationship object because the provided class instance is not a resource.',
expect(() => serializeRelationshipObject({})).toThrow(
'Failed to serialize relationship object because the provided class instance is not a resource.',
);
});
});
Expand Down Expand Up @@ -101,7 +101,7 @@ describe('`serializeResourceRelationshipObject`', () => {
}
);

const result = serializeResourceRelationshipObject({});
const result = serializeRelationshipObject({});

expect(result.data).toHaveProperty('id', id);
});
Expand All @@ -121,8 +121,8 @@ describe('`serializeResourceRelationshipObject`', () => {
(_object: object, symbol: symbol) => undefined
);

expect(() => serializeResourceRelationshipObject({})).toThrow(
'Failed to serialize resource relationship object because the provided class instance does not have an id field.',
expect(() => serializeRelationshipObject({})).toThrow(
'Failed to serialize relationship object because the provided class instance does not have an id field.',
);
});
});
Expand Down Expand Up @@ -156,7 +156,7 @@ describe('`serializeResourceRelationshipObject`', () => {
return undefined;
});

const result = serializeResourceRelationshipObject({});
const result = serializeRelationshipObject({});

expect(result.links).toEqual(links);
});
Expand Down Expand Up @@ -190,7 +190,7 @@ describe('`serializeResourceRelationshipObject`', () => {
return undefined;
});

const result = serializeResourceRelationshipObject({});
const result = serializeRelationshipObject({});

expect(result.meta).toEqual(meta);
});
Expand Down
2 changes: 1 addition & 1 deletion src/serializers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './utils';
export * from './serializeIncludedResourceObjects';
export * from './serializeResourceObject';
export * from './serializeResourceRelationshipObject';
export * from './serializeRelationshipObject';
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ import type {
} from '../types';
import { clean } from './utils/clean';

export const serializeResourceRelationshipObject = <I extends object>(
export const serializeRelationshipObject = <I extends object>(
classInstance: I,
): JSONAPIRelationshipObject => {
const type = getMetadataBySymbol<string>(classInstance, resourceSymbol);

if (type === undefined) {
throw new Error(
'Failed to serialize resource relationship object because the provided class instance is not a resource.',
'Failed to serialize relationship object because the provided class instance is not a resource.',
);
}

const id = collect<string>(classInstance, idSymbol);

if (id === undefined) {
throw new Error(
'Failed to serialize resource relationship object because the provided class instance does not have an id field.',
'Failed to serialize relationship object because the provided class instance does not have an id field.',
);
}

Expand Down
10 changes: 5 additions & 5 deletions src/serializers/serializeResourceObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
relationshipsSymbol,
resourceSymbol,
} from '../decorators';
import { serializeResourceRelationshipObject } from './serializeResourceRelationshipObject';
import { serializeRelationshipObject } from './serializeRelationshipObject';
import { collect } from './utils/collect';
import { getMetadataBySymbol } from './utils/getMetadataBySymbol';
import { isObject } from './utils/isObject';
Expand Down Expand Up @@ -42,25 +42,25 @@ export const serializeResourceObject = <I extends object>(

if (!isObject(relatedClassInstance_s)) {
throw new Error(
`Failed to serialize resource relationship object for ${key.toString()} because the value is not an object.`,
`Failed to serialize relationship object for ${key.toString()} because the value is not an object.`,
);
}

if (Array.isArray(relatedClassInstance_s)) {
if (!relatedClassInstance_s.every(isObject)) {
throw new Error(
`Failed to serialize resource relationship object for ${key.toString()} becuase not all elements in the array are objects.`,
`Failed to serialize relationship object for ${key.toString()} becuase not all elements in the array are objects.`,
);
}

acc[key] = relatedClassInstance_s.map((classInstance) =>
serializeResourceRelationshipObject(classInstance),
serializeRelationshipObject(classInstance),
);

return acc;
}

acc[key] = serializeResourceRelationshipObject(relatedClassInstance_s);
acc[key] = serializeRelationshipObject(relatedClassInstance_s);

return acc;
},
Expand Down

0 comments on commit 4c6dd5b

Please sign in to comment.