-
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.
Merge pull request #1298 from rainlanguage/2024-02-14-share
share button fix
- Loading branch information
Showing
4 changed files
with
139 additions
and
6 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
packages/ui-components/src/__tests__/handleShareChoices.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,52 @@ | ||
import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
import { handleShareChoices } from '../lib/services/handleShareChoices'; | ||
import type { DotrainOrderGui } from '@rainlanguage/orderbook/js_api'; | ||
|
||
describe('handleShareChoices', () => { | ||
beforeEach(() => { | ||
// Mock clipboard API | ||
Object.assign(navigator, { | ||
clipboard: { | ||
writeText: vi.fn() | ||
} | ||
}); | ||
|
||
// Mock Svelte's page store | ||
vi.mock('$app/stores', () => ({ | ||
page: { | ||
subscribe: vi.fn((fn) => { | ||
fn({ url: new URL('http://example.com') }); | ||
return () => {}; | ||
}) | ||
} | ||
})); | ||
}); | ||
|
||
it('should share the choices with state', async () => { | ||
const mockGui = { | ||
serializeState: vi.fn().mockReturnValue('mockState123') | ||
}; | ||
|
||
await handleShareChoices(mockGui as unknown as DotrainOrderGui); | ||
|
||
expect(navigator.clipboard.writeText).toHaveBeenCalledWith( | ||
'http://example.com/?state=mockState123' | ||
); | ||
}); | ||
|
||
it('should handle null state', async () => { | ||
const mockGui = { | ||
serializeState: vi.fn().mockReturnValue(null) | ||
}; | ||
|
||
await handleShareChoices(mockGui as unknown as DotrainOrderGui); | ||
|
||
expect(navigator.clipboard.writeText).toHaveBeenCalledWith('http://example.com/?state='); | ||
}); | ||
|
||
it('should handle undefined gui', async () => { | ||
await handleShareChoices(undefined as unknown as DotrainOrderGui); | ||
|
||
expect(navigator.clipboard.writeText).toHaveBeenCalledWith('http://example.com/?state='); | ||
}); | ||
}); |
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
12 changes: 12 additions & 0 deletions
12
packages/ui-components/src/lib/services/handleShareChoices.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,12 @@ | ||
import type { DotrainOrderGui } from '@rainlanguage/orderbook/js_api'; | ||
import { page } from '$app/stores'; | ||
import { get } from 'svelte/store'; | ||
|
||
export async function handleShareChoices(gui: DotrainOrderGui) { | ||
// get the current url | ||
const url = get(page).url; | ||
// get the current state | ||
const state = gui?.serializeState(); | ||
url.searchParams.set('state', state || ''); | ||
navigator.clipboard.writeText(url.toString()); | ||
} |
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