Tarea #333 Ultimas Entradas

This commit is contained in:
gerard 2018-06-20 12:29:30 +02:00
parent 42eacd4905
commit b8ff2dbb0a
2 changed files with 65 additions and 17 deletions

View File

@ -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;
});

View File

@ -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);
});
});
});
});