context('Control Link', () => {
	before(() => {
		cy.login();
		cy.visit('/app/website');
	});

	beforeEach(() => {
		cy.visit('/app/website');
		cy.create_records({
			doctype: 'ToDo',
			description: 'this is a test todo for link'
		}).as('todos');
	});

	function get_dialog_with_link() {
		return cy.dialog({
			title: 'Link',
			fields: [
				{
					'label': 'Select ToDo',
					'fieldname': 'link',
					'fieldtype': 'Link',
					'options': 'ToDo'
				}
			]
		});
	}

	it('should set the valid value', () => {
		get_dialog_with_link().as('dialog');

		cy.intercept('POST', '/api/method/frappe.desk.search.search_link').as('search_link');

		cy.get('.frappe-control[data-fieldname=link] input').focus().as('input');
		cy.wait('@search_link');
		cy.get('@input').type('todo for link', { delay: 200 });
		cy.wait('@search_link');
		cy.get('.frappe-control[data-fieldname=link]').findByRole('listbox').should('be.visible');
		cy.get('.frappe-control[data-fieldname=link] input').type('{enter}', { delay: 100 });
		cy.get('.frappe-control[data-fieldname=link] input').blur();
		cy.get('@dialog').then(dialog => {
			cy.get('@todos').then(todos => {
				let value = dialog.get_value('link');
				expect(value).to.eq(todos[0]);
			});
		});
	});

	it('should unset invalid value', () => {
		get_dialog_with_link().as('dialog');

		cy.intercept('POST', '/api/method/frappe.client.validate_link').as('validate_link');

		cy.get('.frappe-control[data-fieldname=link] input')
			.type('invalid value', { delay: 100 })
			.blur();
		cy.wait('@validate_link');
		cy.get('.frappe-control[data-fieldname=link] input').should('have.value', '');
	});

	it('should route to form on arrow click', () => {
		get_dialog_with_link().as('dialog');

		cy.intercept('POST', '/api/method/frappe.client.validate_link').as('validate_link');
		cy.intercept('POST', '/api/method/frappe.desk.search.search_link').as('search_link');

		cy.get('@todos').then(todos => {
			cy.get('.frappe-control[data-fieldname=link] input').focus().as('input');
			cy.wait('@search_link');
			cy.get('@input').type(todos[0]);
			cy.wait('@search_link');
			cy.get('.frappe-control[data-fieldname=link] ul').should('be.visible');
			cy.get('.frappe-control[data-fieldname=link] input').type('{enter}', { delay: 100 });
			cy.wait('@validate_link');
			cy.get('@input').focus();
			cy.findByTitle('Open Link')
				.should('be.visible')
				.click();
			cy.location('hash').should('eq', `#Form/ToDo/${todos[0]}`);
		});
	});

	it('show title field in link', () => {
		get_dialog_with_link().as('dialog');

		cy.server();
		cy.insert_doc("Property Setter", {
			property: "show_title_field_in_link",
			doc_type: "ToDo",
			value: 1,
			doctype_or_field: "DocType"
		}, true);
		cy.route('POST', '/api/method/frappe.desk.search.search_link').as('search_link');

		cy.get('.frappe-control[data-fieldname=link] input').focus().as('input');
		cy.wait('@search_link');
		cy.get('@input').type('todo for link');
		cy.wait('@search_link');
		cy.get('.frappe-control[data-fieldname=link] ul').should('be.visible');
		cy.get('.frappe-control[data-fieldname=link] input').type('{enter}', { delay: 100 });
		cy.get('.frappe-control[data-fieldname=link] input').blur();
		cy.get('@dialog').then(dialog => {
			cy.get('@todos').then(todos => {
				let field = dialog.get_field('link');
				let value = field.get_value();
				let label = field.get_label_value();

				expect(value).to.eq(todos[0]);
				expect(label).to.eq('this is a test todo for link');
			});
		});
	});

	it('should fetch valid value', () => {
		cy.get('@todos').then(todos => {
			cy.visit(`/app/todo/${todos[0]}`);
			cy.intercept('POST', '/api/method/frappe.client.validate_link').as('validate_link');

			cy.get('.frappe-control[data-fieldname=assigned_by] input').focus().as('input');
			cy.get('@input').type('Administrator', {delay: 100}).blur();
			cy.wait('@validate_link');
			cy.get('.frappe-control[data-fieldname=assigned_by_full_name] .control-value').should(
				'contain', 'Administrator'
			);
		});
	});

});

@@ -1989,32 +1989,24 @@
 nk%5D input').
-focus().
 as('input');
@@ -2010,36 +2010,8 @@
 ');%0A
-%09%09%09cy.wait('@search_link');%0A
 %09%09%09c
@@ -2030,21 +2030,14 @@
 t').
-type(todos%5B0%5D
+focus(
 );%0A%09
@@ -2078,158 +2078,37 @@
 et('
-.frappe-control%5Bdata-fieldname=link%5D ul').should('be.visible');%0A%09%09%09cy.get('.frappe-control%5Bdata-fieldname=link%5D input').type('%7Benter%7D', %7B delay: 100 %7D
+@input').type(todos%5B0%5D).blur(
 );%0A%09
@@ -2256,12 +2256,16 @@
 on('
-hash
+pathname
 ').s
@@ -2281,17 +2281,16 @@
 ', %60
-#Form/ToD
+/app/tod
 o/$%7B

undo changes to link ui test