Skip to content

Commit

Permalink
feat: added button export and added test (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
wootsbot authored Dec 23, 2023
1 parent 8b58798 commit f7a027c
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 26 deletions.
6 changes: 4 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module.exports = {
modulePathIgnorePatterns: [
"<rootDir>/node_modules",
"<rootDir>/packages/(.+)/dist/",
"<rootDir>/packages/@blocks-unstyled",
],
// collectCoverageFrom: [
// "packages/**/*.{js,jsx,ts,tsx}",
Expand All @@ -12,5 +11,8 @@ module.exports = {
globals: {
__DEV__: true,
},
setupFilesAfterEnv: ["@testing-library/jest-native/extend-expect"],
setupFilesAfterEnv: [
"@testing-library/jest-native/extend-expect",
"@testing-library/react-native/extend-expect",
],
};
4 changes: 4 additions & 0 deletions packages/@blocks-unstyled/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@
"react-native-builder-bob": {
"source": "src",
"output": "build",
"exclude": [
"**/*.test.tsx",
"**/*.test.ts"
],
"targets": [
"commonjs",
"module",
Expand Down
17 changes: 0 additions & 17 deletions packages/@blocks-unstyled/src/Button/Button.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion packages/@blocks-unstyled/src/Button/ButtonContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import type { ButtonContextProps } from './Button.types';

export const ButtonContext = createContext<ButtonContextProps>(null as any);

ButtonContext.displayName = 'BlockButton.Context';
ButtonContext.displayName = 'Block.ButtonContext';
2 changes: 1 addition & 1 deletion packages/@blocks-unstyled/src/Button/ButtonLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ const styles = StyleSheet.create({
},
});

ButtonLabel.displayName = 'BlockButton.Label';
ButtonLabel.displayName = 'Block.ButtonLabel';
2 changes: 1 addition & 1 deletion packages/@blocks-unstyled/src/Button/ButtonLoading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ const styles = StyleSheet.create({
root: {},
});

ButtonLoading.displayName = 'BlockButton.Loading';
ButtonLoading.displayName = 'Block.ButtonLoading';
2 changes: 1 addition & 1 deletion packages/@blocks-unstyled/src/Button/ButtonRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ const styles = StyleSheet.create({
},
});

ButtonRoot.displayName = 'BlockButton.Root';
ButtonRoot.displayName = 'Block.Button';
12 changes: 10 additions & 2 deletions packages/@blocks-unstyled/src/Button/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
export * from './Button';
import { ButtonLabel } from './ButtonLabel';
import { ButtonLoading } from './ButtonLoading';
import { ButtonRoot } from './ButtonRoot';

export * from './Button.types';
export { default } from './Button';

const Root = ButtonRoot;
const Label = ButtonLabel;
const Loading = ButtonLoading;

export { Root, Label, Loading };
144 changes: 144 additions & 0 deletions packages/@blocks-unstyled/src/Button/tests/Button.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import React from 'react';

import { fireEvent } from '@testing-library/react-native';
import { render, screen } from '@testing-library/react-native';

import * as Button from '..';

const LABEL_TEXT = 'Hello Blocks';

describe('<Button />', () => {
it('should render correctly', () => {
render(
<Button.Root>
<Button.Label>{LABEL_TEXT}</Button.Label>
</Button.Root>,
);

const result = screen.toJSON();
expect(result.props.accessibilityRole).toEqual('button');
expect(result.props.accessible).toBeTruthy();

const element = screen.getByRole('button');
expect(element).toBeTruthy();
});

it('should render button with testID', () => {
const BUTTON_TEST_ID = 'buttonTestId';

render(
<Button.Root testID={BUTTON_TEST_ID}>
<Button.Label>{LABEL_TEXT}</Button.Label>
</Button.Root>,
);

const result = screen.toJSON();
expect(result.props.testID).toEqual(BUTTON_TEST_ID);

const element = screen.getByTestId(BUTTON_TEST_ID);
expect(element).toBeTruthy();
});

it('should render button in accessibilityState.disabled when props.disabled is true', () => {
render(
<Button.Root disabled>
<Button.Label>{LABEL_TEXT}</Button.Label>
</Button.Root>,
);

const element = screen.getByRole('button', { name: LABEL_TEXT, disabled: true });
expect(element).toBeTruthy();
});

it('should render button in accessibilityState.busy and render "ActivityIndicator" when props.loading is true', () => {
render(
<Button.Root loading>
<Button.Label>{LABEL_TEXT}</Button.Label>
<Button.Loading />
</Button.Root>,
);

const element = screen.getByRole('button', { name: LABEL_TEXT, busy: true });
expect(element).toBeTruthy();

const result = screen.toJSON();
const ActivityIndicator = result.children[1];
expect(ActivityIndicator.type).toEqual('ActivityIndicator');
});

it('should NOT render label when hideLabelOnLoading is true and loading is true', () => {
render(
<Button.Root loading hideLabelOnLoading>
<Button.Label>{LABEL_TEXT}</Button.Label>
<Button.Loading />
</Button.Root>,
);

const element = screen.getByRole('button', { busy: true, disabled: true });
expect(element).toBeTruthy();

expect(screen.queryAllByText(LABEL_TEXT)).toEqual([]);
});

it('should be call onPress events', () => {
const onPress = jest.fn();

render(
<Button.Root onPress={onPress}>
<Button.Label>{LABEL_TEXT}</Button.Label>
</Button.Root>,
);

const element = screen.getByRole('button', { name: LABEL_TEXT });
expect(element).toBeTruthy();

fireEvent(element, 'press');
expect(onPress).toHaveBeenCalled();
});

it('should be NOT call onPress events while loading', () => {
const onPress = jest.fn();
render(
<Button.Root loading>
<Button.Label>{LABEL_TEXT}</Button.Label>
<Button.Loading />
</Button.Root>,
);

const element = screen.getByRole('button', { name: LABEL_TEXT });
expect(element).toBeTruthy();

fireEvent(element, 'press');
expect(onPress).not.toHaveBeenCalled();
});

it('should be NOT call onPress events if disabled', () => {
const onPress = jest.fn();
render(
<Button.Root disabled>
<Button.Label>{LABEL_TEXT}</Button.Label>
<Button.Loading />
</Button.Root>,
);

const element = screen.getByRole('button', { name: LABEL_TEXT });
expect(element).toBeTruthy();

fireEvent(element, 'press');
expect(onPress).not.toHaveBeenCalled();
});

it('should render button with testID and nativeID', () => {
render(
<Button.Root disabled testID='testIDbuttonTest' nativeID='nativeIDbuttonTest'>
<Button.Label>{LABEL_TEXT}</Button.Label>
<Button.Loading />
</Button.Root>,
);

const result = screen.toJSON();

expect(result.props.testID).toEqual('testIDbuttonTest');
expect(result.props.nativeID).toEqual('nativeIDbuttonTest');
});
});
2 changes: 1 addition & 1 deletion packages/@blocks-unstyled/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"baseUrl": "."
},
"include": ["./src"],
"exclude": ["node_modules", ".turbo"]
"exclude": ["node_modules", ".turbo", "**/*.test.tsx"]
}

0 comments on commit f7a027c

Please sign in to comment.