Skip to content

Commit f23d783

Browse files
committed
dashboard/settings: add unit tests
Change-Id: I3af704a688986c37b42c4a1107e208c65b99ce30 JIRA-Ref: CMK-27747
1 parent 9162277 commit f23d783

6 files changed

Lines changed: 911 additions & 0 deletions

File tree

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* Copyright (C) 2026 Checkmk GmbH - License: GNU General Public License v2
3+
* This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
4+
* conditions defined in the file COPYING, which is part of this source code package.
5+
*/
6+
import { describe, expect, it, vi } from 'vitest'
7+
8+
import {
9+
generateUniqueId,
10+
isIdInUse,
11+
isValidSnakeCase,
12+
toSnakeCase
13+
} from '@/dashboard/components/Wizard/components/DashboardSettings/utils'
14+
15+
vi.mock('@/dashboard/utils', () => ({
16+
dashboardAPI: {
17+
listDashboardMetadata: vi.fn().mockResolvedValue([
18+
{ name: 'existing_dashboard', owner: 'admin' },
19+
{ name: 'another_dashboard', owner: 'admin' },
20+
{ name: 'guest_dashboard', owner: 'guest' },
21+
{ name: 'guest_dashboard_1', owner: 'guest' }
22+
])
23+
}
24+
}))
25+
26+
describe('toSnakeCase', () => {
27+
it.each([
28+
['camelCase', 'camel_case'],
29+
['PascalCase', 'pascal_case'],
30+
['My New Dashboard', 'my_new_dashboard'],
31+
['some-dashboard--name!', 'some_dashboard_name_'],
32+
['already_snake_case', 'already_snake_case'],
33+
['dashboard', 'dashboard'],
34+
['', '']
35+
])('converts %j to %j', (input, expected) => {
36+
expect(toSnakeCase(input)).toBe(expected)
37+
})
38+
})
39+
40+
describe('isValidSnakeCase', () => {
41+
it.each([
42+
['valid_snake_case', true],
43+
['dashboard', true],
44+
['my_dashboard_123', true],
45+
['_leading', true],
46+
['Invalid', false],
47+
['not-valid', false],
48+
['has spaces', false],
49+
['', false]
50+
])('%j returns %s', (input, expected) => {
51+
expect(isValidSnakeCase(input)).toBe(expected)
52+
})
53+
})
54+
55+
describe('isIdInUse', () => {
56+
it('returns true when the ID is owned by the given owner', async () => {
57+
expect(await isIdInUse('admin', 'existing_dashboard')).toBe(true)
58+
})
59+
60+
it('returns false when the ID is not owned by the given owner', async () => {
61+
expect(await isIdInUse('guest', 'existing_dashboard')).toBe(false)
62+
})
63+
64+
it('returns false when the ID does not exist at all', async () => {
65+
expect(await isIdInUse('admin', 'nonexistent_dashboard')).toBe(false)
66+
})
67+
68+
it('returns false when the ID matches the ignoreId', async () => {
69+
expect(await isIdInUse('admin', 'existing_dashboard', 'existing_dashboard')).toBe(false)
70+
})
71+
72+
it('returns true when the ID is in use and ignoreId is different', async () => {
73+
expect(await isIdInUse('admin', 'existing_dashboard', 'other_id')).toBe(true)
74+
})
75+
76+
it('returns false for an owner with no dashboards', async () => {
77+
expect(await isIdInUse('nobody', 'existing_dashboard')).toBe(false)
78+
})
79+
})
80+
81+
describe('generateUniqueId', () => {
82+
it('returns the baseId when it is not in use', async () => {
83+
expect(await generateUniqueId('admin', 'new_dashboard')).toBe('new_dashboard')
84+
})
85+
86+
it('appends _1 when the baseId is already in use', async () => {
87+
expect(await generateUniqueId('admin', 'existing_dashboard')).toBe('existing_dashboard_1')
88+
})
89+
90+
it('appends _2 when both baseId and baseId_1 are in use', async () => {
91+
expect(await generateUniqueId('guest', 'guest_dashboard')).toBe('guest_dashboard_2')
92+
})
93+
94+
it('returns the baseId when it matches the ignoreId', async () => {
95+
expect(await generateUniqueId('admin', 'existing_dashboard', 'existing_dashboard')).toBe(
96+
'existing_dashboard'
97+
)
98+
})
99+
100+
it('returns the baseId for a different owner even if it exists', async () => {
101+
expect(await generateUniqueId('nobody', 'existing_dashboard')).toBe('existing_dashboard')
102+
})
103+
})

0 commit comments

Comments
 (0)