{"size": 3178, "ext": "js", "max_stars_repo_path": "packages/web/components/Modal/__tests__/BuyTechnologyModal.js", "max_stars_repo_name": "samirsm/plataforma-sabia", "max_stars_repo_stars_event_min_datetime": "2020-03-21T09:40:51.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-11T18:01:02.000Z", "max_issues_repo_path": "packages/web/components/Modal/__tests__/BuyTechnologyModal.js", "max_issues_repo_name": "samirsm/plataforma-sabia", "max_issues_repo_issues_event_min_datetime": "2020-03-22T18:47:46.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-02T13:27:05.000Z", "max_forks_repo_path": "packages/web/components/Modal/__tests__/BuyTechnologyModal.js", "max_forks_repo_name": "samirsm/plataforma-sabia", "max_forks_repo_forks_event_min_datetime": "2020-05-14T01:09:59.000Z", "max_forks_repo_forks_event_max_datetime": "2021-11-12T12:33:37.000Z"}
{"max_stars_count": 37, "max_issues_count": 924, "max_forks_count": 6, "avg_line_length": 33.8085106383, "max_line_length": 94, "alphanum_fraction": 0.7101950913}
import React from 'react';

import { render, screen, fireEvent } from 'test-utils';
import BuyTechnologyModal from '../BuyTechnologyModal';

const fakeTechnology = {
	id: 1,
	title: 'Abcdefgh',
	thumbnail: '/technology-thumbnail.jpg',
};

jest.mock('react-select', () => ({ options, value, onChange }) => {
	function handleChange(event) {
		const option = options.find((opt) => opt.value === event.currentTarget.value);
		onChange(option);
	}
	return (
		<select data-testid="select" value={value} onChange={handleChange}>
			{options.map(({ label, value: optValue }) => (
				<option key={optValue} value={optValue}>
					{label}
				</option>
			))}
		</select>
	);
});

describe('<BuyTechnologyModal />', () => {
	it('should render correctly', async () => {
		const { container } = await render(<BuyTechnologyModal technology={fakeTechnology} />);

		expect(screen.getByRole('img', { src: '/technology-thumbnail.jpg' })).toBeInTheDocument();
		expect(screen.getByRole('heading', { name: /abcdefgh/i })).toBeInTheDocument();
		expect(screen.getByLabelText(/quantas unidades da tecnologia\?/i)).toBeInTheDocument();
		expect(screen.getAllByRole('textbox')[0].textContent).toBe('0');
		expect(screen.getByLabelText(/a tecnologia será adquirida para uso:/i)).toBeInTheDocument();
		expect(screen.getByTestId('select')).toBeInTheDocument();
		expect(screen.getByRole('button', { name: /adquirir tecnologia/i })).toBeDisabled();

		expect(container).toMatchSnapshot();
	});

	it('should increase and decrease technology quantity when clicking buttons', () => {
		render(<BuyTechnologyModal technology={fakeTechnology} />);

		const decreaseButton = screen.getByRole('button', { name: /decrease quantity/i });
		const increaseButton = screen.getByRole('button', { name: /increase quantity/i });

		expect(decreaseButton).toBeDisabled();
		expect(increaseButton).toBeEnabled();

		fireEvent.click(increaseButton);

		expect(screen.getAllByRole('textbox')[0].textContent).toBe('1');

		expect(decreaseButton).toBeEnabled();
		fireEvent.click(decreaseButton);

		expect(screen.getAllByRole('textbox')[0].textContent).toBe('0');
	});

	it('should be able to choose technology use', () => {
		render(<BuyTechnologyModal technology={fakeTechnology} />);

		const privateRadio = screen.getByLabelText(/privado/i);
		const companyRadio = screen.getByLabelText(/empresa/i);

		fireEvent.click(privateRadio);
		expect(privateRadio).toBeChecked();

		fireEvent.click(companyRadio);
		expect(companyRadio).toBeChecked();
		expect(privateRadio).not.toBeChecked();
	});

	it('should enable submit button if required fields is filled', () => {
		render(<BuyTechnologyModal technology={fakeTechnology} />);

		const buyButton = screen.getByRole('button', { name: /adquirir tecnologia/i });
		expect(buyButton).toBeDisabled();

		const increaseButton = screen.getByRole('button', { name: /increase quantity/i });
		fireEvent.click(increaseButton);

		const privateRadio = screen.getByLabelText(/privado/i);
		fireEvent.click(privateRadio);

		const select = screen.getByTestId('select');
		fireEvent.change(select, { target: { value: 'has_funding' } });

		expect(buyButton).toBeEnabled();
	});
});
