UNPKG

product-admin

Version:

EA admin screens

427 lines (367 loc) 13.8 kB
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="../../bower_components/webcomponentsjs/webcomponents-lite.js"></script> <script src="../../bower_components/web-component-tester/browser.js"></script> <link rel="import" href="../../element.html" /> <link rel="import" href="./product-table-view.html"> </head> <body> <test-fixture id="product-table-view-fixture"> <template> <product-table-view resources='{ "en": { "headingCreate": "Create", "headingEdit": "Edit", "name": "Name", "productCode": "productCode", "productDescription": "Description", "productDescriptionParen": "(>150 characters)", "currentSpecificationRate": "Production Rate", "currentSpecificationRateLabel": "PPH", "estCycleTime": "Estimated Cycle Time", "hours": "Hours", "minutes": "Min", "btnCreateEditPositive": "Save", "btnCreateEditNegative": "Cancel", "hr": "hr", "min": "min", "getProductError": "fakeProductError", "error": "fakeError"} }' language="en"></product-table-view> </template> </test-fixture> <script> suite('<product-table-view>', function() { setup(function(done) { window.myEl = fixture("product-table-view-fixture"); Polymer.dom.flush(); flush(done); }); teardown(function(done) { flush(done); }); test('product-table-view exists', function() { expect(myEl).to.exist; }); test('table should only contain a header if no data is passed to it', function(done) { var productTable = fixture("product-table-view-fixture"); productTable.tableData = []; flush(function() { var listItems = Polymer.dom(productTable.root).querySelectorAll('tr'); assert.equal(listItems.length, 1); productTable.tableData = []; // teardown productData done(); }); }); test('table should contain product data when data is provided', function(done) { var listItems; var productTable = fixture("product-table-view-fixture"); productTable.tableData = [{ 'productCode': 'hello1234' }]; flush(function() { listItems = Polymer.dom(productTable.root).querySelectorAll('tr'); assert.equal(listItems[1].children[1].textContent, 'hello1234'); productTable.tableData = []; // teardown productData done(); }); }); }); suite('<iron-ajax>', function() { var ajax; var request; var server; var responseHeaders = { json: { 'Content-Type': 'application/json' } }; setup(function() { server = sinon.fakeServer.create(); server.respondWith( 'GET', ///\/responds_to_get_with_json.*/, [ "/getProducts", [ 200, responseHeaders.json, //TODO: some day... make this into separate file. JSON.stringify({ "last": false, "numberOfElements": 20, "totalPages": 3, "totalElements": 46, "sort": [{ "direction": "DESC", "property": "productDescription", "ignoreCase": false, "nullHandling": "NATIVE", "ascending": false }], "first": true, "size": 20, "number": 0, "link": { "next": { "href": "http://bmsconfig-ui-stuf-dev.run.asv-pr.ice.predix.io/products/v1/products?page=1&size=20" }, "self": { "href": "http://bmsconfig-ui-stuf-dev.run.asv-pr.ice.predix.io/products/v1/products?page=0&size=20" }, "first": { "href": "http://bmsconfig-ui-stuf-dev.run.asv-pr.ice.predix.io/products/v1/products?page=1&size=20" }, "last": { "href": "http://bmsconfig-ui-stuf-dev.run.asv-pr.ice.predix.io/products/v1/products?page=3&size=20" } }, "_embedded": { "resource": [{ "pk": "0ead00ac-6536-491f-be19-75f1a11f300d", "productDescription": "Vent Seal", "productCode": "Vent Seal Code", "currentSpecificationRate": 100.0, "currentSpecificationCycleTime": 140.0, "longDescription": "This is the long description", "assets": ["8e70c7e4-b589-3fac-89da-ccdd55b66bdd", "b3d3123f-a2f6-3c84-879e-116e7823e431", "d798108a-262d-3b53-846d-26599cd03227"], "specifications": [{ "id": "29e1ee43-035a-4588-a029-75ac564c82ac", "rate": 140.0, "effectiveDate": "2017-02-08T13:57:47.490+0000", "expirationDate": null }], "tenantZoneId": "1b3e5fc2-a2d9-49a3-a877-6e9e6e5b7764" }, { "pk": "e719dd66-d0b5-4139-a34f-675258013b48", "productDescription": "TCF Hanger Code", "productCode": "TCF Hanger", "currentSpecificationRate": 190.0, "currentSpecificationCycleTime": 110.0, "longDescription": "This is the very long description", "assets": ["8e70c7e4-b589-3fac-89da-ccdd55b66bdd", "b3d3123f-a2f6-3c84-879e-116e7823e431", "d798108a-262d-3b53-846d-26599cd03227"], "specifications": [{ "id": "37f11a2e-2181-48a6-93fa-0f8fa4ebef52", "rate": 110.0, "effectiveDate": "2017-02-08T13:56:50.670+0000", "expirationDate": null }], "tenantZoneId": "1b3e5fc2-a2d9-49a3-a877-6e9e6e5b7764" }] } }) ] ); window.myEl = fixture("product-table-view-fixture"); }); teardown(function() { server.restore(); }); test('just some ajax request', function(done) { var productTable = fixture("product-table-view-fixture"); var ajax = window.myEl.getElementsByTagName('iron-ajax')[0]; ajax.lastResponse = null; ajax.generateRequest(); server.respond(); flush(function() { listItems = Polymer.dom(productTable.root).querySelectorAll('tr'); assert.equal(productTable.tableData[1].productDescription, 'TCF Hanger Code'); assert.equal(productTable.products[1].productDescription, 'TCF Hanger Code'); assert.equal(productTable.tableData[1].productCode, 'TCF Hanger'); assert.equal(productTable.products[1].productCode, 'TCF Hanger'); assert.equal(productTable.tableData[0].productCode, 'Vent Seal Code'); assert.equal(productTable.products[0].productDescription, 'Vent Seal'); assert.equal(listItems[2].children[1].textContent, 'TCF Hanger'); assert.equal(listItems[1].children[1].textContent, 'Vent Seal Code'); assert.equal(listItems[2].children[5].textContent, 'This is the very long description'); assert.equal(listItems[1].children[5].textContent, 'This is the long description'); done(); }); }); test('some failed ajax request', function(done) { server.respondWith( 'GET', "/getProducts", [ 500, responseHeaders.json, JSON.stringify({ error: "some error" }) ] ); var productTable = fixture("product-table-view-fixture"); var ajax = window.myEl.getElementsByTagName('iron-ajax')[0]; var testFunction = function(event) { assert.equal(event.detail.errorMessage, 'fakeProductError'); assert.equal(event.detail.messageTitle, 'fakeError'); window.removeEventListener('alert-error-message', testFunction); done(); } window.addEventListener('alert-error-message', testFunction); ajax.lastResponse = null; ajax.generateRequest(); server.respond(); flush(function() { }); }); }); //px-dropdown-value-changed suite('filter test', function() { var productTable setup(function() { productTable = fixture("product-table-view-fixture"); productTable.tableData = [{ 'productCode': 'hello1234', 'active': 'true' }, { 'productCode': 'hello5678', 'active': 'false' }, { 'productCode': 'hello5678', 'active': 'false' }]; productTable.products = [{ 'productCode': 'hello1234', 'active': 'true' }, { 'productCode': 'hello5678', 'active': 'false' }, { 'productCode': 'hello5678', 'active': 'false' }]; }); teardown(function() { productTable.tableData = []; productTable.products = []; }); test('filter test filter by all by default', function(done) { productTable.fire("px-dropdown-value-changed", { "key": undefined }); flush(function() { assert.equal(productTable.products.length, 3); done(); }) }); test('filter test filter by active', function(done) { productTable.fire("px-dropdown-value-changed", { "key": "products.active" }); flush(function() { assert.equal(productTable.tableData.length, 1); done(); }) }); test('filter test filter by inactive', function(done) { productTable.fire("px-dropdown-value-changed", { "key": "products.inactive" }); flush(function() { assert.equal(productTable.tableData.length, 2); done(); }) }); test('filter test filter by all', function() { productTable.fire("px-dropdown-value-changed", { "key": "products.all" }); assert.equal(productTable.tableData.length, 3); }); }); suite('modal', function() { var productTable setup(done => { productTable = fixture("product-table-view-fixture"); // replace('edit-product').with('fake-px-modal'); flush(done); }); teardown(function(done) { flush(done); }); test('filter test filter by all', function(done) { stub('edit-product', { querySelector: function() { return { modalButtonClicked: assert, fire: function() {} } } }); productTable._openEditModal({ model: { __data__: { item: { productDescription: "productDescription1", productLongDescription: "productLongDescription1", productCode: "productCode1", currentSpecificationRate: "currentSpecificationRate1", estCycleTimeMinutes: "estCycleTimeMinutes1", estCycleTimeHours: "estCycleTimeHours1", assets: "assets1", specifications: "specifications1" } } } }); function assert() { done(); } }); }); suite("#_openCreateModal", function() { setup(function(done) { window.productTable = fixture("product-table-view-fixture"); window.createProductModalStub = { querySelector: function() { console.log("calling all query selectors"); return { modalButtonClicked: sinon.stub(), fire: sinon.stub() }; } }; flush(done); }); teardown(function(done) { delete window.createProductModalStub; delete window.productTable; flush(done); }); test("should reset the createProductModal fields", function() { stub('edit-product', createProductModalStub); productTable._openCreateModal(); expect(productTable.$.createProductModal.product).to.deep.equal({ productDescription: "", productCode: "", longDescription: "", currentSpecificationRate: 0, estCycleTimeHours: 0, estCycleTimeMinutes: 0, assets: [], specifications: [], active: "true" }); }); test("should refresh product list when createProduct fired", function() { sinon.stub(productTable.$.getProductsRequest, 'generateRequest'); productTable.fire("createProduct"); expect(productTable.$.getProductsRequest.generateRequest).to.have.been.called; productTable.$.getProductsRequest.generateRequest.restore(); }); // this is the offender test("should update products when updateProduct fired", function(done) { productTable.products = [{ 'pk': 'pk1', 'productCode': 'hello1234', 'active': 'true' }, { 'pk': 'pk2', 'productCode': 'hello5678', 'active': 'false' }, { 'pk': 'pk3', 'productCode': 'hello5678', 'active': 'false' }]; window.addEventListener("px-dropdown-value-changed", function(dropdownChangedEvent) { expect(dropdownChangedEvent.detail.key).to.equal(""); expect(productTable.products.length).to.equal(3); expect(productTable.products[1].estCycleTimeMinutes).to.equal(40); expect(productTable.products[1].estCycleTimeHours).to.equal(1); done(); }); let theEvent = { pk: "pk2", currentSpecificationCycleTime: 100, active: "true" }; flush(function(){ productTable.fire("updateProduct", theEvent); }); }); }); </script> </body> </html>