-
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: relationship object serialization (#5)
- Loading branch information
1 parent
87f7d89
commit 113ea51
Showing
7 changed files
with
114 additions
and
21 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
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,35 @@ | ||
import { isRelationshipObject } from '../../../src/serializers/utils/isRelationshipObject'; | ||
import { isResourceLinkage } from '../../../src/serializers/utils/isResourceLinkage'; | ||
|
||
jest.mock('../../../src/serializers/utils/isResourceLinkage'); | ||
const isResourceLinkageMocked = jest.mocked(isResourceLinkage); | ||
|
||
describe('`isRelationshipObject`', () => { | ||
it('should return false if `relationshipObjectCandidate` is `null`', () => { | ||
const relationshipObjectCandidate = null; | ||
|
||
expect(isRelationshipObject(relationshipObjectCandidate)).toBe(false); | ||
}); | ||
|
||
it('should return false if `relationshipObjectCandidate` is not an object', () => { | ||
const relationshipObjectCandidate = 'not an object'; | ||
|
||
expect(isRelationshipObject(relationshipObjectCandidate)).toBe(false); | ||
}); | ||
|
||
it('should return false if `relationshipObjectCandidate` does not have a `data` property', () => { | ||
const relationshipObjectCandidate = {}; | ||
|
||
expect(isRelationshipObject(relationshipObjectCandidate)).toBe(false); | ||
}); | ||
|
||
it('should return true if `relationshipObjectCandidate.data` is a `JSONAPIResourceLinkage`', () => { | ||
isResourceLinkageMocked.mockReturnValue(true); | ||
|
||
const relationshipObjectCandidate = { | ||
data: 'foo', | ||
}; | ||
|
||
expect(isRelationshipObject(relationshipObjectCandidate)).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
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,23 @@ | ||
import { isResourceLinkage } from './isResourceLinkage'; | ||
|
||
import type { JSONAPIRelationshipObject } from '../../types/relationshipObject'; | ||
|
||
export const isRelationshipObject = ( | ||
relationshipObjectCandidate: unknown, | ||
): relationshipObjectCandidate is JSONAPIRelationshipObject => { | ||
if (relationshipObjectCandidate === null) { | ||
return false; | ||
} | ||
|
||
if (typeof relationshipObjectCandidate !== 'object') { | ||
return false; | ||
} | ||
|
||
if (!('data' in relationshipObjectCandidate)) { | ||
return false; | ||
} | ||
|
||
const { data } = relationshipObjectCandidate; | ||
|
||
return isResourceLinkage(data); | ||
}; |