Skip to content

Commit dfcae0c

Browse files
committed
refactor: Create getDuringSnapshot function
1 parent f3c8cd6 commit dfcae0c

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

packages/test-case-component/src/generateHtml.ts

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createHighlighter } from "./createHighlighter";
22
import type { BundledLanguage } from "shiki";
33
import type { StepNameType, DataFixture } from "./types";
4+
import type { ExtendedTestCaseSnapshot } from "./types";
45

56
import { renderClipboard, renderError } from "./renderHtml";
67
import { getDecorations } from "./helpers/decorations";
@@ -27,23 +28,7 @@ function createHtmlGenerator(data: DataFixture) {
2728
const raw = data;
2829
const testCaseStates = {
2930
before: data.initialState,
30-
during: {
31-
...(
32-
/**
33-
* Spread the document state with more lines (finalState vs initialState),
34-
* so Shiki decorations stay in bounds and don't go out of range.
35-
*/
36-
data.finalState &&
37-
(data.finalState.documentContents?.split("\n").length > data.initialState.documentContents?.split("\n").length)
38-
? data.finalState
39-
: data.initialState
40-
),
41-
...data.ide,
42-
finalStateMarkHelpers: {
43-
thatMark: data?.finalState?.thatMark,
44-
sourceMark: data?.finalState?.sourceMark
45-
}
46-
},
31+
during: getDuringSnapshot(data),
4732
after: data.finalState
4833
};
4934

@@ -122,3 +107,29 @@ function createHtmlGenerator(data: DataFixture) {
122107

123108
return { generate, generateAll };
124109
}
110+
111+
/**
112+
* Selects the appropriate snapshot for the "during" step of a test case.
113+
* If the final state has more lines than the initial state, it uses the final state as the base;
114+
* otherwise, it uses the initial state. It then merges in IDE state and final mark helpers.
115+
*
116+
* @param {DataFixture} data - The test case data containing initialState, finalState, and ide info.
117+
* @returns {ExtendedTestCaseSnapshot} The snapshot to use for the "during" step.
118+
*/
119+
function getDuringSnapshot(data: DataFixture): ExtendedTestCaseSnapshot {
120+
// Spread the document state with more lines (finalState vs initialState),
121+
// so Shiki decorations stay in bounds and don't go out of range.
122+
const base =
123+
data.finalState &&
124+
(data.finalState.documentContents?.split("\n").length > data.initialState.documentContents?.split("\n").length)
125+
? data.finalState
126+
: data.initialState;
127+
return {
128+
...base,
129+
...data.ide,
130+
finalStateMarkHelpers: {
131+
thatMark: data?.finalState?.thatMark,
132+
sourceMark: data?.finalState?.sourceMark
133+
}
134+
};
135+
}

0 commit comments

Comments
 (0)