-
-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improved typing, added more tests
- Loading branch information
Showing
13 changed files
with
622 additions
and
529 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { describe, it, expect, vi } from 'vitest'; | ||
import { fetchByLanguage, postStopWord, removeStopWord } from './stop-words'; | ||
|
||
global.fetch = vi.fn(); | ||
|
||
describe('Stop Words API', () => { | ||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('fetchByLanguage should fetch stop words by language', async () => { | ||
const mockResponse = [{ id: 1, lang: 'en', stopword: 'example' }]; | ||
(fetch as vi.Mock).mockResolvedValue({ | ||
ok: true, | ||
json: async () => mockResponse, | ||
}); | ||
|
||
const result = await fetchByLanguage('en'); | ||
expect(fetch).toHaveBeenCalledWith('./api/stopwords?language=en', { | ||
method: 'GET', | ||
headers: { | ||
Accept: 'application/json, text/plain, */*', | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
expect(result).toEqual(mockResponse); | ||
}); | ||
|
||
it('postStopWord should post a new stop word', async () => { | ||
const mockResponse = { success: true }; | ||
(fetch as vi.Mock).mockResolvedValue({ | ||
ok: true, | ||
json: async () => mockResponse, | ||
}); | ||
|
||
const result = await postStopWord('csrfToken', 'example', 1, 'en'); | ||
expect(fetch).toHaveBeenCalledWith('./api/stopword/save', { | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/json, text/plain, */*', | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
csrf: 'csrfToken', | ||
stopWord: 'example', | ||
stopWordId: 1, | ||
stopWordsLang: 'en', | ||
}), | ||
}); | ||
expect(result).toEqual(mockResponse); | ||
}); | ||
|
||
it('removeStopWord should delete a stop word', async () => { | ||
const mockResponse = { success: true }; | ||
(fetch as vi.Mock).mockResolvedValue({ | ||
ok: true, | ||
json: async () => mockResponse, | ||
}); | ||
|
||
const result = await removeStopWord('csrfToken', 1, 'en'); | ||
expect(fetch).toHaveBeenCalledWith('./api/stopword/delete', { | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/json, text/plain, */*', | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
csrf: 'csrfToken', | ||
stopWordId: 1, | ||
stopWordsLang: 'en', | ||
}), | ||
}); | ||
expect(result).toEqual(mockResponse); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Fetch data for stop words | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* @package phpMyFAQ | ||
* @author Thorsten Rinne <[email protected]> | ||
* @copyright 2025 phpMyFAQ Team | ||
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 | ||
* @link https://www.phpmyfaq.de | ||
* @since 2025-02-08 | ||
*/ | ||
|
||
export const fetchByLanguage = async (language: string): Promise<void> => { | ||
try { | ||
const response = await fetch(`./api/stopwords?language=${language}`, { | ||
method: 'GET', | ||
headers: { | ||
Accept: 'application/json, text/plain, */*', | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
return await response.json(); | ||
} catch (error) { | ||
throw error; | ||
} | ||
}; | ||
|
||
export const postStopWord = async ( | ||
csrf: string, | ||
stopWord: string, | ||
stopWordId: number, | ||
stopWordLanguage: string | ||
): Promise<void> => { | ||
try { | ||
const response = await fetch('./api/stopword/save', { | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/json, text/plain, */*', | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
csrf: csrf, | ||
stopWord: stopWord, | ||
stopWordId: stopWordId, | ||
stopWordsLang: stopWordLanguage, | ||
}), | ||
}); | ||
|
||
return await response.json(); | ||
} catch (error) { | ||
throw error; | ||
} | ||
}; | ||
|
||
export const removeStopWord = async (csrf: string, stopWordId: number, stopWordLanguage: string): Promise<void> => { | ||
try { | ||
const response = await fetch('./api/stopword/delete', { | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/json, text/plain, */*', | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
csrf: csrf, | ||
stopWordId: stopWordId, | ||
stopWordsLang: stopWordLanguage, | ||
}), | ||
}); | ||
|
||
return await response.json(); | ||
} catch (error) { | ||
throw error; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.