/* eslint-disable max-lines-per-function */
import React from 'react';
import { Text } from 'react-native';
import { cleanup, render, screen, setup } from '@/lib/test-utils';
import { Button } from './button';
afterEach(cleanup);
describe('Button component ', () => {
it('should render correctly ', () => {
render();
expect(screen.getByTestId('button')).toBeOnTheScreen();
});
it('should render correctly if we add explicit child ', () => {
render(
,
);
expect(screen.getByText('Custom child')).toBeOnTheScreen();
});
it('should render the label correctly', () => {
render();
expect(screen.getByTestId('button')).toBeOnTheScreen();
expect(screen.getByText('Submit')).toBeOnTheScreen();
});
it('should render the loading indicator correctly', () => {
render();
expect(screen.getByTestId('button')).toBeOnTheScreen();
expect(screen.getByTestId('button-activity-indicator')).toBeOnTheScreen();
});
it('should call onClick handler when clicked', async () => {
const onClick = jest.fn();
const { user } = setup(
,
);
expect(screen.getByTestId('button')).toBeOnTheScreen();
await user.press(screen.getByTestId('button'));
expect(onClick).toHaveBeenCalledTimes(1);
});
it('should be disabled when loading', async () => {
const onClick = jest.fn();
const { user } = setup(
,
);
expect(screen.getByTestId('button')).toBeOnTheScreen();
expect(screen.getByTestId('button-activity-indicator')).toBeOnTheScreen();
expect(screen.getByTestId('button')).toBeDisabled();
await user.press(screen.getByTestId('button'));
expect(onClick).toHaveBeenCalledTimes(0);
});
it('should be disabled when disabled prop is true', () => {
render();
expect(screen.getByTestId('button')).toBeDisabled();
});
it("shouldn't call onClick when disabled", async () => {
const onClick = jest.fn();
const { user } = setup(
,
);
expect(screen.getByTestId('button')).toBeOnTheScreen();
await user.press(screen.getByTestId('button'));
expect(screen.getByTestId('button')).toBeDisabled();
expect(onClick).toHaveBeenCalledTimes(0);
});
it('should apply correct styles based on size prop', () => {
render();
const button = screen.getByTestId('button');
// TODO: should be fixed to use haveStyle instead of comparing the class name
const expectedStyle =
'font-inter font-semibold text-white dark:text-black text-xl';
const receivedStyle =
button.props.children[0].props.children.props.className;
expect(receivedStyle).toContain(expectedStyle);
});
it('should apply correct styles for label when variant is secondary', () => {
render();
const button = screen.getByTestId('button');
const expectedStyle =
'font-inter font-semibold text-secondary-600 text-base';
const receivedStyle =
button.props.children[0].props.children.props.className;
expect(receivedStyle).toContain(expectedStyle);
});
it('should apply correct styles for label when is disabled', () => {
render();
const button = screen.getByTestId('button');
const expectedStyle =
'font-inter font-semibold text-base text-neutral-600 dark:text-neutral-600';
const receivedStyle =
button.props.children[0].props.children.props.className;
expect(receivedStyle).toContain(expectedStyle);
});
});