chore: Replace availability mixin
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
import { ref } from 'vue';
|
||||
import { useAvailability } from '../useAvailability';
|
||||
import * as useNextAvailabilityTime from '../useNextAvailabilityTime';
|
||||
import * as useI18n from 'dashboard/composables/useI18n';
|
||||
import * as useMapGetter from 'dashboard/composables/store';
|
||||
|
||||
vi.mock('../useNextAvailabilityTime');
|
||||
vi.mock('dashboard/composables/useI18n');
|
||||
vi.mock('dashboard/composables/store');
|
||||
|
||||
describe('useAvailability', () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
useNextAvailabilityTime.useNextAvailabilityTime.mockReturnValue({
|
||||
timeLeftToBackInOnline: ref('2 hours'),
|
||||
});
|
||||
useI18n.useI18n.mockReturnValue({
|
||||
t: vi.fn(key => key),
|
||||
});
|
||||
useMapGetter.useMapGetter.mockReturnValue(ref([]));
|
||||
|
||||
global.chatwootWebChannel = {
|
||||
workingHoursEnabled: true,
|
||||
workingHours: [
|
||||
{
|
||||
day_of_week: 3,
|
||||
closed_all_day: false,
|
||||
open_hour: 8,
|
||||
open_minutes: 30,
|
||||
close_hour: 17,
|
||||
close_minutes: 35,
|
||||
open_all_day: false,
|
||||
},
|
||||
{
|
||||
day_of_week: 4,
|
||||
closed_all_day: false,
|
||||
open_hour: 8,
|
||||
open_minutes: 30,
|
||||
close_hour: 17,
|
||||
close_minutes: 30,
|
||||
open_all_day: false,
|
||||
},
|
||||
],
|
||||
utcOffset: '-07:00',
|
||||
replyTime: 'in_a_few_hours',
|
||||
};
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('returns valid isInBetweenTheWorkingHours if in different timezone', () => {
|
||||
vi.setSystemTime(new Date('Thu Apr 14 2022 06:04:46 GMT+0530'));
|
||||
const { isInBetweenTheWorkingHours } = useAvailability();
|
||||
expect(isInBetweenTheWorkingHours.value).toBe(true);
|
||||
});
|
||||
|
||||
it('returns valid isInBetweenTheWorkingHours if in same timezone', () => {
|
||||
global.chatwootWebChannel.utcOffset = '+05:30';
|
||||
vi.setSystemTime(new Date('Thu Apr 14 2022 09:01:46 GMT+0530'));
|
||||
const { isInBetweenTheWorkingHours } = useAvailability();
|
||||
expect(isInBetweenTheWorkingHours.value).toBe(true);
|
||||
});
|
||||
|
||||
it('returns false if closed all day', () => {
|
||||
global.chatwootWebChannel.utcOffset = '-07:00';
|
||||
global.chatwootWebChannel.workingHours = [
|
||||
{ day_of_week: 3, closed_all_day: true },
|
||||
];
|
||||
vi.setSystemTime(new Date('Thu Apr 14 2022 09:01:46 GMT+0530'));
|
||||
const { isInBetweenTheWorkingHours } = useAvailability();
|
||||
expect(isInBetweenTheWorkingHours.value).toBe(false);
|
||||
});
|
||||
|
||||
it('returns true if open all day', () => {
|
||||
global.chatwootWebChannel.utcOffset = '-07:00';
|
||||
global.chatwootWebChannel.workingHours = [
|
||||
{ day_of_week: 3, open_all_day: true },
|
||||
];
|
||||
vi.setSystemTime(new Date('Thu Apr 14 2022 09:01:46 GMT+0530'));
|
||||
const { isInBetweenTheWorkingHours } = useAvailability();
|
||||
expect(isInBetweenTheWorkingHours.value).toBe(true);
|
||||
});
|
||||
|
||||
it('returns correct replyTimeStatus', () => {
|
||||
const { replyTimeStatus } = useAvailability();
|
||||
expect(replyTimeStatus.value).toBe('REPLY_TIME.IN_A_FEW_HOURS');
|
||||
});
|
||||
|
||||
it('returns correct replyWaitMessage when online', () => {
|
||||
global.chatwootWebChannel.workingHoursEnabled = true;
|
||||
global.chatwootWebChannel.utcOffset = '+05:30';
|
||||
vi.setSystemTime(new Date('Thu Apr 14 2022 09:01:46 GMT+0530'));
|
||||
const { replyWaitMessage, isInBetweenTheWorkingHours } = useAvailability();
|
||||
expect(isInBetweenTheWorkingHours.value).toBe(true);
|
||||
expect(replyWaitMessage.value).toBe('REPLY_TIME.IN_A_FEW_HOURS');
|
||||
});
|
||||
|
||||
it('returns correct replyWaitMessage when offline', () => {
|
||||
global.chatwootWebChannel.workingHoursEnabled = true;
|
||||
global.chatwootWebChannel.workingHours = [
|
||||
{ day_of_week: 3, closed_all_day: true },
|
||||
];
|
||||
vi.setSystemTime(new Date('Thu Apr 14 2022 09:01:46 GMT+0530'));
|
||||
const { replyWaitMessage } = useAvailability();
|
||||
expect(replyWaitMessage.value).toBe('REPLY_TIME.BACK_IN 2 hours');
|
||||
});
|
||||
|
||||
it('returns correct isInBusinessHours', () => {
|
||||
global.chatwootWebChannel.workingHoursEnabled = true;
|
||||
global.chatwootWebChannel.utcOffset = '+05:30';
|
||||
vi.setSystemTime(new Date('Thu Apr 14 2022 09:01:46 GMT+0530'));
|
||||
const { isInBusinessHours, isInBetweenTheWorkingHours } = useAvailability();
|
||||
expect(isInBetweenTheWorkingHours.value).toBe(true);
|
||||
expect(isInBusinessHours.value).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,275 @@
|
||||
import { useNextAvailabilityTime } from '../useNextAvailabilityTime';
|
||||
import { useI18n } from 'dashboard/composables/useI18n';
|
||||
import { generateRelativeTime } from 'shared/helpers/DateHelper';
|
||||
import { utcToZonedTime } from 'date-fns-tz';
|
||||
|
||||
vi.mock('dashboard/composables/useI18n');
|
||||
|
||||
describe('useNextAvailabilityTime', () => {
|
||||
const chatwootWebChannel = {
|
||||
workingHoursEnabled: true,
|
||||
workingHours: [
|
||||
{
|
||||
day_of_week: 0,
|
||||
open_hour: 9,
|
||||
closed_all_day: false,
|
||||
open_minutes: 0,
|
||||
close_hour: 17,
|
||||
},
|
||||
{
|
||||
day_of_week: 1,
|
||||
open_hour: 9,
|
||||
closed_all_day: false,
|
||||
open_minutes: 0,
|
||||
close_hour: 17,
|
||||
},
|
||||
{
|
||||
day_of_week: 2,
|
||||
open_hour: 9,
|
||||
closed_all_day: false,
|
||||
open_minutes: 0,
|
||||
close_hour: 17,
|
||||
},
|
||||
{
|
||||
day_of_week: 3,
|
||||
open_hour: 9,
|
||||
closed_all_day: false,
|
||||
open_minutes: 0,
|
||||
close_hour: 17,
|
||||
},
|
||||
{
|
||||
day_of_week: 4,
|
||||
open_hour: 9,
|
||||
closed_all_day: false,
|
||||
open_minutes: 0,
|
||||
close_hour: 17,
|
||||
},
|
||||
{
|
||||
day_of_week: 5,
|
||||
open_hour: 9,
|
||||
closed_all_day: false,
|
||||
open_minutes: 0,
|
||||
close_hour: 17,
|
||||
},
|
||||
{
|
||||
day_of_week: 6,
|
||||
open_hour: 9,
|
||||
closed_all_day: false,
|
||||
open_minutes: 0,
|
||||
close_hour: 17,
|
||||
},
|
||||
],
|
||||
timezone: 'UTC',
|
||||
locale: 'en',
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
window.chatwootWebChannel = chatwootWebChannel;
|
||||
useI18n.mockReturnValue({
|
||||
t: vi.fn(key => {
|
||||
if (key === 'DAY_NAMES') {
|
||||
return [
|
||||
'Sunday',
|
||||
'Monday',
|
||||
'Tuesday',
|
||||
'Wednesday',
|
||||
'Thursday',
|
||||
'Friday',
|
||||
'Saturday',
|
||||
];
|
||||
}
|
||||
return key;
|
||||
}),
|
||||
});
|
||||
generateRelativeTime.mockImplementation((value, unit) => {
|
||||
if (unit === 'hour') return `in ${value} hour${value > 1 ? 's' : ''}`;
|
||||
if (unit === 'minutes')
|
||||
return `in ${value} minute${value > 1 ? 's' : ''}`;
|
||||
if (unit === 'days') return value === 1 ? 'tomorrow' : `in ${value} days`;
|
||||
return `in ${value} ${unit}`;
|
||||
});
|
||||
utcToZonedTime.mockImplementation(date => new Date(date));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
delete window.chatwootWebChannel;
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should return day names', () => {
|
||||
const { dayNames } = useNextAvailabilityTime();
|
||||
expect(dayNames).toEqual([
|
||||
'Sunday',
|
||||
'Monday',
|
||||
'Tuesday',
|
||||
'Wednesday',
|
||||
'Thursday',
|
||||
'Friday',
|
||||
'Saturday',
|
||||
]);
|
||||
});
|
||||
|
||||
it('should return channelConfig', () => {
|
||||
const { channelConfig } = useNextAvailabilityTime();
|
||||
expect(channelConfig.value).toEqual(chatwootWebChannel);
|
||||
});
|
||||
|
||||
it('should return workingHours', () => {
|
||||
const { workingHours } = useNextAvailabilityTime();
|
||||
expect(workingHours.value).toEqual(chatwootWebChannel.workingHours);
|
||||
});
|
||||
|
||||
it('should return currentDayWorkingHours', () => {
|
||||
const { currentDayWorkingHours } = useNextAvailabilityTime();
|
||||
const currentDay = new Date().getDay();
|
||||
const expectedWorkingHours = chatwootWebChannel.workingHours.find(
|
||||
slot => slot.day_of_week === currentDay
|
||||
);
|
||||
expect(currentDayWorkingHours.value).toEqual(expectedWorkingHours);
|
||||
});
|
||||
|
||||
it('should return nextDayWorkingHours', () => {
|
||||
const { nextDayWorkingHours } = useNextAvailabilityTime();
|
||||
const currentDay = new Date().getDay();
|
||||
const nextDay = currentDay === 6 ? 0 : currentDay + 1;
|
||||
const expectedWorkingHours = chatwootWebChannel.workingHours.find(
|
||||
slot => slot.day_of_week === nextDay
|
||||
);
|
||||
expect(nextDayWorkingHours.value).toEqual(expectedWorkingHours);
|
||||
});
|
||||
|
||||
it('should return presentHour', () => {
|
||||
const { presentHour } = useNextAvailabilityTime();
|
||||
expect(presentHour.value).toBe(new Date().getUTCHours());
|
||||
});
|
||||
|
||||
it('should return presentMinute', () => {
|
||||
const { presentMinute } = useNextAvailabilityTime();
|
||||
expect(presentMinute.value).toBe(new Date().getUTCMinutes());
|
||||
});
|
||||
|
||||
it('should return currentDay', () => {
|
||||
const { currentDay } = useNextAvailabilityTime();
|
||||
const date = new Date();
|
||||
const day = date.getUTCDay();
|
||||
expect(currentDay.value).toBe(day);
|
||||
});
|
||||
|
||||
it('should return currentDayTimings', () => {
|
||||
const { currentDayTimings, currentDayWorkingHours } =
|
||||
useNextAvailabilityTime();
|
||||
const {
|
||||
open_hour: openHour,
|
||||
open_minutes: openMinute,
|
||||
close_hour: closeHour,
|
||||
} = currentDayWorkingHours.value;
|
||||
expect(currentDayTimings.value).toEqual({
|
||||
openHour,
|
||||
openMinute,
|
||||
closeHour,
|
||||
});
|
||||
});
|
||||
|
||||
it('should return nextDayTimings', () => {
|
||||
const { nextDayTimings, nextDayWorkingHours } = useNextAvailabilityTime();
|
||||
const { open_hour: openHour, open_minutes: openMinute } =
|
||||
nextDayWorkingHours.value;
|
||||
expect(nextDayTimings.value).toEqual({
|
||||
openHour,
|
||||
openMinute,
|
||||
});
|
||||
});
|
||||
|
||||
it('should return dayDiff', () => {
|
||||
const { dayDiff, currentDay, nextDayWorkingHours } =
|
||||
useNextAvailabilityTime();
|
||||
const nextDay = nextDayWorkingHours.value.day_of_week;
|
||||
const totalDays = 6;
|
||||
const expectedDayDiff =
|
||||
nextDay > currentDay.value
|
||||
? nextDay - currentDay.value - 1
|
||||
: totalDays - currentDay.value + nextDay;
|
||||
expect(dayDiff.value).toEqual(expectedDayDiff);
|
||||
});
|
||||
|
||||
it('should return dayNameOfNextWorkingDay', () => {
|
||||
const { dayNameOfNextWorkingDay, nextDayWorkingHours, dayNames } =
|
||||
useNextAvailabilityTime();
|
||||
const nextDay = nextDayWorkingHours.value.day_of_week;
|
||||
const expectedDayName = dayNames[nextDay];
|
||||
expect(dayNameOfNextWorkingDay.value).toEqual(expectedDayName);
|
||||
});
|
||||
|
||||
it('should return hoursAndMinutesBackInOnline', () => {
|
||||
const { hoursAndMinutesBackInOnline } = useNextAvailabilityTime();
|
||||
expect(hoursAndMinutesBackInOnline.value).toBeDefined();
|
||||
expect(typeof hoursAndMinutesBackInOnline.value.hoursLeft).toBe('number');
|
||||
expect(typeof hoursAndMinutesBackInOnline.value.minutesLeft).toBe('number');
|
||||
});
|
||||
|
||||
// Time-based tests
|
||||
describe('time-based tests', () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('should return in 30 minutes', () => {
|
||||
vi.setSystemTime(new Date('2022-04-14T17:30:00.000Z')); // Thursday
|
||||
chatwootWebChannel.workingHours[4].open_hour = 18;
|
||||
chatwootWebChannel.workingHours[4].open_minutes = 0;
|
||||
chatwootWebChannel.workingHours[4].close_hour = 23;
|
||||
|
||||
const { timeLeftToBackInOnline, setTimeSlot } = useNextAvailabilityTime();
|
||||
setTimeSlot();
|
||||
expect(timeLeftToBackInOnline.value).toBe('in 30 minutes');
|
||||
});
|
||||
|
||||
it('should return in 2 hours', () => {
|
||||
vi.setSystemTime(new Date('2022-04-14T17:00:00.000Z')); // Thursday
|
||||
chatwootWebChannel.workingHours[4].open_hour = 19;
|
||||
chatwootWebChannel.workingHours[4].open_minutes = 0;
|
||||
chatwootWebChannel.workingHours[4].close_hour = 23;
|
||||
|
||||
const { timeLeftToBackInOnline, setTimeSlot } = useNextAvailabilityTime();
|
||||
setTimeSlot();
|
||||
expect(timeLeftToBackInOnline.value).toBe('in 2 hours');
|
||||
});
|
||||
|
||||
it('should return at 10:00 AM', () => {
|
||||
vi.setSystemTime(new Date('2022-04-14T22:00:00.000Z')); // Thursday
|
||||
chatwootWebChannel.workingHours[5].open_hour = 10; // Friday
|
||||
chatwootWebChannel.workingHours[5].open_minutes = 0;
|
||||
chatwootWebChannel.workingHours[5].close_hour = 18;
|
||||
|
||||
const { timeLeftToBackInOnline, setTimeSlot } = useNextAvailabilityTime();
|
||||
setTimeSlot();
|
||||
expect(timeLeftToBackInOnline.value).toBe('at 10:00 AM');
|
||||
});
|
||||
|
||||
it('should return tomorrow', () => {
|
||||
vi.setSystemTime(new Date('2022-04-14T22:00:00.000Z')); // Thursday
|
||||
chatwootWebChannel.workingHours[5].open_hour = 9; // Friday
|
||||
chatwootWebChannel.workingHours[5].open_minutes = 0;
|
||||
chatwootWebChannel.workingHours[5].close_hour = 17;
|
||||
|
||||
const { timeLeftToBackInOnline, setTimeSlot } = useNextAvailabilityTime();
|
||||
setTimeSlot();
|
||||
expect(timeLeftToBackInOnline.value).toBe('tomorrow');
|
||||
});
|
||||
|
||||
it('should return on Saturday', () => {
|
||||
vi.setSystemTime(new Date('2022-04-14T22:00:00.000Z')); // Thursday
|
||||
chatwootWebChannel.workingHours[4].open_hour = 9;
|
||||
chatwootWebChannel.workingHours[4].close_hour = 17;
|
||||
chatwootWebChannel.workingHours[5].closed_all_day = true; // Friday closed
|
||||
|
||||
const { timeLeftToBackInOnline, setTimeSlot } = useNextAvailabilityTime();
|
||||
setTimeSlot();
|
||||
expect(timeLeftToBackInOnline.value).toBe('on Saturday');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user