test: add MC/DC unit tests for prepareElementsForExport (packages/excalidraw) #10273
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.
Summary
Add 6 unit tests that apply Modified Condition/Decision Coverage (MC/DC) to the function prepareElementsForExport (packages/excalidraw/data/index.ts).
The tests validate internal decisions (D1, D2a, D2b) and verify correct export behavior across main scenarios:
export all (fallback)
export selection with single frame
export selection with multiple elements
What changed
Added test file:
packages/excalidraw/tests/prepareElementsForExport.test.ts
No production code changes.
Tests use Vitest and vi.mock/vi.mocked to isolate dependencies (getSelectedElements, getElementsOverlappingFrame, isFrameLikeElement, cloneJSON, etc.).
Test cases implemented (mapping)
CT1 — A=V, B=V, multi-selection (D1 → D2b true)
CT2 — A=V, B=F (exportSelectionOnly true, but no valid selection)
CT3 — A=F, B=V (exportSelectionOnly false, selection exists — selection ignored)
CT4 — D2a single-frame (A=V, B=V, exportedElements.length === 1 && isFrameLike true)
CT5 — D2a single non-frame (A=V, B=F, exportedElements.length === 1 && isFrameLike false)
CT6 — fallback / export all (A=F, B=F)
MC/DC rationale (short)
Decisions covered:
D1: exportSelectionOnly && isSomeElementSelected(...)
D2a: exportedElements.length === 1 && isFrameLikeElement(...)
D2b: exportedElements.length > 1 (single-condition decision)
For D1 and D2a, MC/DC pairs were constructed so each atomic condition is shown to independently influence the decision outcome. D2b is covered with both true and false cases.
How to run locally
From repository root:
yarn test:app packages/excalidraw/tests/prepareElementsForExport.test.ts
or: npx vitest run packages/excalidraw/tests/prepareElementsForExport.test.ts --coverage
Expected: all tests pass (6 passed). Coverage HTML will be generated under coverage/lcov-report.
Notes about coverage & scope
Scope: tests focus exclusively on the method prepareElementsForExport. That method lives inside a larger module (packages/excalidraw/data/index.ts). As a result, the overall file coverage may remain low if other functions in the same file are not tested.
This is intentional: the assignment objective was to apply MC/DC to a chosen method. The coverage evidence included with this PR demonstrates the method’s lines are covered; the file-level percentage is shown for transparency.
If maintainers request higher file-level coverage, I can add follow-up tests to cover other functions in index.ts.
Files changed / where to review
packages/excalidraw/tests/prepareElementsForExport.test.ts
Comments inside the test file indicate: which condition(s) are exercised per test, the MC/DC pairs demonstrated, and main assertions.
Mocks: getSelectedElements, getElementsOverlappingFrame, isFrameLikeElement, cloneJSON.
Screenshots / artifacts (please attach to PR)
tests-result.png — terminal showing "6 passed"
coverage-after-function.png — HTML coverage view zoomed to prepareElementsForExport (lines showing green coverage)
coverage-after-index.png — file summary for packages/excalidraw/data/index.ts (transparency about file-level percent)
Suggested reviewer notes
Focus review on:
correctness of test scenarios vs decisions (D1, D2a, D2b)
appropriateness of mocks and assertions (exports & exportingFrame)
naming / readability of tests (I can rename IDs to match repo conventions if requested)
Checklist
Add 6 Vitest unit tests covering MC/DC for prepareElementsForExport
Use vi.mock/vi.mocked to isolate dependencies
Local coverage was generated and the method’s lines are covered (see attached HTML screenshot)
(Optional) Add broader tests to increase file-level coverage if requested
Thank you for reviewing — happy to iterate on test names, mocks, or assertions per maintainers’ feedback.