Skip to content

Commit 46d23bd

Browse files
Adam GoughAdam Gough
authored andcommitted
added tests
1 parent a089394 commit 46d23bd

5 files changed

Lines changed: 602 additions & 12 deletions

File tree

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
/**
2+
* Deployment Controls Change Detection Logic Tests
3+
*
4+
* This file tests the core logic of how DeploymentControls handles change detection,
5+
* specifically focusing on the needsRedeployment prop handling and state management.
6+
*/
7+
8+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
9+
10+
// Mock the workflow registry store
11+
const mockDeploymentStatus = {
12+
isDeployed: false,
13+
needsRedeployment: false,
14+
}
15+
16+
const mockWorkflowRegistry = {
17+
getState: vi.fn(() => ({
18+
getWorkflowDeploymentStatus: vi.fn((workflowId) => mockDeploymentStatus),
19+
})),
20+
}
21+
22+
vi.mock('@/stores/workflows/registry/store', () => ({
23+
useWorkflowRegistry: vi.fn((selector) => {
24+
if (typeof selector === 'function') {
25+
return selector(mockWorkflowRegistry.getState())
26+
}
27+
return mockWorkflowRegistry.getState()
28+
}),
29+
}))
30+
31+
describe('DeploymentControls Change Detection Logic', () => {
32+
beforeEach(() => {
33+
vi.clearAllMocks()
34+
mockDeploymentStatus.isDeployed = false
35+
mockDeploymentStatus.needsRedeployment = false
36+
})
37+
38+
afterEach(() => {
39+
vi.resetAllMocks()
40+
})
41+
42+
describe('needsRedeployment Priority Logic', () => {
43+
it('should prioritize parent needsRedeployment over workflow registry', () => {
44+
// Simulate the logic from DeploymentControls component
45+
const parentNeedsRedeployment = true
46+
const workflowRegistryNeedsRedeployment = false
47+
48+
// The component logic: Trust the parent's change detection
49+
const workflowNeedsRedeployment = parentNeedsRedeployment
50+
51+
expect(workflowNeedsRedeployment).toBe(true)
52+
expect(workflowNeedsRedeployment).not.toBe(workflowRegistryNeedsRedeployment)
53+
})
54+
55+
it('should handle false needsRedeployment correctly', () => {
56+
const parentNeedsRedeployment = false
57+
const workflowNeedsRedeployment = parentNeedsRedeployment
58+
59+
expect(workflowNeedsRedeployment).toBe(false)
60+
})
61+
62+
it('should maintain consistency with parent state changes', () => {
63+
// Simulate state changes
64+
let parentNeedsRedeployment = false
65+
let workflowNeedsRedeployment = parentNeedsRedeployment
66+
67+
expect(workflowNeedsRedeployment).toBe(false)
68+
69+
// Parent detects changes
70+
parentNeedsRedeployment = true
71+
workflowNeedsRedeployment = parentNeedsRedeployment
72+
73+
expect(workflowNeedsRedeployment).toBe(true)
74+
75+
// Parent clears changes (after redeployment)
76+
parentNeedsRedeployment = false
77+
workflowNeedsRedeployment = parentNeedsRedeployment
78+
79+
expect(workflowNeedsRedeployment).toBe(false)
80+
})
81+
})
82+
83+
describe('Deployment Status Integration', () => {
84+
it('should handle deployment status correctly', () => {
85+
// Mock deployment status
86+
mockDeploymentStatus.isDeployed = true
87+
mockDeploymentStatus.needsRedeployment = false
88+
89+
const deploymentStatus = mockWorkflowRegistry
90+
.getState()
91+
.getWorkflowDeploymentStatus('test-id')
92+
93+
expect(deploymentStatus.isDeployed).toBe(true)
94+
expect(deploymentStatus.needsRedeployment).toBe(false)
95+
})
96+
97+
it('should handle missing deployment status', () => {
98+
// Create a separate mock for this test case
99+
const tempMockRegistry = {
100+
getState: vi.fn(() => ({
101+
getWorkflowDeploymentStatus: vi.fn(() => null),
102+
})),
103+
}
104+
105+
// Temporarily replace the mock
106+
const originalMock = mockWorkflowRegistry.getState
107+
mockWorkflowRegistry.getState = tempMockRegistry.getState as any
108+
109+
const deploymentStatus = mockWorkflowRegistry
110+
.getState()
111+
.getWorkflowDeploymentStatus('test-id')
112+
113+
expect(deploymentStatus).toBe(null)
114+
115+
// Restore original mock
116+
mockWorkflowRegistry.getState = originalMock
117+
})
118+
119+
it('should handle undefined deployment status properties', () => {
120+
mockWorkflowRegistry.getState = vi.fn(() => ({
121+
getWorkflowDeploymentStatus: vi.fn(() => ({})),
122+
})) as any
123+
124+
const deploymentStatus = mockWorkflowRegistry
125+
.getState()
126+
.getWorkflowDeploymentStatus('test-id')
127+
128+
// Should handle undefined properties gracefully
129+
const isDeployed = deploymentStatus?.isDeployed || false
130+
expect(isDeployed).toBe(false)
131+
})
132+
})
133+
134+
describe('Change Detection Scenarios', () => {
135+
it('should handle the redeployment cycle correctly', () => {
136+
// Scenario 1: Initial state - deployed, no changes
137+
mockDeploymentStatus.isDeployed = true
138+
let parentNeedsRedeployment = false
139+
let shouldShowIndicator = mockDeploymentStatus.isDeployed && parentNeedsRedeployment
140+
141+
expect(shouldShowIndicator).toBe(false)
142+
143+
// Scenario 2: User makes changes
144+
parentNeedsRedeployment = true
145+
shouldShowIndicator = mockDeploymentStatus.isDeployed && parentNeedsRedeployment
146+
147+
expect(shouldShowIndicator).toBe(true)
148+
149+
// Scenario 3: User redeploys
150+
parentNeedsRedeployment = false // Reset after redeployment
151+
shouldShowIndicator = mockDeploymentStatus.isDeployed && parentNeedsRedeployment
152+
153+
expect(shouldShowIndicator).toBe(false)
154+
})
155+
156+
it('should not show indicator when workflow is not deployed', () => {
157+
mockDeploymentStatus.isDeployed = false
158+
const parentNeedsRedeployment = true
159+
const shouldShowIndicator = mockDeploymentStatus.isDeployed && parentNeedsRedeployment
160+
161+
expect(shouldShowIndicator).toBe(false)
162+
})
163+
164+
it('should show correct tooltip messages based on state', () => {
165+
const getTooltipMessage = (isDeployed: boolean, needsRedeployment: boolean) => {
166+
if (isDeployed && needsRedeployment) {
167+
return 'Workflow changes detected'
168+
}
169+
if (isDeployed) {
170+
return 'Deployment Settings'
171+
}
172+
return 'Deploy as API'
173+
}
174+
175+
// Not deployed
176+
expect(getTooltipMessage(false, false)).toBe('Deploy as API')
177+
expect(getTooltipMessage(false, true)).toBe('Deploy as API')
178+
179+
// Deployed, no changes
180+
expect(getTooltipMessage(true, false)).toBe('Deployment Settings')
181+
182+
// Deployed, changes detected
183+
expect(getTooltipMessage(true, true)).toBe('Workflow changes detected')
184+
})
185+
})
186+
187+
describe('Error Handling', () => {
188+
it('should handle null activeWorkflowId gracefully', () => {
189+
const deploymentStatus = mockWorkflowRegistry.getState().getWorkflowDeploymentStatus(null)
190+
191+
// Should return the mocked result without throwing
192+
expect(deploymentStatus).toBeDefined()
193+
})
194+
})
195+
196+
describe('Props Integration', () => {
197+
it('should correctly pass needsRedeployment to child components', () => {
198+
const parentNeedsRedeployment = true
199+
const propsToModal = {
200+
needsRedeployment: parentNeedsRedeployment,
201+
workflowId: 'test-id',
202+
}
203+
204+
expect(propsToModal.needsRedeployment).toBe(true)
205+
})
206+
207+
it('should maintain prop consistency across re-renders', () => {
208+
let needsRedeployment = false
209+
210+
// Initial render
211+
let componentProps = { needsRedeployment }
212+
expect(componentProps.needsRedeployment).toBe(false)
213+
214+
// State change
215+
needsRedeployment = true
216+
componentProps = { needsRedeployment }
217+
expect(componentProps.needsRedeployment).toBe(true)
218+
219+
// State change back
220+
needsRedeployment = false
221+
componentProps = { needsRedeployment }
222+
expect(componentProps.needsRedeployment).toBe(false)
223+
})
224+
})
225+
})

0 commit comments

Comments
 (0)