Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: enzyme click bug #48

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions demo/components/presentational/EthereumSignUpForm.js
Expand Up @@ -13,10 +13,15 @@ export default class EthereumSignUpForm extends React.Component {
address: "",
};

createAccount = async () => {
// TODO: Remove await when SDK 0.0.3 is out
handleAsync = async () => {
const wallet = await Account.create();
this.setState({ address: wallet.address });
console.log("Async button clicked");
};

handleSync = () => {
this.setState({ address: "0x" });
console.log("Sync button clicked");
};

render() {
Expand All @@ -38,7 +43,8 @@ export default class EthereumSignUpForm extends React.Component {
</View>
</View>
<View style={styles.buttonView}>
<Button title="Continue" onPress={() => this.createAccount()} />
<Button title="Aync" onPress={() => this.handleAsync()} />
<Button title="Sync" onPress={() => this.handleSync()} />
</View>
</React.Fragment>
);
Expand Down
94 changes: 89 additions & 5 deletions demo/components/presentational/EthereumSignUpForm.test.js
Expand Up @@ -4,19 +4,103 @@ import { shallow } from "enzyme";
import EthereumSignUpForm from "./EthereumSignUpForm";

describe("EthereumSignUpForm", () => {
jest.useFakeTimers();
beforeEach(() => {
NavigationTestUtils.resetInternalState();
});

it("renders the component", async () => {
expect(shallow(<EthereumSignUpForm />)).toMatchSnapshot();
it("creates a wallet - calling function", async () => {
const wrapper = shallow(<EthereumSignUpForm />);
expect(wrapper.state("address")).toEqual("");
await wrapper.instance().handleAsync();
expect(wrapper.state("address")).not.toEqual("");
});

it("creates a wallet - calling function", async () => {
it("creates a wallet - pressing sync button", async () => {
const wrapper = shallow(<EthereumSignUpForm />);

expect(wrapper.state("address")).toEqual("");
await wrapper.instance().createAccount();

wrapper
.find("Button")
.find({ title: "Sync" })
.simulate("press");

wrapper.update();

expect(wrapper.state("address")).not.toEqual("");
});

it("creates a wallet - pressing async button (1)", async () => {
const wrapper = shallow(<EthereumSignUpForm />);

expect(wrapper.state("address")).toEqual("");

wrapper
.find("Button")
.find({ title: "Aync" })
.simulate("press");

await Promise.resolve();

wrapper.update();

expect(wrapper.state("address")).not.toEqual("");
});

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));

it("creates a wallet - pressing async button (2.1)", async () => {
jest.useFakeTimers();
const wrapper = shallow(<EthereumSignUpForm />);

expect(wrapper.state("address")).toEqual("");

wrapper
.find("Button")
.find({ title: "Aync" })
.simulate("press");

// Note: Bad practice
await sleep(200);

wrapper.update();

expect(wrapper.state("address")).not.toEqual("");
});

it("creates a wallet - pressing async button (2.2)", async () => {
jest.useRealTimers();
const wrapper = shallow(<EthereumSignUpForm />);

expect(wrapper.state("address")).toEqual("");

wrapper
.find("Button")
.find({ title: "Aync" })
.simulate("press");

// Note: Bad practice
await sleep(200);

wrapper.update();

expect(wrapper.state("address")).not.toEqual("");
});

it("creates a wallet - pressing async button (3)", async () => {
const wrapper = shallow(<EthereumSignUpForm />);

expect(wrapper.state("address")).toEqual("");

// Note: Has an PR to improve API with invoke() function that will work same as code below
// https://github.com/airbnb/enzyme/pull/1856
await wrapper
.find("Button")
.find({ title: "Aync" })
.prop("onPress")();

wrapper.update();

expect(wrapper.state("address")).not.toEqual("");
});
});

This file was deleted.

1 change: 1 addition & 0 deletions demo/package.json
Expand Up @@ -10,6 +10,7 @@
"ios": "npx expo start --ios",
"eject": "npx expo eject",
"test": "npx jest",
"test:bug": "npx jest components/presentational/EthereumSignUpForm.test.js --noStackTrace",
"lint": "npx prettier --write '{*.js,**/*.js,*.jsx,**/*.jsx}' && npx eslint '{*.js,**/*.js,*.jsx,**/*.jsx,**/*.snap}'"
},
"dependencies": {
Expand Down