-
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.
fix: serializing resource linkage returns itself if already a linkage (…
…#4)
- Loading branch information
1 parent
4a6343b
commit c10507d
Showing
6 changed files
with
184 additions
and
2 deletions.
There are no files selected for viewing
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
80 changes: 80 additions & 0 deletions
80
__tests__/serializers/utils/isResourceIdentifierObject.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,80 @@ | ||
import { isResourceIdentifierObject } from '../../../src/serializers/utils/isResourceIdentifierObject'; | ||
|
||
describe('`isResourceIdentifierObject`', () => { | ||
it('it should return `false` if `resourceIdentifierObjectCandidate` is null', () => { | ||
const resourceIdentifierObjectCandidate = null; | ||
|
||
expect(isResourceIdentifierObject(resourceIdentifierObjectCandidate)).toBe( | ||
false, | ||
); | ||
}); | ||
|
||
it('it should return `false` if `resourceIdentifierObjectCandidate` is not an object', () => { | ||
const resourceIdentifierObjectCandidate = 'not an object'; | ||
|
||
expect(isResourceIdentifierObject(resourceIdentifierObjectCandidate)).toBe( | ||
false, | ||
); | ||
}); | ||
|
||
it('it should return `false` if `resourceIdentifierObjectCandidate` is an array', () => { | ||
const resourceIdentifierObjectCandidate: unknown[] = []; | ||
|
||
expect(isResourceIdentifierObject(resourceIdentifierObjectCandidate)).toBe( | ||
false, | ||
); | ||
}); | ||
|
||
it('it should return `false` if `resourceIdentifierObjectCandidate` does not have a `type` property', () => { | ||
const resourceIdentifierObjectCandidate = { | ||
id: '1', | ||
}; | ||
|
||
expect(isResourceIdentifierObject(resourceIdentifierObjectCandidate)).toBe( | ||
false, | ||
); | ||
}); | ||
|
||
it('it should return `false` if `resourceIdentifierObjectCandidate.type` is not a string', () => { | ||
const resourceIdentifierObjectCandidate = { | ||
type: 1, | ||
id: '1', | ||
}; | ||
|
||
expect(isResourceIdentifierObject(resourceIdentifierObjectCandidate)).toBe( | ||
false, | ||
); | ||
}); | ||
|
||
it('it should return `false` if `resourceIdentifierObjectCandidate` does not have an `id` property', () => { | ||
const resourceIdentifierObjectCandidate = { | ||
type: 'test', | ||
}; | ||
|
||
expect(isResourceIdentifierObject(resourceIdentifierObjectCandidate)).toBe( | ||
false, | ||
); | ||
}); | ||
|
||
it('it should return `false` if `resourceIdentifierObjectCandidate.id` is not a string', () => { | ||
const resourceIdentifierObjectCandidate = { | ||
type: 'test', | ||
id: 1, | ||
}; | ||
|
||
expect(isResourceIdentifierObject(resourceIdentifierObjectCandidate)).toBe( | ||
false, | ||
); | ||
}); | ||
|
||
it('it should return `true` if `resourceIdentifierObjectCandidate` is a valid JSON:API Resource Identifier Object', () => { | ||
const resourceIdentifierObjectCandidate = { | ||
type: 'test', | ||
id: '1', | ||
}; | ||
|
||
expect(isResourceIdentifierObject(resourceIdentifierObjectCandidate)).toBe( | ||
true, | ||
); | ||
}); | ||
}); |
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,33 @@ | ||
import { isResourceLinkage } from '../../../src/serializers/utils/isResourceLinkage'; | ||
|
||
import { isResourceIdentifierObject } from '../../../src/serializers/utils/isResourceIdentifierObject'; | ||
|
||
jest.mock('../../../src/serializers/utils/isResourceIdentifierObject'); | ||
|
||
const isResourceIdentifierObjectMocked = jest.mocked( | ||
isResourceIdentifierObject, | ||
); | ||
|
||
describe('`isResourceLinkage`', () => { | ||
it('should return true if `resourceLinkageCandidate` is `null`', () => { | ||
const resourceLinkageCandidate = null; | ||
|
||
expect(isResourceLinkage(resourceLinkageCandidate)).toBe(true); | ||
}); | ||
|
||
it('should return true if `resourceLinkageCandidate` is an array of `JSONAPIResourceIdentifierObject`', () => { | ||
isResourceIdentifierObjectMocked.mockReturnValue(true); | ||
|
||
const resourceLinkageCandidate = ['foo', 'bar']; | ||
|
||
expect(isResourceLinkage(resourceLinkageCandidate)).toBe(true); | ||
}); | ||
|
||
it('should return true if `resourceLinkageCandidate` is a `JSONAPIResourceIdentifierObject`', () => { | ||
isResourceIdentifierObjectMocked.mockReturnValue(true); | ||
|
||
const resourceLinkageCandidate = 'foo'; | ||
|
||
expect(isResourceLinkage(resourceLinkageCandidate)).toBe(true); | ||
}); | ||
}); |
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
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,34 @@ | ||
import type { JSONAPIResourceIdentifierObject } from '../../types/resourceIdentifierObject'; | ||
|
||
export const isResourceIdentifierObject = ( | ||
resourceIdentifierObjectCandidate: unknown, | ||
): resourceIdentifierObjectCandidate is JSONAPIResourceIdentifierObject => { | ||
if (resourceIdentifierObjectCandidate === null) { | ||
return false; | ||
} | ||
|
||
if ( | ||
typeof resourceIdentifierObjectCandidate !== 'object' || | ||
Array.isArray(resourceIdentifierObjectCandidate) | ||
) { | ||
return false; | ||
} | ||
|
||
if ('type' in resourceIdentifierObjectCandidate === false) { | ||
return false; | ||
} | ||
|
||
if (typeof resourceIdentifierObjectCandidate.type !== 'string') { | ||
return false; | ||
} | ||
|
||
if ('id' in resourceIdentifierObjectCandidate === false) { | ||
return false; | ||
} | ||
|
||
if (typeof resourceIdentifierObjectCandidate.id !== 'string') { | ||
return false; | ||
} | ||
|
||
return true; | ||
}; |
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,17 @@ | ||
import { isResourceIdentifierObject } from './isResourceIdentifierObject'; | ||
|
||
import type { JSONAPIResourceLinkage } from '../../types/resourceLinkage'; | ||
|
||
export const isResourceLinkage = ( | ||
resourceLinkageCandidate: unknown, | ||
): resourceLinkageCandidate is JSONAPIResourceLinkage => { | ||
if (resourceLinkageCandidate === null) { | ||
return true; | ||
} | ||
|
||
if (Array.isArray(resourceLinkageCandidate)) { | ||
return resourceLinkageCandidate.every(isResourceIdentifierObject); | ||
} | ||
|
||
return isResourceIdentifierObject(resourceLinkageCandidate); | ||
}; |