Skip to content

Commit 9ec39d0

Browse files
committed
test: added more tests
1 parent a9ede14 commit 9ec39d0

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { describe, it, expect, vi, Mock } from 'vitest';
2+
import { fetchMarkdownContent } from './markdown';
3+
4+
describe('fetchMarkdownContent', () => {
5+
it('should fetch markdown content successfully', async () => {
6+
const mockResponse = {
7+
success: true,
8+
data: 'Mocked markdown content',
9+
};
10+
11+
// Mock the fetch function
12+
global.fetch = vi.fn(() =>
13+
Promise.resolve({
14+
ok: true,
15+
json: () => Promise.resolve(mockResponse),
16+
})
17+
) as Mock;
18+
19+
const result = await fetchMarkdownContent('test markdown text');
20+
expect(result).toEqual(mockResponse);
21+
});
22+
23+
it('should throw an error if the fetch fails', async () => {
24+
// Mock the fetch function to reject
25+
global.fetch = vi.fn((): Promise<never> => Promise.reject(new Error('Network error'))) as Mock;
26+
27+
await expect(fetchMarkdownContent('test markdown text')).rejects.toThrow('Network error');
28+
});
29+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { describe, it, expect, vi, Mock } from 'vitest';
2+
import { fetchMediaBrowserContent } from './media-browser';
3+
import { MediaBrowserApiResponse } from '../interfaces';
4+
5+
describe('fetchMediaBrowserContent', () => {
6+
it('should fetch media browser content successfully', async (): Promise<void> => {
7+
const mockResponse: MediaBrowserApiResponse = {
8+
success: true,
9+
data: {
10+
sources: [
11+
{
12+
baseurl: 'http://example.com',
13+
path: 'images',
14+
files: [{ file: 'image1.jpg' }, { file: 'image2.jpg' }],
15+
},
16+
],
17+
},
18+
};
19+
20+
// Mock the fetch function
21+
global.fetch = vi.fn(() =>
22+
Promise.resolve({
23+
ok: true,
24+
json: (): Promise<MediaBrowserApiResponse> => Promise.resolve(mockResponse),
25+
})
26+
) as Mock;
27+
28+
const result = (await fetchMediaBrowserContent()) as MediaBrowserApiResponse;
29+
expect(result).toEqual(mockResponse);
30+
});
31+
32+
it('should throw an error if the fetch fails', async () => {
33+
// Mock the fetch function to reject
34+
global.fetch = vi.fn((): Promise<never> => Promise.reject(new Error('Network error'))) as Mock;
35+
36+
await expect(fetchMediaBrowserContent()).rejects.toThrow('Network error');
37+
});
38+
});

0 commit comments

Comments
 (0)