Skip to content

Commit 156cb9f

Browse files
committed
Add tests
1 parent c2b179d commit 156cb9f

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

MyApp.Client/package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
"preview": "vite preview",
1111
"build-only": "vite build",
1212
"type-check": "vue-tsc --build --force",
13-
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore"
13+
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore",
14+
"test": "vitest",
15+
"test:ui": "vitest --ui",
16+
"test:run": "vitest run"
1417
},
1518
"files": [
1619
"module.d.ts"
@@ -36,11 +39,14 @@
3639
"@types/node": "^24.10.1",
3740
"@types/prismjs": "^1.26.5",
3841
"@vitejs/plugin-vue": "^6.0.2",
42+
"@vitest/ui": "^4.0.15",
3943
"@vue/eslint-config-typescript": "^14.6.0",
44+
"@vue/test-utils": "^2.4.6",
4045
"@vue/tsconfig": "^0.8.1",
4146
"autoprefixer": "^10.4.22",
4247
"eslint": "^9.39.1",
4348
"eslint-plugin-vue": "^10.6.2",
49+
"happy-dom": "^20.0.11",
4450
"markdown-it": "^14.1.0",
4551
"markdown-it-anchor": "^9.2.0",
4652
"markdown-it-container": "^4.0.0",
@@ -56,6 +62,7 @@
5662
"vite-plugin-press": "^1.0.10",
5763
"vite-plugin-vue-layouts-next": "^1.3.0",
5864
"vite-svg-loader": "^5.1.0",
65+
"vitest": "^4.0.15",
5966
"vue-tsc": "^3.1.6"
6067
}
6168
}

MyApp.Client/src/lib/auth.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { describe, it, expect } from 'vitest'
2+
import { createAttrs } from './auth'
3+
4+
describe('auth utilities', () => {
5+
describe('createAttrs', () => {
6+
it('should return empty array for null auth', () => {
7+
const result = createAttrs(null)
8+
expect(result).toEqual([])
9+
})
10+
11+
it('should return empty array for undefined auth', () => {
12+
const result = createAttrs(undefined)
13+
expect(result).toEqual([])
14+
})
15+
16+
it('should return auth attr for authenticated user with no roles or permissions', () => {
17+
const result = createAttrs({})
18+
expect(result).toEqual(['auth'])
19+
})
20+
21+
it('should include roles with role: prefix', () => {
22+
const result = createAttrs({
23+
roles: ['Admin', 'Manager']
24+
})
25+
expect(result).toEqual(['auth', 'role:Admin', 'role:Manager'])
26+
})
27+
28+
it('should include permissions with perm: prefix', () => {
29+
const result = createAttrs({
30+
permissions: ['ReadData', 'WriteData']
31+
})
32+
expect(result).toEqual(['auth', 'perm:ReadData', 'perm:WriteData'])
33+
})
34+
35+
it('should include both roles and permissions', () => {
36+
const result = createAttrs({
37+
roles: ['Admin'],
38+
permissions: ['ReadData', 'WriteData']
39+
})
40+
expect(result).toEqual(['auth', 'role:Admin', 'perm:ReadData', 'perm:WriteData'])
41+
})
42+
43+
it('should handle empty roles and permissions arrays', () => {
44+
const result = createAttrs({
45+
roles: [],
46+
permissions: []
47+
})
48+
expect(result).toEqual(['auth'])
49+
})
50+
})
51+
})

MyApp.Client/vite.config.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { fileURLToPath, URL } from 'node:url'
22

33
import fs from 'fs'
4-
import { env } from 'process'
5-
6-
import { defineConfig } from 'vite'
4+
import { defineConfig } from 'vitest/config'
75
import tailwindcss from '@tailwindcss/vite'
86
import Vue from '@vitejs/plugin-vue'
97
import Press, { matter } from 'vite-plugin-press'
@@ -96,5 +94,9 @@ export default defineConfig({
9694
server: {
9795
host: true, // Listen on all interfaces (both IPv4 and IPv6)
9896
open: false,
99-
}
97+
},
98+
test: {
99+
environment: 'happy-dom',
100+
globals: true,
101+
}
100102
})

0 commit comments

Comments
 (0)