Skip to content

Commit

Permalink
Merge pull request #29 from marcelomorgado/feature/tests
Browse files Browse the repository at this point in the history
First round of tests for the demo mobile app
  • Loading branch information
pcowgill committed Dec 18, 2018
2 parents aff489b + 650c5c3 commit ec81f57
Show file tree
Hide file tree
Showing 9 changed files with 686 additions and 16 deletions.
4 changes: 3 additions & 1 deletion demo/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ module.exports = {
es6: true,
node: true,
},
plugins: ["react", "prettier", "react-native"],
plugins: ["react", "prettier", "react-native", "jest"],
extends: [
"eslint:recommended",
"plugin:react/recommended",
"plugin:react-native/all",
"prettier",
"prettier/react",
"plugin:jest/recommended",
],
rules: {
"no-console": "off",
"react-native/no-raw-text": "off",
"react/prop-types": "off",
"prettier/prettier": "error",
"jest/no-large-snapshots": ["warn", { maxSize: 20 }],
},
settings: {
react: {
Expand Down
10 changes: 9 additions & 1 deletion demo/components/presentational/EthereumSignUpForm.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import React from "react";
import { Button, StyleSheet, View, Text, TextInput } from "react-native";
import { Account } from "tasit-sdk";

export default class EthereumSignUpForm extends React.Component {
state = {
text: "",
address: "",
};

createAccount = async () => {
// TODO: Remove await when SDK 0.0.3 is out
const wallet = await Account.create();
this.setState({ address: wallet.address });
};

render() {
Expand All @@ -25,7 +33,7 @@ export default class EthereumSignUpForm extends React.Component {
</View>
</View>
<View style={styles.buttonView}>
<Button title="Continue" onPress={() => {}} />
<Button title="Continue" onPress={() => this.createAccount()} />
</View>
</React.Fragment>
);
Expand Down
22 changes: 22 additions & 0 deletions demo/components/presentational/EthereumSignUpForm.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from "react";
import NavigationTestUtils from "react-navigation/NavigationTestUtils";
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().createAccount();
expect(wrapper.state("address")).not.toEqual("");
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`EthereumSignUpForm renders the component 1`] = `
<Fragment>
<Component
style={
Object {
"flexDirection": "row",
}
}
>
<Component
style={
Object {
"alignItems": "flex-end",
"flex": 1,
}
}
>
<TextInput
allowFontScaling={true}
autoCapitalize="none"
autoCorrect={false}
onChangeText={[Function]}
placeholder="username"
style={
Object {
"fontSize": 20,
"justifyContent": "flex-start",
"width": 90,
}
}
underlineColorAndroid="transparent"
value=""
/>
</Component>
<Component
style={
Object {
"flex": 1,
}
}
>
<Component
style={
Object {
"fontSize": 20,
"justifyContent": "flex-end",
}
}
>
.tasitid.eth
</Component>
</Component>
</Component>
<Component
style={
Object {
"flexDirection": "row",
"marginTop": 30,
}
}
>
<Button
onPress={[Function]}
title="Continue"
/>
</Component>
</Fragment>
`;
124 changes: 124 additions & 0 deletions demo/jest.preprocessor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/

/* eslint-env node */

"use strict";

const { transformSync: babelTransformSync } = require("@babel/core");
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
* found when Flow v0.54 was deployed. To see the error delete this comment and
* run Flow. */
const babelRegisterOnly = require("metro-babel-register");
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
* found when Flow v0.54 was deployed. To see the error delete this comment and
* run Flow. */
const createCacheKeyFunction = require("fbjs-scripts/jest/createCacheKeyFunction");
const generate = require("@babel/generator").default;

const nodeFiles = RegExp(
[
"/local-cli/",
"/metro(?:-[^/]*)?/", // metro, metro-core, metro-source-map, metro-etc
].join("|")
);
const nodeOptions = babelRegisterOnly.config([nodeFiles]);

babelRegisterOnly([]);

/* $FlowFixMe(site=react_native_oss) */
const transformer = require("metro/src/reactNativeTransformer");
module.exports = {
process(src /*: string */, file /*: string */) {
if (nodeFiles.test(file)) {
// node specific transforms only
return babelTransformSync(src, {
filename: file,
sourceType: "script",
...nodeOptions,
ast: false,
}).code;
}

const { ast } = transformer.transform({
filename: file,
localPath: file,
options: {
ast: true, // needed for open source (?) https://github.com/facebook/react-native/commit/f8d6b97140cffe8d18b2558f94570c8d1b410d5c#r28647044
dev: true,
//inlineRequires: true,
minify: false,
platform: "",
projectRoot: "",
retainLines: true,
sourceType: "unambiguous", // b7 required. detects module vs script mode
},
src,
plugins: [
[require("@babel/plugin-transform-block-scoping")],
// the flow strip types plugin must go BEFORE class properties!
// there'll be a test case that fails if you don't.
[require("@babel/plugin-transform-flow-strip-types")],
[
require("@babel/plugin-proposal-class-properties"),
// use `this.foo = bar` instead of `this.defineProperty('foo', ...)`
{ loose: true },
],
[require("@babel/plugin-transform-computed-properties")],
[require("@babel/plugin-transform-destructuring")],
[require("@babel/plugin-transform-function-name")],
[require("@babel/plugin-transform-literals")],
[require("@babel/plugin-transform-parameters")],
[require("@babel/plugin-transform-shorthand-properties")],
[require("@babel/plugin-transform-react-jsx")],
[require("@babel/plugin-transform-regenerator")],
[require("@babel/plugin-transform-sticky-regex")],
[require("@babel/plugin-transform-unicode-regex")],
[
require("@babel/plugin-transform-modules-commonjs"),
{ strict: false, allowTopLevelThis: true },
],
[require("@babel/plugin-transform-classes")],
[require("@babel/plugin-transform-arrow-functions")],
[require("@babel/plugin-transform-spread")],
[require("@babel/plugin-proposal-object-rest-spread")],
[
require("@babel/plugin-transform-template-literals"),
{ loose: true }, // dont 'a'.concat('b'), just use 'a'+'b'
],
[require("@babel/plugin-transform-exponentiation-operator")],
[require("@babel/plugin-transform-object-assign")],
[require("@babel/plugin-transform-for-of"), { loose: true }],
[require("@babel/plugin-transform-react-display-name")],
[require("@babel/plugin-transform-react-jsx-source")],
],
});

return generate(
ast,
{
code: true,
comments: false,
compact: false,
filename: file,
retainLines: true,
sourceFileName: file,
sourceMaps: true,
},
src
).code;
},

getCacheKey: createCacheKeyFunction([
__filename,
require.resolve("metro/src/reactNativeTransformer"),
require.resolve("@babel/core/package.json"),
]),
};
4 changes: 4 additions & 0 deletions demo/jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Enzyme from "enzyme";
import Adapter from "enzyme-adapter-react-16";

Enzyme.configure({ adapter: new Adapter() });
Loading

0 comments on commit ec81f57

Please sign in to comment.