Skip to content

Commit

Permalink
test: add tests for insertAfterMedia utility
Browse files Browse the repository at this point in the history
  • Loading branch information
farmerpaul committed Sep 5, 2024
1 parent bfc5b43 commit e56fd76
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
85 changes: 85 additions & 0 deletions src/shared/utils/helpers/insertAfterMedia.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { insertAfterMedia } from './insertAfterMedia';

describe('insertAfterMedia', () => {
it('should insert the string after the first line containing content that does not contain solely media', () => {
const markdown = `![Image](image.jpg)
<img src="image.jpg" alt="Image">
<video src="video.mp4"></video>
<audio src="audio.mp3"></audio>
This line does not contain media.
This line also does not contain media.`;
const inserted = 'Inserted string';

const result = insertAfterMedia(markdown, inserted);

expect(result).toBe(`![Image](image.jpg)
<img src="image.jpg" alt="Image">
<video src="video.mp4"></video>
<audio src="audio.mp3"></audio>
Inserted string
This line does not contain media.
This line also does not contain media.`);
});

it('should append the string to the end if there is no line containing content that does not contain solely media', () => {
const markdown = `![Image](image.jpg)
<img src="image.jpg" alt="Image">
<video src="video.mp4"></video>
<audio src="audio.mp3"></audio>`;
const inserted = 'Inserted string';

const result = insertAfterMedia(markdown, inserted);

expect(result).toBe(`![Image](image.jpg)
<img src="image.jpg" alt="Image">
<video src="video.mp4"></video>
<audio src="audio.mp3"></audio>
Inserted string`);
});

it('should insert the string before a line containing both media and text content', () => {
const markdown = `![Image](image.jpg)
<img src="image.jpg" alt="Image">
<video src="video.mp4"></video>
<audio src="audio.mp3"></audio>
![Image](image.jpg) This line contains text and media.
This line does not contain media.`;
const inserted = 'Inserted string';

const result = insertAfterMedia(markdown, inserted);

expect(result).toBe(`![Image](image.jpg)
<img src="image.jpg" alt="Image">
<video src="video.mp4"></video>
<audio src="audio.mp3"></audio>
Inserted string
![Image](image.jpg) This line contains text and media.
This line does not contain media.`);
});

it('should insert the string after a line that contains media wrapped in an alignment block', () => {
const markdown = `::: hljs-center
![Image](image.jpg)
<img src="image.jpg" alt="Image">
<video src="video.mp4"></video>
<audio src="audio.mp3"></audio>
:::
::: hljs-center <img src="image.jpg" alt="Image"> :::
::: hljs-right This line does not contain media. :::
This line also does not contain media.`;
const inserted = 'Inserted string';

const result = insertAfterMedia(markdown, inserted);

expect(result).toBe(`::: hljs-center
![Image](image.jpg)
<img src="image.jpg" alt="Image">
<video src="video.mp4"></video>
<audio src="audio.mp3"></audio>
:::
::: hljs-center <img src="image.jpg" alt="Image"> :::
Inserted string
::: hljs-right This line does not contain media. :::
This line also does not contain media.`);
});
});
5 changes: 4 additions & 1 deletion src/shared/utils/helpers/insertAfterMedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export const insertAfterMedia = (markdown: string, inserted: string) => {
let i: number;
for (i = 0; i < lines.length; i++) {
const lineWithoutMedia = lines[i]
.replaceAll(/^:::\s+hljs-\S+|:::$|<(img|video|audio)[^>]+>|!\[[^\]]*\]\([^)]*\)/gi, '')
.replaceAll(
/^:::\s+hljs-\S+|:::$|<img[^>]+>|<video[^>]+>.*<\/video>|<audio[^>]+>.*<\/audio>|!\[[^\]]*\]\([^)]*\)/gi,
'',
)
.trim();
if (lineWithoutMedia) break;
}
Expand Down

0 comments on commit e56fd76

Please sign in to comment.