-
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
ac6874d
commit 69e315f
Showing
2 changed files
with
170 additions
and
2 deletions.
There are no files selected for viewing
168 changes: 168 additions & 0 deletions
168
__tests__/serializers/utils/serializeResourceLinkage.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,168 @@ | ||
import { Chance } from "chance"; | ||
import { resourceSymbol } from "../../../src/decorators/resource"; | ||
import { serializeResourceLinkage } from "../../../src/serializers/serializeResourceLinkage"; | ||
import { collect } from "../../../src/serializers/utils/collect"; | ||
import { getMetadataBySymbol } from "../../../src/serializers/utils/getMetadataBySymbol"; | ||
import { idSymbol } from "../../../src"; | ||
|
||
jest.mock('../../../src/serializers/utils/getMetadataBySymbol'); | ||
const getMetadataBySymbolMocked = jest.mocked(getMetadataBySymbol); | ||
|
||
jest.mock('../../../src/serializers/utils/collect'); | ||
const collectMocked = jest.mocked(collect); | ||
|
||
describe('`serializeResourceLinkage`', () => { | ||
let chance: Chance.Chance; | ||
|
||
beforeEach(() => { | ||
chance = new Chance(); | ||
}); | ||
|
||
describe('when given an array of class instances', () => { | ||
it('should throw an error if some element in the array is not an object', () => { | ||
const classInstances = [1]; | ||
|
||
expect(() => serializeResourceLinkage(classInstances)).toThrow( | ||
'Failed to serialize resource linkage because not all elements in the array are objects.', | ||
); | ||
}); | ||
|
||
it('should throw an error if some element in the array is not a resource', () => { | ||
const classInstances = [ | ||
{ | ||
a: 'a', | ||
}, | ||
]; | ||
|
||
getMetadataBySymbolMocked.mockImplementation((_, symbol) => { | ||
if(symbol === resourceSymbol) { | ||
return undefined; | ||
} | ||
}) | ||
|
||
expect(() => serializeResourceLinkage(classInstances)).toThrow( | ||
'Failed to serialize relationship object because the provided class instance is not a resource.', | ||
); | ||
}); | ||
|
||
it('should throw an error if some element in the array does not have an id field', () => { | ||
const classInstances = [ | ||
{ | ||
a: 'a', | ||
}, | ||
]; | ||
|
||
getMetadataBySymbolMocked.mockImplementation((_, symbol) => { | ||
if(symbol === resourceSymbol) { | ||
return 'type'; | ||
} | ||
}) | ||
|
||
collectMocked.mockImplementation(() => undefined); | ||
|
||
expect(() => serializeResourceLinkage(classInstances)).toThrow( | ||
'Failed to serialize relationship object because the provided class instance does not have an id field.', | ||
); | ||
}); | ||
|
||
it('should serialize an array of class instances into an array of resource identifier objects', () => { | ||
const classInstances = [ | ||
{ | ||
someIdField: chance.string(), | ||
someTypeField: chance.string(), | ||
}, | ||
{ | ||
someIdField: chance.string(), | ||
someTypeField: chance.string(), | ||
} | ||
]; | ||
|
||
getMetadataBySymbolMocked.mockImplementation((object, symbol) => { | ||
if(symbol === resourceSymbol) { | ||
// @ts-expect-error | ||
return object.someTypeField; | ||
} | ||
}); | ||
|
||
collectMocked.mockImplementation((object, symbol) => { | ||
if(symbol === idSymbol) { | ||
// @ts-expect-error | ||
return object.someIdField; | ||
} | ||
}); | ||
|
||
const result = serializeResourceLinkage(classInstances); | ||
|
||
expect(result).toEqual( | ||
classInstances.map(({ someIdField, someTypeField }) => ({ | ||
type: someTypeField, | ||
id: someIdField, | ||
})) | ||
); | ||
}); | ||
}); | ||
|
||
describe('when given a single class instance', () => { | ||
it('should throw an error if the class instance is not a resource', () => { | ||
const classInstance = { | ||
a: 'a', | ||
}; | ||
|
||
getMetadataBySymbolMocked.mockImplementation((_, symbol) => { | ||
if(symbol === resourceSymbol) { | ||
return undefined; | ||
} | ||
}) | ||
|
||
expect(() => serializeResourceLinkage(classInstance)).toThrow( | ||
'Failed to serialize relationship object because the provided class instance is not a resource.', | ||
); | ||
}); | ||
|
||
it('should throw an error if the class instance does not have an id field', () => { | ||
const classInstance = { | ||
a: 'a', | ||
}; | ||
|
||
getMetadataBySymbolMocked.mockImplementation((_, symbol) => { | ||
if(symbol === resourceSymbol) { | ||
return 'type'; | ||
} | ||
}) | ||
|
||
collectMocked.mockImplementation(() => undefined); | ||
|
||
expect(() => serializeResourceLinkage(classInstance)).toThrow( | ||
'Failed to serialize relationship object because the provided class instance does not have an id field.', | ||
); | ||
}); | ||
|
||
it('should serialize a class instance into a resource identifier object', () => { | ||
const classInstance = { | ||
someIdField: chance.string(), | ||
someTypeField: chance.string(), | ||
}; | ||
|
||
getMetadataBySymbolMocked.mockImplementation((object, symbol) => { | ||
if(symbol === resourceSymbol) { | ||
// @ts-expect-error | ||
return object.someTypeField; | ||
} | ||
}); | ||
|
||
collectMocked.mockImplementation((object, symbol) => { | ||
if(symbol === idSymbol) { | ||
// @ts-expect-error | ||
return object.someIdField; | ||
} | ||
}); | ||
|
||
const result = serializeResourceLinkage(classInstance); | ||
|
||
expect(result).toEqual({ | ||
type: classInstance.someTypeField, | ||
id: classInstance.someIdField, | ||
}); | ||
}); | ||
}); | ||
}) |
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