/**
 * Container Select 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 ContainerSelect from './ContainerSelect';
import { render, act } from '../../../../../../tests/js/test-utils';
import { MODULES_TAGMANAGER, ACCOUNT_CREATE } from '../../datastore/constants';
import { createTestRegistry } from '../../../../../../tests/js/utils';
import * as factories from '../../datastore/__factories__';

describe( 'ContainerSelect', () => {
	let registry;
	beforeEach( () => {
		registry = createTestRegistry();
		// Set settings to prevent fetch in resolver.
		registry.dispatch( MODULES_TAGMANAGER ).setSettings( {} );
		// Set set no existing tag.
		registry.dispatch( MODULES_TAGMANAGER ).receiveGetExistingTag( null );
	} );

	it( 'should be disabled if there is an existing tag', async () => {
		const account = factories.accountBuilder();
		const { accountId: accountID } = account; // eslint-disable-line sitekit/acronym-case
		registry
			.dispatch( MODULES_TAGMANAGER )
			.receiveGetAccounts( [ account ] );
		registry.dispatch( MODULES_TAGMANAGER ).setAccountID( accountID );
		registry
			.dispatch( MODULES_TAGMANAGER )
			.receiveGetContainers( [], { accountID } );
		registry
			.dispatch( MODULES_TAGMANAGER )
			.finishResolution( 'getAccounts', [] );
		registry
			.dispatch( MODULES_TAGMANAGER )
			.finishResolution( 'getContainers', [ accountID ] );

		const { container } = render( <ContainerSelect containers={ [] } />, {
			registry,
		} );
		const select = container.querySelector( '.mdc-select' );

		expect( select ).not.toHaveClass( 'mdc-select--disabled' );

		await act( () =>
			registry
				.dispatch( MODULES_TAGMANAGER )
				.receiveGetExistingTag( 'GTM-G000GL3' )
		);

		expect( select ).toHaveClass( 'mdc-select--disabled' );
	} );

	it( 'should be disabled if the selected account is not a valid account', async () => {
		const account = factories.accountBuilder();
		const { accountId: accountID } = account; // eslint-disable-line sitekit/acronym-case
		registry
			.dispatch( MODULES_TAGMANAGER )
			.receiveGetAccounts( [ account ] );
		registry.dispatch( MODULES_TAGMANAGER ).setAccountID( accountID );
		registry
			.dispatch( MODULES_TAGMANAGER )
			.receiveGetContainers( [], { accountID } );
		registry
			.dispatch( MODULES_TAGMANAGER )
			.finishResolution( 'getAccounts', [] );
		registry
			.dispatch( MODULES_TAGMANAGER )
			.finishResolution( 'getContainers', [ accountID ] );
		registry
			.dispatch( MODULES_TAGMANAGER )
			.finishResolution( 'getContainers', [ ACCOUNT_CREATE ] );

		const { container } = render( <ContainerSelect containers={ [] } />, {
			registry,
		} );
		const select = container.querySelector( '.mdc-select' );

		expect( select ).not.toHaveClass( 'mdc-select--disabled' );

		// The account option to "set up a new account" is technically an invalid accountID.
		await act( () =>
			registry
				.dispatch( MODULES_TAGMANAGER )
				.setAccountID( ACCOUNT_CREATE )
		);

		expect( select ).toHaveClass( 'mdc-select--disabled' );
	} );
} );

@@ -1344,1048 +1344,8 @@
 );%0A%0A
-%09it( 'should be disabled if there is an existing tag', async () =%3E %7B%0A%09%09const account = factories.accountBuilder();%0A%09%09const %7B accountId: accountID %7D = account; // eslint-disable-line sitekit/acronym-case%0A%09%09registry%0A%09%09%09.dispatch( MODULES_TAGMANAGER )%0A%09%09%09.receiveGetAccounts( %5B account %5D );%0A%09%09registry.dispatch( MODULES_TAGMANAGER ).setAccountID( accountID );%0A%09%09registry%0A%09%09%09.dispatch( MODULES_TAGMANAGER )%0A%09%09%09.receiveGetContainers( %5B%5D, %7B accountID %7D );%0A%09%09registry%0A%09%09%09.dispatch( MODULES_TAGMANAGER )%0A%09%09%09.finishResolution( 'getAccounts', %5B%5D );%0A%09%09registry%0A%09%09%09.dispatch( MODULES_TAGMANAGER )%0A%09%09%09.finishResolution( 'getContainers', %5B accountID %5D );%0A%0A%09%09const %7B container %7D = render( %3CContainerSelect containers=%7B %5B%5D %7D /%3E, %7B%0A%09%09%09registry,%0A%09%09%7D );%0A%09%09const select = container.querySelector( '.mdc-select' );%0A%0A%09%09expect( select ).not.toHaveClass( 'mdc-select--disabled' );%0A%0A%09%09await act( () =%3E%0A%09%09%09registry%0A%09%09%09%09.dispatch( MODULES_TAGMANAGER )%0A%09%09%09%09.receiveGetExistingTag( 'GTM-G000GL3' )%0A%09%09);%0A%0A%09%09expect( select ).toHaveClass( 'mdc-select--disabled' );%0A%09%7D );%0A%0A
 %09it(

Remove obsolate test.