From 206ecec2aa319df004ec01134c8bdcdb736f9c1c Mon Sep 17 00:00:00 2001 From: Michael An <2331806369@qq.com> Date: Tue, 7 Jan 2025 17:33:50 +0800 Subject: [PATCH] Fix get wiki page id (#7322) * fix get wiki page link * add test workflow * add unit for wiki utils function --- .github/workflows/test.yml | 2 +- frontend/src/pages/wiki2/utils/index.js | 7 ++- .../src/tests/pages/wiki2/utils/index.test.js | 51 +++++++++++++++++++ frontend/src/utils/constants.js | 7 +++ tests/github_actions_npm_lint.sh | 2 +- 5 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 frontend/src/tests/pages/wiki2/utils/index.test.js diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 10ec37fb05..b374a3e7d8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/frontend/src/pages/wiki2/utils/index.js b/frontend/src/pages/wiki2/utils/index.js index e7fd6a412a..342dcd8f2c 100644 --- a/frontend/src/pages/wiki2/utils/index.js +++ b/frontend/src/pages/wiki2/utils/index.js @@ -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) => { diff --git a/frontend/src/tests/pages/wiki2/utils/index.test.js b/frontend/src/tests/pages/wiki2/utils/index.test.js new file mode 100644 index 0000000000..3d78d8f38a --- /dev/null +++ b/frontend/src/tests/pages/wiki2/utils/index.test.js @@ -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; + }); +}); diff --git a/frontend/src/utils/constants.js b/frontend/src/utils/constants.js index ef3d1aae86..3b5263f07f 100644 --- a/frontend/src/utils/constants.js +++ b/frontend/src/utils/constants.js @@ -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; diff --git a/tests/github_actions_npm_lint.sh b/tests/github_actions_npm_lint.sh index ad464366c2..f206da2642 100644 --- a/tests/github_actions_npm_lint.sh +++ b/tests/github_actions_npm_lint.sh @@ -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