From 65ff06f72bac1c2937cd7c5ed22162785a452f70 Mon Sep 17 00:00:00 2001 From: Richard Sustek Date: Thu, 2 Jan 2025 13:43:16 +0100 Subject: [PATCH] test: Add tests covering rich-text-processor --- tests/rich-text-processor.test.ts | 156 ++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 tests/rich-text-processor.test.ts diff --git a/tests/rich-text-processor.test.ts b/tests/rich-text-processor.test.ts new file mode 100644 index 0000000..deb5370 --- /dev/null +++ b/tests/rich-text-processor.test.ts @@ -0,0 +1,156 @@ +import { describe, expect, it } from 'vitest'; +import { richTextProcessor as _richTextProcessor, CodenameReplaceFunc, ItemStateInTargetEnvironmentByCodename } from '../lib/index.js'; + +const richTextProcessor = _richTextProcessor(); + +describe('Translate ids in rich text processor', () => { + it('Asset id should be translated', () => { + const result = richTextProcessor.processAssetIds( + `
`, + (id) => ({ + codename: `${id}-x` + }) + ); + expect(result.html).toStrictEqual(`
`); + expect(result.ids).toStrictEqual(new Set(['id'])); + }); + + it('Item id should be translated', () => { + const result = richTextProcessor.processDataIds( + ``, + (id) => ({ + codename: `${id}-x` + }) + ); + expect(result.html).toStrictEqual(``); + expect(result.ids).toStrictEqual(new Set(['id'])); + }); + + it('Link asset id should be translated', () => { + const result = richTextProcessor.processLinkAssetIds(`link to an asset`, (id) => ({ + codename: `${id}-x` + })); + expect(result.html).toStrictEqual(`link to an asset`); + expect(result.ids).toStrictEqual(new Set(['id'])); + }); + + it('Link item id should be translated', () => { + const result = richTextProcessor.processLinkItemIds(`link to a content item`, (id) => ({ + codename: `${id}-x` + })); + expect(result.html).toStrictEqual(`link to a content item`); + expect(result.ids).toStrictEqual(new Set(['id'])); + }); +}); + +describe('Translate codenames in rich text processor', () => { + it('Asset codename should be translated', () => { + const result = richTextProcessor.processAssetCodenames( + `
`, + (codename) => { + const replaceRes: ItemStateInTargetEnvironmentByCodename = { + externalIdToUse: `${codename}-x`, + itemCodename: codename, + item: undefined, + state: 'doesNotExists' + }; + + return replaceRes; + } + ); + expect(result.html).toStrictEqual( + `
` + ); + expect(result.codenames).toStrictEqual(new Set(['codename'])); + }); + + it('Item codename should be translated', () => { + const result = richTextProcessor.processItemCodenames( + ``, + (codename) => { + const replaceRes: ItemStateInTargetEnvironmentByCodename = { + externalIdToUse: `${codename}-x`, + itemCodename: codename, + item: undefined, + state: 'doesNotExists' + }; + + return replaceRes; + } + ); + expect(result.html).toStrictEqual( + `` + ); + expect(result.codenames).toStrictEqual(new Set(['codename'])); + }); + + it('Link asset codename should be translated', () => { + const result = richTextProcessor.processLinkAssetCodenames( + `link to an asset`, + (codename) => { + const replaceRes: ItemStateInTargetEnvironmentByCodename = { + externalIdToUse: `${codename}-x`, + itemCodename: codename, + item: undefined, + state: 'doesNotExists' + }; + + return replaceRes; + } + ); + expect(result.html).toStrictEqual(`link to an asset`); + expect(result.codenames).toStrictEqual(new Set(['codename'])); + }); + + it('Link item codename should be translated', () => { + const result = richTextProcessor.processLinkItemCodenames( + `link to a content item`, + (codename) => { + const replaceRes: ItemStateInTargetEnvironmentByCodename = { + externalIdToUse: `${codename}-x`, + itemCodename: codename, + item: undefined, + state: 'doesNotExists' + }; + + return replaceRes; + } + ); + expect(result.html).toStrictEqual(`link to a content item`); + expect(result.codenames).toStrictEqual(new Set(['codename'])); + }); +}); + +describe('Special caces', () => { + it('Link item id should be translated when text contains line breaks', () => { + const result = richTextProcessor.processLinkItemIds(`\nlink to a content item`, (id) => ({ + codename: `${id}-x` + })); + expect(result.html).toStrictEqual(`\nlink to a content item`); + expect(result.ids).toStrictEqual(new Set(['id'])); + }); + + it('Link asset id should be translated when text contains line breaks', () => { + const result = richTextProcessor.processLinkAssetIds(`\nlink to an asset`, (id) => ({ + codename: `${id}-x` + })); + expect(result.html).toStrictEqual(`\nlink to an asset`); + expect(result.ids).toStrictEqual(new Set(['id'])); + }); + + it('Link asset id should be translated when text contains line breaks', () => { + const result = richTextProcessor.processLinkAssetIds(`\nlink to an asset`, (id) => ({ + codename: `${id}-x` + })); + expect(result.html).toStrictEqual(`\nlink to an asset`); + expect(result.ids).toStrictEqual(new Set(['id'])); + }); + + it('Link asset id should be translated when text contains line breaks', () => { + const result = richTextProcessor.processLinkAssetIds(`\nlink to an asset`, (id) => ({ + codename: `${id}-x` + })); + expect(result.html).toStrictEqual(`\nlink to an asset`); + expect(result.ids).toStrictEqual(new Set(['id'])); + }); +});