From fdc3aa054e8b82259308c1422c2fd6b95380333b Mon Sep 17 00:00:00 2001 From: Aly Thobani Date: Mon, 11 May 2026 17:28:48 -0700 Subject: [PATCH] refactor(tests): deduplicate code fence motion specs --- tests/jumpToCodeFence.test.ts | 123 +++++++++++++--------------------- 1 file changed, 46 insertions(+), 77 deletions(-) diff --git a/tests/jumpToCodeFence.test.ts b/tests/jumpToCodeFence.test.ts index ed228bc..cc481d5 100644 --- a/tests/jumpToCodeFence.test.ts +++ b/tests/jumpToCodeFence.test.ts @@ -5,113 +5,82 @@ import { } from "../motions/jumpToCodeFence"; import { createFakeCodeMirrorEditor } from "./createFakeCodeMirrorEditor"; +const CODE_FENCE_CONTENT_LINES = [ + "intro", + "```ts", + "const answer = 42;", + "```", + "outro", +]; + describe("jumpToNextCodeFence", () => { test("jumps to the next opening code fence", () => { - const cm = createFakeCodeMirrorEditor([ - "intro", - "```ts", - "const answer = 42;", - "```", - "outro", - ]); - - const nextCodeFence = jumpToNextCodeFence( - cm as any, + expectNextCodeFencePosition( { line: 0, ch: 0 }, - { repeat: 1 } + { line: 1, ch: 0 } ); - - expect(nextCodeFence).toEqual({ line: 1, ch: 0 }); }); test("jumps to the next closing code fence", () => { - const cm = createFakeCodeMirrorEditor([ - "intro", - "```ts", - "const answer = 42;", - "```", - "outro", - ]); - - const nextCodeFence = jumpToNextCodeFence( - cm as any, + expectNextCodeFencePosition( { line: 2, ch: 0 }, - { repeat: 1 } + { line: 3, ch: 0 } ); - - expect(nextCodeFence).toEqual({ line: 3, ch: 0 }); }); test("wraps to the first code fence after the last one", () => { - const cm = createFakeCodeMirrorEditor([ - "intro", - "```ts", - "const answer = 42;", - "```", - "outro", - ]); - - const nextCodeFence = jumpToNextCodeFence( - cm as any, + expectNextCodeFencePosition( { line: 4, ch: 0 }, - { repeat: 1 } + { line: 1, ch: 0 } ); - - expect(nextCodeFence).toEqual({ line: 1, ch: 0 }); }); test("ignores backticks that are not code fence lines", () => { - const cm = createFakeCodeMirrorEditor([ - "here are inline backticks ``` not a fence", - "```ts", - "const answer = 42;", - "```", - ]); - - const nextCodeFence = jumpToNextCodeFence( - cm as any, + expectNextCodeFencePosition( { line: 0, ch: 0 }, - { repeat: 1 } + { line: 1, ch: 0 }, + [ + "here are inline backticks ``` not a fence", + "```ts", + "const answer = 42;", + "```", + ] ); - - expect(nextCodeFence).toEqual({ line: 1, ch: 0 }); }); }); describe("jumpToPreviousCodeFence", () => { test("jumps to the previous closing code fence", () => { - const cm = createFakeCodeMirrorEditor([ - "intro", - "```ts", - "const answer = 42;", - "```", - "outro", - ]); - - const previousCodeFence = jumpToPreviousCodeFence( - cm as any, + expectPreviousCodeFencePosition( { line: 4, ch: 0 }, - { repeat: 1 } + { line: 3, ch: 0 } ); - - expect(previousCodeFence).toEqual({ line: 3, ch: 0 }); }); test("jumps to the previous opening code fence", () => { - const cm = createFakeCodeMirrorEditor([ - "intro", - "```ts", - "const answer = 42;", - "```", - "outro", - ]); - - const previousCodeFence = jumpToPreviousCodeFence( - cm as any, + expectPreviousCodeFencePosition( { line: 2, ch: 0 }, - { repeat: 1 } + { line: 1, ch: 0 } ); - - expect(previousCodeFence).toEqual({ line: 1, ch: 0 }); }); }); + +function expectNextCodeFencePosition( + cursorPosition: { line: number; ch: number }, + expectedPosition: { line: number; ch: number }, + contentLines: string[] = CODE_FENCE_CONTENT_LINES +): void { + const cm = createFakeCodeMirrorEditor(contentLines); + const nextCodeFence = jumpToNextCodeFence(cm as any, cursorPosition, { repeat: 1 }); + expect(nextCodeFence).toEqual(expectedPosition); +} + +function expectPreviousCodeFencePosition( + cursorPosition: { line: number; ch: number }, + expectedPosition: { line: number; ch: number }, + contentLines: string[] = CODE_FENCE_CONTENT_LINES +): void { + const cm = createFakeCodeMirrorEditor(contentLines); + const previousCodeFence = jumpToPreviousCodeFence(cm as any, cursorPosition, { repeat: 1 }); + expect(previousCodeFence).toEqual(expectedPosition); +}