The Node.js® Website
1import {
2 getAcronymFromString,
3 parseRichTextIntoPlainText,
4} from '@/util/stringUtils';
5
6describe('String utils', () => {
7 it('getAcronymFromString returns the correct acronym', () => {
8 expect(getAcronymFromString('John Doe')).toBe('JD');
9 });
10
11 it('getAcronymFromString returns the correct acronym for a single word', () => {
12 expect(getAcronymFromString('John')).toBe('J');
13 });
14
15 it('getAcronymFromString if the string is empty, it returns NA', () => {
16 expect(getAcronymFromString('')).toBe('');
17 });
18
19 it('parseRichTextIntoPlainText returns the correct plain text from an HTML tag', () => {
20 expect(parseRichTextIntoPlainText('<p>John Doe</p>')).toBe('John Doe');
21 });
22
23 it('parseRichTextIntoPlainText returns only the text of a link tag', () => {
24 expect(
25 parseRichTextIntoPlainText('[this is a link](https://www.google.com)')
26 ).toBe('this is a link');
27 });
28
29 it('parseRichTextIntoPlainText replaces markdown lists with their content', () => {
30 expect(
31 parseRichTextIntoPlainText('- this is a list item\n- this is another')
32 ).toBe('this is a list item\nthis is another');
33 });
34
35 it('parseRichTextIntoPlainText removes underscore, bold and italic with their content', () => {
36 expect(
37 parseRichTextIntoPlainText(
38 '**bold content**, *italic content*, _underscore content_'
39 )
40 ).toBe('bold content, italic content, underscore content');
41 });
42
43 it('parseRichTextIntoPlainText removes code blocks with their content', () => {
44 expect(
45 parseRichTextIntoPlainText('this is a\n```code block```\nwith content')
46 ).toBe('this is a\nwith content');
47 });
48
49 it('parseRichTextIntoPlainText replaces empty lines or lines just with spaces with an empty string', () => {
50 expect(parseRichTextIntoPlainText('\n \n')).toBe('');
51 });
52
53 it('parseRichTextIntoPlainText replaces leading and trailing spaces from each line with an empty string', () => {
54 expect(parseRichTextIntoPlainText(' this is a line ')).toBe(
55 'this is a line'
56 );
57 });
58
59 it('parseRichTextIntoPlainText replaces leading numbers and dots from each line with an empty string', () => {
60 expect(
61 parseRichTextIntoPlainText('1. this is a line\n2. this is a second line')
62 ).toBe('this is a line\nthis is a second line');
63 });
64});