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

Fix tests #8

Merged
merged 1 commit into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
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
47 changes: 47 additions & 0 deletions app/common/testSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,50 @@ import Adapter from 'enzyme-adapter-react-16';
import Enzyme from 'enzyme';

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

jest.mock('react-native-fs', () => ({
CachesDirectoryPath: jest.fn(),
DocumentDirectoryPath: jest.fn(),
ExternalDirectoryPath: jest.fn(),
ExternalStorageDirectoryPath: jest.fn(),
LibraryDirectoryPath: jest.fn(),
MainBundlePath: 'testPath',
PicturesDirectoryPath: jest.fn(),
TemporaryDirectoryPath: jest.fn(),
appendFile: jest.fn(),
completeHandlerIOS: jest.fn(),
copyAssetsVideoIOS: jest.fn(),
copyFile: jest.fn(),
copyFileAssets: jest.fn(),
copyFileAssetsIOS: jest.fn(),
downloadFile: jest.fn(),
exists: jest.fn(),
existsAssets: jest.fn(),
getAllExternalFilesDirs: jest.fn(),
getFSInfo: jest.fn(),
hash: jest.fn(),
isResumable: jest.fn(),
mkdir: jest.fn(),
moveFile: jest.fn(),
pathForBundle: jest.fn(),
pathForGroup: jest.fn(),
read: jest.fn(),
readDir: jest.fn(),
readDirAssets: jest.fn(),
readFile: () =>
new Promise(resolve => {
resolve('console.log()');
}),
readFileAssets: jest.fn(),
readdir: jest.fn(),
resumeDownload: jest.fn(),
setReadable: jest.fn(),
stat: jest.fn(),
stopDownload: jest.fn(),
stopUpload: jest.fn(),
touch: jest.fn(),
unlink: jest.fn(),
uploadFiles: jest.fn(),
write: jest.fn(),
writeFile: jest.fn()
}));
2 changes: 2 additions & 0 deletions app/components/Browser/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ exports[`Browser should render correctly 1`] = `
</Component>
<WKWebView
injectedJavaScriptForMainFrameOnly={true}
javaScriptEnabled={true}
onMessage={[Function]}
onNavigationStateChange={[Function]}
openNewWindowInWebView={true}
source={
Expand Down
1 change: 1 addition & 0 deletions app/components/Browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export default class Browser extends Component {
webview = React.createRef();

async componentDidMount() {
// TODO: The presence of this async statement breaks Jest code coverage
const entryScript = await RNFS.readFile(`${RNFS.MainBundlePath}/entry.js`, 'utf8');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just remove it and turn it into a promise?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue; that improves coverage, but it's still not 100%. No workaround I tried worked so I figured we should use the cleanest syntax for now.

this.setState({ entryScript });
}
Expand Down
25 changes: 24 additions & 1 deletion app/components/Browser/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import WKWebView from 'react-native-wkwebview-reborn';
import { TextInput } from 'react-native';
import { Alert, TextInput } from 'react-native';
import { shallow } from 'enzyme';
import Browser from './';

Expand Down Expand Up @@ -78,4 +78,27 @@ describe('Browser', () => {
wrapper.find('[name="refresh"]').simulate('press');
expect(stub).toBeCalled();
});

it('should show injection approval dialog', () => {
jest.mock('Alert', () => ({ alert: jest.fn() }));
const wrapper = shallow(<Browser defaultURL="http://metamask.io" />);
wrapper.find(WKWebView).simulate('Message', { nativeEvent: {} });
wrapper.find(WKWebView).simulate('Message', {
nativeEvent: {
data: { type: 'ETHEREUM_PROVIDER_REQUEST' }
}
});
expect(Alert.alert).toHaveBeenCalled();
jest.unmock('Alert');
});

it('should inject entry script after approval', () => {
const MockWebView = { evaluateJavaScript() {} }; // eslint-disable-line no-empty-function
const stub = spyOn(MockWebView, 'evaluateJavaScript');
const wrapper = shallow(<Browser defaultURL="http://metamask.io" />);
wrapper.instance().webview = { current: MockWebView };
wrapper.setState({ entryScript: 'console.log()' });
wrapper.instance().injectEntryScript();
expect(stub).toBeCalled();
});
});
37 changes: 22 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.