-
Notifications
You must be signed in to change notification settings - Fork 18
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 #129 from Arnarkari93/mocked-provider
Bind mocked provider
- Loading branch information
Showing
8 changed files
with
120 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
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,5 @@ | ||
module Core = ApolloClient__Testing_Core | ||
module Types = ApolloClient__Testing_Types | ||
module MockedProvider = ApolloClient__Testing_React.MockedProvider | ||
|
||
let makeResult = Types.makeResult |
39 changes: 39 additions & 0 deletions
39
src/@apollo/client/testing/ApolloClient__Testing_Types.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,39 @@ | ||
module Graphql = ApolloClient__Graphql | ||
module ApolloError = ApolloClient__Errors_ApolloError | ||
|
||
type rec request<'jsVariables> = { | ||
query: Graphql.documentNode, | ||
variables: 'jsVariables, | ||
} | ||
|
||
type result | ||
|
||
type mock<'jsVariables> = { | ||
request: request<'jsVariables>, | ||
result: result, | ||
} | ||
|
||
type queryResult = { | ||
data: Js.Nullable.t<Js.Json.t>, | ||
error: Js.Nullable.t<ApolloError.t>, | ||
loading: bool, | ||
} | ||
|
||
external mockResult: queryResult => result = "%identity" | ||
|
||
let makeResult = ( | ||
~data: option<'jsData>=?, | ||
~error: option<ApolloError.t>=?, | ||
~loading=false, | ||
toJson: 'jsData => Js.Json.t, | ||
): result => { | ||
let queryResult = { | ||
data: data->Belt.Option.mapWithDefault(Js.Nullable.null, data => | ||
data->toJson->Js.Nullable.return | ||
), | ||
error: error->Belt.Option.mapWithDefault(Js.Nullable.null, Js.Nullable.return), | ||
loading: loading, | ||
} | ||
|
||
mockResult(queryResult) | ||
} |
6 changes: 6 additions & 0 deletions
6
src/@apollo/client/testing/core/ApolloClient__Testing_Core.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,6 @@ | ||
module Link = ApolloClient__Link_Core_ApolloLink | ||
module Types = ApolloClient__Testing_Types | ||
|
||
@new @module("@apollo/client/testing") | ||
external mockLink: (~mocks: array<Types.mock<'jsVariables>>, ~addTypename: bool) => Link.t = | ||
"MockLink" |
1 change: 1 addition & 0 deletions
1
src/@apollo/client/testing/react/ApolloClient__Testing_React.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 @@ | ||
module MockedProvider = ApolloClient__Testing_React_MockedProvider |
55 changes: 55 additions & 0 deletions
55
src/@apollo/client/testing/react/ApolloClient__Testing_React_MockedProvider.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,55 @@ | ||
module InMemoryCache = ApolloClient__Cache_InMemory_InMemoryCache | ||
module ApolloClient = ApolloClient__Core_ApolloClient | ||
module ApolloProvider = ApolloClient__React_Context_ApolloProvider | ||
module Core = ApolloClient__Testing_Core | ||
module Types = ApolloClient__Testing_Types | ||
|
||
// export interface MockedProviderProps<TSerializedCache = {}> { | ||
// mocks?: ReadonlyArray<MockedResponse>; | ||
// addTypename?: boolean; | ||
// defaultOptions?: DefaultOptions; | ||
// cache?: ApolloCache<TSerializedCache>; | ||
// resolvers?: Resolvers; | ||
// childProps?: object; | ||
// children?: any; | ||
// link?: ApolloLink; | ||
// } | ||
|
||
/* Then making an ApolloClient in Rescript, additional wrapper methods (e.g. `rescript_query`) | ||
* are created for the underlying javascript methods. So for the client to be set up correctly, | ||
* we need to do so by calling the rescript method. | ||
*/ | ||
@react.component | ||
let make = ( | ||
~addTypename=true, | ||
~cache=?, | ||
~childProps=?, | ||
~children, | ||
~defaultOptions=?, | ||
~link=?, | ||
~mocks: array<Types.mock<'jsVariables>>, | ||
~resolvers=?, | ||
) => { | ||
let client = React.useRef( | ||
ApolloClient.make( | ||
~cache=cache->Belt.Option.getWithDefault(InMemoryCache.make(~addTypename, ())), | ||
~defaultOptions?, | ||
~link=link->Belt.Option.getWithDefault(Core.mockLink(~mocks, ~addTypename)), | ||
~resolvers?, | ||
(), | ||
), | ||
) | ||
|
||
React.useEffect0(() => { | ||
Some(client.current.stop) | ||
}) | ||
|
||
<ApolloProvider client=client.current> | ||
{React.cloneElement( | ||
React.Children.only(children), | ||
childProps->Belt.Option.mapWithDefault(Js.Obj.empty(), props => | ||
Js.Obj.assign(props, Js.Obj.empty()) | ||
), | ||
)} | ||
</ApolloProvider> | ||
} |
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