-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7224 from beaucollins/relay-incoming-page-cursors
Ensure relayStylePagination preserves pageInfo.{start,end}Cursor if edges empty.
- Loading branch information
Showing
2 changed files
with
133 additions
and
15 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
src/utilities/policies/__tests__/relayStylePagination.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,99 @@ | ||
import { FieldFunctionOptions, InMemoryCache, isReference, makeReference } from '../../../core'; | ||
import { relayStylePagination } from '../pagination'; | ||
|
||
describe('relayStylePagination', () => { | ||
const policy = relayStylePagination(); | ||
|
||
describe('merge', () => { | ||
const merge = policy.merge; | ||
// The merge function should exist, make TS aware | ||
if (typeof merge !== 'function') { | ||
throw new Error('Expecting merge function'); | ||
} | ||
|
||
const options: FieldFunctionOptions = { | ||
args: null, | ||
fieldName: 'fake', | ||
storeFieldName: 'fake', | ||
field: null, | ||
isReference: isReference, | ||
toReference: () => undefined, | ||
storage: {}, | ||
cache: new InMemoryCache(), | ||
readField: () => undefined, | ||
canRead: () => false, | ||
mergeObjects: (existing, _incoming) => existing, | ||
}; | ||
|
||
it('should maintain endCursor and startCursor with empty edges', () => { | ||
const incoming: Parameters<typeof merge>[1] = { | ||
pageInfo: { | ||
hasPreviousPage: false, | ||
hasNextPage: true, | ||
startCursor: 'abc', | ||
endCursor: 'xyz', | ||
} | ||
}; | ||
const result = merge(undefined, incoming, options); | ||
expect(result).toEqual({ | ||
edges: [], | ||
pageInfo: { | ||
hasPreviousPage: false, | ||
hasNextPage: true, | ||
startCursor: 'abc', | ||
endCursor: 'xyz' | ||
} | ||
}); | ||
}); | ||
|
||
it('should maintain existing PageInfo when adding a page', () => { | ||
const existingEdges = [ | ||
{ cursor: 'alpha', node: makeReference("fakeAlpha") }, | ||
]; | ||
|
||
const incomingEdges = [ | ||
{ cursor: 'omega', node: makeReference("fakeOmega") }, | ||
]; | ||
|
||
const result = merge( | ||
{ | ||
edges: existingEdges, | ||
pageInfo: { | ||
hasPreviousPage: false, | ||
hasNextPage: true, | ||
startCursor: 'alpha', | ||
endCursor: 'alpha' | ||
}, | ||
}, | ||
{ | ||
edges: incomingEdges, | ||
pageInfo: { | ||
hasPreviousPage: true, | ||
hasNextPage: true, | ||
startCursor: incomingEdges[0].cursor, | ||
endCursor: incomingEdges[incomingEdges.length - 1].cursor, | ||
}, | ||
}, | ||
{ | ||
...options, | ||
args: { | ||
after: 'alpha', | ||
}, | ||
}, | ||
); | ||
|
||
expect(result).toEqual({ | ||
edges: [ | ||
...existingEdges, | ||
...incomingEdges, | ||
], | ||
pageInfo: { | ||
hasPreviousPage: false, | ||
hasNextPage: true, | ||
startCursor: 'alpha', | ||
endCursor: 'omega', | ||
} | ||
}); | ||
}); | ||
}) | ||
}); |
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