context("Workspace 2.0", () => {
	before(() => {
		cy.visit("/login");
		cy.login();
		return cy
			.window()
			.its("frappe")
			.then((frappe) => {
				return frappe.xcall("frappe.tests.ui_test_helpers.empty_my_workspaces");
			});
	});

	it("Navigate to page from sidebar", () => {
		cy.visit("/desk/build");
		cy.get(".codex-editor__redactor .ce-block");
		cy.get('.sidebar-item-container[item-name="Page"]').first().click();
		cy.location("pathname").should("eq", "/desk/page");
	});

	it("Create Private Page", () => {
		cy.visit("/desk/website");
		cy.intercept({
			method: "POST",
			url: "api/method/frappe.desk.doctype.workspace.workspace.new_page",
		}).as("new_page");

		cy.get(".codex-editor__redactor .ce-block");
		cy.get(".btn-new-workspace").click();
		cy.fill_field("title", "Test Private Page", "Data");
		cy.fill_field("type", "Workspace", "Select");
		cy.wait(300);

		cy.get_open_dialog().find(".modal-header").click();
		cy.get_open_dialog().find(".btn-primary").click();

		// check if sidebar item is added in pubic section
		cy.get('.sidebar-item-container[item-name="Test Private Page"]');
		cy.wait(300);
		cy.get('.standard-actions .btn-primary[data-label="Save"]').click();
		cy.wait(300);
		cy.get('.sidebar-item-container[item-name="Test Private Page"]');

		cy.wait("@new_page");
	});

	it("Add New Block", () => {
		cy.get(".btn-edit-workspace").click({ force: true });

		cy.get(".ce-block").click().type("{enter}");
		cy.get(".block-list-container .block-list-item").contains("Heading").click();
		cy.get(":focus").type("Header");
		cy.get(".ce-block:last").find(".ce-header").should("exist");

		cy.get(".ce-block:last").click().type("{enter}");
		cy.get(".block-list-container .block-list-item").contains("Text").click();
		cy.get(":focus").type("Paragraph text");
		cy.get(".ce-block:last").find(".ce-paragraph").should("exist");
	});

	it("Delete A Block", () => {
		cy.get(":focus").click();
		cy.get(".paragraph-control .setting-btn").click();
		cy.get(".paragraph-control .dropdown-item").contains("Delete").click();
		cy.get(".ce-block:last").find(".ce-paragraph").should("not.exist");
	});

	it("Shrink and Expand A Block", () => {
		cy.get(":focus").click();
		cy.get(".ce-block:last .setting-btn").click();
		cy.get(".ce-block:last .dropdown-item").contains("Shrink").click();
		cy.get(".ce-block:last").should("have.class", "col-xs-11");
		cy.get(".ce-block:last .dropdown-item").contains("Shrink").click();
		cy.get(".ce-block:last").should("have.class", "col-xs-10");
		cy.get(".ce-block:last .dropdown-item").contains("Shrink").click();
		cy.get(".ce-block:last").should("have.class", "col-xs-9");
		cy.get(".ce-block:last .dropdown-item").contains("Expand").click();
		cy.get(".ce-block:last").should("have.class", "col-xs-10");
		cy.get(".ce-block:last .dropdown-item").contains("Expand").click();
		cy.get(".ce-block:last").should("have.class", "col-xs-11");
		cy.get(".ce-block:last .dropdown-item").contains("Expand").click();
		cy.get(".ce-block:last").should("have.class", "col-xs-12");
		cy.wait(300);
		cy.get('.standard-actions .btn-primary[data-label="Save"]').click();
	});
});
