Skip to content

Infra/test renderer #1470

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

Merged
merged 6 commits into from
Oct 4, 2021
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"@react-native-community/netinfo": "^5.6.2",
"@react-native-picker/picker": "^1.9.4",
"@testing-library/react-hooks": "^3.4.2",
"@testing-library/react-native": "^7.2.0",
"@types/hoist-non-react-statics": "^3.3.1",
"@types/lodash": "^4.0.0",
"@types/prop-types": "^15.5.3",
Expand Down
62 changes: 62 additions & 0 deletions src/components/hint/__tests__/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React from 'react';
import {Hint} from 'react-native-ui-lib';
import {Colors} from '../../../style';
import {render, findStyle} from '../../../uilib-test-renderer';

const HintTestComponent = ({showHint}) => {
return (
<Hint
visible={showHint}
message={'Hint message to hint things'}
position={Hint.positions.TOP}
useSideTip
key={'1'}
targetFrame={{x: 1, y: 1, height: 1, width: 1}}
onBackgroundPress={() => {}}
color={Colors.white}
removePaddings
enableShadow
testID={'Hint'}
/>
);
};

describe('Hint Screen component test', () => {
it('Test Hint component background color', async () => {
const hintTestId = 'Hint.message';

const expectedColor = Colors.primary;
const element = <HintTestComponent showHint/>;

const {getByTestId} = render(element);

const wrapper = getByTestId('Hint');
expect(wrapper).toBeTruthy();

const hint = getByTestId(hintTestId);
const color = findStyle('backgroundColor', hint);

expect(color).toBe(expectedColor);
expect(hint).toBeTruthy();
});

it('Test Hint modal is not visible when showHint is false', async () => {
const element = <HintTestComponent showHint={false}/>;

const {queryByTestId} = render(element);

const modal = queryByTestId('Hint.modal');

expect(modal).toBeNull();
});

it('Test Hint modal is visible when showHint is true', async () => {
const element = <HintTestComponent showHint/>;

const {getByTestId, queryAllByTestId} = render(element);
const hint = getByTestId('Hint');
expect(hint).toBeTruthy();

expect(queryAllByTestId('Hint.modal')).toBeTruthy();
});
});
17 changes: 17 additions & 0 deletions src/uilib-test-renderer/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import _ from 'lodash';

interface Props {
style: any;
}

interface Component {
props: Props;
}

const findStyle = <T>(key: string, component: Component): T => {
const flat = _.flatMap(component.props.style) as Array<any | undefined>;
const color = _.find(flat, key)[key];
return color;
};

export {findStyle};
8 changes: 8 additions & 0 deletions src/uilib-test-renderer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export * from '@testing-library/react-native';
import {findStyle} from './helper';

export {
findStyle
};

/* Docs: https://callstack.github.io/react-native-testing-library/docs/api/ */