[tests] refactored (#9984)

* [tests] refactored

* [fix] test_quotation.js

* [fix] tests.text

* [fix] fiscal year not needed

* [test] add long test

* [fix] add timeout in lead
diff --git a/erpnext/tests/ui/accounts/test_account.js b/erpnext/accounts/doctype/account/test_account.js
similarity index 77%
rename from erpnext/tests/ui/accounts/test_account.js
rename to erpnext/accounts/doctype/account/test_account.js
index 6d7709b..7b23ef0 100644
--- a/erpnext/tests/ui/accounts/test_account.js
+++ b/erpnext/accounts/doctype/account/test_account.js
@@ -5,16 +5,16 @@
 	let done = assert.async();
 	frappe.run_serially([
 		() => frappe.set_route('Tree', 'Account'),
-		() => frappe.tests.click_button('Expand All'),
-		() => frappe.tests.click_link('Debtors'),
-		() => frappe.tests.click_button('Edit'),
+		() => frappe.click_button('Expand All'),
+		() => frappe.click_link('Debtors'),
+		() => frappe.click_button('Edit'),
 		() => frappe.timeout(1),
 		() => {
 			assert.ok(cur_frm.doc.root_type=='Asset');
 			assert.ok(cur_frm.doc.report_type=='Balance Sheet');
 			assert.ok(cur_frm.doc.account_type=='Receivable');
 		},
-		() => frappe.tests.click_button('Ledger'),
+		() => frappe.click_button('Ledger'),
 		() => frappe.timeout(1),
 		() => {
 			// check if general ledger report shown
diff --git a/erpnext/crm/doctype/lead/test_lead.js b/erpnext/crm/doctype/lead/test_lead.js
new file mode 100644
index 0000000..66d3337
--- /dev/null
+++ b/erpnext/crm/doctype/lead/test_lead.js
@@ -0,0 +1,43 @@
+QUnit.module("sales");
+
+QUnit.test("test: lead", function (assert) {
+	assert.expect(4);
+	let done = assert.async();
+	let lead_name = frappe.utils.get_random(10);
+	frappe.run_serially([
+		// test lead creation
+		() => frappe.set_route("List", "Lead"),
+		() => frappe.new_doc("Lead"),
+		() => frappe.timeout(1),
+		() => cur_frm.set_value("lead_name", lead_name),
+		() => cur_frm.save(),
+		() => frappe.timeout(1),
+		() => {
+			assert.ok(cur_frm.doc.lead_name.includes(lead_name),
+				'name correctly set');
+			frappe.lead_name = cur_frm.doc.name;
+		},
+		// create address and contact
+		() => frappe.click_link('Address & Contact'),
+		() => frappe.click_button('New Address'),
+		() => frappe.timeout(1),
+		() => frappe.set_control('address_line1', 'Gateway'),
+		() => frappe.set_control('city', 'Mumbai'),
+		() => cur_frm.save(),
+		() => frappe.timeout(3),
+		() => assert.equal(frappe.get_route()[1], 'Lead',
+			'back to lead form'),
+		() => frappe.click_link('Address & Contact'),
+		() => assert.ok($('.address-box').text().includes('Mumbai'),
+			'city is seen in address box'),
+
+		// make opportunity
+		() => frappe.click_button('Make'),
+		() => frappe.click_link('Opportunity'),
+		() => frappe.timeout(2),
+		() => assert.equal(cur_frm.doc.lead, frappe.lead_name,
+			'lead name correctly mapped'),
+
+		() => done()
+	]);
+});
diff --git a/erpnext/crm/doctype/opportunity/test_opportunity.js b/erpnext/crm/doctype/opportunity/test_opportunity.js
new file mode 100644
index 0000000..f2b04f8
--- /dev/null
+++ b/erpnext/crm/doctype/opportunity/test_opportunity.js
@@ -0,0 +1,56 @@
+QUnit.test("test: opportunity", function (assert) {
+	assert.expect(8);
+	let done = assert.async();
+	frappe.run_serially([
+		() => frappe.set_route('List', 'Opportunity'),
+		() => frappe.timeout(1),
+		() => frappe.click_button('New'),
+		() => frappe.timeout(1),
+		() => cur_frm.set_value('enquiry_from', 'Customer'),
+		() => cur_frm.set_value('customer', 'Test Customer 1'),
+
+		// check items
+		() => cur_frm.set_value('with_items', 1),
+		() => frappe.tests.set_grid_values(cur_frm, 'items', [
+			[
+				{item_code:'Test Product 1'},
+				{qty: 4}
+			]
+		]),
+		() => cur_frm.save(),
+		() => frappe.timeout(1),
+		() => {
+			assert.notOk(cur_frm.is_new(), 'saved');
+			frappe.opportunity_name = cur_frm.doc.name;
+		},
+
+		// close and re-open
+		() => frappe.click_button('Close'),
+		() => frappe.timeout(1),
+		() => assert.equal(cur_frm.doc.status, 'Closed',
+			'closed'),
+
+		() => frappe.click_button('Reopen'),
+		() => assert.equal(cur_frm.doc.status, 'Open',
+			'reopened'),
+		() => frappe.timeout(1),
+
+		// make quotation
+		() => frappe.click_button('Make'),
+		() => frappe.click_link('Quotation', 1),
+		() => frappe.timeout(2),
+		() => {
+			assert.equal(frappe.get_route()[1], 'Quotation',
+				'made quotation');
+			assert.equal(cur_frm.doc.customer, 'Test Customer 1',
+				'customer set in quotation');
+			assert.equal(cur_frm.doc.items[0].item_code, 'Test Product 1',
+				'item set in quotation');
+			assert.equal(cur_frm.doc.items[0].qty, 4,
+				'qty set in quotation');
+			assert.equal(cur_frm.doc.items[0].prevdoc_docname, frappe.opportunity_name,
+				'opportunity set in quotation');
+		},
+		() => done()
+	]);
+});
diff --git a/erpnext/projects/doctype/timesheet/timesheet.js b/erpnext/projects/doctype/timesheet/timesheet.js
index 2edfa9e..441ab1a 100644
--- a/erpnext/projects/doctype/timesheet/timesheet.js
+++ b/erpnext/projects/doctype/timesheet/timesheet.js
@@ -133,10 +133,15 @@
 	var child = locals[cdt][cdn];
 
 	var d = moment(child.from_time);
-	d.add(child.hours, "hours");
-	frm._setting_hours = true;
-	frappe.model.set_value(cdt, cdn, "to_time", d.format(moment.defaultDatetimeFormat));
-	frm._setting_hours = false;
+	if(child.hours) {
+		d.add(child.hours, "hours");
+		frm._setting_hours = true;
+		frappe.model.set_value(cdt, cdn, "to_time",
+			d.format(moment.defaultDatetimeFormat)).then(() => {
+				frm._setting_hours = false;
+			});
+	}
+
 
 	if((frm.doc.__islocal || frm.doc.__onload.maintain_bill_work_hours_same) && child.hours){
 		frappe.model.set_value(cdt, cdn, "billing_hours", child.hours);
diff --git a/erpnext/selling/doctype/quotation/test_quotation.js b/erpnext/selling/doctype/quotation/test_quotation.js
new file mode 100644
index 0000000..44173cc
--- /dev/null
+++ b/erpnext/selling/doctype/quotation/test_quotation.js
@@ -0,0 +1,53 @@
+QUnit.test("test: quotation", function (assert) {
+	assert.expect(10);
+	let done = assert.async();
+	frappe.run_serially([
+		() => {
+			return frappe.tests.make("Quotation", [
+				{customer: "Test Customer 1"},
+				{items: [
+					[
+						{"item_code": "Test Product 1"},
+						{"qty": 5}
+					]]
+				}
+			]);
+		},
+		() => {
+			// get_item_details
+			assert.ok(cur_frm.doc.items[0].item_name == "Test Product 1", "Added Test Product 1");
+
+			// calculate_taxes_and_totals
+			assert.ok(cur_frm.doc.grand_total === 500, "Total Amount is correct");
+		},
+		() => cur_frm.set_value("customer_address", "Test1-Billing"),
+		() => cur_frm.set_value("shipping_address_name", "Test1-Warehouse"),
+		() => cur_frm.set_value("contact_person", "Contact 1-Test Customer 1"),
+		() => cur_frm.set_value("currency", "USD"),
+		() => frappe.timeout(0.3),
+		() => cur_frm.set_value("selling_price_list", "Test-Selling-USD"),
+		() => frappe.timeout(0.5),
+		() => cur_frm.doc.items[0].rate = 200,
+		() => frappe.timeout(0.3),
+		() => cur_frm.set_value("tc_name", "Test Term 1"),
+		() => frappe.timeout(0.3),
+		() => cur_frm.save(),
+		() => {
+			// Check Address and Contact Info
+			assert.ok(cur_frm.doc.address_display.includes("Billing Street 1"), "Address Changed");
+			assert.ok(cur_frm.doc.shipping_address.includes("Warehouse Street 1"), "Address Changed");
+			assert.ok(cur_frm.doc.contact_display == "Contact 1", "Contact info changed");
+
+			// Check Currency
+			assert.ok(cur_frm.doc_currency == "USD", "Currency Changed");
+			assert.ok(cur_frm.doc.selling_price_list == "Test-Selling-USD", "Price List Changed");
+			assert.ok(cur_frm.doc.items[0].rate == 200, "Price Changed Manually");
+			assert.equal(cur_frm.doc.total, 1000, "New Total Calculated");
+
+			// Check Terms and Condtions
+			assert.ok(cur_frm.doc.tc_name == "Test Term 1", "Terms and Conditions Checked");
+
+		},
+		() => done()
+	]);
+});
\ No newline at end of file
diff --git a/erpnext/tests/ui/data/test_fixtures.js b/erpnext/tests/ui/make_fixtures.js
similarity index 87%
rename from erpnext/tests/ui/data/test_fixtures.js
rename to erpnext/tests/ui/make_fixtures.js
index 2ba5db5..0c5b4be 100644
--- a/erpnext/tests/ui/data/test_fixtures.js
+++ b/erpnext/tests/ui/make_fixtures.js
@@ -1,4 +1,11 @@
 $.extend(frappe.test_data, {
+	// "Fiscal Year": {
+	// 	"2017-18": [
+	// 		{"year": "2017-18"},
+	// 		{"year_start_date": "2017-04-01"},
+	// 		{"year_end_date": "2018-03-31"},
+	// 	]
+	// },
 	"Customer": {
 		"Test Customer 1": [
 			{customer_name: "Test Customer 1"}
@@ -198,19 +205,20 @@
 			{title: "Test Term 2"}
 		]
 	},
-	"Sales Taxes and Charges Template": {
-		"TEST In State GST": [
-			{title: "TEST In State GST"},
-			{taxes:[
-				[
-					{charge_type:"On Net Total"},
-					{account_head:"CGST - "+frappe.get_abbr(frappe.defaults.get_default("Company")) }
-				],
-				[
-					{charge_type:"On Net Total"},
-					{account_head:"SGST - "+frappe.get_abbr(frappe.defaults.get_default("Company")) }
-				]
-			]}
-		]
-	}
+});
+
+
+// this is a script that creates all fixtures
+// called as a test
+QUnit.module('fixture');
+
+QUnit.test('Make fixtures', assert => {
+	// create all fixtures first
+	assert.expect(0);
+	let done = assert.async();
+	let tasks = [];
+	Object.keys(frappe.test_data).forEach(function(doctype) {
+		tasks.push(function() { return frappe.tests.setup_doctype(doctype); });
+	});
+	frappe.run_serially(tasks).then(() => done());
 });
diff --git a/erpnext/tests/ui/selling/_test_lead.js b/erpnext/tests/ui/selling/_test_lead.js
deleted file mode 100644
index 2b895d9..0000000
--- a/erpnext/tests/ui/selling/_test_lead.js
+++ /dev/null
@@ -1,18 +0,0 @@
-QUnit.module("sales");
-
-QUnit.test("test: lead", function (assert) {
-	assert.expect(1);
-	let done = assert.async();
-	let random = frappe.utils.get_random(10);
-	frappe.run_serially([
-		() => frappe.tests.setup_doctype("Lead"),
-		() => frappe.set_route("List", "Lead"),
-		() => frappe.new_doc("Lead"),
-		() => cur_frm.set_value("lead_name", random),
-		() => cur_frm.save(),
-		() => {
-			assert.ok(cur_frm.doc.lead_name.includes(random));
-			return done();
-		}
-	]);
-});
diff --git a/erpnext/tests/ui/selling/_test_opportunity.js b/erpnext/tests/ui/selling/_test_opportunity.js
deleted file mode 100644
index 716a36e..0000000
--- a/erpnext/tests/ui/selling/_test_opportunity.js
+++ /dev/null
@@ -1,19 +0,0 @@
-QUnit.test("test: opportunity", function (assert) {
-	assert.expect(1);
-	let done = assert.async();
-	frappe.run_serially([
-		() => {
-			return frappe.tests.make("Opportunity", [{
-				enquiry_from: "Lead"
-			},
-			{
-				lead: "LEAD-00002"
-			}
-			]);
-		},
-		() => {
-			assert.ok(cur_frm.doc.lead === "LEAD-00002");
-			return done();
-		}
-	]);
-});
diff --git a/erpnext/tests/ui/selling/_test_quotation.js b/erpnext/tests/ui/selling/_test_quotation.js
deleted file mode 100644
index 62dd05d..0000000
--- a/erpnext/tests/ui/selling/_test_quotation.js
+++ /dev/null
@@ -1,81 +0,0 @@
-QUnit.test("test: quotation", function (assert) {
-	assert.expect(18);
-	let done = assert.async();
-	frappe.run_serially([
-		() => frappe.tests.setup_doctype("Customer"),
-		() => frappe.tests.setup_doctype("Item"),
-		() => frappe.tests.setup_doctype("Address"),
-		() => frappe.tests.setup_doctype("Contact"),
-		() => frappe.tests.setup_doctype("Price List"),
-		() => frappe.tests.setup_doctype("Terms and Conditions"),
-		() => frappe.tests.setup_doctype("Sales Taxes and Charges Template"),
-		() => {
-			return frappe.tests.make("Quotation", [{
-				customer: "Test Customer 1"
-			},
-			{
-				items: [
-					[{
-						"item_code": "Test Product 1"
-					},
-					{
-						"qty": 5
-					}
-					]
-				]
-			}
-			]);
-		},
-		() => {
-			// get_item_details
-			assert.ok(cur_frm.doc.items[0].item_name == "Test Product 1", "Added Test Product 1");
-
-			// calculate_taxes_and_totals
-			assert.ok(cur_frm.doc.grand_total === 500, "Total Amount is correct");
-		},
-		() => cur_frm.set_value("customer_address", "Test1-Billing"),
-		() => cur_frm.set_value("shipping_address_name", "Test1-Warehouse"),
-		() => cur_frm.set_value("contact_person", "Contact 1-Test Customer 1"),
-		() => cur_frm.set_value("currency", "USD"),
-		() => frappe.timeout(0.3),
-		() => cur_frm.set_value("selling_price_list", "Test-Selling-USD"),
-		() => frappe.timeout(0.5),
-		() => cur_frm.doc.items[0].rate = 200,
-		() => frappe.timeout(0.3),
-		() => cur_frm.set_value("tc_name", "Test Term 1"),
-		() => cur_frm.set_value("taxes_and_charges", "TEST In State GST"),
-		() => frappe.timeout(0.3),
-		() => cur_frm.save(),
-		() => {
-			// Check Address and Contact Info
-			assert.ok(cur_frm.doc.address_display.includes("Billing Street 1"), "Address Changed");
-			assert.ok(cur_frm.doc.shipping_address.includes("Warehouse Street 1"), "Address Changed");
-			assert.ok(cur_frm.doc.contact_display == "Contact 1", "Contact info changed");
-
-			// Check Currency
-			assert.ok(cur_frm.doc_currency == "USD", "Currency Changed");
-			assert.ok(cur_frm.doc.selling_price_list == "Test-Selling-USD", "Price List Changed");
-			assert.ok(cur_frm.doc.items[0].rate == 200, "Price Changed Manually");
-			assert.ok(cur_frm.doc.total == 1000, "New Total Calculated");
-
-			// Check Terms and Condtions
-			assert.ok(cur_frm.doc.tc_name == "Test Term 1", "Terms and Conditions Checked");
-
-			// Check Taxes
-			assert.ok(cur_frm.doc.taxes[0].account_head.includes("CGST"));
-			assert.ok(cur_frm.doc.taxes[1].account_head.includes("SGST"));
-			assert.ok(cur_frm.doc.grand_total == 1180, "Tax Amount Added to Total");
-			assert.ok(cur_frm.doc.taxes_and_charges == "TEST In State GST", "Tax Template Selected");
-		},
-		() => frappe.timeout(0.3),
-		() => cur_frm.print_doc(),
-		() => frappe.timeout(1),
-		() => assert.ok($('.btn-print-print').is(':visible'), "Print Format Available"),
-		() => assert.ok(RegExp(/QTN-\d\d\d\d\d/g).test($("#header-html small").text())),
-		() => assert.ok($(".important .text-right.value").text().includes("$ 1,180.00")),
-		() => assert.ok($(".section-break+ .section-break .column-break:nth-child(1) .data-field:nth-child(1) .value").text().includes("Billing Street 1"), "Print Preview Works As Expected"),
-		() => frappe.timeout(0.3),
-		() => cur_frm.print_doc(),
-		() => done()
-	]);
-});
\ No newline at end of file
diff --git a/erpnext/tests/ui/tests.txt b/erpnext/tests/ui/tests.txt
new file mode 100644
index 0000000..42233dc
--- /dev/null
+++ b/erpnext/tests/ui/tests.txt
@@ -0,0 +1,5 @@
+erpnext/tests/ui/make_fixtures.js #long
+erpnext/accounts/doctype/account/test_account.js
+erpnext/crm/doctype/lead/test_lead.js
+erpnext/crm/doctype/opportunity/test_opportunity.js
+erpnext/selling/doctype/quotation/test_quotation.js
\ No newline at end of file