import './index'; import crudModel from 'core/mocks/crud-model'; describe('client defaulter', () => { describe('Component vnClientDefaulter', () => { let controller; let $httpBackend; beforeEach(ngModule('client')); beforeEach(inject(($componentController, _$httpBackend_) => { $httpBackend = _$httpBackend_; const $element = angular.element(''); controller = $componentController('vnClientDefaulter', {$element}); controller.$.model = crudModel; controller.$.model.data = [ {clientFk: 1101, amount: 125}, {clientFk: 1102, amount: 500}, {clientFk: 1103, amount: 250} ]; })); describe('checked() getter', () => { it('should return the checked lines', () => { const data = controller.$.model.data; data[1].checked = true; data[2].checked = true; const checkedRows = controller.checked; const firstCheckedRow = checkedRows[0]; const secondCheckedRow = checkedRows[1]; expect(firstCheckedRow.clientFk).toEqual(1102); expect(secondCheckedRow.clientFk).toEqual(1103); }); }); describe('chipColor()', () => { it('should return undefined when the date is the present', () => { let today = new Date(); let result = controller.chipColor(today); expect(result).toEqual(undefined); }); it('should return warning when the date is 10 days in the past', () => { let pastDate = new Date(); pastDate = pastDate.setDate(pastDate.getDate() - 11); let result = controller.chipColor(pastDate); expect(result).toEqual('warning'); }); it('should return alert when the date is 20 days in the past', () => { let pastDate = new Date(); pastDate = pastDate.setDate(pastDate.getDate() - 21); let result = controller.chipColor(pastDate); expect(result).toEqual('alert'); }); }); describe('onResponse()', () => { it('should return error for empty message', () => { let error; try { controller.onResponse(); } catch (e) { error = e; } expect(error).toBeDefined(); expect(error.message).toBe(`The message can't be empty`); }); it('should return saved message', () => { const data = controller.$.model.data; data[1].checked = true; controller.defaulter = {observation: 'My new observation'}; const params = [{text: controller.defaulter.observation, clientFk: data[1].clientFk}]; jest.spyOn(controller.vnApp, 'showMessage'); $httpBackend.expect('GET', `Defaulters/filter`).respond(200); $httpBackend.expect('POST', `ClientObservations`, params).respond(200, params); controller.onResponse(); $httpBackend.flush(); expect(controller.vnApp.showMessage).toHaveBeenCalledWith('Observation saved!'); }); }); describe('exprBuilder()', () => { it('should search by sales person', () => { const expr = controller.exprBuilder('salesPersonFk', '5'); expect(expr).toEqual({'d.salesPersonFk': '5'}); }); it('should search by client', () => { const expr = controller.exprBuilder('clientFk', '5'); expect(expr).toEqual({'d.clientFk': '5'}); }); }); }); });