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

Convert WelcomeScreen to hooks and Typescript #943

Merged
merged 3 commits into from
May 30, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"react/prop-types": 0,
"react/display-name": 0,
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/camelcase": "off",
"@typescript-eslint/indent": "off",
"@typescript-eslint/no-use-before-define": "off",
Expand Down
2 changes: 2 additions & 0 deletions __mock__/react-native-firebase.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
jest.mock('react-native-firebase', () => ({
links: jest.fn(),
onLink: jest.fn(),
getInitialLink: jest.fn(() => Promise.resolve('firebaseDeepLinkUri')),
}));
1 change: 1 addition & 0 deletions __mock__/react-native-omniture.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ jest.mock('react-native-omniture', () => ({
trackState: jest.fn(),
syncIdentifier: jest.fn(),
loadMarketingCloudId: jest.fn(),
collectLifecycleData: jest.fn(),
}));
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"react-native-vector-icons": "^6.4.2",
"react-native-view-overflow": "^0.0.4",
"react-navigation": "3.11.0",
"react-navigation-hooks": "https://github.com/react-navigation/react-navigation-hooks#master",
"react-navigation-redux-helpers": "^3.0.2",
"react-redux": "^5.1.1",
"redux": "^4.0.1",
Expand All @@ -84,10 +85,14 @@
"@babel/plugin-transform-runtime": "^7.4.4",
"@brainly/onesky-utils": "^1.3.1",
"@types/color": "^3.0.0",
"@types/enzyme": "^3.9.3",
"@types/enzyme-adapter-react-16": "^1.0.5",
"@types/jest": "^24.0.13",
"@types/react": "^16.8.18",
"@types/react-native": "^0.57.60",
"@types/react-redux": "^7.0.9",
"@types/react-test-renderer": "^16.8.1",
"@types/redux-mock-store": "^1.0.1",
"@typescript-eslint/eslint-plugin": "^1.9.0",
"@typescript-eslint/parser": "^1.9.0",
"appcenter-cli": "^1.1.18",
Expand All @@ -109,6 +114,7 @@
"prettier": "^1.17.1",
"pretty-quick": "^1.11.0",
"react-dom": "^16.8.6",
"react-native-testing-library": "^1.7.0",
"react-test-renderer": "^16.8.6",
"redux-mock-store": "^1.5.3",
"typescript": "^3.4.5"
Expand Down
4 changes: 2 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ export default class App extends Component {
} else {
rollbar.error(Error(`Unknown Error:\n${JSON.stringify(e, null, 2)}`));
}

global.LOG(e);
// @ts-ignore
LOG(e);
}

showOfflineAlert = () => {
Expand Down
95 changes: 0 additions & 95 deletions src/containers/WelcomeScreen/__tests__/WelcomeScreen.js

This file was deleted.

91 changes: 91 additions & 0 deletions src/containers/WelcomeScreen/__tests__/WelcomeScreen.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import 'react-native';
import React from 'react';
import { fireEvent } from 'react-native-testing-library';

import WelcomeScreen from '..';

import { renderWithContext, snapshotWithContext } from '../../../../testUtils';
import * as common from '../../../utils/common';
import { trackActionWithoutData } from '../../../actions/analytics';
import { ACTIONS } from '../../../constants';

const next = jest.fn(() => () => {});

jest.mock('react-native-device-info');
jest.mock('../../../actions/analytics');
jest.mock('../../../components/BottomButton', () => 'BottomButton');
jest.mock('../../../components/common', () => ({
Flex: 'Flex',
Text: 'Text',
Button: 'Button',
}));

// @ts-ignore
common.disableBack = { add: jest.fn(), remove: jest.fn() };

(trackActionWithoutData as jest.Mock).mockReturnValue({
type: 'tracked action without data',
});

describe('WelcomeScreen', () => {
const allowSignInVariantConfig = {
navParams: { allowSignIn: true },
};

it('should render correctly', () => {
snapshotWithContext(<WelcomeScreen next={next} />);
});

it('should render correctly with sign in button', () => {
snapshotWithContext(
<WelcomeScreen next={next} />,
allowSignInVariantConfig,
);
});

it('getStarted btn should call next', () => {
const { getByTestId } = renderWithContext(<WelcomeScreen next={next} />);
fireEvent(getByTestId('get-started'), 'onPress');

expect(common.disableBack.remove).toHaveBeenCalledTimes(1);
expect(next).toHaveBeenCalledTimes(1);
expect(next).toHaveBeenCalledWith({ signin: false });
});

it('tryItNow btn should call next', () => {
const { getByTestId } = renderWithContext(
<WelcomeScreen next={next} />,
allowSignInVariantConfig,
);
fireEvent(getByTestId('get-started-sign-in-variant'), 'onPress');

expect(common.disableBack.remove).toHaveBeenCalledTimes(1);
expect(next).toHaveBeenCalledTimes(1);
expect(next).toHaveBeenCalledWith({ signin: false });
});

it('signIn btn should call next with signIn = true', () => {
const { getByTestId } = renderWithContext(
<WelcomeScreen next={next} />,
allowSignInVariantConfig,
);
fireEvent(getByTestId('sign-in'), 'onPress');
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting. This might simplify a lot of the testing set-up. I gotta get familiar with react-native-testing-library as well!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ya let's have a conversation about testing sometime :) I like the new helper function and expanded upon it and fixed a few things in my graphql branch. I think react-native-testing-library is a good compromise of old and new. We can mock stuff to approximate shallow rendering and shallow snapshots. But it tries to keep you from calling implementation details directly.

I've also looked at native-testing-library but it's a lot more opinionated. It pushes you towards integration testing and doesn't support events on mocked components. testing-library/native-testing-library#17

Enzyme just still doesn't support the new React Context APIs used by a lot of the newer libraries or Hooks...


expect(next).toHaveBeenCalledTimes(1);
expect(next).toHaveBeenCalledWith({ signin: true });
});

it('should fire analytics event on mount', () => {
renderWithContext(<WelcomeScreen next={next} />);

expect(trackActionWithoutData).toHaveBeenCalledWith(
ACTIONS.ONBOARDING_STARTED,
);
});

it('should clean up back handler on unmount', () => {
renderWithContext(<WelcomeScreen next={next} />).unmount();

expect(common.disableBack.remove).toHaveBeenCalledTimes(1);
});
});