1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2025-10-10 19:32:02 +00:00

add unit tests for js

This commit is contained in:
Maxim Slipenko 2025-06-07 19:02:13 +03:00
parent 009a1bcf86
commit 3b0b5d0513
4 changed files with 30 additions and 6 deletions

View file

@ -3,6 +3,7 @@ import {
parseUrl, translateMonth, translateDay, blobToDataURI,
toAbsoluteUrl, encodeURLEncodedBase64, decodeURLEncodedBase64,
isDarkTheme, getCurrentLocale, parseDom, serializeXml, sleep,
parseRepoOwnerPathInfo,
} from './utils.js';
afterEach(() => {

View file

@ -49,10 +49,9 @@ export function matchIssue(queryText) {
const query = queryText.toLowerCase().trim();
if (!query) {
// Return latest 5 issues/prs sorted by number descending
return [...issues]
.sort((a, b) => b.number - a.number)
.slice(0, 5);
.slice(0, maxMatches);
}
const isDigital = /^\d+$/.test(query);
@ -67,7 +66,7 @@ export function matchIssue(queryText) {
results.push(...prefixMatches);
}
if (!isDigital || results.length < 5) {
if (!isDigital || results.length < maxMatches) {
// Fallback: find by title match, sorted by number descending
const titleMatches = issues
.filter(issue =>
@ -79,10 +78,10 @@ export function matchIssue(queryText) {
for (const match of titleMatches) {
if (!results.includes(match)) {
results.push(match);
if (results.length >= 5) break;
if (results.length >= maxMatches) break;
}
}
}
return results.slice(0, 5);
return results.slice(0, maxMatches);
}

View file

@ -1,4 +1,4 @@
import {matchEmoji, matchMention} from './match.js';
import {matchEmoji, matchMention, matchIssue} from './match.js';
test('matchEmoji', () => {
expect(matchEmoji('')).toEqual([
@ -48,3 +48,15 @@ test('matchMention', () => {
expect(matchMention('')).toEqual(window.config.mentionValues.slice(0, 6));
expect(matchMention('user4')).toEqual([window.config.mentionValues[3]]);
});
test('matchIssue', () => {
expect(matchIssue('')).toEqual(window.config.issueValues.slice(0, 6));
expect(matchIssue('1')).toEqual([window.config.issueValues[9], window.config.issueValues[0]]);
expect(matchIssue('issue')).toEqual([
window.config.issueValues[0],
window.config.issueValues[1],
window.config.issueValues[2],
window.config.issueValues[7],
window.config.issueValues[9],
]);
});

View file

@ -15,4 +15,16 @@ window.config = {
{key: 'org6 User 6', value: 'org6', name: 'org6', fullname: 'User 6', avatar: 'https://avatar6.com'},
{key: 'org7 User 7', value: 'org7', name: 'org7', fullname: 'User 7', avatar: 'https://avatar7.com'},
],
issueValues: [
{number: 10, title:'Issue'},
{number: 9, title:'Foo issue'},
{number: 8, title:'Foo issue'},
{number: 7, title:'Merged PR'},
{number: 6, title:'Closed PR'},
{number: 5, title:'PR'},
{number: 4, title:'WIP: PR'},
{number: 3, title:'Closed issue'},
{number: 2, title:'PR'},
{number: 1, title:'Issue'},
]
};