-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathclusterSelection.test.ts
More file actions
122 lines (109 loc) · 3.27 KB
/
Copy pathclusterSelection.test.ts
File metadata and controls
122 lines (109 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { describe, expect, it } from 'vitest'
import { replaceKubernetesClusterId } from '../../routes/kubernetesRoutesConsts'
import {
getDefaultKubernetesClusterId,
isKubernetesClusterMissing,
withCurrentCluster,
} from './clusterSelection'
describe('replaceKubernetesClusterId', () => {
it('swaps only the kubernetes cluster path segment', () => {
expect(
replaceKubernetesClusterId(
'/kubernetes/cluster-a/workloads/deployments',
'cluster-a',
'cluster-b'
)
).toBe('/kubernetes/cluster-b/workloads/deployments')
})
it('does not use a raw substring replace', () => {
expect(
replaceKubernetesClusterId(
'/kubernetes/cluster-a/workloads/cluster-a',
'cluster-a',
'cluster-b'
)
).toBe('/kubernetes/cluster-b/workloads/cluster-a')
})
it('leaves unrelated paths unchanged', () => {
expect(
replaceKubernetesClusterId(
'/cd/clusters/cluster-a',
'cluster-a',
'cluster-b'
)
).toBe('/cd/clusters/cluster-a')
})
})
describe('withCurrentCluster', () => {
it('prepends the current cluster when it is missing from the page', () => {
expect(
withCurrentCluster([{ id: 'a' }, { id: 'b' }], { id: 'current' })
).toEqual([{ id: 'current' }, { id: 'a' }, { id: 'b' }])
})
it('does not duplicate the current cluster', () => {
expect(withCurrentCluster([{ id: 'a' }, { id: 'b' }], { id: 'a' })).toEqual(
[{ id: 'a' }, { id: 'b' }]
)
})
})
describe('getDefaultKubernetesClusterId', () => {
const clusters = [
{ id: 'worker', self: false },
{ id: 'mgmt', self: true },
]
it('prefers the last selected cluster when it still exists', () => {
expect(getDefaultKubernetesClusterId(clusters, 'worker')).toBe('worker')
})
it('falls back to the management cluster', () => {
expect(getDefaultKubernetesClusterId(clusters, 'gone')).toBe('mgmt')
})
it('returns undefined when there are no clusters', () => {
expect(getDefaultKubernetesClusterId([], 'worker')).toBeUndefined()
})
})
describe('isKubernetesClusterMissing', () => {
it('does not treat a cluster as missing while the query is in flight', () => {
expect(
isKubernetesClusterMissing({
clusterId: 'b',
loading: true,
hasData: true,
currentClusterId: 'a',
clusterIds: ['a'],
})
).toBe(false)
})
it('does not redirect when the requested cluster is in the page even if cluster(id:) is stale', () => {
expect(
isKubernetesClusterMissing({
clusterId: 'b',
loading: false,
hasData: true,
currentClusterId: 'a',
clusterIds: ['a', 'b'],
})
).toBe(false)
})
it('does not redirect when the requested cluster is only on cluster(id:)', () => {
expect(
isKubernetesClusterMissing({
clusterId: 'b',
loading: false,
hasData: true,
currentClusterId: 'b',
clusterIds: ['a'],
})
).toBe(false)
})
it('flags a settled query whose cluster is neither listed nor returned by id', () => {
expect(
isKubernetesClusterMissing({
clusterId: 'missing',
loading: false,
hasData: true,
currentClusterId: undefined,
clusterIds: ['a', 'mgmt'],
})
).toBe(true)
})
})