From c0f3755d5107607d91d31fc09acd78286224e812 Mon Sep 17 00:00:00 2001 From: Daniel Kuschny Date: Fri, 28 Mar 2025 17:20:50 +0100 Subject: [PATCH] test(theme-common): Add tests for getLineNumbersStart (#11017) --- .../__snapshots__/codeBlockUtils.test.ts.snap | 24 ++++ .../utils/__tests__/codeBlockUtils.test.ts | 118 ++++++++++++++++++ 2 files changed, 142 insertions(+) diff --git a/packages/docusaurus-theme-common/src/utils/__tests__/__snapshots__/codeBlockUtils.test.ts.snap b/packages/docusaurus-theme-common/src/utils/__tests__/__snapshots__/codeBlockUtils.test.ts.snap index 7ab1326fdd..ca223444b4 100644 --- a/packages/docusaurus-theme-common/src/utils/__tests__/__snapshots__/codeBlockUtils.test.ts.snap +++ b/packages/docusaurus-theme-common/src/utils/__tests__/__snapshots__/codeBlockUtils.test.ts.snap @@ -1,5 +1,29 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`getLineNumbersStart handles metadata combined with other options set as flag 1`] = `1`; + +exports[`getLineNumbersStart handles metadata combined with other options set with number 1`] = `10`; + +exports[`getLineNumbersStart handles metadata standalone set as flag 1`] = `1`; + +exports[`getLineNumbersStart handles metadata standalone set with number 1`] = `10`; + +exports[`getLineNumbersStart handles prop combined with metastring set to false 1`] = `undefined`; + +exports[`getLineNumbersStart handles prop combined with metastring set to number 1`] = `10`; + +exports[`getLineNumbersStart handles prop combined with metastring set to true 1`] = `1`; + +exports[`getLineNumbersStart handles prop standalone set to false 1`] = `undefined`; + +exports[`getLineNumbersStart handles prop standalone set to number 1`] = `10`; + +exports[`getLineNumbersStart handles prop standalone set to true 1`] = `1`; + +exports[`getLineNumbersStart with nothing set 1`] = `undefined`; + +exports[`getLineNumbersStart with nothing set 2`] = `undefined`; + exports[`parseLines does not parse content with metastring 1`] = ` { "code": "aaaaa diff --git a/packages/docusaurus-theme-common/src/utils/__tests__/codeBlockUtils.test.ts b/packages/docusaurus-theme-common/src/utils/__tests__/codeBlockUtils.test.ts index 47c62a91ca..91d2efe659 100644 --- a/packages/docusaurus-theme-common/src/utils/__tests__/codeBlockUtils.test.ts +++ b/packages/docusaurus-theme-common/src/utils/__tests__/codeBlockUtils.test.ts @@ -6,6 +6,7 @@ */ import { + getLineNumbersStart, type MagicCommentConfig, parseCodeBlockTitle, parseLanguage, @@ -360,3 +361,120 @@ line ).toMatchSnapshot(); }); }); + +describe('getLineNumbersStart', () => { + it('with nothing set', () => { + expect( + getLineNumbersStart({ + showLineNumbers: undefined, + metastring: undefined, + }), + ).toMatchSnapshot(); + expect( + getLineNumbersStart({ + showLineNumbers: undefined, + metastring: '', + }), + ).toMatchSnapshot(); + }); + + describe('handles prop', () => { + describe('combined with metastring', () => { + it('set to true', () => { + expect( + getLineNumbersStart({ + showLineNumbers: true, + metastring: 'showLineNumbers=2', + }), + ).toMatchSnapshot(); + }); + + it('set to false', () => { + expect( + getLineNumbersStart({ + showLineNumbers: false, + metastring: 'showLineNumbers=2', + }), + ).toMatchSnapshot(); + }); + + it('set to number', () => { + expect( + getLineNumbersStart({ + showLineNumbers: 10, + metastring: 'showLineNumbers=2', + }), + ).toMatchSnapshot(); + }); + }); + + describe('standalone', () => { + it('set to true', () => { + expect( + getLineNumbersStart({ + showLineNumbers: true, + metastring: undefined, + }), + ).toMatchSnapshot(); + }); + + it('set to false', () => { + expect( + getLineNumbersStart({ + showLineNumbers: false, + metastring: undefined, + }), + ).toMatchSnapshot(); + }); + + it('set to number', () => { + expect( + getLineNumbersStart({ + showLineNumbers: 10, + metastring: undefined, + }), + ).toMatchSnapshot(); + }); + }); + }); + + describe('handles metadata', () => { + describe('standalone', () => { + it('set as flag', () => { + expect( + getLineNumbersStart({ + showLineNumbers: undefined, + metastring: 'showLineNumbers', + }), + ).toMatchSnapshot(); + }); + it('set with number', () => { + expect( + getLineNumbersStart({ + showLineNumbers: undefined, + metastring: 'showLineNumbers=10', + }), + ).toMatchSnapshot(); + }); + }); + + describe('combined with other options', () => { + it('set as flag', () => { + expect( + getLineNumbersStart({ + showLineNumbers: undefined, + metastring: '{1,2-3} title="file.txt" showLineNumbers noInline', + }), + ).toMatchSnapshot(); + }); + it('set with number', () => { + expect( + getLineNumbersStart({ + showLineNumbers: undefined, + metastring: '{1,2-3} title="file.txt" showLineNumbers=10 noInline', + }), + ).toMatchSnapshot(); + }); + }); + }); +});