Skip to content

Commit

Permalink
get util file
Browse files Browse the repository at this point in the history
  • Loading branch information
hardingjam committed Feb 19, 2025
1 parent 79487fc commit a3a36ef
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 12 deletions.
88 changes: 88 additions & 0 deletions packages/webapp/src/__tests__/handleGuiInitialization.test.ts
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 packages/webapp/src/lib/services/handleGuiInitialization.ts
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.' };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { handleUpdateGuiState } from '$lib/services/handleUpdateGuiState';
import { DotrainOrderGui } from '@rainlanguage/orderbook/js_api';
import { onMount } from 'svelte';
import { handleGuiInitialization } from '$lib/services/handleGuiInitialization';
const { settings } = $page.data.stores;
const { dotrain, deployment, strategyDetail } = $page.data;
Expand All @@ -22,18 +23,13 @@
}
onMount(async () => {
try {
if (stateFromUrl) {
return (gui = await DotrainOrderGui.deserializeState(
dotrain,
$page.url.searchParams.get('state') || ''
));
} else {
gui = await DotrainOrderGui.chooseDeployment(dotrain, deployment.key);
}
} catch (err) {
getGuiError = 'Could not get deployment form.';
}
const { gui: initializedGui, error } = await handleGuiInitialization(
dotrain,
deployment.key,
stateFromUrl
);
gui = initializedGui;
getGuiError = error;
});
</script>

Expand Down

0 comments on commit a3a36ef

Please sign in to comment.