<commit_before>/**
 * Survey component tests.
 *
 * Site Kit by Google, Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Internal dependencies
 */
import CurrentSurvey from './CurrentSurvey';
import {
	render,
	fireEvent,
	createTestRegistry,
	waitFor,
} from '../../../../tests/js/test-utils';
import { CORE_USER } from '../../googlesitekit/datastore/user/constants';
import { CORE_FORMS } from '../../googlesitekit/datastore/forms/constants';
import * as fixtures from './__fixtures__';

// Text input should only allow up to 100 characters of input.
const STRING_100_CHARACTERS =
	'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec suscipit auctor dui, id faucibus nisl';

const STRING_110_CHARACTERS = `${ STRING_100_CHARACTERS } rhoncus n`;

describe( 'CurrentSurvey', () => {
	let registry;

	beforeEach( () => {
		registry = createTestRegistry();
		registry.dispatch( CORE_USER ).receiveGetTracking( { enabled: true } );
	} );

	it( 'should render a survey when one exists in the datastore', async () => {
		registry
			.dispatch( CORE_USER )
			.receiveTriggerSurvey( fixtures.singleQuestionSurvey, {
				triggerID: 'jestSurvey',
			} );

		fetchMock.postOnce(
			/^\/google-site-kit\/v1\/core\/user\/data\/survey-event/,
			{ body: {}, status: 200 }
		);

		const { container } = render( <CurrentSurvey />, { registry } );

		expect( fetchMock ).toHaveFetched(
			/^\/google-site-kit\/v1\/core\/user\/data\/survey-event/
		);

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

	it( "should render a rating question when the `question_type` is 'rating'", async () => {
		registry
			.dispatch( CORE_USER )
			.receiveTriggerSurvey( fixtures.singleQuestionSurvey, {
				triggerID: 'jestSurvey',
			} );

		fetchMock.postOnce(
			/^\/google-site-kit\/v1\/core\/user\/data\/survey-event/,
			{ body: {}, status: 200 }
		);

		const { container } = render( <CurrentSurvey />, { registry } );

		expect( fetchMock ).toHaveFetched(
			/^\/google-site-kit\/v1\/core\/user\/data\/survey-event/
		);

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

	describe( "should render an open text question when the `question_type` is 'open_text'", () => {
		beforeEach( () => {
			registry
				.dispatch( CORE_USER )
				.receiveTriggerSurvey( fixtures.singleQuestionOpenText, {
					triggerID: 'jestSurvey',
				} );

			fetchMock.post(
				/^\/google-site-kit\/v1\/core\/user\/data\/survey-event/,
				{ body: {}, status: 200 }
			);
		} );

		it( 'should  display question text and subtitle correctly', async () => {
			const { getByText } = render( <CurrentSurvey />, {
				registry,
			} );

			// Check question_text is set by question_text prop.
			expect(
				getByText( 'How satisfied are you with Site Kit?' )
			).toBeInTheDocument();

			// Check subtitle is set by subtitle prop.
			expect(
				getByText( 'Based on your experience so far, tell us.' )
			).toBeInTheDocument();
		} );

		it( 'should limit text input to 100 characters', async () => {
			const { getByText, getByLabelText, getByDisplayValue } = render(
				<CurrentSurvey />,
				{
					registry,
				}
			);

			// Check question_text is set by question_text prop.
			expect(
				getByText( 'How satisfied are you with Site Kit?' )
			).toBeInTheDocument();

			// Check subtitle is set by subtitle prop.<commit_msg>Improve punctuation in text.<commit_after>/**
 * Survey component tests.
 *
 * Site Kit by Google, Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Internal dependencies
 */
import CurrentSurvey from './CurrentSurvey';
import {
	render,
	fireEvent,
	createTestRegistry,
	waitFor,
} from '../../../../tests/js/test-utils';
import { CORE_USER } from '../../googlesitekit/datastore/user/constants';
import { CORE_FORMS } from '../../googlesitekit/datastore/forms/constants';
import * as fixtures from './__fixtures__';

// Text input should only allow up to 100 characters of input.
const STRING_100_CHARACTERS =
	'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec suscipit auctor dui, id faucibus nisl';

const STRING_110_CHARACTERS = `${ STRING_100_CHARACTERS } rhoncus n`;

describe( 'CurrentSurvey', () => {
	let registry;

	beforeEach( () => {
		registry = createTestRegistry();
		registry.dispatch( CORE_USER ).receiveGetTracking( { enabled: true } );
	} );

	it( 'should render a survey when one exists in the datastore', async () => {
		registry
			.dispatch( CORE_USER )
			.receiveTriggerSurvey( fixtures.singleQuestionSurvey, {
				triggerID: 'jestSurvey',
			} );

		fetchMock.postOnce(
			/^\/google-site-kit\/v1\/core\/user\/data\/survey-event/,
			{ body: {}, status: 200 }
		);

		const { container } = render( <CurrentSurvey />, { registry } );

		expect( fetchMock ).toHaveFetched(
			/^\/google-site-kit\/v1\/core\/user\/data\/survey-event/
		);

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

	it( "should render a rating question when the `question_type` is 'rating'", async () => {
		registry
			.dispatch( CORE_USER )
			.receiveTriggerSurvey( fixtures.singleQuestionSurvey, {
				triggerID: 'jestSurvey',
			} );

		fetchMock.postOnce(
			/^\/google-site-kit\/v1\/core\/user\/data\/survey-event/,
			{ body: {}, status: 200 }
		);

		const { container } = render( <CurrentSurvey />, { registry } );

		expect( fetchMock ).toHaveFetched(
			/^\/google-site-kit\/v1\/core\/user\/data\/survey-event/
		);

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

	describe( "should render an open text question when the `question_type` is 'open_text'", () => {
		beforeEach( () => {
			registry
				.dispatch( CORE_USER )
				.receiveTriggerSurvey( fixtures.singleQuestionOpenText, {
					triggerID: 'jestSurvey',
				} );

			fetchMock.post(
				/^\/google-site-kit\/v1\/core\/user\/data\/survey-event/,
				{ body: {}, status: 200 }
			);
		} );

		it( 'should display the question prompt and subtitle', async () => {
			const { getByText } = render( <CurrentSurvey />, {
				registry,
			} );

			// Check the question's prompt is set by the `question_text` prop.
			expect(
				getByText( 'How satisfied are you with Site Kit?' )
			).toBeInTheDocument();

			// Check subtitle is set by subtitle prop.
			expect(
				getByText( 'Based on your experience so far, tell us.' )
			).toBeInTheDocument();
		} );

		it( 'should limit text input to 100 characters', async () => {
			const { getByText, getByLabelText, getByDisplayValue } = render(
				<CurrentSurvey />,
				{
					registry,
				}
			);

			// Check question_text is set by question_text prop.
			expect(
				getByText( 'How satisfied are you with Site Kit?' )
			).toBeInTheDocument();

			// Check subtitle is set by subtitle prop.
{'fraction_non_alphanumeric': 0.13844957220637802, 'fraction_numerical': 0.009592947886958776, 'mean_word_length': 3.218151995626025, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 27, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6638', 'n_tokens_mistral': 2754, 'n_tokens_neox': 2381, 'n_words': 699}