-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79487fc
commit a3a36ef
Showing
3 changed files
with
123 additions
and
12 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
packages/webapp/src/__tests__/handleGuiInitialization.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { describe, it, expect, beforeEach, vi } from 'vitest'; | ||
import { handleGuiInitialization } from '../lib/services/handleGuiInitialization'; | ||
|
||
const mockDotrainOrderGui = await vi.hoisted(() => ({ | ||
deserializeState: vi.fn(), | ||
chooseDeployment: vi.fn() | ||
})); | ||
|
||
vi.mock('@rainlanguage/orderbook/js_api', () => ({ | ||
DotrainOrderGui: mockDotrainOrderGui | ||
})); | ||
|
||
describe('handleGuiInitialization', () => { | ||
const mockDotrain = 'mockDotrain'; | ||
const mockDeploymentKey = 'mockDeploymentKey'; | ||
const mockGui = { id: 'mockGui' }; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('should initialize GUI with state from URL when valid', async () => { | ||
mockDotrainOrderGui.deserializeState.mockResolvedValue(mockGui); | ||
|
||
const result = await handleGuiInitialization( | ||
mockDotrain, | ||
mockDeploymentKey, | ||
'validStateUrl' | ||
); | ||
|
||
expect(result).toEqual({ gui: mockGui, error: null }); | ||
expect(mockDotrainOrderGui.deserializeState).toHaveBeenCalledWith( | ||
mockDotrain, | ||
'validStateUrl' | ||
); | ||
expect(mockDotrainOrderGui.chooseDeployment).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should fall back to chooseDeployment when deserializeState fails', async () => { | ||
mockDotrainOrderGui.deserializeState.mockRejectedValue(new Error('deserialize failed')); | ||
mockDotrainOrderGui.chooseDeployment.mockResolvedValue(mockGui); | ||
|
||
const result = await handleGuiInitialization( | ||
mockDotrain, | ||
mockDeploymentKey, | ||
'invalidStateUrl' | ||
); | ||
|
||
expect(result).toEqual({ gui: mockGui, error: null }); | ||
expect(mockDotrainOrderGui.deserializeState).toHaveBeenCalled(); | ||
expect(mockDotrainOrderGui.chooseDeployment).toHaveBeenCalledWith( | ||
mockDotrain, | ||
mockDeploymentKey | ||
); | ||
}); | ||
|
||
it('should use chooseDeployment when no state URL is provided', async () => { | ||
mockDotrainOrderGui.chooseDeployment.mockResolvedValue(mockGui); | ||
|
||
const result = await handleGuiInitialization( | ||
mockDotrain, | ||
mockDeploymentKey, | ||
null | ||
); | ||
|
||
expect(result).toEqual({ gui: mockGui, error: null }); | ||
expect(mockDotrainOrderGui.deserializeState).not.toHaveBeenCalled(); | ||
expect(mockDotrainOrderGui.chooseDeployment).toHaveBeenCalledWith( | ||
mockDotrain, | ||
mockDeploymentKey | ||
); | ||
}); | ||
|
||
it('should handle errors and return error message', async () => { | ||
mockDotrainOrderGui.chooseDeployment.mockRejectedValue(new Error('deployment failed')); | ||
|
||
const result = await handleGuiInitialization( | ||
mockDotrain, | ||
mockDeploymentKey, | ||
null | ||
); | ||
|
||
expect(result).toEqual({ | ||
gui: null, | ||
error: 'Could not get deployment form.' | ||
}); | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
packages/webapp/src/lib/services/handleGuiInitialization.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { DotrainOrderGui } from '@rainlanguage/orderbook/js_api'; | ||
|
||
export async function handleGuiInitialization( | ||
dotrain: string, | ||
deploymentKey: string, | ||
stateFromUrl: string | null | ||
): Promise<{ gui: DotrainOrderGui | null; error: string | null }> { | ||
try { | ||
let gui: DotrainOrderGui | null = null; | ||
|
||
if (stateFromUrl) { | ||
try { | ||
gui = await DotrainOrderGui.deserializeState( | ||
dotrain, | ||
stateFromUrl | ||
); | ||
} catch (deserializeErr) { | ||
gui = await DotrainOrderGui.chooseDeployment(dotrain, deploymentKey); | ||
} | ||
} else { | ||
gui = await DotrainOrderGui.chooseDeployment(dotrain, deploymentKey); | ||
} | ||
return { gui, error: null }; | ||
} catch (err) { | ||
return { gui: null, error: 'Could not get deployment form.' }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters