-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
usePrefetchableForwardPagination (#551)
* wip * fix test + changelog * changelog about useBlockingPagination
- Loading branch information
Showing
12 changed files
with
1,183 additions
and
81 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
94 changes: 94 additions & 0 deletions
94
packages/rescript-relay/__tests__/Test_prefetchablePagination-tests.js
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,94 @@ | ||
const t = require("@testing-library/react"); | ||
const React = require("react"); | ||
const queryMock = require("./queryMock"); | ||
const ReactTestUtils = require("react-dom/test-utils"); | ||
|
||
const { | ||
test_prefetchablePagination, | ||
} = require("./Test_prefetchablePagination.bs"); | ||
|
||
describe("Prefetchable pagination", () => { | ||
test("prefetching works", async () => { | ||
queryMock.mockQuery({ | ||
name: "TestPrefetchablePaginationQuery", | ||
data: { | ||
loggedInUser: { | ||
__typename: "User", | ||
id: "user-1", | ||
friendsConnection: { | ||
pageInfo: { | ||
endCursor: "2", | ||
hasNextPage: true, | ||
startCursor: "", | ||
hasPreviousPage: false, | ||
}, | ||
edges: [ | ||
{ | ||
cursor: "1", | ||
node: { | ||
id: "user-2", | ||
__typename: "User", | ||
}, | ||
}, | ||
{ | ||
cursor: "2", | ||
node: { | ||
id: "user-3", | ||
__typename: "User", | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
queryMock.mockQuery({ | ||
name: "TestPrefetchablePaginationRefetchQuery", | ||
variables: { | ||
count: 2, | ||
cursor: "2", | ||
id: "user-1", | ||
}, | ||
data: { | ||
node: { | ||
__typename: "User", | ||
id: "user-1", | ||
friendsConnection: { | ||
pageInfo: { | ||
endCursor: "4", | ||
hasNextPage: false, | ||
startCursor: "", | ||
hasPreviousPage: false, | ||
}, | ||
edges: [ | ||
{ | ||
cursor: "3", | ||
node: { | ||
id: "user-4", | ||
__typename: "User", | ||
}, | ||
}, | ||
{ | ||
cursor: "4", | ||
node: { | ||
id: "user-5", | ||
__typename: "User", | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
t.render(test_prefetchablePagination()); | ||
await t.screen.findByText("user-2, user-3"); | ||
|
||
ReactTestUtils.act(() => { | ||
t.fireEvent.click(t.screen.getByText("Load more")); | ||
}); | ||
|
||
await t.screen.findByText("user-2, user-3, user-4, user-5"); | ||
}); | ||
}); |
67 changes: 67 additions & 0 deletions
67
packages/rescript-relay/__tests__/Test_prefetchablePagination.res
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,67 @@ | ||
module Query = %relay(` | ||
query TestPrefetchablePaginationQuery { | ||
loggedInUser { | ||
...TestPrefetchablePagination_user | ||
} | ||
} | ||
`) | ||
|
||
module Fragment = %relay(` | ||
fragment TestPrefetchablePagination_user on User | ||
@refetchable(queryName: "TestPrefetchablePaginationRefetchQuery") | ||
@argumentDefinitions( | ||
count: { type: "Int", defaultValue: 2 } | ||
cursor: { type: "String" } | ||
) { | ||
friendsConnection( | ||
first: $count | ||
after: $cursor | ||
) @connection(key: "TestPrefetchablePagination_friendsConnection", prefetchable_pagination: true) { | ||
edges { | ||
node { | ||
id | ||
} | ||
} | ||
} | ||
} | ||
`) | ||
|
||
module Test = { | ||
@react.component | ||
let make = () => { | ||
let query = Query.use(~variables=()) | ||
let {edges, hasNext, isLoadingNext, loadNext} = Fragment.usePrefetchableForwardPagination( | ||
query.loggedInUser.fragmentRefs, | ||
~bufferSize=2, | ||
) | ||
|
||
<div> | ||
<div> | ||
{edges | ||
->Belt.Array.keepMap(({node}) => node) | ||
->Belt.Array.map(friend => friend.id) | ||
->Js.Array2.joinWith(", ") | ||
->React.string} | ||
</div> | ||
{hasNext | ||
? <button onClick={_ => loadNext(~count=2)->RescriptRelay.Disposable.ignore}> | ||
{React.string(isLoadingNext ? "Loading..." : "Load more")} | ||
</button> | ||
: React.null} | ||
</div> | ||
} | ||
} | ||
|
||
@live | ||
let test_prefetchablePagination = () => { | ||
let network = RescriptRelay.Network.makePromiseBased(~fetchFunction=RelayEnv.fetchQuery) | ||
|
||
let environment = RescriptRelay.Environment.make( | ||
~network, | ||
~store=RescriptRelay.Store.make(~source=RescriptRelay.RecordSource.make()), | ||
) | ||
|
||
<TestProviders.Wrapper environment> | ||
<Test /> | ||
</TestProviders.Wrapper> | ||
} |
Oops, something went wrong.