From 6a14ebec1727afbaf915b5b9c18c7e39a060a2d0 Mon Sep 17 00:00:00 2001 From: Daniel Kuznetsov Date: Mon, 2 Sep 2024 14:46:36 +0300 Subject: [PATCH] fix: tests --- .../liquidInFrontMatter.test.ts.snap | 10 ++++++++++ .../services/liquidInFrontMatter.test.ts | 13 +++++++++++++ .../services/metadataAuthors.test.ts | 19 +++++++++++++++---- .../services/metadataContributors.test.ts | 11 +++++++++-- .../metadata/substitutionsWithEmptyString.md | 7 +++++++ 5 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 tests/mocks/fileContent/metadata/substitutionsWithEmptyString.md diff --git a/tests/integrations/services/__snapshots__/liquidInFrontMatter.test.ts.snap b/tests/integrations/services/__snapshots__/liquidInFrontMatter.test.ts.snap index a2d6f86b..1c678a5c 100644 --- a/tests/integrations/services/__snapshots__/liquidInFrontMatter.test.ts.snap +++ b/tests/integrations/services/__snapshots__/liquidInFrontMatter.test.ts.snap @@ -45,3 +45,13 @@ prop7: '{{this}} one is a multiline {{property}}.' Blah. " `; + +exports[`Front matter (metadata) transformations emit valid metadata when a variable is substituted with an ampty string 1`] = ` +"--- +prop1: '' +prop2: '' +prop3: '' +prop4: '' +--- +" +`; diff --git a/tests/integrations/services/liquidInFrontMatter.test.ts b/tests/integrations/services/liquidInFrontMatter.test.ts index e6472891..f88d6522 100644 --- a/tests/integrations/services/liquidInFrontMatter.test.ts +++ b/tests/integrations/services/liquidInFrontMatter.test.ts @@ -1,9 +1,11 @@ +import liquid from '@diplodoc/transform/lib/liquid'; import {readFile} from 'fs/promises'; import {parseExistingMetadata} from 'services/metadata/parse'; import {emplaceMetadata} from 'services/metadata/utils'; const propValuesMockPath = 'mocks/fileContent/metadata/substitutionsInMetadataPropertyValues.md'; const propKeysMockPath = 'mocks/fileContent/metadata/substitutionsInMetadataPropertyKeys.md'; +const emptyStringMockPath = 'mocks/fileContent/metadata/substitutionsWithEmptyString.md'; describe('Front matter (metadata) transformations', () => { it('do not break when a property value contains Liquid-style variable substitutions', async () => { @@ -25,4 +27,15 @@ describe('Front matter (metadata) transformations', () => { expect(metadata).toMatchSnapshot(); expect(processedContent).toMatchSnapshot(); }); + + it('emit valid metadata when a variable is substituted with an ampty string', async () => { + const fileContent = await readFile(emptyStringMockPath, {encoding: 'utf-8'}); + + const {metadata, metadataStrippedContent} = parseExistingMetadata(fileContent); + const processedContent = emplaceMetadata(metadataStrippedContent, metadata); + + const liquidProcessedInput = liquid(processedContent, {var: ''}); + + expect(liquidProcessedInput).toMatchSnapshot(); + }); }); diff --git a/tests/integrations/services/metadataAuthors.test.ts b/tests/integrations/services/metadataAuthors.test.ts index 642efcf1..ce2727ea 100644 --- a/tests/integrations/services/metadataAuthors.test.ts +++ b/tests/integrations/services/metadataAuthors.test.ts @@ -119,7 +119,10 @@ describe('getContentWithUpdatedMetadata (Authors)', () => { resolvedFrontMatterVars: {}, }); - expect(updatedFileContent).toEqual(fileContent); + const {metadata: metadataBeforeEnrichment} = parseExistingMetadata(fileContent); + const {metadata: metadataAfterEnrichment} = parseExistingMetadata(updatedFileContent); + + expect(metadataAfterEnrichment).toEqual(metadataBeforeEnrichment); }); test( @@ -136,7 +139,11 @@ describe('getContentWithUpdatedMetadata (Authors)', () => { resolvedFrontMatterVars: {}, }); - expect(updatedFileContent).toEqual(fileContent); + const {metadata: metadataBeforeEnrichment} = parseExistingMetadata(fileContent); + const {metadata: metadataAfterEnrichment} = + parseExistingMetadata(updatedFileContent); + + expect(metadataAfterEnrichment).toEqual(metadataBeforeEnrichment); }, ); @@ -158,7 +165,11 @@ describe('getContentWithUpdatedMetadata (Authors)', () => { resolvedFrontMatterVars: {}, }); - expect(updatedFileContent).toEqual(fileContent); + const {metadata: metadataBeforeEnrichment} = parseExistingMetadata(fileContent); + const {metadata: metadataAfterEnrichment} = + parseExistingMetadata(updatedFileContent); + + expect(metadataAfterEnrichment).toEqual(metadataBeforeEnrichment); }, ); @@ -172,7 +183,7 @@ describe('getContentWithUpdatedMetadata (Authors)', () => { metadataOptions, resolvedFrontMatterVars: {}, }); - + const {metadata: originalMeta} = parseExistingMetadata(fileContent); const {metadata: updatedMeta} = parseExistingMetadata(updatedFileContent); diff --git a/tests/integrations/services/metadataContributors.test.ts b/tests/integrations/services/metadataContributors.test.ts index f914a8b9..021fd297 100644 --- a/tests/integrations/services/metadataContributors.test.ts +++ b/tests/integrations/services/metadataContributors.test.ts @@ -280,7 +280,10 @@ describe('getContentWithUpdatedMetadata (Contributors)', () => { resolvedFrontMatterVars: {}, }); - expect(updatedFileContent).toEqual(fileContent); + const {metadata: metadataBeforeEnrichment} = parseExistingMetadata(fileContent); + const {metadata: metadataAfterEnrichment} = parseExistingMetadata(updatedFileContent); + + expect(metadataAfterEnrichment).toEqual(metadataBeforeEnrichment); }); test( @@ -297,7 +300,11 @@ describe('getContentWithUpdatedMetadata (Contributors)', () => { resolvedFrontMatterVars: {}, }); - expect(updatedFileContent).toEqual(fileContent); + const {metadata: metadataBeforeEnrichment} = parseExistingMetadata(fileContent); + const {metadata: metadataAfterEnrichment} = + parseExistingMetadata(updatedFileContent); + + expect(metadataAfterEnrichment).toEqual(metadataBeforeEnrichment); }, ); }); diff --git a/tests/mocks/fileContent/metadata/substitutionsWithEmptyString.md b/tests/mocks/fileContent/metadata/substitutionsWithEmptyString.md new file mode 100644 index 00000000..55d8ac3c --- /dev/null +++ b/tests/mocks/fileContent/metadata/substitutionsWithEmptyString.md @@ -0,0 +1,7 @@ +--- +prop1: {{var}} +prop2: '{{var}}' +prop3: '{{var}}' +prop4: >- + {{var}} +---