Merge pull request #1784 from garyhgohoos/23593-2
[xtuple] / test / extensions / sales / sales_order_workspace.js
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,
6   clearInterval:true */
7
8 (function () {
9   "use strict";
10
11   var zombieAuth = require("../../lib/zombie_auth"),
12     _ = require("underscore"),
13     async = require("async"),
14     submodels,
15     smoke = require("../../lib/smoke"),
16     assert = require("chai").assert,
17     gridRow,
18     gridBox,
19     workspace,
20     primeSubmodels = function (done) {
21       var submodels = {};
22       async.series([
23         function (callback) {
24           submodels.customerModel = new XM.SalesCustomer();
25           submodels.customerModel.fetch({number: "TTOYS", success: function () {
26             assert.equal(submodels.customerModel.get("shiptos").length, 3);
27             callback();
28           }});
29         },
30         function (callback) {
31           submodels.itemModel = new XM.ItemRelation();
32           submodels.itemModel.fetch({number: "BTRUCK1", success: function () {
33             callback();
34           }});
35         },
36         function (callback) {
37           submodels.siteModel = new XM.SiteRelation();
38           submodels.siteModel.fetch({code: "WH1", success: function () {
39             callback();
40           }});
41         }
42       ], function (err) {
43         done(err, submodels);
44       });
45     };
46
47   // TODO: move to sales order spec
48   describe.skip('Sales Order Workspace', function () {
49     this.timeout(30 * 1000);
50
51     //
52     // We'll want to have TTOYS, BTRUCK1, and WH1 onhand and ready for the test to work.
53     //
54     before(function (done) {
55       zombieAuth.loadApp(function () {
56         primeSubmodels(function (err, submods) {
57           submodels = submods;
58           done();
59         });
60       });
61     });
62
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;
67
68           assert.equal(workspace.value.recordType, "XM.SalesOrder");
69           //
70           // Set the customer from the appropriate workspace quantityWidget
71           //
72           var createHash = {
73             customer: submodels.customerModel
74           };
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 () {
86             done();
87             /* The following save was moved to the second test
88             smoke.saveWorkspace(workspace, function (err, model) {
89               assert.isNull(err);
90               // TODO: sloppy
91               setTimeout(function () {
92                 smoke.deleteFromList(XT.app, model, done);
93               }, 2000);
94             });*/
95           });
96
97           //
98           // Set the line item fields
99           //
100           // Be sure that there are no rows
101           gridBox = workspace.$.salesOrderLineItemBox;
102           assert.equal(gridBox.liveModels().length, 0);
103
104           gridBox.newItem();
105           gridRow = gridBox.$.editableGridRow;
106
107           gridRow.$.itemSiteWidget.doValueChange({value: {item: submodels.itemModel, site: submodels.siteModel}});
108           gridRow.$.quantityWidget.doValueChange({value: 5});
109
110           // Verify that there is currently one row
111           assert.equal(gridBox.liveModels().length, 1);
112         });
113       });
114       it('adding a second line item should not copy the item', function (done) {
115         workspace.value.once("change:total", done());
116
117         gridRow.$.itemSiteWidget.$.privateItemSiteWidget.$.input.focus();
118         // Add a new item, check that row exists, and make sure the itemSiteWidget doesn't copy irrelevantly
119         gridBox.newItem();
120         assert.equal(gridBox.liveModels().length, 2);
121         assert.notEqual(submodels.itemModel.id, gridRow.$.itemSiteWidget.$.privateItemSiteWidget.$.input.value);
122
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});
127
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();
134               }
135             });
136         */
137       });
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;
144
145         /** Open the first model's salesOrderLineWorkspace...
146             Copied from gridBox buttonTapped function (expandGridRowButton)
147         */
148         lineItemBox.doChildWorkspace({
149           workspace: lineItemBox.getWorkspace(),
150           collection: lineItemBox.getValue(),
151           index: lineItemBox.getValue().indexOf(model)
152         });
153
154         /** The line item's workspace model has been deleted (DESTROYED_CLEAN).
155             Client is now in SalesOrderWorkspace.
156         */
157         var statusChanged = function () {
158           assert.notEqual(startModelLength, lineItemBox.liveModels().length);
159           done();
160         };
161
162         model.once("status:DESTROYED_CLEAN", statusChanged);
163
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" }});
170         }, 100);
171         // Delete the item in the workspace
172         moduleContainer.getActive().deleteItem();
173       });
174
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) {
179           assert.isNull(err);
180           // XXX - sloppy
181           setTimeout(function () {
182             smoke.deleteFromList(XT.app, model, done);
183           }, 4000);
184         }, true);
185       });
186     });
187   });
188 }());