From 12bd10a095aba01ed678a4441d4e22eedf54483c Mon Sep 17 00:00:00 2001 From: Bart Enkelaar Date: Mon, 5 Oct 2020 16:19:47 +0200 Subject: [PATCH] fix(task) - 11139 use start-from hours in due date calculation (#12609) * fix(task) - 11139 use start-from hours in due date calculation * fix(task) - 11139 - Initial setup of Task unit test * Add more unit tests for formatDueDate --- website/client/src/components/tasks/task.vue | 7 +- .../tests/unit/components/tasks/task.spec.js | 88 +++++++++++++++++++ 2 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 website/client/tests/unit/components/tasks/task.spec.js diff --git a/website/client/src/components/tasks/task.vue b/website/client/src/components/tasks/task.vue index c14ec2ce69..a434bc6a8f 100644 --- a/website/client/src/components/tasks/task.vue +++ b/website/client/src/components/tasks/task.vue @@ -1071,7 +1071,7 @@ export default { this.scoreChecklistItem({ taskId: this.task._id, itemId: item.id }); }, calculateTimeTillDue () { - const endOfToday = moment().endOf('day'); + const endOfToday = moment().subtract(this.user.preferences.dayStart, 'hours').endOf('day'); const endOfDueDate = moment(this.task.date).endOf('day'); return moment.duration(endOfDueDate.diff(endOfToday)); @@ -1080,9 +1080,8 @@ export default { return this.calculateTimeTillDue().asDays() <= 0; }, formatDueDate () { - const dueIn = this.calculateTimeTillDue().asDays() === 0 - ? this.$t('today') - : this.calculateTimeTillDue().humanize(true); + const timeTillDue = this.calculateTimeTillDue(); + const dueIn = timeTillDue.asDays() === 0 ? this.$t('today') : timeTillDue.humanize(true); return this.task.date && this.$t('dueIn', { dueIn }); }, diff --git a/website/client/tests/unit/components/tasks/task.spec.js b/website/client/tests/unit/components/tasks/task.spec.js new file mode 100644 index 0000000000..9349d7ea2f --- /dev/null +++ b/website/client/tests/unit/components/tasks/task.spec.js @@ -0,0 +1,88 @@ +import { shallowMount, createLocalVue } from '@vue/test-utils'; +import moment from 'moment'; + +import Task from '@/components/tasks/task.vue'; +import Store from '@/libs/store'; + +const localVue = createLocalVue(); +localVue.use(Store); + +describe('Task', () => { + let wrapper; + + function makeWrapper (additionalTaskData = {}, additionalUserData = {}) { + return shallowMount(Task, { + propsData: { + task: { + group: {}, + ...additionalTaskData, + }, + }, + store: { + state: { + user: { + data: { + preferences: {}, + ...additionalUserData, + }, + }, + }, + getters: { + 'tasks:getTaskClasses': () => ({}), + 'tasks:canEdit': () => ({}), + 'tasks:canDelete': () => ({}), + }, + }, + mocks: { $t: (key, params) => key + (params ? JSON.stringify(params) : '') }, + directives: { 'b-tooltip': {} }, + localVue, + }); + } + + it('returns a vue instance', () => { + wrapper = makeWrapper(); + expect(wrapper.isVueInstance()).to.be.true; + }); + + describe('Due date calculation', () => { + let clock; + + function setClockTo (time) { + const now = moment(time); + clock = sinon.useFakeTimers(now.toDate()); + return now; + } + + afterEach(() => { + clock.restore(); + }); + + it('formats due date to today if due today', () => { + const now = setClockTo('2019-09-17T17:57:00+02:00'); + wrapper = makeWrapper({ date: now }); + + expect(wrapper.vm.formatDueDate()).to.equal('dueIn{"dueIn":"today"}'); + }); + + it('formats due date to tomorrow if due tomorrow', () => { + const now = setClockTo('2012-06-12T14:17:28Z'); + wrapper = makeWrapper({ date: now.add(1, 'day') }); + + expect(wrapper.vm.formatDueDate()).to.equal('dueIn{"dueIn":"in a day"}'); + }); + + it('formats due date to 5 days if due in 5 days', () => { + const now = setClockTo(); + wrapper = makeWrapper({ date: now.add(5, 'days') }); + + expect(wrapper.vm.formatDueDate()).to.equal('dueIn{"dueIn":"in 5 days"}'); + }); + + it('formats due date to tomorrow if today but before dayStart', () => { + const now = setClockTo('2019-06-12T04:23:37+02:00'); + wrapper = makeWrapper({ date: now.add(8, 'hours') }, { preferences: { dayStart: 7 } }); + + expect(wrapper.vm.formatDueDate()).to.equal('dueIn{"dueIn":"in a day"}'); + }); + }); +});