Files
dify/web/utils/navigation.spec.ts
longbingljw 36e6205c6c feat:update latest commit (#51)
* test: adding some web tests (#27792)

* feat: add validation to prevent saving empty opening statement in conversation opener modal (#27843)

* fix(web): improve the consistency of the inputs-form UI (#27837)

* fix(web): increase z-index of PortalToFollowElemContent (#27823)

* fix: installation_id is missing when in tools page (#27849)

* fix: avoid passing empty uniqueIdentifier to InstallFromMarketplace (#27802)

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>

* test: create new test scripts and update some existing test scripts o… (#27850)

* feat: change feedback to forum (#27862)

* chore: translate i18n files and update type definitions (#27868)

Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com>

* Fix/template transformer line number (#27867)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com>

* bump vite to 6.4.1 (#27877)

* Add WEAVIATE_GRPC_ENDPOINT as designed in weaviate migration guide (#27861)

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

* Fix: correct DraftWorkflowApi.post response model (#27289)

Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

* fix Version 2.0.0-beta.2: Chat annotations Api Error #25506  (#27206)

Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Asuka Minato <i@asukaminato.eu.org>

* fix jina reader creadential migration command (#27883)

* fix agent putout the output of workflow-tool twice (#26835) (#27087)

* fix jina reader transform (#27922)

* fix: prevent fetch version info in enterprise edition (#27923)

* fix(api): fix `VariablePool.get` adding unexpected keys to variable_dictionary (#26767)

Co-authored-by: -LAN- <laipz8200@outlook.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* refactor: implement tenant self queue for rag tasks (#27559)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: -LAN- <laipz8200@outlook.com>

* fix: bump brotli to 1.2.0 resloved CVE-2025-6176 (#27950)

Signed-off-by: kenwoodjw <blackxin55+@gmail.com>

---------

Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com>
Signed-off-by: kenwoodjw <blackxin55+@gmail.com>
Co-authored-by: aka James4u <smart.jamesjin@gmail.com>
Co-authored-by: Novice <novice12185727@gmail.com>
Co-authored-by: yangzheli <43645580+yangzheli@users.noreply.github.com>
Co-authored-by: Elliott <105957288+Elliott-byte@users.noreply.github.com>
Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com>
Co-authored-by: johnny0120 <johnny0120@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Gritty_dev <101377478+codomposer@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: wangjifeng <163279492+kk-wangjifeng@users.noreply.github.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Boris Polonsky <BorisPolonsky@users.noreply.github.com>
Co-authored-by: Yongtao Huang <yongtaoh2022@gmail.com>
Co-authored-by: Cursx <33718736+Cursx@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Asuka Minato <i@asukaminato.eu.org>
Co-authored-by: Jyong <76649700+JohnJyong@users.noreply.github.com>
Co-authored-by: red_sun <56100962+redSun64@users.noreply.github.com>
Co-authored-by: NFish <douxc512@gmail.com>
Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com>
Co-authored-by: -LAN- <laipz8200@outlook.com>
Co-authored-by: hj24 <huangjian@dify.ai>
Co-authored-by: kenwoodjw <blackxin55+@gmail.com>
2025-11-07 17:25:58 +08:00

298 lines
9.5 KiB
TypeScript

/**
* Test suite for navigation utility functions
* Tests URL and query parameter manipulation for consistent navigation behavior
* Includes helpers for preserving state during navigation (pagination, filters, etc.)
*/
import {
createBackNavigation,
createNavigationPath,
createNavigationPathWithParams,
datasetNavigation,
extractQueryParams,
mergeQueryParams,
} from './navigation'
describe('navigation', () => {
const originalWindow = globalThis.window
beforeEach(() => {
// Mock window.location with sample query parameters
delete (globalThis as any).window
globalThis.window = {
location: {
search: '?page=3&limit=10&keyword=test',
},
} as any
})
afterEach(() => {
globalThis.window = originalWindow
})
/**
* Tests createNavigationPath which builds URLs with optional query parameter preservation
*/
describe('createNavigationPath', () => {
test('preserves query parameters by default', () => {
const result = createNavigationPath('/datasets/123/documents')
expect(result).toBe('/datasets/123/documents?page=3&limit=10&keyword=test')
})
test('returns clean path when preserveParams is false', () => {
const result = createNavigationPath('/datasets/123/documents', false)
expect(result).toBe('/datasets/123/documents')
})
test('handles empty query string', () => {
globalThis.window.location.search = ''
const result = createNavigationPath('/datasets/123/documents')
expect(result).toBe('/datasets/123/documents')
})
test('handles path with trailing slash', () => {
const result = createNavigationPath('/datasets/123/documents/')
expect(result).toBe('/datasets/123/documents/?page=3&limit=10&keyword=test')
})
test('handles root path', () => {
const result = createNavigationPath('/')
expect(result).toBe('/?page=3&limit=10&keyword=test')
})
})
/**
* Tests createBackNavigation which creates a navigation callback function
*/
describe('createBackNavigation', () => {
/**
* Tests that the returned function properly navigates with preserved params
*/
test('returns function that calls router.push with correct path', () => {
const mockRouter = { push: jest.fn() }
const backNav = createBackNavigation(mockRouter, '/datasets/123/documents')
backNav()
expect(mockRouter.push).toHaveBeenCalledWith('/datasets/123/documents?page=3&limit=10&keyword=test')
})
test('returns function that navigates without params when preserveParams is false', () => {
const mockRouter = { push: jest.fn() }
const backNav = createBackNavigation(mockRouter, '/datasets/123/documents', false)
backNav()
expect(mockRouter.push).toHaveBeenCalledWith('/datasets/123/documents')
})
test('can be called multiple times', () => {
const mockRouter = { push: jest.fn() }
const backNav = createBackNavigation(mockRouter, '/datasets/123/documents')
backNav()
backNav()
expect(mockRouter.push).toHaveBeenCalledTimes(2)
})
})
/**
* Tests extractQueryParams which extracts specific parameters from current URL
*/
describe('extractQueryParams', () => {
/**
* Tests selective parameter extraction
*/
test('extracts specified parameters', () => {
const result = extractQueryParams(['page', 'limit'])
expect(result).toEqual({ page: '3', limit: '10' })
})
test('extracts all specified parameters including keyword', () => {
const result = extractQueryParams(['page', 'limit', 'keyword'])
expect(result).toEqual({ page: '3', limit: '10', keyword: 'test' })
})
test('ignores non-existent parameters', () => {
const result = extractQueryParams(['page', 'nonexistent'])
expect(result).toEqual({ page: '3' })
})
test('returns empty object when no parameters match', () => {
const result = extractQueryParams(['foo', 'bar'])
expect(result).toEqual({})
})
test('returns empty object for empty array', () => {
const result = extractQueryParams([])
expect(result).toEqual({})
})
test('handles empty query string', () => {
globalThis.window.location.search = ''
const result = extractQueryParams(['page', 'limit'])
expect(result).toEqual({})
})
})
/**
* Tests createNavigationPathWithParams which builds URLs with specific parameters
*/
describe('createNavigationPathWithParams', () => {
/**
* Tests URL construction with custom parameters
*/
test('creates path with specified parameters', () => {
const result = createNavigationPathWithParams('/datasets/123/documents', {
page: '1',
limit: '25',
})
expect(result).toBe('/datasets/123/documents?page=1&limit=25')
})
test('handles string and number values', () => {
const result = createNavigationPathWithParams('/datasets/123/documents', {
page: 1,
limit: 25,
keyword: 'search',
})
expect(result).toBe('/datasets/123/documents?page=1&limit=25&keyword=search')
})
test('filters out empty string values', () => {
const result = createNavigationPathWithParams('/datasets/123/documents', {
page: '1',
keyword: '',
})
expect(result).toBe('/datasets/123/documents?page=1')
})
test('filters out null and undefined values', () => {
const result = createNavigationPathWithParams('/datasets/123/documents', {
page: '1',
keyword: null as any,
filter: undefined as any,
})
expect(result).toBe('/datasets/123/documents?page=1')
})
test('returns base path when params are empty', () => {
const result = createNavigationPathWithParams('/datasets/123/documents', {})
expect(result).toBe('/datasets/123/documents')
})
test('encodes special characters in values', () => {
const result = createNavigationPathWithParams('/datasets/123/documents', {
keyword: 'search term',
})
expect(result).toBe('/datasets/123/documents?keyword=search+term')
})
})
/**
* Tests mergeQueryParams which combines new parameters with existing URL params
*/
describe('mergeQueryParams', () => {
/**
* Tests parameter merging and overriding
*/
test('merges new params with existing ones', () => {
const result = mergeQueryParams({ keyword: 'new', page: '1' })
expect(result.get('page')).toBe('1')
expect(result.get('limit')).toBe('10')
expect(result.get('keyword')).toBe('new')
})
test('overrides existing parameters', () => {
const result = mergeQueryParams({ page: '5' })
expect(result.get('page')).toBe('5')
expect(result.get('limit')).toBe('10')
})
test('adds new parameters', () => {
const result = mergeQueryParams({ filter: 'active' })
expect(result.get('filter')).toBe('active')
expect(result.get('page')).toBe('3')
})
test('removes parameters with null value', () => {
const result = mergeQueryParams({ page: null })
expect(result.get('page')).toBeNull()
expect(result.get('limit')).toBe('10')
})
test('removes parameters with undefined value', () => {
const result = mergeQueryParams({ page: undefined })
expect(result.get('page')).toBeNull()
expect(result.get('limit')).toBe('10')
})
test('does not preserve existing when preserveExisting is false', () => {
const result = mergeQueryParams({ filter: 'active' }, false)
expect(result.get('filter')).toBe('active')
expect(result.get('page')).toBeNull()
expect(result.get('limit')).toBeNull()
})
test('handles number values', () => {
const result = mergeQueryParams({ page: 5, limit: 20 })
expect(result.get('page')).toBe('5')
expect(result.get('limit')).toBe('20')
})
test('does not add empty string values', () => {
const result = mergeQueryParams({ newParam: '' })
expect(result.get('newParam')).toBeNull()
// Existing params are preserved
expect(result.get('keyword')).toBe('test')
})
})
/**
* Tests datasetNavigation helper object with common dataset navigation patterns
*/
describe('datasetNavigation', () => {
/**
* Tests navigation back to dataset documents list
*/
describe('backToDocuments', () => {
test('creates navigation function with preserved params', () => {
const mockRouter = { push: jest.fn() }
const backNav = datasetNavigation.backToDocuments(mockRouter, 'dataset-123')
backNav()
expect(mockRouter.push).toHaveBeenCalledWith('/datasets/dataset-123/documents?page=3&limit=10&keyword=test')
})
})
/**
* Tests navigation to document detail page
*/
describe('toDocumentDetail', () => {
test('creates navigation function to document detail', () => {
const mockRouter = { push: jest.fn() }
const navFunc = datasetNavigation.toDocumentDetail(mockRouter, 'dataset-123', 'doc-456')
navFunc()
expect(mockRouter.push).toHaveBeenCalledWith('/datasets/dataset-123/documents/doc-456')
})
})
/**
* Tests navigation to document settings page
*/
describe('toDocumentSettings', () => {
test('creates navigation function to document settings', () => {
const mockRouter = { push: jest.fn() }
const navFunc = datasetNavigation.toDocumentSettings(mockRouter, 'dataset-123', 'doc-456')
navFunc()
expect(mockRouter.push).toHaveBeenCalledWith('/datasets/dataset-123/documents/doc-456/settings')
})
})
})
})