Skip to content

Commit

Permalink
Fix tests (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitpshr authored Jul 25, 2018
1 parent ffb4505 commit 325c429
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 16 deletions.
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');
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.

0 comments on commit 325c429

Please sign in to comment.