1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-04-27 11:01:14 +00:00

Fix get wiki page id (#7322)

* fix get wiki page link

* add test workflow

* add unit for wiki utils function
This commit is contained in:
Michael An 2025-01-07 17:33:50 +08:00 committed by GitHub
parent 4b33271e5b
commit 206ecec2aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 66 additions and 3 deletions

View File

@ -49,7 +49,7 @@ jobs:
with:
node-version: "20.x"
- name: run npm lint
- name: run npm lint and npm test
run: |
cd $GITHUB_WORKSPACE/tests/
if chmod +x test_frontend_changes.sh && ./test_frontend_changes.sh; then chmod +x github_actions_npm_lint.sh && ./github_actions_npm_lint.sh; else true; fi

View File

@ -48,7 +48,12 @@ const getCurrentPageConfig = (pages, pageId) => {
};
const getWikPageLink = (pageId) => {
return window.location.url.replace(/\/[^\/]+$/, `/${pageId}`);
let { origin, pathname } = window.location;
let pathArr = pathname.split('/');
// pathname is like `/wikis/${wikiId}/{pageId}/`
pathArr[3] = pageId;
pathname = pathArr.join('/');
return `${origin}${pathname}`;
};
const throttle = (fn, delay) => {

View File

@ -0,0 +1,51 @@
import { generatorBase64Code, generateUniqueId, getWikPageLink } from '../../../../pages/wiki2/utils/index.js';
describe('generatorBase64Code', () => {
it('should generate a base64 code of length 4 by default', () => {
const code = generatorBase64Code();
expect(code.length).toBe(4);
});
it('should generate a base64 code of length 6 when given 6', () => {
const code = generatorBase64Code(6);
expect(code.length).toBe(6);
});
it('should generate a base64 code which is a string', () => {
const code = generatorBase64Code();
expect(typeof code).toBe('string');
});
});
describe('generateUniqueId', () => {
it('should generate a unique id', () => {
const navigation = [
{ id: 'page1', children: [] },
{ id: 'page2', children: [{ id: 'page21', children: [] }] },
];
expect(generateUniqueId(navigation)).not.toMatch(/page1|page2|page21/);
});
it('should generate a unique id with custom length', () => {
const navigation = [
{ id: 'page1', children: [] },
{ id: 'page2', children: [{ id: 'page21', children: [] }] },
];
expect(generateUniqueId(navigation, 6)).not.toMatch(/page1|page2|page21/);
expect(generateUniqueId(navigation, 6).length).toBe(6);
});
});
describe('getWikPageLink', () => {
it('returns the correct URL', () => {
const originalLocation = window.location;
// Mock window.location
delete window.location;
window.location = {
origin: 'https://cloud.seafile.com',
pathname: '/wikis/6cbbded99bd272796a2/7Lj3/'
};
const pageId = 'y4Jw';
const expectedUrl = 'https://cloud.seafile.com/wikis/6cbbded99bd272796a2/y4Jw/';
expect(getWikPageLink(pageId)).toBe(expectedUrl);
// Restore original window.location
window.location = originalLocation;
});
});

View File

@ -8,6 +8,13 @@ export const gettext = window.gettext;
export const internalFilePath = '/_Internal/seatable-integration.json';
// for unit test global variable
if (!window.app) {
window.app = {};
window.app.config = {};
window.app.pageOptions = {};
}
export const siteRoot = window.app.config.siteRoot;
export const loginUrl = window.app.config.loginUrl;
export const avatarInfo = window.app.config.avatarInfo;

View File

@ -17,4 +17,4 @@ echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >~/.npmrc
cd $GITHUB_WORKSPACE
cd ./frontend && npm install && npm run lint
cd ./frontend && npm install && npm run lint && npm run test