From 1f1b09d456df07902c2cecbcbcfe9e3f5709a70d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Tue, 15 Sep 2020 14:14:13 +0200 Subject: [PATCH] Updated unit tests --- db/dump/fixtures.sql | 4 +- .../methods/order/specs/catalogFilter.spec.js | 28 ++++-- .../front/catalog-search-panel/index.html | 5 -- modules/order/front/catalog/index.js | 73 ++++++--------- modules/order/front/catalog/index.spec.js | 88 +++++++++++++++---- 5 files changed, 123 insertions(+), 75 deletions(-) diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 8f2f3bb35..fdaf72440 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -1056,7 +1056,7 @@ INSERT INTO `vn`.`itemTag`(`id`,`itemFk`,`tagFk`,`value`,`priority`) (10, 2, 27, '15cm', 3), (11, 2, 36, 'Stark Industries', 4), (12, 2, 1, 'Silver', 5), - (13, 2, 67, 'concussion', 6), + (13, 2, 67, 'Concussion', 6), (14, 2, 23, '1', 7), (15, 3, 56, 'Ranged weapon', 1), (16, 3, 58, 'sniper rifle', 2), @@ -1105,7 +1105,7 @@ INSERT INTO `vn`.`itemTag`(`id`,`itemFk`,`tagFk`,`value`,`priority`) (59, 9, 27, '15cm', 3), (60, 9, 36, 'Stark Industries', 4), (61, 9, 1, 'Silver', 5), - (62, 9, 67, 'concussion', 6), + (62, 9, 67, 'Concussion', 6), (63, 9, 23, '1', 7), (64, 10, 56, 'Ranged Reinforced weapon', 1), (65, 10, 58, 'sniper rifle', 2), diff --git a/modules/order/back/methods/order/specs/catalogFilter.spec.js b/modules/order/back/methods/order/specs/catalogFilter.spec.js index 12cafa5b7..64bf4f17c 100644 --- a/modules/order/back/methods/order/specs/catalogFilter.spec.js +++ b/modules/order/back/methods/order/specs/catalogFilter.spec.js @@ -1,6 +1,9 @@ const app = require('vn-loopback/server/server'); describe('order catalogFilter()', () => { + const colorTagId = 1; + const categoryTagId = 67; + it('should return an array of items', async() => { let filter = { where: { @@ -19,21 +22,30 @@ describe('order catalogFilter()', () => { }); it('should now return an array of items based on tag filter', async() => { - let filter = { + const filter = { where: { categoryFk: 1, typeFk: 2 } }; - let tags = [{tagFk: 56, value: 'Melee Reinforced weapon'}]; - let orderFk = 11; - let orderBy = {field: 'relevancy DESC, name', way: 'DESC'}; - let result = await app.models.Order.catalogFilter(orderFk, orderBy, filter, tags); + const tagGroups = [ + {tagFk: colorTagId, values: [{value: 'Silver'}, {value: 'Brown'}]}, + {tagFk: categoryTagId, values: [{value: 'Concussion'}]} + ]; + const orderFk = 11; + const orderBy = {field: 'relevancy DESC, name', way: 'DESC'}; + const result = await app.models.Order.catalogFilter(orderFk, orderBy, filter, tagGroups); - let firstItemId = result[0].id; + const randomIndex = Math.round(Math.random()); + const item = result[randomIndex]; + const itemTags = item.tags; - expect(result.length).toEqual(1); - expect(firstItemId).toEqual(9); + const colorTag = itemTags.find(tag => tag.tagFk == colorTagId); + const categoryTag = itemTags.find(tag => tag.tagFk == categoryTagId); + + expect(result.length).toEqual(2); + expect(colorTag.value).toEqual('Silver'); + expect(categoryTag.value).toEqual('Concussion'); }); }); diff --git a/modules/order/front/catalog-search-panel/index.html b/modules/order/front/catalog-search-panel/index.html index 822445a18..19719e00b 100644 --- a/modules/order/front/catalog-search-panel/index.html +++ b/modules/order/front/catalog-search-panel/index.html @@ -1,11 +1,6 @@
- - - Tags - - 0) - this.applyFilters(); */ + if (this.tagGroups.length > 0) + this.applyFilters(); if (value) this.updateItemTypes(); @@ -105,9 +104,7 @@ class Controller extends Section { this.updateStateParams(); - /* if (value || this.tags.length > 0) - this.applyFilters(); */ - if (value) + if (value || this.tagGroups.length > 0) this.applyFilters(); } @@ -124,19 +121,6 @@ class Controller extends Section { this.applyFilters(); } - /* get tags() { - return this._tags; - } - - set tags(value) { - this._tags = value; - - this.updateStateParams(); - - if (value.length) - this.applyFilters(); - } */ - /** * Get order way ASC/DESC */ @@ -166,9 +150,7 @@ class Controller extends Section { * Apply order to model */ applyOrder() { - /* if (this.typeId || this.tags.length > 0) - this.$.model.addFilter(null, {orderBy: this.getOrderBy()}); */ - if (this.typeId) + if (this.typeId || this.tagGroups.length > 0) this.$.model.addFilter(null, {orderBy: this.getOrderBy()}); } @@ -289,32 +271,35 @@ class Controller extends Section { params.typeId = this.typeId; params.tagGroups = undefined; - if (this.tagGroups && this.tagGroups.length) { - const tagGroups = []; - for (let tagGroup of this.tagGroups) { - const tagParam = {values: []}; - - for (let tagValue of tagGroup.values) - tagParam.values.push({value: tagValue.value}); - - if (tagGroup.tagFk) - tagParam.tagFk = tagGroup.tagFk; - - if (tagGroup.tagSelection) { - tagParam.tagSelection = { - name: tagGroup.tagSelection.name - }; - } - - tagGroups.push(tagParam); - } - - params.tagGroups = JSON.stringify(tagGroups); - } + if (this.tagGroups && this.tagGroups.length) + params.tagGroups = JSON.stringify(this.sanitizedTagGroupParam()); this.$state.go(this.$state.current.name, params); } + sanitizedTagGroupParam() { + const tagGroups = []; + for (let tagGroup of this.tagGroups) { + const tagParam = {values: []}; + + for (let tagValue of tagGroup.values) + tagParam.values.push({value: tagValue.value}); + + if (tagGroup.tagFk) + tagParam.tagFk = tagGroup.tagFk; + + if (tagGroup.tagSelection) { + tagParam.tagSelection = { + name: tagGroup.tagSelection.name + }; + } + + tagGroups.push(tagParam); + } + + return tagGroups; + } + buildTagsFilter(items) { const tagValues = []; items.forEach(item => { diff --git a/modules/order/front/catalog/index.spec.js b/modules/order/front/catalog/index.spec.js index 4ef9cbeb9..a6d8cf053 100644 --- a/modules/order/front/catalog/index.spec.js +++ b/modules/order/front/catalog/index.spec.js @@ -115,12 +115,12 @@ describe('Order', () => { }); }); - describe('tags() setter', () => { - it(`should set tags property and then call updateStateParams() and applyFilters() methods`, () => { + describe('tagGroups() setter', () => { + it(`should set tagGroups property and then call updateStateParams() and applyFilters() methods`, () => { jest.spyOn(controller, 'updateStateParams'); jest.spyOn(controller, 'applyFilters'); - controller.tags = [{tagFk: 11, value: 'Brown'}]; + controller.tagGroups = [{tagFk: 11, values: [{value: 'Brown'}]}]; expect(controller.updateStateParams).toHaveBeenCalledWith(); expect(controller.applyFilters).toHaveBeenCalledWith(); @@ -184,7 +184,7 @@ describe('Order', () => { expect(controller.$.model.applyFilter).toHaveBeenCalledWith( {where: {categoryFk: 2, typeFk: 4}}, - {orderFk: 4, orderBy: controller.getOrderBy(), tags: []}); + {orderFk: 4, orderBy: controller.getOrderBy(), tagGroups: []}); }); }); @@ -192,11 +192,16 @@ describe('Order', () => { it(`should remove a tag from tags property`, () => { jest.spyOn(controller, 'applyFilters'); - controller.tags = [{tagFk: 1, value: 'Blue'}, {tagFk: 2, value: '70'}]; + controller.tagGroups = [ + {tagFk: 1, values: [{value: 'Brown'}]}, + {tagFk: 67, values: [{value: 'Concussion'}]} + ]; controller.remove(0); - expect(controller.tags.length).toEqual(1); - expect(controller.tags[0].tagFk).toEqual(2); + const firstTag = controller.tagGroups[0]; + + expect(controller.tagGroups.length).toEqual(1); + expect(firstTag.tagFk).toEqual(67); expect(controller.applyFilters).toHaveBeenCalledWith(); }); @@ -205,10 +210,10 @@ describe('Order', () => { controller._categoryId = 1; controller._typeId = 1; - controller.tags = [{tagFk: 1, value: 'Blue'}]; + controller.tagGroups = [{tagFk: 1, values: [{value: 'Blue'}]}]; controller.remove(0); - expect(controller.tags.length).toEqual(0); + expect(controller.tagGroups.length).toEqual(0); expect(controller.applyFilters).toHaveBeenCalledWith(); }); }); @@ -219,17 +224,16 @@ describe('Order', () => { controller._categoryId = 2; controller._typeId = 4; - controller._tags = [ - {tagFk: 11, value: 'Precission', tagSelection: {name: 'Category'}} + controller._tagGroups = [ + {tagFk: 67, values: [{value: 'Concussion'}], tagSelection: {name: 'Category'}} ]; - const tags = JSON.stringify([{ - value: 'Precission', - tagFk: 11, tagSelection: {name: 'Category'}} + const tagGroups = JSON.stringify([ + {values: [{value: 'Concussion'}], tagFk: 67, tagSelection: {name: 'Category'}} ]); - let result = {categoryId: 2, typeId: 4, tags: tags}; + const expectedResult = {categoryId: 2, typeId: 4, tagGroups: tagGroups}; controller.updateStateParams(); - expect(controller.$state.go).toHaveBeenCalledWith('my.current.state', result); + expect(controller.$state.go).toHaveBeenCalledWith('my.current.state', expectedResult); }); }); @@ -307,6 +311,58 @@ describe('Order', () => { expect(controller.orderFields.length).toEqual(7); }); }); + + describe('formatTooltip()', () => { + it(`should return a formatted text with the tag name and values`, () => { + const tagGroup = { + values: [{value: 'Silver'}, {value: 'Brown'}], + tagFk: 1, + tagSelection: { + name: 'Color' + } + }; + + const result = controller.formatTooltip(tagGroup); + + expect(result).toEqual(`Color: "Silver", "Brown"`); + }); + + it(`should return a formatted text with the tag value`, () => { + const tagGroup = { + values: [{value: 'Silver'}] + }; + + const result = controller.formatTooltip(tagGroup); + + expect(result).toEqual(`"Silver"`); + }); + }); + + describe('sanitizedTagGroupParam()', () => { + it(`should return an array of tags`, () => { + const dirtyTagGroups = [{ + values: [{value: 'Silver'}, {value: 'Brown'}], + tagFk: 1, + tagSelection: { + name: 'Color', + $orgRow: {name: 'Color'} + }, + $orgIndex: 1 + }]; + controller.tagGroups = dirtyTagGroups; + + const expectedResult = [{ + values: [{value: 'Silver'}, {value: 'Brown'}], + tagFk: 1, + tagSelection: { + name: 'Color' + } + }]; + const result = controller.sanitizedTagGroupParam(); + + expect(result).toMatchObject(expectedResult); + }); + }); }); });