Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix KDateRange failing tests on prs opened on last day of the month #718

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/KDateRange/KDateCalendar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@
},
numOfDays: 7,
isFirstChoice: this.selectedStartDate == null ? true : false,
activeMonth: new Date().getMonth() - 1 == -1 ? 11 : new Date().getMonth() - 1,
activeYearStart: new Date().getFullYear(),
activeMonth:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious to know why 1 is subtracted from the active month. It means that the activeMonth will always be the month before the lastAllowedDate right? But if we are in January, the previous month would cause the year to also have to be reduced, but I see that activeYearStart is taken only from getFullYear.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because of how the Date() constructor in JavaScript sets the months. It uses an integer value to represent the month, beginning with 0 for January to 11 for December.

Copy link
Member

@AlexVelezLl AlexVelezLl Aug 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh no, I meant what if the lastAllowedDate was for example new Date(2024, 0, 1), then activeMonth would be 11, but activeYearStart would be 2024, so the active date would be December 2024 instead of December 2023. But I looked at the code and I see that this activeYearStart value is updated in the created hook if this happens, so there is no problem.

Anyways I think we could avoid the ternary here, and the check in the created hook if we do something similar to get the previous month:

    data() {
      const previousMonth = new Date(this.lastAllowedDate);
      previousMonth.setMonth(lastAllowedDate.getMonth() - 1);
      return {
        ...,
        activeMonth: previousMonth.getMonth(),
        activeYearStart: previousMonth.getFullYear(),
      };
    },

But it is totally optional, although I think it would clarify the intention that activeMonth and activeYearStart refer to the previous month of the lastAllowedDate.

this.lastAllowedDate.getMonth() - 1 == -1 ? 11 : this.lastAllowedDate.getMonth() - 1,
activeYearStart: this.lastAllowedDate.getFullYear(),
};
},
computed: {
Expand Down
14 changes: 8 additions & 6 deletions lib/KDateRange/__tests__/KDateRange.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ describe('KDateRange component', () => {

const KDATECALENDAR_PROPS = {
firstAllowedDate: new Date(2022, 0, 1),
lastAllowedDate: new Date(),
lastAllowedDate: new Date(2022, 10, 1),
selectedStartDate: new Date(2022, 8, 1),
selectedEndDate: new Date(),
selectedEndDate: new Date(2022, 10, 1),
previousMonthText: 'Previous Month',
nextMonthText: 'Next Month',
};
Expand Down Expand Up @@ -120,16 +120,18 @@ describe('KDateCalendar component', () => {
expect(wrapper.exists()).toBe(true);
});

it('the month and year display on the left-side calendar view should contain the previous month', async () => {
it('the month and year display on the left-side calendar view should display the previous month of the lastAllowedDate', async () => {
const previousMonthDisplay = getKDateCalendarElements(wrapper).previousMonthDisplay();
const previousMonth = new Date().setDate(0);
const lastAllowedDate = wrapper.props().lastAllowedDate;
const previousMonth = new Date(lastAllowedDate) - 1;
LianaHarris360 marked this conversation as resolved.
Show resolved Hide resolved
const date = Vue.prototype.$formatDate(previousMonth, { month: 'long', year: 'numeric' });
expect(previousMonthDisplay.text()).toBe(date);
});

it('the month and year display on the right-side calendar view should contain the current month', async () => {
it('the month and year display on the right-side calendar view should display the current month of the lastAllowedDate', async () => {
const currentMonthDisplay = getKDateCalendarElements(wrapper).currentMonthDisplay();
const date = Vue.prototype.$formatDate(new Date(), { month: 'long', year: 'numeric' });
const lastAllowedDate = wrapper.props().lastAllowedDate;
const date = Vue.prototype.$formatDate(lastAllowedDate, { month: 'long', year: 'numeric' });
expect(currentMonthDisplay.text()).toBe(date);
});
});
Loading