File "frontend.js"

Full Path: /home/warrior1/public_html/wp-content/plugins/woocommerce/packages/woocommerce-blocks/assets/js/blocks/checkout/inner-blocks/checkout-terms-block/test/frontend.js
File size: 2.14 KB
MIME-type: text/x-java
Charset: utf-8

/**
 * External dependencies
 */
import {
	render,
	findByLabelText,
	queryByLabelText,
} from '@testing-library/react';
import userEvent from '@testing-library/user-event';

/**
 * Internal dependencies
 */
import FrontendBlock from '../frontend';

describe( 'FrontendBlock', () => {
	let validationData = {
		hasValidationErrors: false,
		getValidationError: jest.fn(),
		clearValidationError: jest.fn(),
		hideValidationError: jest.fn(),
		setValidationErrors: jest.fn(),
	};
	beforeEach( () => {
		validationData = {
			hasValidationErrors: false,
			getValidationError: jest.fn(),
			clearValidationError: jest.fn(),
			hideValidationError: jest.fn(),
			setValidationErrors: jest.fn(),
		};
	} );

	it( 'Renders a checkbox if the checkbox prop is true', async () => {
		const { container } = render(
			<FrontendBlock
				checkbox={ true }
				text={ 'I agree to the terms and conditions' }
				validation={ validationData }
			/>
		);

		const checkbox = await findByLabelText(
			container,
			'I agree to the terms and conditions'
		);

		expect( checkbox ).toBeInTheDocument();
	} );

	it( 'Does not render a checkbox if the checkbox prop is false', async () => {
		const { container } = render(
			<FrontendBlock
				checkbox={ false }
				text={ 'I agree to the terms and conditions' }
				validation={ validationData }
			/>
		);

		const checkbox = queryByLabelText(
			container,
			'I agree to the terms and conditions'
		);

		expect( checkbox ).not.toBeInTheDocument();
	} );

	it( 'Clears any validation errors when the checkbox is checked', async () => {
		validationData.getValidationError.mockImplementation( () => {
			return {
				message: 'Please read and accept the terms and conditions.',
				hidden: false,
			};
		} );
		const { container } = render(
			<FrontendBlock
				checkbox={ true }
				text={ 'I agree to the terms and conditions' }
				validation={ validationData }
			/>
		);
		const checkbox = await findByLabelText(
			container,
			'I agree to the terms and conditions'
		);
		userEvent.click( checkbox );
		expect( validationData.clearValidationError ).toHaveBeenLastCalledWith(
			expect.stringMatching( /terms-and-conditions-\d/ )
		);
	} );
} );