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

import Button from '.';
import * as stories from './Button.stories';
import { generateSnapshots } from '../../../.rnstorybook/generateSnapshots';

const onPressMock = jest.fn();

const testProps = {
	title: 'Press me!',
	type: 'primary' as const,
	onPress: onPressMock,
	testID: 'testButton',
	initialText: 'Initial text',
	textAfterPress: 'Button pressed!'
};

const TestButton = ({ loading = false, disabled = false }) => (
	<View>
		<Button
			title={testProps.title}
			type={testProps.type}
			onPress={testProps.onPress}
			testID={testProps.testID}
			accessibilityLabel={testProps.title}
			disabled={disabled}
			loading={loading}
		/>
	</View>
);

describe('ButtonTests', () => {
	beforeEach(() => {
		onPressMock.mockClear();
	});

	test('rendered', async () => {
		const { findByTestId } = render(<TestButton />);
		const Button = await findByTestId(testProps.testID);
		expect(Button).toBeTruthy();
	});

	test('rendered with correct title', async () => {
		const { findByText } = render(<TestButton />);
		const ButtonTitle = await findByText(testProps.title);
		expect(ButtonTitle).toBeTruthy();
		expect(ButtonTitle.props.children).toEqual(testProps.title);
	});

	test('find button using accessibilityLabel', async () => {
		const { getByLabelText } = render(<TestButton />);
		const Button = await getByLabelText(testProps.title);
		expect(Button).toBeTruthy();
	});

	test('title not visible while loading', async () => {
		const { queryByText } = render(<TestButton loading={true} />);
		const ButtonTitle = await queryByText(testProps.title);
		expect(ButtonTitle).toBeNull();
	});

	test('disabled button is in disabled state', async () => {
		const { findByTestId } = render(<TestButton disabled={true} />);
		const button = await findByTestId(testProps.testID);
		expect(button.props.enabled).toBe(false);
	});

	test('should trigger onPress function on button press', async () => {
		const { findByTestId } = render(<TestButton />);
		const Button = await findByTestId(testProps.testID);
		fireEvent.press(Button);
		expect(onPressMock).toHaveBeenCalled();
	});
});

generateSnapshots(stories);
