/**
 * WordPress dependencies
 */
import { activatePlugin } from '@wordpress/e2e-test-utils';

/**
 * Internal dependencies
 */
import { setSiteVerification, setSearchConsoleProperty } from '../utils';
import { visitAdminPage } from '@wordpress/e2e-test-utils/build/visit-admin-page';

describe( 'Site Kit dashboard post search', () => {
	beforeAll( async () => {
		await activatePlugin( 'e2e-tests-auth-plugin' );
		await setSiteVerification();
		await setSearchConsoleProperty();
	} );

	beforeEach( async () => {
		await visitAdminPage( 'admin.php', 'page=googlesitekit-dashboard' );
	} );

	it( 'displays results when searching with a post title, and loads the details page when clicking View Data', async () => {
		const postSearcher = await page.$( '.googlesitekit-post-searcher' );

		await expect( postSearcher ).toFill( 'input', 'hello' );

		// Ensure expected options appear.
		await expect( postSearcher ).toMatchElement( '.autocomplete__option', { text: /hello world/i } );
		await expect( postSearcher ).toMatchElement( '.autocomplete__option', { text: /hello solar system/i } );
		await expect( postSearcher ).toMatchElement( '.autocomplete__option', { text: /hello universe/i } );

		// Select the post.
		await expect( postSearcher ).toClick( '.autocomplete__option', { text: /hello world/i } );
		// Search input becomes the post title
		expect( await postSearcher.$eval( 'input', ( el ) => el.value ) ).toEqual( 'Hello world!' );

		await Promise.all( [
			page.waitForNavigation(),
			expect( postSearcher ).toClick( 'button', { text: /view data/i } ),
		] );

		await expect( page ).toMatchElement( '.googlesitekit-page-header__title', { text: /detailed page stats/i } );
		await expect( page ).toMatchElement( '.googlesitekit-dashboard-single-url__title', { text: 'Hello world!' } );
	} );

	it( 'displays "No results found" when searching by title if no post is found', async () => {
		const postSearcher = await page.$( '.googlesitekit-post-searcher' );

		await expect( postSearcher ).toFill( 'input', 'non-existent title' );

		await expect( postSearcher ).toMatchElement( '.autocomplete__option', { text: /no results found/i } );

		// Ensure no other options are displayed.
		expect( await postSearcher.$$( '.autocomplete__option' ) ).toHaveLength( 1 );
	} );
} );

@@ -50,16 +50,43 @@
 tePlugin
+, createURL, visitAdminPage
  %7D from 
@@ -114,16 +114,16 @@
 utils';%0A
-
 %0A/**%0A * 
@@ -225,91 +225,8 @@
 ls';
-%0Aimport %7B visitAdminPage %7D from '@wordpress/e2e-test-utils/build/visit-admin-page';
 %0A%0Ade
@@ -1023,32 +1023,32 @@
 r system/i %7D );%0A
-
 %09%09await expect( 
@@ -1742,32 +1742,1177 @@
 d!' %7D );%0A%09%7D );%0A%0A
+%09it( 'displays results when searching with a URL, and loads the details page when clicking View Data', async () =%3E %7B%0A%09%09const postSearcher = await page.$( '.googlesitekit-post-searcher' );%0A%0A%09%09await expect( postSearcher ).toFill( 'input', createURL( 'hello-world' ) );%0A%0A%09%09// Ensure expected options appear.%0A%09%09await expect( postSearcher ).toMatchElement( '.autocomplete__option', %7B text: /hello world/i %7D );%0A%09%09// Ensure no other options are displayed.%0A%09%09expect( await postSearcher.$$( '.autocomplete__option' ) ).toHaveLength( 1 );%0A%0A%09%09// Select the post.%0A%09%09await expect( postSearcher ).toClick( '.autocomplete__option', %7B text: /hello world/i %7D );%0A%09%09// Search input becomes the post title%0A%09%09expect( await postSearcher.$eval( 'input', ( el ) =%3E el.value ) ).toEqual( 'Hello world!' );%0A%0A%09%09await Promise.all( %5B%0A%09%09%09page.waitForNavigation(),%0A%09%09%09expect( postSearcher ).toClick( 'button', %7B text: /view data/i %7D ),%0A%09%09%5D );%0A%0A%09%09await expect( page ).toMatchElement( '.googlesitekit-page-header__title', %7B text: /detailed page stats/i %7D );%0A%09%09await expect( page ).toMatchElement( '.googlesitekit-dashboard-single-url__title', %7B text: 'Hello world!' %7D );%0A%09%7D );%0A%0A
 %09it( 'displays %22

add test for post search with URL