Skip to content

Commit c9cba1b

Browse files
committed
test: added test for OpenSearch API
1 parent 15164bb commit c9cba1b

File tree

3 files changed

+229
-154
lines changed

3 files changed

+229
-154
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@
5252
"@types/bootstrap": "^5.2.10",
5353
"@types/highlightjs": "^9.12.6",
5454
"@types/masonry-layout": "^4.2.8",
55-
"@types/node": "^20.17.46",
55+
"@types/node": "^20.17.48",
5656
"@types/sortablejs": "^1.15.8",
5757
"@vitest/coverage-istanbul": "^3.1.3",
58-
"@vitest/coverage-v8": "2.1.5",
58+
"@vitest/coverage-v8": "^3.1.3",
5959
"autoprefixer": "^10.4.21",
6060
"babel-preset-env": "^1.7.0",
6161
"husky": "^9.1.7",
@@ -64,7 +64,7 @@
6464
"prettier": "^3.5.3",
6565
"pretty-quick": "^4.1.1",
6666
"rollup-plugin-sbom": "^1.1.1",
67-
"sass": "^1.87.0",
67+
"sass": "^1.89.0",
6868
"sigmund": "^1.0.1",
6969
"typescript": "^5.8.3",
7070
"vite": "^6.3.5",
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { describe, it, expect, vi, afterEach } from 'vitest';
2+
import { fetchOpenSearchAction, fetchOpenSearchStatistics } from './opensearch';
3+
4+
describe('OpenSearch API', () => {
5+
afterEach(() => {
6+
vi.restoreAllMocks();
7+
});
8+
9+
describe('fetchOpenSearchAction', () => {
10+
it('should fetch OpenSearch action and return JSON response if successful', async () => {
11+
const mockResponse = { success: true, message: 'Action executed' };
12+
global.fetch = vi.fn(() =>
13+
Promise.resolve({
14+
ok: true,
15+
json: () => Promise.resolve(mockResponse),
16+
} as Response)
17+
);
18+
19+
const action = 'index';
20+
const result = await fetchOpenSearchAction(action);
21+
22+
expect(result).toEqual(mockResponse);
23+
expect(global.fetch).toHaveBeenCalledWith('./api/opensearch/index', {
24+
method: 'GET',
25+
cache: 'no-cache',
26+
headers: {
27+
'Content-Type': 'application/json',
28+
},
29+
redirect: 'follow',
30+
referrerPolicy: 'no-referrer',
31+
});
32+
});
33+
34+
it('should throw an error if fetch fails', async () => {
35+
const mockError = new Error('Fetch failed');
36+
global.fetch = vi.fn(() => Promise.reject(mockError));
37+
38+
await expect(fetchOpenSearchAction('index')).rejects.toThrow(mockError);
39+
});
40+
});
41+
42+
describe('fetchOpenSearchStatistics', () => {
43+
it('should fetch OpenSearch statistics and return JSON response if successful', async () => {
44+
const mockResponse = {
45+
status: 'green',
46+
documents: 123,
47+
indices: 5,
48+
size: '10GB',
49+
};
50+
51+
global.fetch = vi.fn(() =>
52+
Promise.resolve({
53+
ok: true,
54+
json: () => Promise.resolve(mockResponse),
55+
} as Response)
56+
);
57+
58+
const result = await fetchOpenSearchStatistics();
59+
60+
expect(result).toEqual(mockResponse);
61+
expect(global.fetch).toHaveBeenCalledWith('./api/opensearch/statistics', {
62+
method: 'GET',
63+
cache: 'no-cache',
64+
headers: {
65+
'Content-Type': 'application/json',
66+
},
67+
redirect: 'follow',
68+
referrerPolicy: 'no-referrer',
69+
});
70+
});
71+
72+
it('should throw an error if fetch fails', async () => {
73+
const mockError = new Error('Fetch failed');
74+
global.fetch = vi.fn(() => Promise.reject(mockError));
75+
76+
await expect(fetchOpenSearchStatistics()).rejects.toThrow(mockError);
77+
});
78+
});
79+
});

0 commit comments

Comments
 (0)