From e53af13b3ce94d1a2362c4f2807e4fc48355a068 Mon Sep 17 00:00:00 2001 From: carlosjr Date: Thu, 18 Nov 2021 11:04:51 +0100 Subject: [PATCH] unit tests for smart table and ticket index --- front/core/components/smart-table/index.js | 16 +-- .../core/components/smart-table/index.spec.js | 97 +++++++++++++++++++ .../monitor/front/index/tickets/index.spec.js | 18 ++++ 3 files changed, 116 insertions(+), 15 deletions(-) create mode 100644 front/core/components/smart-table/index.spec.js diff --git a/front/core/components/smart-table/index.js b/front/core/components/smart-table/index.js index f32d8aa4f..cc359e41f 100644 --- a/front/core/components/smart-table/index.js +++ b/front/core/components/smart-table/index.js @@ -54,20 +54,6 @@ export default class SmartTable extends Component { set viewConfigId(value) { this._viewConfigId = value; - - /* if (value) { - this.defaultViewConfig = {}; - - const url = 'DefaultViewConfigs'; - const filter = {where: {tableCode: value}}; - this.$http.get(url, {filter}) - .then(res => { - if (res && res.data.length) { - const columns = res.data[0].columns; - this.defaultViewConfig = columns; - } - }); - } */ } getDefaultViewConfig() { @@ -163,7 +149,7 @@ export default class SmartTable extends Component { } } - let styleElement = document.querySelector('style[id="smart-table"]'); + const styleElement = document.querySelector('style[id="smart-table"]'); if (styleElement) styleElement.parentNode.removeChild(styleElement); diff --git a/front/core/components/smart-table/index.spec.js b/front/core/components/smart-table/index.spec.js new file mode 100644 index 000000000..d0ff2f53f --- /dev/null +++ b/front/core/components/smart-table/index.spec.js @@ -0,0 +1,97 @@ +describe('Component smartTable', () => { + let $element; + let controller; + let $httpBackend; + + beforeEach(ngModule('vnCore')); + + beforeEach(inject(($compile, $rootScope, _$httpBackend_) => { + $httpBackend = _$httpBackend_; + $element = $compile(``)($rootScope); + controller = $element.controller('smartTable'); + })); + + afterEach(() => { + $element.remove(); + }); + + describe('options setter()', () => { + it(`should throw an error if the table doesn't have an identifier`, () => { + const options = {activeButtons: {shownColumns: []}}; + + expect(() => controller.options = options).toThrowError(/View identifier not defined/); + }); + + it('should not throw an error if the table does have an identifier', () => { + const options = {activeButtons: {shownColumns: []}}; + controller.viewConfigId = 'test'; + + expect(() => controller.options = options).not.toThrow(); + }); + }); + + describe('getDefaultViewConfig()', () => { + it('should perform a query and return the default view columns', done => { + const expectedResponse = [{ + columns: {} + }]; + + $httpBackend.expectGET('DefaultViewConfigs').respond(expectedResponse); + controller.getDefaultViewConfig().then(columns => { + expect(columns).toEqual(expectedResponse[0].columns); + done(); + }).catch(done.fail); + $httpBackend.flush(); + }); + }); + + describe('viewConfig setter', () => { + it('should just call applyViewConfig() if a viewConfig was provided', () => { + spyOn(controller, 'applyViewConfig'); + controller.viewConfig = [{}]; + + expect(controller.applyViewConfig).toHaveBeenCalled(); + }); + + it('should not get a defaultConfig then insert a new one', () => { + spyOn(controller, 'applyViewConfig'); + + controller.$.userViewModel = { + insert: jest.fn() + }; + + const emptyResponse = [{ + columns: {} + }]; + + controller.columns = [ + {field: 'test1'}, + {field: 'test2'} + ]; + + $httpBackend.expectGET('DefaultViewConfigs').respond(emptyResponse); + controller.viewConfig = []; + $httpBackend.flush(); + + const expectedObject = {configuration: {'test1': true, 'test2': true}}; + + expect(controller.$.userViewModel.insert).toHaveBeenCalledWith(expect.objectContaining(expectedObject)); + expect(controller.applyViewConfig).toHaveBeenCalled(); + }); + }); + + describe('applyViewConfig()', () => { + it('should ', () => { + controller.$.userViewModel = { + viewConfig: {'test1': true, 'test2': false} + }; + + controller._columns = [ + {field: 'test1'}, + {field: 'test2'} + ]; + + controller.applyViewConfig(); + }); + }); +}); diff --git a/modules/monitor/front/index/tickets/index.spec.js b/modules/monitor/front/index/tickets/index.spec.js index 8d06b8d7d..fb507dbbc 100644 --- a/modules/monitor/front/index/tickets/index.spec.js +++ b/modules/monitor/front/index/tickets/index.spec.js @@ -123,6 +123,24 @@ describe('Component vnMonitorSalesTickets', () => { }); }); + describe('dateRange()', () => { + it('should return two dates with the hours at the start and end of the given date', () => { + const now = new Date(); + + const today = now.getDate(); + + const dateRange = controller.dateRange(now); + const start = dateRange[0].toString(); + const end = dateRange[1].toString(); + + expect(start).toContain(today); + expect(start).toContain('00:00:00'); + + expect(end).toContain(today); + expect(end).toContain('23:59:59'); + }); + }); + describe('preview()', () => { it('should show the dialog summary', () => { controller.$.summary = {show: () => {}};