/**
 * AdSenseConnectCTA 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 AdSenseConnectCTA from '.';
import {
	act,
	fireEvent,
	render,
	createTestRegistry,
	provideSiteInfo,
	provideUserAuthentication,
} from '../../../../../../../tests/js/test-utils';
import { CORE_MODULES } from '../../../../../googlesitekit/modules/datastore/constants';
import { MODULES_ADSENSE } from '../../../datastore/constants';
import { withActive } from '../../../../../googlesitekit/modules/datastore/__fixtures__';

describe( 'AdSenseConnectCTA', () => {
	let registry;
	beforeEach( () => {
		registry = createTestRegistry();
		registry.dispatch( MODULES_ADSENSE ).setSettings( {} );
		provideSiteInfo( registry );
		provideUserAuthentication( registry );
		registry
			.dispatch( CORE_MODULES )
			.receiveGetModules( withActive( 'adsense' ) );
	} );

	describe( 'after click', () => {
		let container;
		beforeEach( () => {
			container = render(
				<div>
					<div id="adminmenu">
						<a href="http://test.test/wp-admin/admin.php?page=googlesitekit-settings">
							Settings
						</a>
					</div>
					<AdSenseConnectCTA />
				</div>,
				{ registry }
			).container;

			fireEvent.click(
				container.querySelector( 'button.googlesitekit-cta-link' )
			);

			fetchMock.postOnce(
				RegExp( '^/google-site-kit/v1/core/user/data/dismiss-item' ),
				{
					body: JSON.stringify( [ 'adsense-connect-cta' ] ),
					status: 200,
				}
			);
		} );

		it( 'should open the tooltip', async () => {
			expect(
				document.querySelector( '.googlesitekit-tour-tooltip' )
			).toBeInTheDocument();
			expect( fetchMock ).toHaveFetchedTimes( 0 );
		} );

		it( 'should close the tooltip on clicking the close button', async () => {
			await act( async () => {
				fireEvent.click(
					document.querySelector( '.googlesitekit-tooltip-close' )
				);
			} );
			expect(
				document.querySelector( '.googlesitekit-tour-tooltip' )
			).not.toBeInTheDocument();
			expect( fetchMock ).toHaveFetchedTimes( 1 );
		} );

		it( 'should close the modal on clicking the dismiss button', async () => {
			await act( async () => {
				fireEvent.click(
					document.querySelector(
						'.googlesitekit-tooltip-buttons > button'
					)
				);
			} );
			expect(
				document.querySelector( '.googlesitekit-tour-tooltip' )
			).not.toBeInTheDocument();
			expect( fetchMock ).toHaveFetchedTimes( 1 );
		} );
	} );
} );

@@ -17,16 +17,22 @@
 nnectCTA
+Widget
  compone
@@ -41,16 +41,16 @@
  tests.%0A
-
  *%0A * Si
@@ -714,24 +714,30 @@
 nnectCTA
+Widget
  from '.
 ';%0Aimpor
@@ -728,16 +728,40 @@
  from '.
+/AdSenseConnectCTAWidget
 ';%0Aimpor
@@ -884,19 +884,16 @@
 ./../../
-../
 tests/js
@@ -940,35 +940,32 @@
 om '../../../../
-../
 googlesitekit/mo
@@ -984,32 +984,112 @@
 ore/constants';%0A
+import %7B CORE_USER %7D from '../../../../googlesitekit/datastore/user/constants';%0A
 import %7B MODULES
@@ -1111,19 +1111,16 @@
  '../../
-../
 datastor
@@ -1165,35 +1165,32 @@
 om '../../../../
-../
 googlesitekit/mo
@@ -1547,24 +1547,89 @@
 sense' ) );%0A
+%09%09registry.dispatch( CORE_USER ).receiveGetDismissedItems( %5B%5D );%0A
 %09%7D );%0A%0A%09desc
@@ -1696,16 +1696,125 @@
 () =%3E %7B%0A
+%09%09%09const Widget = ( %7B children %7D ) =%3E %3Cdiv%3E%7B children %7D%3C/div%3E;%0A%09%09%09const WidgetNull = () =%3E %3Cdiv%3ENULL%3C/div%3E;%0A%0A
 %09%09%09conta
@@ -2007,17 +2007,84 @@
 nnectCTA
- 
+Widget%0A%09%09%09%09%09%09Widget=%7B Widget %7D%0A%09%09%09%09%09%09WidgetNull=%7B WidgetNull %7D%0A%09%09%09%09%09
 /%3E%0A%09%09%09%09%3C

Fix AdSenseConnectCTAWidget tests.