Reduce changes in test

This commit is contained in:
Sidharth Vinod
2023-04-26 10:51:54 +05:30
parent 395a794758
commit 5bbce45c56

View File

@@ -1,391 +1,244 @@
import { markdownToLines, markdownToHTML } from './handle-markdown-text.js'; import { markdownToLines, markdownToHTML } from './handle-markdown-text.js';
import { test, expect } from 'vitest'; import { test, expect } from 'vitest';
describe('markdownToLines', () => { test('markdownToLines - Basic test', () => {
test('Basic test', () => { const input = `This is regular text
const input = `This is regular text
Here is a new line Here is a new line
There is some words **with a bold** section There is some words **with a bold** section
Here is a line *with an italic* section`; Here is a line *with an italic* section`;
const output = markdownToLines(input); const expectedOutput = [
expect(output).toMatchInlineSnapshot(`
[ [
[ { content: 'This', type: 'normal' },
{ { content: 'is', type: 'normal' },
"content": "This", { content: 'regular', type: 'normal' },
"type": "normal", { content: 'text', type: 'normal' },
}, ],
{ [
"content": "is", { content: 'Here', type: 'normal' },
"type": "normal", { content: 'is', type: 'normal' },
}, { content: 'a', type: 'normal' },
{ { content: 'new', type: 'normal' },
"content": "regular", { content: 'line', type: 'normal' },
"type": "normal", ],
}, [
{ { content: 'There', type: 'normal' },
"content": "text", { content: 'is', type: 'normal' },
"type": "normal", { content: 'some', type: 'normal' },
}, { content: 'words', type: 'normal' },
], { content: 'with', type: 'strong' },
[ { content: 'a', type: 'strong' },
{ { content: 'bold', type: 'strong' },
"content": "Here", { content: 'section', type: 'normal' },
"type": "normal", ],
}, [
{ { content: 'Here', type: 'normal' },
"content": "is", { content: 'is', type: 'normal' },
"type": "normal", { content: 'a', type: 'normal' },
}, { content: 'line', type: 'normal' },
{ { content: 'with', type: 'emphasis' },
"content": "a", { content: 'an', type: 'emphasis' },
"type": "normal", { content: 'italic', type: 'emphasis' },
}, { content: 'section', type: 'normal' },
{ ],
"content": "new", ];
"type": "normal",
},
{
"content": "line",
"type": "normal",
},
],
[
{
"content": "There",
"type": "normal",
},
{
"content": "is",
"type": "normal",
},
{
"content": "some",
"type": "normal",
},
{
"content": "words",
"type": "normal",
},
{
"content": "with",
"type": "strong",
},
{
"content": "a",
"type": "strong",
},
{
"content": "bold",
"type": "strong",
},
{
"content": "section",
"type": "normal",
},
],
[
{
"content": "Here",
"type": "normal",
},
{
"content": "is",
"type": "normal",
},
{
"content": "a",
"type": "normal",
},
{
"content": "line",
"type": "normal",
},
{
"content": "with",
"type": "emphasis",
},
{
"content": "an",
"type": "emphasis",
},
{
"content": "italic",
"type": "emphasis",
},
{
"content": "section",
"type": "normal",
},
],
]
`);
});
test('Empty input', () => { const output = markdownToLines(input);
const input = ''; expect(output).toEqual(expectedOutput);
const expectedOutput = [[]]; });
const output = markdownToLines(input);
expect(output).toEqual(expectedOutput);
});
test('No formatting', () => { test('markdownToLines - Empty input', () => {
const input = `This is a simple test const input = '';
const expectedOutput = [[]];
const output = markdownToLines(input);
expect(output).toEqual(expectedOutput);
});
test('markdownToLines - No formatting', () => {
const input = `This is a simple test
with no formatting`; with no formatting`;
const expectedOutput = [ const expectedOutput = [
[ [
{ content: 'This', type: 'normal' }, { content: 'This', type: 'normal' },
{ content: 'is', type: 'normal' }, { content: 'is', type: 'normal' },
{ content: 'a', type: 'normal' }, { content: 'a', type: 'normal' },
{ content: 'simple', type: 'normal' }, { content: 'simple', type: 'normal' },
{ content: 'test', type: 'normal' }, { content: 'test', type: 'normal' },
], ],
[ [
{ content: 'with', type: 'normal' }, { content: 'with', type: 'normal' },
{ content: 'no', type: 'normal' }, { content: 'no', type: 'normal' },
{ content: 'formatting', type: 'normal' }, { content: 'formatting', type: 'normal' },
], ],
]; ];
const output = markdownToLines(input); const output = markdownToLines(input);
expect(output).toEqual(expectedOutput); expect(output).toEqual(expectedOutput);
}); });
test('Only bold formatting', () => { test('markdownToLines - Only bold formatting', () => {
const input = `This is a **bold** test`; const input = `This is a **bold** test`;
const expectedOutput = [ const expectedOutput = [
[ [
{ content: 'This', type: 'normal' }, { content: 'This', type: 'normal' },
{ content: 'is', type: 'normal' }, { content: 'is', type: 'normal' },
{ content: 'a', type: 'normal' }, { content: 'a', type: 'normal' },
{ content: 'bold', type: 'strong' }, { content: 'bold', type: 'strong' },
{ content: 'test', type: 'normal' }, { content: 'test', type: 'normal' },
], ],
]; ];
const output = markdownToLines(input); const output = markdownToLines(input);
expect(output).toEqual(expectedOutput); expect(output).toEqual(expectedOutput);
}); });
test('paragraph 1', () => { test('markdownToLines - paragraph 1', () => {
const input = `**Start** with const input = `**Start** with
a second line`; a second line`;
const expectedOutput = [ const expectedOutput = [
[ [
{ content: 'Start', type: 'strong' }, { content: 'Start', type: 'strong' },
{ content: 'with', type: 'normal' }, { content: 'with', type: 'normal' },
], ],
[ [
{ content: 'a', type: 'normal' }, { content: 'a', type: 'normal' },
{ content: 'second', type: 'normal' }, { content: 'second', type: 'normal' },
{ content: 'line', type: 'normal' }, { content: 'line', type: 'normal' },
], ],
]; ];
const output = markdownToLines(input); const output = markdownToLines(input);
expect(output).toEqual(expectedOutput); expect(output).toEqual(expectedOutput);
}); });
test('paragraph', () => {
const input = `**Start** with
test('markdownToLines - paragraph', () => {
const input = `**Start** with
a second line`; a second line`;
const expectedOutput = [ const expectedOutput = [
[
{ content: 'Start', type: 'strong' },
{ content: 'with', type: 'normal' },
],
[
{ content: 'a', type: 'normal' },
{ content: 'second', type: 'normal' },
{ content: 'line', type: 'normal' },
],
];
const output = markdownToLines(input);
expect(output).toEqual(expectedOutput);
});
test('Only italic formatting', () => {
const input = `This is an *italic* test`;
const output = markdownToLines(input);
expect(output).toMatchInlineSnapshot(`
[ [
[ { content: 'Start', type: 'strong' },
{ { content: 'with', type: 'normal' },
"content": "This", ],
"type": "normal",
},
{
"content": "is",
"type": "normal",
},
{
"content": "an",
"type": "normal",
},
{
"content": "italic",
"type": "emphasis",
},
{
"content": "test",
"type": "normal",
},
],
]
`);
});
test('Mixed formatting', () => {
const input = `*Italic* and **bold** formatting`;
const output = markdownToLines(input);
expect(output).toMatchInlineSnapshot(`
[ [
[ { content: 'a', type: 'normal' },
{ { content: 'second', type: 'normal' },
"content": "Italic", { content: 'line', type: 'normal' },
"type": "emphasis", ],
}, ];
{
"content": "and",
"type": "normal",
},
{
"content": "bold",
"type": "strong",
},
{
"content": "formatting",
"type": "normal",
},
],
]
`);
});
test('Mixed formatting with newline', () => { const output = markdownToLines(input);
const input = `The dog in **the** hog... a *very long text* about it expect(output).toEqual(expectedOutput);
});
test('markdownToLines - Only italic formatting', () => {
const input = `This is an *italic* test`;
const expectedOutput = [
[
{ content: 'This', type: 'normal' },
{ content: 'is', type: 'normal' },
{ content: 'an', type: 'normal' },
{ content: 'italic', type: 'emphasis' },
{ content: 'test', type: 'normal' },
],
];
const output = markdownToLines(input);
expect(output).toEqual(expectedOutput);
});
it('markdownToLines - Mixed formatting', () => {
const input = `*Italic* and **bold** formatting`;
const expectedOutput = [
[
{ content: 'Italic', type: 'emphasis' },
{ content: 'and', type: 'normal' },
{ content: 'bold', type: 'strong' },
{ content: 'formatting', type: 'normal' },
],
];
const output = markdownToLines(input);
expect(output).toEqual(expectedOutput);
});
it('markdownToLines - Mixed formatting', () => {
const input = `The dog in **the** hog... a *very long text* about it
Word!`; Word!`;
const output = markdownToLines(input); const expectedOutput = [
expect(output).toMatchInlineSnapshot(`
[ [
[ { content: 'The', type: 'normal' },
{ { content: 'dog', type: 'normal' },
"content": "The", { content: 'in', type: 'normal' },
"type": "normal", { content: 'the', type: 'strong' },
}, { content: 'hog...', type: 'normal' },
{ { content: 'a', type: 'normal' },
"content": "dog", { content: 'very', type: 'emphasis' },
"type": "normal", { content: 'long', type: 'emphasis' },
}, { content: 'text', type: 'emphasis' },
{ { content: 'about', type: 'normal' },
"content": "in", { content: 'it', type: 'normal' },
"type": "normal", ],
}, [{ content: 'Word!', type: 'normal' }],
{ ];
"content": "the",
"type": "strong", const output = markdownToLines(input);
}, expect(output).toEqual(expectedOutput);
{
"content": "hog...",
"type": "normal",
},
{
"content": "a",
"type": "normal",
},
{
"content": "very",
"type": "emphasis",
},
{
"content": "long",
"type": "emphasis",
},
{
"content": "text",
"type": "emphasis",
},
{
"content": "about",
"type": "normal",
},
{
"content": "it",
"type": "normal",
},
],
[
{
"content": "Word!",
"type": "normal",
},
],
]
`);
});
}); });
describe('markdownToHTML', () => { test('markdownToHTML - Basic test', () => {
test('Basic test', () => { const input = `This is regular text
const input = `This is regular text
Here is a new line Here is a new line
There is some words **with a bold** section There is some words **with a bold** section
Here is a line *with an italic* section`; Here is a line *with an italic* section`;
const output = markdownToHTML(input); const expectedOutput = `<p>This is regular text<br/>Here is a new line<br/>There is some words <strong>with a bold</strong> section<br/>Here is a line <em>with an italic</em> section</p>`;
expect(output).toMatchInlineSnapshot(
'"<p>This is regular text<br/>Here is a new line<br/>There is some words <strong>with a bold</strong> section<br/>Here is a line <em>with an italic</em> section</p>"'
);
});
test('Empty input', () => { const output = markdownToHTML(input);
const input = ''; expect(output).toEqual(expectedOutput);
const expectedOutput = ''; });
const output = markdownToHTML(input);
expect(output).toEqual(expectedOutput);
});
test('No formatting', () => { test('markdownToHTML - Empty input', () => {
const input = `This is a simple test const input = '';
const expectedOutput = '';
const output = markdownToHTML(input);
expect(output).toEqual(expectedOutput);
});
test('markdownToHTML - No formatting', () => {
const input = `This is a simple test
with no formatting`; with no formatting`;
const expectedOutput = `<p>This is a simple test<br/>with no formatting</p>`; const expectedOutput = `<p>This is a simple test<br/>with no formatting</p>`;
const output = markdownToHTML(input); const output = markdownToHTML(input);
expect(output).toEqual(expectedOutput); expect(output).toEqual(expectedOutput);
}); });
test('Only bold formatting', () => { test('markdownToHTML - Only bold formatting', () => {
const input = `This is a **bold** test`; const input = `This is a **bold** test`;
const expectedOutput = `<p>This is a <strong>bold</strong> test</p>`; const expectedOutput = `<p>This is a <strong>bold</strong> test</p>`;
const output = markdownToHTML(input); const output = markdownToHTML(input);
expect(output).toEqual(expectedOutput); expect(output).toEqual(expectedOutput);
}); });
test('Only italic formatting', () => { test('markdownToHTML - Only italic formatting', () => {
const input = `This is an *italic* test`; const input = `This is an *italic* test`;
const expectedOutput = `<p>This is an <em>italic</em> test</p>`; const expectedOutput = `<p>This is an <em>italic</em> test</p>`;
const output = markdownToHTML(input); const output = markdownToHTML(input);
expect(output).toEqual(expectedOutput); expect(output).toEqual(expectedOutput);
}); });
test('Mixed formatting', () => { test('markdownToHTML - Mixed formatting', () => {
const input = `*Italic* and **bold** formatting`; const input = `*Italic* and **bold** formatting`;
const expectedOutput = `<p><em>Italic</em> and <strong>bold</strong> formatting</p>`; const expectedOutput = `<p><em>Italic</em> and <strong>bold</strong> formatting</p>`;
const output = markdownToHTML(input); const output = markdownToHTML(input);
expect(output).toEqual(expectedOutput); expect(output).toEqual(expectedOutput);
// const expectedOutput = 'this text has some special characters: `&lt;p&gt;hi&lt;/p&gt;`';
expect(
markdownToHTML('this text has some special characters: `<p>hi</p>`')
).toMatchInlineSnapshot('"<p>this text has some special characters: </p>"');
});
}); });