Skip to content

Commit

Permalink
Add tests for arrays of custom scalar (#484)
Browse files Browse the repository at this point in the history
* 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
reck753 authored Jan 18, 2024
1 parent e56162f commit 5c0b160
Show file tree
Hide file tree
Showing 13 changed files with 601 additions and 5 deletions.
4 changes: 4 additions & 0 deletions packages/rescript-relay/__tests__/ProvidedVariables.res
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ module InputCArr = {
module IntStr = {
let get = () => 456
}

module IntStrArr = {
let get = () => [456]
}
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 packages/rescript-relay/__tests__/Test_parseCustomScalarArray.res
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>
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe("Provided variables", () => {
},
],
__relay_internal__pv__ProvidedVariablesIntStr: "456",
__relay_internal__pv__ProvidedVariablesIntStrArr: ["456"],
},
data: {
loggedInUser: {
Expand Down
5 changes: 4 additions & 1 deletion packages/rescript-relay/__tests__/Test_providedVariables.res
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ module Fragment = %relay(`
# Custom scalar
intStr: { type: "IntString!", provider: "ProvidedVariables.IntStr" }
# Custom scalar array
intStrArr: { type: "[IntString!]!", provider: "ProvidedVariables.IntStrArr" }
) {
someRandomArgField(bool: $bool, inputC: $inputC, inputCArr: $inputCArr, intStr: $intStr)
someRandomArgField(bool: $bool, inputC: $inputC, inputCArr: $inputCArr, intStr: $intStr, intStrArr: $intStrArr)
}
`)

Expand Down
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);
});
});
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>
}
6 changes: 5 additions & 1 deletion packages/rescript-relay/__tests__/TestsUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ module Datetime = {
module IntString = {
type t = int
@live let parse = Belt.Int.fromString
let serialize = Belt.Int.toString
let serialize = int => {
// This log is used for testing purposes - do not remove
Js.log2("serialize", int)
Belt.Int.toString(int)
}
}

type number = array<int>
Expand Down
Loading

0 comments on commit 5c0b160

Please sign in to comment.