Repository navigation
Add focused confirmPaste coverage for clipboard validation and modal handoff #1473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
marcuscastelo
merged 4 commits into
rc/v0.16.0
from
copilot/add-tests-confirm-paste-use-case
Mar 25, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
src/modules/clipboard/application/usecases/tests/clipboardUseCases.test.ts
This file contains hidden or 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,138 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| vi.mock('~/modules/clipboard/ui/PasteConfirmModal', () => ({ | ||
| openPasteConfirmModal: vi.fn(), | ||
| })) | ||
|
|
||
| import { createClipboardStore } from '~/modules/clipboard/application/store/clipboardStore' | ||
| import { createClipboardUseCases } from '~/modules/clipboard/application/usecases/clipboardUseCases' | ||
| import { openPasteConfirmModal } from '~/modules/clipboard/ui/PasteConfirmModal' | ||
| import { createItem, itemSchema } from '~/modules/diet/item/schema/itemSchema' | ||
| import { createMacroNutrients } from '~/modules/diet/macro-nutrients/domain/macroNutrients' | ||
| import { createNewMeal, promoteMeal } from '~/modules/diet/meal/domain/meal' | ||
| import { parseWithStack } from '~/shared/utils/parseWithStack' | ||
|
|
||
| describe('clipboardUseCases.confirmPaste', () => { | ||
| const showError = vi.fn() | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| function createValidItemPayload() { | ||
| return createItem({ | ||
| id: 1, | ||
| name: 'Banana', | ||
| quantity: 100, | ||
| reference: { | ||
| type: 'food', | ||
| id: 1, | ||
| macros: createMacroNutrients({ | ||
| proteinInGrams: 1.2, | ||
| carbsInGrams: 22.8, | ||
| fatInGrams: 0.3, | ||
| }), | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| it('opens the paste confirmation modal with the parsed payload', () => { | ||
| const clipboardStore = createClipboardStore() | ||
| const payload = createValidItemPayload() | ||
| const parsedPayload = parseWithStack(itemSchema, payload) | ||
| const onPasteConfirmed = vi.fn() | ||
| const useCases = createClipboardUseCases({ | ||
| clipboardStore, | ||
| showError, | ||
| }) | ||
|
|
||
| clipboardStore.copy(payload) | ||
|
|
||
| useCases.confirmPaste(itemSchema, onPasteConfirmed) | ||
|
|
||
| expect(openPasteConfirmModal).toHaveBeenCalledTimes(1) | ||
| expect(openPasteConfirmModal).toHaveBeenCalledWith( | ||
| parsedPayload, | ||
| onPasteConfirmed, | ||
| ) | ||
| expect(showError).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('passes the confirmed payload to the paste handler on valid clipboard data', () => { | ||
| const clipboardStore = createClipboardStore() | ||
| const payload = createValidItemPayload() | ||
| const onPasteConfirmed = vi.fn() | ||
| const useCases = createClipboardUseCases({ | ||
| clipboardStore, | ||
| showError, | ||
| }) | ||
|
|
||
| clipboardStore.copy(payload) | ||
|
|
||
| useCases.confirmPaste(itemSchema, onPasteConfirmed) | ||
|
|
||
| const modalCall = vi.mocked(openPasteConfirmModal).mock.calls[0] | ||
|
|
||
| expect(modalCall).toBeDefined() | ||
| if (!modalCall) { | ||
| throw new Error('Expected openPasteConfirmModal to be called') | ||
| } | ||
|
|
||
| const [modalPayload, modalHandler] = modalCall | ||
|
|
||
| modalHandler(modalPayload) | ||
|
|
||
| expect(onPasteConfirmed).toHaveBeenCalledTimes(1) | ||
| expect(onPasteConfirmed).toHaveBeenCalledWith(modalPayload) | ||
| }) | ||
|
|
||
| it('shows an error and does not open the modal when the clipboard is empty', () => { | ||
| const clipboardStore = createClipboardStore() | ||
| const onPasteConfirmed = vi.fn() | ||
| const useCases = createClipboardUseCases({ | ||
| clipboardStore, | ||
| showError, | ||
| }) | ||
|
|
||
| useCases.confirmPaste(itemSchema, onPasteConfirmed) | ||
|
|
||
| expect(openPasteConfirmModal).not.toHaveBeenCalled() | ||
| expect(onPasteConfirmed).not.toHaveBeenCalled() | ||
| expect(showError).toHaveBeenCalledTimes(1) | ||
| expect(showError).toHaveBeenCalledWith( | ||
| 'A área de transferência está vazia ou o conteúdo é inválido.', | ||
| ) | ||
| }) | ||
|
|
||
| it('shows validation errors and does not open the modal when clipboard data is incompatible', () => { | ||
| const clipboardStore = createClipboardStore() | ||
| const onPasteConfirmed = vi.fn() | ||
| const useCases = createClipboardUseCases({ | ||
| clipboardStore, | ||
| showError, | ||
| }) | ||
|
|
||
| clipboardStore.copy( | ||
| promoteMeal( | ||
| createNewMeal({ name: 'Meal clipboard payload', items: [] }), | ||
| { | ||
| id: 999, | ||
| }, | ||
| ), | ||
| ) | ||
|
|
||
| useCases.confirmPaste(itemSchema, onPasteConfirmed) | ||
|
|
||
| expect(openPasteConfirmModal).not.toHaveBeenCalled() | ||
| expect(onPasteConfirmed).not.toHaveBeenCalled() | ||
| expect(showError).toHaveBeenCalledTimes(2) | ||
| expect(showError).toHaveBeenNthCalledWith( | ||
| 1, | ||
| 'O conteúdo da área de transferência não é compatível.', | ||
| ) | ||
| expect(showError).toHaveBeenNthCalledWith( | ||
| 2, | ||
| 'A área de transferência está vazia ou o conteúdo é inválido.', | ||
| ) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.