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:
2025-10-29 18:12:46 +01:00
parent 0ff842b845
commit 54de913c16
5 changed files with 1085 additions and 37 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -11,7 +11,8 @@
"build": "vite build",
"preview": "vite preview",
"lint": "eslint . --fix",
"format": "prettier --write src/"
"format": "prettier --write src/",
"test": "vitest"
},
"dependencies": {
"@jamescoyle/vue-icon": "^0.1.2",
@@ -30,8 +31,10 @@
"eslint": "^9.31.0",
"eslint-plugin-vue": "~10.3.0",
"globals": "^16.3.0",
"jsdom": "^27.0.1",
"prettier": "3.6.2",
"vite": "^7.0.6",
"vite-plugin-vue-devtools": "^8.0.0"
"vite-plugin-vue-devtools": "^8.0.0",
"vitest": "^4.0.5"
}
}

View File

@@ -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

View 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)
})
})

View File

@@ -1,16 +1,20 @@
/// <reference types="vitest" />
import { fileURLToPath, URL } from 'node:url'
import VueDevTools from 'vite-plugin-vue-devtools'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
// https://vite.dev/config/
export default defineConfig({
// https://vitejs.dev/config/
export default defineConfig(({ command }) => ({
base: '/odoo/',
plugins: [vue(), vueDevTools()],
plugins: [vue(), ...(command === 'serve' ? [VueDevTools()] : [])],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
},
},
})
test: {
globals: true,
environment: 'jsdom',
},
}))