From f900b7ef62fac5691c82724e634c690e84ea0184 Mon Sep 17 00:00:00 2001 From: Bernat Exposito Domenech Date: Mon, 17 Feb 2020 13:50:54 +0100 Subject: [PATCH] crud model test --- .../core/components/crud-model/crud-model.js | 4 +- .../core/components/crud-model/index.spec.js | 190 +++++++++++++++++- modules/ticket/back/models/alert-level.json | 32 +-- 3 files changed, 206 insertions(+), 20 deletions(-) diff --git a/front/core/components/crud-model/crud-model.js b/front/core/components/crud-model/crud-model.js index ef5c346b9..52052cc7f 100644 --- a/front/core/components/crud-model/crud-model.js +++ b/front/core/components/crud-model/crud-model.js @@ -100,7 +100,7 @@ export default class CrudModel extends ModelProxy { } removeFilter() { - return applyFilter(null, null); + return this.applyFilter(null, null); } /** @@ -240,14 +240,12 @@ export default class CrudModel extends ModelProxy { onRemoteDone(json, filter, append) { let data = json.data; - if (append) this.orgData = this.orgData.concat(data); else { this.orgData = data; this.currentFilter = filter; } - this.data = this.proxiedData.slice(); this.moreRows = filter.limit && data.length == filter.limit; this.onRequestEnd(); diff --git a/front/core/components/crud-model/index.spec.js b/front/core/components/crud-model/index.spec.js index e0daa2558..0927b75eb 100644 --- a/front/core/components/crud-model/index.spec.js +++ b/front/core/components/crud-model/index.spec.js @@ -25,7 +25,7 @@ describe('Component vnCrudModel', () => { }); describe('save()', () => { - it(`should make an HTTP post query and then update the original rows with the returned values`, () => { + it('should make an HTTP post query and then update the original rows with the returned values', () => { spyOn(controller, 'applyChanges'); controller.insert({value: 'My new item 1'}); @@ -47,4 +47,192 @@ describe('Component vnCrudModel', () => { expect(controller.applyChanges).toHaveBeenCalledWith(); }); }); + + describe('setter url()', () => { + it('should set the url', () => { + spyOn(controller, 'autoRefresh'); + spyOn(controller, 'clear'); + + controller.url = '/TestUrl'; + + expect(controller.url).toEqual('/TestUrl'); + }); + }); + + describe('isLoading()', () => { + it('should return false if canceler is null', () => { + controller.canceler = null; + + expect(controller.isLoading).toBe(false); + }); + + it('should return true if canceler is not null', () => { + controller.canceler = 'validValue'; + + expect(controller.isLoading).toBe(true); + }); + }); + + describe('autoRefresh()', () => { + it('', () => { + // spyOn(controller, 'autoRefresh'); + // spyOn(controller, 'clear'); + + // controller.url = '/TestUrl'; + + // expect(controller.url).toEqual('/TestUrl'); + }); + }); + + describe('buildFilter()', () => { + it('should build a filter and return it', () => { + controller.order = 'id ASC'; + controller.fields = ['id']; + controller.limit = 1; + controller.filter = 'filterTest'; + + const result = controller.buildFilter(); + + expect(Object.keys(result).length).toEqual(13); + }); + }); + + describe('sendRequest()', () => { + it('should call refresh() and check that the sendRequest is called', () => { + spyOn(controller, 'sendRequest').and.callThrough(); + spyOn(controller, 'onRemoteDone'); + spyOn(controller, 'onRemoteError'); + + const filter = {id: 1}; + const serializedParams = encodeURIComponent(JSON.stringify(filter)); + + $httpBackend.whenRoute('GET', `model?filter=${serializedParams}`).respond(); + controller.sendRequest(filter, true); + $httpBackend.flush(); + + expect(controller.isPaging).toBe(false); + }); + }); + + describe('addFilter()', () => { + it('should call addFilter and check that the new filter has been added', () => { + spyOn(controller, 'refresh'); + + const filter = {where: {id: 1}}; + controller.userFilter = {where: {name: 'test'}}; + const filterMerged = {'where': {'and': [{'name': 'test'}, {'id': 1}]}}; + + controller.addFilter(filter); + + expect(controller.userFilter).toEqual(filterMerged); + expect(controller.refresh).toHaveBeenCalledWith(); + }); + }); + + describe('applyFilter()', () => { + it('should call applyFilter and check that the refresh() is called', () => { + spyOn(controller, 'refresh'); + + const filter = {where: {id: 1}}; + const params = {where: {id: 2}}; + + controller.applyFilter(filter, params); + + expect(controller.userFilter).toEqual(filter); + expect(controller.userParams).toEqual(params); + expect(controller.refresh).toHaveBeenCalledWith(); + }); + }); + + describe('removeFilter()', () => { + it('should check the userFilter and userParams are removed', () => { + controller.removeFilter(); + + expect(controller.userFilter).toBe(null); + expect(controller.userParams).toBe(null); + }); + }); + + xdescribe('cancelRequest()', () => { + it('should check the resolve method is called and canceler is null', () => { + controller.canceler = {resolve: () => {}}; + spyOn(controller.canceler, 'resolve'); + controller.cancelRequest(); + + expect(controller.canceler).toBe(null); + }); + }); + + describe('loadMore()', () => { + it('should call sendRequest with the new filter', () => { + spyOn(controller, 'sendRequest'); + + controller.moreRows = true; + + controller.loadMore(); + + expect(controller.sendRequest).toHaveBeenCalledWith({'skip': 2}, true); + }); + }); + + describe('clear()', () => { + it('should check that orgData and moreRows are set to null', () => { + controller.moreRows = true; + + controller.clear(); + + expect(controller.moreRows).toBe(null); + expect(controller.orgData).toBe(null); + }); + }); + + // describe('buildParams()', () => { + // it('', () => { + // // spyOn(controller, 'autoRefresh'); + // // spyOn(controller, 'clear'); + + // // controller.url = '/TestUrl'; + + // // expect(controller.url).toEqual('/TestUrl'); + // }); + // }); + + describe('refresh()', () => { + it('shold resolve a fake promise if this._url is undefined', () => { + spyOn(controller.$q, 'resolve'); + + controller._url = undefined; + + controller.refresh(); + + expect(controller.$q.resolve).toHaveBeenCalledWith(); + }); + }); + + describe('onRemoteDone()', () => { + it('', () => { + spyOn(controller, 'onRequestEnd'); + + const json = {data: [ + { + id: 1, + name: 'test' + }]}; + const filter = {limit: 1}; + controller.onRemoteDone(json, filter, true); + + expect(controller.moreRows).toBe(true); + expect(controller.onRequestEnd).toHaveBeenCalledWith(); + }); + }); + + xdescribe('onRemoteError()', () => { + it('should check the error', () => { + spyOn(controller, 'onRequestEnd'); + + controller.onRemoteError(); + + // expect(controller.url).toEqual('/TestUrl'); + }); + }); }); diff --git a/modules/ticket/back/models/alert-level.json b/modules/ticket/back/models/alert-level.json index 75b5a9c46..a94c01106 100644 --- a/modules/ticket/back/models/alert-level.json +++ b/modules/ticket/back/models/alert-level.json @@ -8,22 +8,22 @@ } }, "properties": { - "code": { - "type": "String", - "id": true, - "description": "Identifier" - }, - "alertLevel": { - "type": "Number", - "id": true - } + "code": { + "type": "String", + "id": true, + "description": "Identifier" + }, + "alertLevel": { + "type": "Number", + "id": true + } }, "acls": [ - { - "accessType": "READ", - "principalType": "ROLE", - "principalId": "$everyone", - "permission": "ALLOW" - } + { + "accessType": "READ", + "principalType": "ROLE", + "principalId": "$everyone", + "permission": "ALLOW" + } ] - } \ No newline at end of file +} \ No newline at end of file