feat: add vitest for testing and implement tests for calculateDay utility
- Added "test" script to package.json for running vitest. - Updated api.js to use localhost for backend during development. - Created utils.spec.js to test calculateDay function with various scenarios. - Updated vite.config.js to include vitest configuration and conditionally load Vue DevTools.
This commit is contained in:
@@ -4,7 +4,8 @@ import router from '@/router'
|
||||
import { useAuthStore } from '@/stores/authStore'
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: '/odoo/api', // Twój backend
|
||||
// baseURL: '/odoo/api', // Twój backend
|
||||
baseURL: 'http://localhost:8000', // Twój backend
|
||||
})
|
||||
|
||||
// Request interceptor – dodawanie tokena
|
||||
|
||||
116
frontend/src/utils/utils.spec.js
Normal file
116
frontend/src/utils/utils.spec.js
Normal file
@@ -0,0 +1,116 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest'
|
||||
import utils from './utils.js'
|
||||
|
||||
const { calculateDay } = utils
|
||||
|
||||
describe('calculateDay', () => {
|
||||
let day
|
||||
|
||||
beforeEach(() => {
|
||||
day = {
|
||||
entryTime: [],
|
||||
exitTime: [],
|
||||
isHolidayLeave: false,
|
||||
isSickLeave: false,
|
||||
dayOfWeek: 'Pn', // Monday
|
||||
isPublicHoliday: false,
|
||||
workedHours: 0,
|
||||
overtime: 0,
|
||||
}
|
||||
})
|
||||
|
||||
it('should calculate worked hours and overtime for a standard workday', () => {
|
||||
day.entryTime = ['08:00:00']
|
||||
day.exitTime = ['16:30:00'] // 8.5 hours of work
|
||||
const totalHours = calculateDay(day)
|
||||
|
||||
// 8.5 hours - 0.25 break = 8.25 workedHours
|
||||
// 8.25 workedHours - 8 standard hours = 0.25 overtime
|
||||
// totalHours should be workedHours, so 8.25
|
||||
expect(day.workedHours).toBe(8.25)
|
||||
expect(day.overtime).toBe(0.25)
|
||||
expect(totalHours).toBe(8.25)
|
||||
})
|
||||
|
||||
it('should handle multiple entries and exits', () => {
|
||||
day.entryTime = ['08:00:00', '13:00:00']
|
||||
day.exitTime = ['12:00:00', '16:30:00'] // 4h + 3.5h = 7.5h
|
||||
const totalHours = calculateDay(day)
|
||||
|
||||
// 7.5 hours - 0.25 break = 7.25 workedHours
|
||||
// 7.25 workedHours - 8 standard hours = -0.75 overtime
|
||||
expect(day.workedHours).toBe(7.25)
|
||||
expect(day.overtime).toBe(-0.75)
|
||||
expect(totalHours).toBe(7.25)
|
||||
})
|
||||
|
||||
it('should calculate hours correctly when currently clocked in', () => {
|
||||
// Mock Date for consistent results
|
||||
const now = new Date('2025-10-29T12:00:00')
|
||||
vi.setSystemTime(now)
|
||||
|
||||
day.entryTime = ['09:00:00']
|
||||
day.exitTime = [] // Clocked in
|
||||
const totalHours = calculateDay(day)
|
||||
|
||||
// 9:00 to 12:00 is 3 hours
|
||||
// 3 hours - 0.25 break = 2.75 workedHours
|
||||
expect(day.workedHours).toBe(2.75)
|
||||
expect(day.overtime).toBe(2.75 - 8) // -5.25
|
||||
expect(totalHours).toBe(2.75)
|
||||
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
it('should return 0 for a day with no entries', () => {
|
||||
const totalHours = calculateDay(day)
|
||||
expect(day.workedHours).toBe(0)
|
||||
expect(day.overtime).toBe(-8)
|
||||
expect(totalHours).toBe(0)
|
||||
})
|
||||
|
||||
it('should handle holiday leave', () => {
|
||||
day.isHolidayLeave = true
|
||||
const totalHours = calculateDay(day)
|
||||
expect(day.workedHours).toBe(0)
|
||||
expect(day.overtime).toBe(0) // Overtime is just workedHours on non-work days
|
||||
expect(totalHours).toBe(8) // 0 worked + 8 holiday
|
||||
})
|
||||
|
||||
it('should handle sick leave', () => {
|
||||
day.isSickLeave = true
|
||||
const totalHours = calculateDay(day)
|
||||
expect(day.workedHours).toBe(0)
|
||||
expect(day.overtime).toBe(0)
|
||||
expect(totalHours).toBe(6.4) // 0 worked + 6.4 sick
|
||||
})
|
||||
|
||||
it('should handle sick leave and holiday leave', () => {
|
||||
day.isHolidayLeave = true
|
||||
day.isSickLeave = true
|
||||
const totalHours = calculateDay(day)
|
||||
expect(day.workedHours).toBe(0)
|
||||
expect(day.overtime).toBe(0)
|
||||
expect(totalHours).toBe(14.4) // 0 worked + 8 holiday + 6.4 sick
|
||||
})
|
||||
|
||||
it('should not subtract break time if worked hours are 0', () => {
|
||||
day.entryTime = []
|
||||
day.exitTime = []
|
||||
const totalHours = calculateDay(day)
|
||||
expect(day.workedHours).toBe(0)
|
||||
expect(totalHours).toBe(0)
|
||||
})
|
||||
|
||||
it('should calculate overtime as workedHours on a weekend', () => {
|
||||
day.dayOfWeek = 'So' // Sunday
|
||||
day.entryTime = ['10:00:00']
|
||||
day.exitTime = ['12:00:00'] // 2 hours of work
|
||||
const totalHours = calculateDay(day)
|
||||
|
||||
// 2 hours - 0.25 break = 1.75
|
||||
expect(day.workedHours).toBe(1.75)
|
||||
expect(day.overtime).toBe(1.75) // On non-work days, all worked time is overtime
|
||||
expect(totalHours).toBe(1.75)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user