-
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.
Add tests for arrays of custom scalar (#484)
* Add array of custom scalar test * Add a test for custom scalar array serialization * Rename test for custom scalar array parse * Extend TestProvidedVariables to check array of custom scalars and apply fix from zth/relay@3f72836
- Loading branch information
Showing
13 changed files
with
601 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,7 @@ module InputCArr = { | |
module IntStr = { | ||
let get = () => 456 | ||
} | ||
|
||
module IntStrArr = { | ||
let get = () => [456] | ||
} |
32 changes: 32 additions & 0 deletions
32
packages/rescript-relay/__tests__/Test_parseCustomScalarArray-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,32 @@ | ||
require("@testing-library/jest-dom/extend-expect"); | ||
const t = require("@testing-library/react"); | ||
const React = require("react"); | ||
const queryMock = require("./queryMock"); | ||
|
||
const { | ||
test_parseCustomScalarArray, | ||
} = require("./Test_parseCustomScalarArray.bs"); | ||
|
||
describe("Parse Custom Scalar Array", () => { | ||
test("array of a custom scalars defined as modules are automatically converted", async () => { | ||
queryMock.mockQuery({ | ||
name: "TestParseCustomScalarArrayQuery", | ||
data: { | ||
loggedInUser: { | ||
id: "user-2", | ||
intStrings: ["1", "2", "3"], | ||
intString: "9", | ||
justStrings: ["10", "20", "30"], | ||
justString: "99", | ||
}, | ||
}, | ||
}); | ||
|
||
t.render(test_parseCustomScalarArray()); | ||
|
||
await t.screen.findByText("10, 20, 30"); | ||
await t.screen.findByText("9"); | ||
await t.screen.findByText("99"); | ||
await t.screen.findByText("1, 2, 3"); | ||
}); | ||
}); |
62 changes: 62 additions & 0 deletions
62
packages/rescript-relay/__tests__/Test_parseCustomScalarArray.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,62 @@ | ||
module Query = %relay(` | ||
query TestParseCustomScalarArrayQuery { | ||
loggedInUser { | ||
intStrings | ||
intString | ||
justStrings | ||
justString | ||
} | ||
} | ||
`) | ||
|
||
module Test = { | ||
@react.component | ||
let make = () => { | ||
let query = { | ||
Query.use(~variables=()) | ||
} | ||
|
||
let intStrings = query.loggedInUser.intStrings->Belt.Option.getWithDefault([]) | ||
let intString = query.loggedInUser.intString | ||
let justStrings = query.loggedInUser.justStrings->Belt.Option.getWithDefault([]) | ||
let justString = query.loggedInUser.justString | ||
|
||
<> | ||
<div> | ||
{intStrings | ||
->Belt.Array.map(Belt.Int.toString(_)) | ||
->Js.Array2.joinWith(", ") | ||
->React.string} | ||
</div> | ||
<div> | ||
{intString | ||
->Belt.Option.map(React.int(_)) | ||
->Belt.Option.getWithDefault(React.null)} | ||
</div> | ||
<div> | ||
{justStrings | ||
->Js.Array2.joinWith(", ") | ||
->React.string} | ||
</div> | ||
<div> | ||
{justString | ||
->Belt.Option.map(React.string(_)) | ||
->Belt.Option.getWithDefault(React.null)} | ||
</div> | ||
</> | ||
} | ||
} | ||
|
||
@live | ||
let test_parseCustomScalarArray = () => { | ||
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> | ||
} |
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
43 changes: 43 additions & 0 deletions
43
packages/rescript-relay/__tests__/Test_serializeCustomScalarArray-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,43 @@ | ||
require("@testing-library/jest-dom/extend-expect"); | ||
const t = require("@testing-library/react"); | ||
const React = require("react"); | ||
const queryMock = require("./queryMock"); | ||
const ReactTestUtils = require("react-dom/test-utils"); | ||
|
||
const { | ||
test_serializeCustomScalarArray, | ||
} = require("./Test_serializeCustomScalarArray.bs"); | ||
|
||
describe("Serialize Custom Scalar Array", () => { | ||
test("serializes custom scalar array", async () => { | ||
const logSpy = jest.spyOn(console, "log"); | ||
t.render(test_serializeCustomScalarArray()); | ||
|
||
const resolveQuery = queryMock.mockQueryWithControlledResolution({ | ||
name: "TestSerializeCustomScalarArrayMutation", | ||
variables: { | ||
input: [97, 98, 99], | ||
}, | ||
data: { | ||
serializeCustomScalarArray: { | ||
works: true, | ||
}, | ||
}, | ||
}); | ||
|
||
ReactTestUtils.act(() => { | ||
t.fireEvent.click(t.screen.getByText("Fire mutation")); | ||
}); | ||
|
||
await t.screen.findByText("Mutating..."); | ||
|
||
ReactTestUtils.act(() => { | ||
resolveQuery(); | ||
}); | ||
|
||
expect(logSpy).toHaveBeenCalledTimes(3); | ||
expect(logSpy).toHaveBeenNthCalledWith(1, "serialize", 97); | ||
expect(logSpy).toHaveBeenNthCalledWith(2, "serialize", 98); | ||
expect(logSpy).toHaveBeenNthCalledWith(3, "serialize", 99); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
packages/rescript-relay/__tests__/Test_serializeCustomScalarArray.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,42 @@ | ||
module Mutation = %relay(` | ||
mutation TestSerializeCustomScalarArrayMutation($input: [IntString!]!) { | ||
serializeCustomScalarArray(input: $input) { | ||
works | ||
} | ||
} | ||
`) | ||
|
||
module Test = { | ||
@react.component | ||
let make = () => { | ||
let (mutate, isMutating) = Mutation.use() | ||
|
||
<div> | ||
<button | ||
onClick={_ => { | ||
mutate(~variables={input: [97, 98, 99]})->ignore | ||
}}> | ||
{React.string("Fire mutation")} | ||
</button> | ||
{if isMutating { | ||
React.string("Mutating...") | ||
} else { | ||
React.null | ||
}} | ||
</div> | ||
} | ||
} | ||
|
||
@live | ||
let test_serializeCustomScalarArray = () => { | ||
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> | ||
} |
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
Oops, something went wrong.