1 /*jshint trailing:true, white:true, indent:2, strict:true, curly:true,
2 immed:true, eqeqeq:true, forin:true, latedef:true,
3 newcap:true, noarg:true, undef:true */
4 /*global XT:true, XM:true, XV:true, describe:true, it:true, setTimeout:true,
5 console:true, before:true, after:true, module:true, require:true, setInterval:true,
11 var zombieAuth = require("../../lib/zombie_auth"),
12 _ = require("underscore"),
13 async = require("async"),
15 smoke = require("../../lib/smoke"),
16 assert = require("chai").assert,
20 primeSubmodels = function (done) {
24 submodels.customerModel = new XM.SalesCustomer();
25 submodels.customerModel.fetch({number: "TTOYS", success: function () {
26 assert.equal(submodels.customerModel.get("shiptos").length, 3);
31 submodels.itemModel = new XM.ItemRelation();
32 submodels.itemModel.fetch({number: "BTRUCK1", success: function () {
37 submodels.siteModel = new XM.SiteRelation();
38 submodels.siteModel.fetch({code: "WH1", success: function () {
47 // TODO: move to sales order spec
48 describe.skip('Sales Order Workspace', function () {
49 this.timeout(30 * 1000);
52 // We'll want to have TTOYS, BTRUCK1, and WH1 onhand and ready for the test to work.
54 before(function (done) {
55 zombieAuth.loadApp(function () {
56 primeSubmodels(function (err, submods) {
63 describe('User selects to create a sales order', function () {
64 it('User navigates to Sales Order-New and selects to create a new Sales order', function (done) {
65 smoke.navigateToNewWorkspace(XT.app, "XV.SalesOrderList", function (workspaceContainer) {
66 workspace = workspaceContainer.$.workspace;
68 assert.equal(workspace.value.recordType, "XM.SalesOrder");
70 // Set the customer from the appropriate workspace quantityWidget
73 customer: submodels.customerModel
75 // TODO: why is the first shipto getting stripped out of TTOYS by now?
76 //assert.equal(submodels.customerModel.get("shiptos").length, 3);
77 //assert.equal(submodels.customerModel.getDefaultShipto().getValue("address.city"), "Alexandoria");
78 smoke.setWorkspaceAttributes(workspace, createHash);
79 //assert.equal(workspace.value.getValue("shipto.address.city"), "Alexandria");
80 // In sales order, setting the line item fields will set off a series
81 // of asynchronous calls. Once the "total" field is computed, we
82 // know that the workspace is ready to save.
83 // It's good practice to set this trigger *before* we change the line
84 // item fields, so that we're 100% sure we're ready for the responses.
85 workspace.value.once("change:total", function () {
87 /* The following save was moved to the second test
88 smoke.saveWorkspace(workspace, function (err, model) {
91 setTimeout(function () {
92 smoke.deleteFromList(XT.app, model, done);
98 // Set the line item fields
100 // Be sure that there are no rows
101 gridBox = workspace.$.salesOrderLineItemBox;
102 assert.equal(gridBox.liveModels().length, 0);
105 gridRow = gridBox.$.editableGridRow;
107 gridRow.$.itemSiteWidget.doValueChange({value: {item: submodels.itemModel, site: submodels.siteModel}});
108 gridRow.$.quantityWidget.doValueChange({value: 5});
110 // Verify that there is currently one row
111 assert.equal(gridBox.liveModels().length, 1);
114 it('adding a second line item should not copy the item', function (done) {
115 workspace.value.once("change:total", done());
117 gridRow.$.itemSiteWidget.$.privateItemSiteWidget.$.input.focus();
118 // Add a new item, check that row exists, and make sure the itemSiteWidget doesn't copy irrelevantly
120 assert.equal(gridBox.liveModels().length, 2);
121 assert.notEqual(submodels.itemModel.id, gridRow.$.itemSiteWidget.$.privateItemSiteWidget.$.input.value);
123 // The intention was to delete the above line after verifying that the item doesn't copy but ran into
124 // many issues so just populating with same data and saving it with 2 line items.
125 gridRow.$.itemSiteWidget.doValueChange({value: {item: submodels.itemModel, site: submodels.siteModel}});
126 gridRow.$.quantityWidget.doValueChange({value: 5});
128 /* Delete the line item
129 workspace.value.get("lineItems").models[1].destroy({
130 success: function () {
131 gridBox.setEditableIndex(null);
132 gridBox.$.editableGridRow.hide();
133 gridBox.valueChanged();
138 it('deleting an item through SalesOrderLineWorkspace should remove it from the line item ' +
139 'list', function (done) {
140 var lineItemBox = workspace.$.salesOrderLineItemBox,
141 model = lineItemBox.value.models[0],
142 startModelLength = lineItemBox.liveModels().length,
143 moduleContainer = XT.app.$.postbooks;
145 /** Open the first model's salesOrderLineWorkspace...
146 Copied from gridBox buttonTapped function (expandGridRowButton)
148 lineItemBox.doChildWorkspace({
149 workspace: lineItemBox.getWorkspace(),
150 collection: lineItemBox.getValue(),
151 index: lineItemBox.getValue().indexOf(model)
154 /** The line item's workspace model has been deleted (DESTROYED_CLEAN).
155 Client is now in SalesOrderWorkspace.
157 var statusChanged = function () {
158 assert.notEqual(startModelLength, lineItemBox.liveModels().length);
162 model.once("status:DESTROYED_CLEAN", statusChanged);
164 // Function to keep checking for notifyPopup showing and then tap yes.
165 // This will fire right after the delete below.
166 var notifyPopupInterval = setInterval(function () {
167 if (!moduleContainer.$.notifyPopup.showing) { return; }
168 clearInterval(notifyPopupInterval);
169 moduleContainer.notifyTap(null, {originator: {name: "notifyYes" }});
171 // Delete the item in the workspace
172 moduleContainer.getActive().deleteItem();
175 it('save, then delete order', function (done) {
176 assert.isTrue((workspace.value.status === XM.Model.READY_DIRTY ||
177 workspace.value.status === XM.Model.READY_NEW));
178 smoke.saveWorkspace(workspace, function (err, model) {
181 setTimeout(function () {
182 smoke.deleteFromList(XT.app, model, done);