-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a618015
commit 0442932
Showing
1 changed file
with
121 additions
and
0 deletions.
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
__tests__/serializers/deserializeResourceObject.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { Chance } from 'chance'; | ||
import { attributesSymbol } from '../../src/decorators/attribute'; | ||
import { idSymbol } from '../../src/decorators/id'; | ||
import { linksSymbol } from '../../src/decorators/link'; | ||
import { metaSymbol } from '../../src/decorators/meta'; | ||
import { relationshipsSymbol } from '../../src/decorators/relationship'; | ||
import { resourceSymbol } from '../../src/decorators/resource'; | ||
import { deserializeResourceObject } from '../../src/serializers/deserializeResourceObject'; | ||
import { getMetadataBySymbol } from '../../src/serializers/utils/getMetadataBySymbol'; | ||
|
||
import type { JSONAPIResourceLinkage } from '../../src/types/resourceLinkage'; | ||
|
||
jest.mock('../../src/serializers/utils/getMetadataBySymbol'); | ||
const getMetadataBySymbolMocked = jest.mocked(getMetadataBySymbol); | ||
|
||
describe('`deserializeResourceObject`', () => { | ||
let chance: Chance.Chance; | ||
|
||
beforeEach(() => { | ||
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; | ||
} | ||
}); | ||
|
||
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', () => { | ||
class SomeResource { | ||
someIdField!: string; | ||
someAttributeField!: string; | ||
someLinkField!: string; | ||
someMetaField!: string; | ||
someRelationshipField!: JSONAPIResourceLinkage; | ||
} | ||
|
||
const classInstance = new SomeResource(); | ||
classInstance.someIdField = chance.string(); | ||
classInstance.someAttributeField = chance.string(); | ||
classInstance.someLinkField = chance.string(); | ||
classInstance.someMetaField = chance.string(); | ||
|
||
const type = chance.string(); | ||
|
||
getMetadataBySymbolMocked.mockImplementation((_, symbol) => { | ||
if (symbol === resourceSymbol) { | ||
return type; | ||
} | ||
|
||
if (symbol === idSymbol) { | ||
return 'someIdField'; | ||
} | ||
|
||
if (symbol === attributesSymbol) { | ||
return ['someAttributeField']; | ||
} | ||
|
||
if (symbol === linksSymbol) { | ||
return ['someLinkField']; | ||
} | ||
|
||
if (symbol === metaSymbol) { | ||
return ['someMetaField']; | ||
} | ||
|
||
if (symbol === relationshipsSymbol) { | ||
return [['someRelationshipField']]; | ||
} | ||
}); | ||
|
||
const resourceObject = { | ||
type, | ||
id: classInstance.someIdField, | ||
attributes: { | ||
someAttributeField: classInstance.someAttributeField, | ||
}, | ||
links: { | ||
someLinkField: classInstance.someLinkField, | ||
}, | ||
meta: { | ||
someMetaField: classInstance.someMetaField, | ||
}, | ||
relationships: { | ||
someRelationshipField: { | ||
data: { | ||
type: chance.string(), | ||
id: chance.string(), | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const result = deserializeResourceObject(resourceObject, SomeResource); | ||
|
||
expect(result.someIdField).toBe(classInstance.someIdField); | ||
expect(result.someAttributeField).toBe(classInstance.someAttributeField); | ||
expect(result.someLinkField).toBe(classInstance.someLinkField); | ||
expect(result.someMetaField).toBe(classInstance.someMetaField); | ||
expect(result.someRelationshipField).toBe( | ||
resourceObject.relationships.someRelationshipField.data, | ||
); | ||
}); | ||
}); |