Skip to content
This repository was archived by the owner on Aug 1, 2020. It is now read-only.

Add Apollo example #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions docs/example-apollo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
id: example-apollo
title: Apollo
sidebar_label: Apollo
---

```javascript
import React, { useState } from 'react';
import { gql, useMutation } from 'react-apollo';
import { MockedProvider } from '@apollo/react-testing'
import { Button, TextInput, View } from 'react-native';
import { render, fireEvent, act } from "@testing-library/react-native";
import wait from "waait";

const UPDATE_EMAIL = gql`
mutation UpdateEmail($email: String!) {
updateEmail(email: $email) {
newEmail
}
}
`;

function UpdateEmailForm() {
const [updateEmail, { data }] = useMutation(UPDATE_EMAIL);
const [email, setEmail] = useState('');

render() {
if (data) {
return (
<p>Email successfully updated</p>
)
};

return (
<View>
<TextInput
placeholder="Email"
onChangeText={(text) => setEmail({ text })}
/>
<Button onPress={() => {
return updateEmail({
variables: {
email
}
})
}} title="Submit" />
</View>
);
}
}

function renderComponent(mocks = []) {
return render(
<MockedProvider mocks={mocks} addTypename={false}>
<UpdateEmailForm />
</>
)
}

describe("UpdateEmailForm", () => {
it("should update my email", async () => {
jest.useRealTimers();

const { getByPlaceholderText, getByText, queryByText } = renderComponent();

const emailInput = getByPlaceholderText("Email");
const sendLinkButton = getByText("update my email");

fireEvent.changeText(emailInput, "[email protected]");
fireEvent.pressOut(sendLinkButton);

await act(async () => {
await wait(0);
});

expect(queryByText(/Email successfully updated/)).toBeTruthy();
});
});
```