Skip to content

Commit

Permalink
test: full coverage on deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhaticus committed Nov 24, 2024
1 parent 0442932 commit a2b88ae
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 42 deletions.
42 changes: 22 additions & 20 deletions __tests__/serializers/deserializeResourceObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,29 @@ describe('`deserializeResourceObject`', () => {
chance = new Chance();
});

it('should throw an error if the type of the resource object does not match the expected type', () => {
const expectedType = chance.string();

getMetadataBySymbolMocked.mockImplementation((_, symbol) => {
if (symbol === resourceSymbol) {
return expectedType;
}
describe('`type`', () => {
it('should throw an error if the type of the resource object does not match the expected type', () => {
const expectedType = chance.string();

getMetadataBySymbolMocked.mockImplementation((_, symbol) => {
if (symbol === resourceSymbol) {
return expectedType;
}
});

const resourceObject = {
type: chance.string(),
id: chance.string(),
};

class SomeResource {}

expect(() =>
deserializeResourceObject(resourceObject, SomeResource),
).toThrow(
`Failed to deserialize resource object because the type ${resourceObject.type} does not match the expected type ${expectedType}.`,
);
});

const resourceObject = {
type: chance.string(),
id: chance.string(),
};

class SomeResource {}

expect(() =>
deserializeResourceObject(resourceObject, SomeResource),
).toThrow(
`Failed to deserialize resource object because the type ${resourceObject.type} does not match the expected type ${expectedType}.`,
);
});

it('should deserialize a resource object into a class instance', () => {
Expand Down
8 changes: 4 additions & 4 deletions __tests__/serializers/serializeResourceLinkage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('`serializeResourceLinkage`', () => {
);
});

it('should throw an error if some element in the array does not have an id field', () => {
it('should throw an error if some element in the array does not have an id', () => {
const classInstances = [
{
a: 'a',
Expand All @@ -61,7 +61,7 @@ describe('`serializeResourceLinkage`', () => {
collectMocked.mockImplementation(() => undefined);

expect(() => serializeResourceLinkage(classInstances)).toThrow(
'Failed to serialize 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.',
);
});

Expand Down Expand Up @@ -119,7 +119,7 @@ describe('`serializeResourceLinkage`', () => {
);
});

it('should throw an error if the class instance does not have an id field', () => {
it('should throw an error if the class instance does not have an id', () => {
const classInstance = {
a: 'a',
};
Expand All @@ -133,7 +133,7 @@ describe('`serializeResourceLinkage`', () => {
collectMocked.mockImplementation(() => undefined);

expect(() => serializeResourceLinkage(classInstance)).toThrow(
'Failed to serialize 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.',
);
});

Expand Down
34 changes: 18 additions & 16 deletions src/serializers/deserializeResourceObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export const deserializeResourceObject = <
T extends JSONAPIResourceObject = JSONAPIResourceObject,
>(
resourceObject: T,
{ prototype }: new () => C,
// biome-ignore lint/suspicious/noExplicitAny: `any` is required to support all class constructors.
{ prototype }: new (..._: any[]) => C,
): C => {
const classInstance = Object.create(prototype);

Expand All @@ -28,7 +29,7 @@ export const deserializeResourceObject = <
const id = getMetadataBySymbol<string>(classInstance, idSymbol);

if (id !== undefined) {
classInstance[id] = resourceObject.id ?? null;
classInstance[id] = resourceObject.id;
}

if (resourceObject.attributes !== undefined) {
Expand All @@ -39,7 +40,7 @@ export const deserializeResourceObject = <

if (attributes !== undefined) {
for (const attribute of attributes) {
classInstance[attribute] = resourceObject.attributes[attribute] ?? null;
classInstance[attribute] = resourceObject.attributes[attribute];
}
}
}
Expand All @@ -49,7 +50,7 @@ export const deserializeResourceObject = <

if (links !== undefined) {
for (const link of links) {
classInstance[link] = resourceObject.links[link] ?? null;
classInstance[link] = resourceObject.links[link];
}
}
}
Expand All @@ -59,26 +60,27 @@ export const deserializeResourceObject = <

if (metas !== undefined) {
for (const meta of metas) {
classInstance[meta] = resourceObject.meta[meta] ?? null;
classInstance[meta] = resourceObject.meta[meta];
}
}
}

if (resourceObject.relationships !== undefined) {
const relationshipTuples =
getMetadataBySymbol<[string, string][]>(
classInstance,
relationshipsSymbol,
) ?? [];
const relationshipTuples = getMetadataBySymbol<[string, string][]>(
classInstance,
relationshipsSymbol,
);

for (const [key] of relationshipTuples) {
const relationship = resourceObject.relationships[key];
if (relationshipTuples !== undefined) {
for (const [key] of relationshipTuples) {
const relationship = resourceObject.relationships[key];

if (relationship !== undefined) {
const resourceLinkage = relationship.data;
if (relationship !== undefined) {
const resourceLinkage = relationship.data;

if (resourceLinkage !== undefined) {
classInstance[key] = resourceLinkage ?? null;
if (resourceLinkage !== undefined) {
classInstance[key] = resourceLinkage;
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/serializers/serializeResourceLinkage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const serializeResourceLinkage = <I extends object>(

if (id === undefined) {
throw new Error(
'Failed to serialize 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.',
);
}

Expand All @@ -52,7 +52,7 @@ export const serializeResourceLinkage = <I extends object>(

if (id === undefined) {
throw new Error(
'Failed to serialize 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.',
);
}

Expand Down

0 comments on commit a2b88ae

Please sign in to comment.