-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Followup of #9053
- Loading branch information
Showing
3 changed files
with
75 additions
and
8 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
55 changes: 55 additions & 0 deletions
55
packages/twenty-front/src/modules/apollo/utils/__tests__/encodeCursor.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,55 @@ | ||
import { encodeCursor } from '@/apollo/utils/encodeCursor'; | ||
import { ObjectRecord } from '@/object-record/types/ObjectRecord'; | ||
|
||
describe('encodeCursor', () => { | ||
it('should create a cursor with id only', () => { | ||
const record: ObjectRecord = { __typename: 'ObjectRecord', id: '123' }; | ||
const cursor = encodeCursor(record); | ||
const decoded = JSON.parse(Buffer.from(cursor, 'base64').toString('utf-8')); | ||
|
||
expect(decoded).toEqual({ id: '123' }); | ||
}); | ||
|
||
it('should create a cursor with id and position', () => { | ||
const record: ObjectRecord = { | ||
__typename: 'ObjectRecord', | ||
id: '123', | ||
position: 1, | ||
}; | ||
const cursor = encodeCursor(record); | ||
const decoded = JSON.parse(Buffer.from(cursor, 'base64').toString('utf-8')); | ||
|
||
expect(decoded).toEqual({ id: '123', position: 1 }); | ||
}); | ||
|
||
it('should create a cursor with id and position as 0', () => { | ||
const record: ObjectRecord = { | ||
__typename: 'ObjectRecord', | ||
id: '123', | ||
position: 0, | ||
}; | ||
const cursor = encodeCursor(record); | ||
const decoded = JSON.parse(Buffer.from(cursor, 'base64').toString('utf-8')); | ||
|
||
expect(decoded).toEqual({ id: '123', position: 0 }); | ||
}); | ||
|
||
it('should create a cursor with id and ignore extra fields', () => { | ||
const record: ObjectRecord = { | ||
__typename: 'ObjectRecord', | ||
id: '123', | ||
position: 1, | ||
extra: 'extra', | ||
}; | ||
const cursor = encodeCursor(record); | ||
const decoded = JSON.parse(Buffer.from(cursor, 'base64').toString('utf-8')); | ||
|
||
expect(decoded).toEqual({ id: '123', position: 1 }); | ||
}); | ||
|
||
it('should throw an error if record does not have an id', () => { | ||
const record = { position: 1 } as any; | ||
|
||
expect(() => encodeCursor(record)).toThrow(); | ||
}); | ||
}); |
18 changes: 18 additions & 0 deletions
18
packages/twenty-front/src/modules/apollo/utils/encodeCursor.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,18 @@ | ||
import { ObjectRecord } from '@/object-record/types/ObjectRecord'; | ||
import { isDefined } from '~/utils/isDefined'; | ||
|
||
export const encodeCursor = (record: ObjectRecord) => { | ||
if (!('id' in record) || !isDefined(record.id)) { | ||
throw new Error('Record does not have an id'); | ||
} | ||
|
||
const payload: { | ||
id: string; | ||
position?: number; | ||
} = { | ||
position: record.position, | ||
id: record.id, | ||
}; | ||
|
||
return Buffer.from(JSON.stringify(payload), 'utf-8').toString('base64'); | ||
}; |