From aa9677ba82ac40f9aa1ae0c83dc59fa39d70baa0 Mon Sep 17 00:00:00 2001 From: Garma <64153597+Garma00@users.noreply.github.com> Date: Mon, 18 Sep 2023 11:03:21 +0200 Subject: [PATCH] [#IOCOM-538] Add "..." at the end of the message content when it's truncated (#280) --- .../__tests__/__snapshots__/utils.test.ts.snap | 2 +- EmailNotification/__tests__/handler.test.ts | 3 ++- EmailNotification/__tests__/utils.test.ts | 17 ++++++++++++++++- EmailNotification/utils.ts | 5 ++++- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/EmailNotification/__tests__/__snapshots__/utils.test.ts.snap b/EmailNotification/__tests__/__snapshots__/utils.test.ts.snap index 74496f8d..c665ae46 100644 --- a/EmailNotification/__tests__/__snapshots__/utils.test.ts.snap +++ b/EmailNotification/__tests__/__snapshots__/utils.test.ts.snap @@ -388,7 +388,7 @@ Object { -

testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttestte

+

testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttestte...

diff --git a/EmailNotification/__tests__/handler.test.ts b/EmailNotification/__tests__/handler.test.ts index 415f9429..56844a34 100644 --- a/EmailNotification/__tests__/handler.test.ts +++ b/EmailNotification/__tests__/handler.test.ts @@ -225,7 +225,8 @@ describe("prepareBody", () => { const markdown = "# Header 1\nsome text\n## Header 2\nsome text\n### Header 3\nsome text\n#### Header 4\nsome text\n##### Header 5\nsome text\ntestesttesttesttestesttesttesttestesttesttesttestesttesttesttestesttesttesttestesttesttest"; const r = prepareBody(markdown); - expect(r).toHaveLength(134); + //this should be 134 + 3 cause "..." is added at the end + expect(r).toHaveLength(137); expect(r).not.toContain("#"); }); }); diff --git a/EmailNotification/__tests__/utils.test.ts b/EmailNotification/__tests__/utils.test.ts index 967c9eda..3136b10c 100644 --- a/EmailNotification/__tests__/utils.test.ts +++ b/EmailNotification/__tests__/utils.test.ts @@ -1,4 +1,8 @@ -import { contentToHtml, messageReducedToHtml } from "../utils"; +import { + contentToHtml, + messageReducedToHtml, + truncateMarkdown +} from "../utils"; import * as E from "fp-ts/Either"; import { aCreatedMessageEventSenderMetadata, @@ -52,3 +56,14 @@ describe("messageReducedToHtml", () => { expect(result).toEqual(E.left(anError)); }); }); + +describe("truncateMarkdown", () => { + test("should add '...' at the end of the string if the plain text length is > 134", () => { + expect(truncateMarkdown(aMessageContent.markdown).slice(-3)).toBe("..."); + }); + test("should not add '...' at the end of the string if the plain text length is <= 134", () => { + expect(truncateMarkdown("This message is < than 134 chars")).not.toContain( + "..." + ); + }); +}); diff --git a/EmailNotification/utils.ts b/EmailNotification/utils.ts index 5ef577b0..7aead9b9 100644 --- a/EmailNotification/utils.ts +++ b/EmailNotification/utils.ts @@ -108,7 +108,10 @@ type MessageReducedToHtmlInput = { }; export const truncateMarkdown = (plainText: string): string => - plainText.substring(0, MAX_CHARACTER_FOR_BODY_MAIL); + // we add "..." only when the message is going to be truncate + plainText.length > MAX_CHARACTER_FOR_BODY_MAIL + ? plainText.substring(0, MAX_CHARACTER_FOR_BODY_MAIL) + "..." + : plainText.substring(0, MAX_CHARACTER_FOR_BODY_MAIL); export const prepareBody = (markdown: string): string => // eslint-disable-next-line functional/immutable-data