diff --git a/client/item/src/last-entries/index.js b/client/item/src/last-entries/index.js index 1d721b744..d731f30a7 100644 --- a/client/item/src/last-entries/index.js +++ b/client/item/src/last-entries/index.js @@ -8,13 +8,10 @@ class Controller { this.entries = []; } - $onInit() { - this._defaultEntriesDate(); - } - set item(value) { this._item = value; - this._getLastEntries(this.entriesDate); + this._defaultEntriesDate(); + this._getLastEntries(); } get item() { @@ -25,22 +22,14 @@ class Controller { let defaultDate; defaultDate = new Date(); defaultDate.setDate(defaultDate.getDate() - 75); + defaultDate.setHours(0, 0, 0, 0); this.entriesDate = defaultDate; } - set entriesDate(value) { - this._entriesDate = value; - this._getLastEntries(value); - } - - get entriesDate() { - return this._entriesDate; - } - - _getLastEntries(entriesDate) { - if (!entriesDate || !this.item) + _getLastEntries() { + if (!this.entriesDate || !this.item) return; - let filter = {itemFk: this.item.id, date: entriesDate}; + let filter = {itemFk: this.item.id, date: this.entriesDate}; this.$http.get(`/item/api/Items/getLastEntries?filter=${JSON.stringify(filter)}`).then(res => { this.entries = res.data; }); diff --git a/client/item/src/last-entries/index.spec.js b/client/item/src/last-entries/index.spec.js new file mode 100644 index 000000000..c279372f8 --- /dev/null +++ b/client/item/src/last-entries/index.spec.js @@ -0,0 +1,59 @@ +import './index.js'; + +fdescribe('Item', () => { + describe('Component vnItemLastEntries', () => { + let $componentController; + let $scope; + let controller; + let $httpBackend; + + beforeEach(() => { + angular.mock.module('item'); + }); + + beforeEach(angular.mock.inject((_$componentController_, $rootScope, _$httpBackend_) => { + $componentController = _$componentController_; + $httpBackend = _$httpBackend_; + $scope = $rootScope.$new(); + controller = $componentController('vnItemLastEntries', {$scope: $scope}); + controller.item = {id: 3}; + })); + + describe('set item()', () => { + it(`should set item and call _defaultEntriesDate() and _getLastEntries()`, () => { + spyOn(controller, '_defaultEntriesDate'); + spyOn(controller, '_getLastEntries'); + controller.item = []; + + expect(controller._defaultEntriesDate).toHaveBeenCalledWith(); + expect(controller._getLastEntries).toHaveBeenCalledWith(); + expect(controller.item).toEqual([]); + }); + }); + + describe('_defaultEntriesDate()', () => { + it(`should set entriesDate to a date 75 days ago`, () => { + let defaultDate; + defaultDate = new Date(); + defaultDate.setDate(defaultDate.getDate() - 75); + defaultDate.setHours(0, 0, 0, 0); + controller._defaultEntriesDate(); + + expect(controller.entriesDate).toEqual(defaultDate); + }); + }); + + describe('_getLastEntries()', () => { + it(`should make a GET if entriesDate and item are defined`, () => { + controller._defaultEntriesDate(); + let filter = {itemFk: controller.item.id, date: controller.entriesDate}; + $httpBackend.whenGET(`/item/api/Items/getLastEntries?filter=${JSON.stringify(filter)}`).respond([]); + $httpBackend.expectGET(`/item/api/Items/getLastEntries?filter=${JSON.stringify(filter)}`); + controller._getLastEntries(); + $httpBackend.flush(); + // expect(controller.entriesDate).toEqual(defaultDate); + }); + }); + }); +}); +