You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

navigation-utils.test.ts 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**
  2. * Navigation Utilities Test
  3. *
  4. * Tests for the navigation utility functions to ensure they handle
  5. * query parameter preservation correctly across different scenarios.
  6. */
  7. import {
  8. createBackNavigation,
  9. createNavigationPath,
  10. createNavigationPathWithParams,
  11. datasetNavigation,
  12. extractQueryParams,
  13. mergeQueryParams,
  14. } from '@/utils/navigation'
  15. // Mock router for testing
  16. const mockPush = jest.fn()
  17. const mockRouter = { push: mockPush }
  18. describe('Navigation Utilities', () => {
  19. beforeEach(() => {
  20. jest.clearAllMocks()
  21. })
  22. describe('createNavigationPath', () => {
  23. test('preserves query parameters by default', () => {
  24. Object.defineProperty(window, 'location', {
  25. value: { search: '?page=3&limit=10&keyword=test' },
  26. writable: true,
  27. })
  28. const path = createNavigationPath('/datasets/123/documents')
  29. expect(path).toBe('/datasets/123/documents?page=3&limit=10&keyword=test')
  30. })
  31. test('returns clean path when preserveParams is false', () => {
  32. Object.defineProperty(window, 'location', {
  33. value: { search: '?page=3&limit=10' },
  34. writable: true,
  35. })
  36. const path = createNavigationPath('/datasets/123/documents', false)
  37. expect(path).toBe('/datasets/123/documents')
  38. })
  39. test('handles empty query parameters', () => {
  40. Object.defineProperty(window, 'location', {
  41. value: { search: '' },
  42. writable: true,
  43. })
  44. const path = createNavigationPath('/datasets/123/documents')
  45. expect(path).toBe('/datasets/123/documents')
  46. })
  47. test('handles errors gracefully', () => {
  48. // Mock window.location to throw an error
  49. Object.defineProperty(window, 'location', {
  50. get: () => {
  51. throw new Error('Location access denied')
  52. },
  53. configurable: true,
  54. })
  55. const consoleSpy = jest.spyOn(console, 'warn').mockImplementation()
  56. const path = createNavigationPath('/datasets/123/documents')
  57. expect(path).toBe('/datasets/123/documents')
  58. expect(consoleSpy).toHaveBeenCalledWith('Failed to preserve query parameters:', expect.any(Error))
  59. consoleSpy.mockRestore()
  60. })
  61. })
  62. describe('createBackNavigation', () => {
  63. test('creates function that navigates with preserved params', () => {
  64. Object.defineProperty(window, 'location', {
  65. value: { search: '?page=2&limit=25' },
  66. writable: true,
  67. })
  68. const backFn = createBackNavigation(mockRouter, '/datasets/123/documents')
  69. backFn()
  70. expect(mockPush).toHaveBeenCalledWith('/datasets/123/documents?page=2&limit=25')
  71. })
  72. test('creates function that navigates without params when specified', () => {
  73. Object.defineProperty(window, 'location', {
  74. value: { search: '?page=2&limit=25' },
  75. writable: true,
  76. })
  77. const backFn = createBackNavigation(mockRouter, '/datasets/123/documents', false)
  78. backFn()
  79. expect(mockPush).toHaveBeenCalledWith('/datasets/123/documents')
  80. })
  81. })
  82. describe('extractQueryParams', () => {
  83. test('extracts specified parameters', () => {
  84. Object.defineProperty(window, 'location', {
  85. value: { search: '?page=3&limit=10&keyword=test&other=value' },
  86. writable: true,
  87. })
  88. const params = extractQueryParams(['page', 'limit', 'keyword'])
  89. expect(params).toEqual({
  90. page: '3',
  91. limit: '10',
  92. keyword: 'test',
  93. })
  94. })
  95. test('handles missing parameters', () => {
  96. Object.defineProperty(window, 'location', {
  97. value: { search: '?page=3' },
  98. writable: true,
  99. })
  100. const params = extractQueryParams(['page', 'limit', 'missing'])
  101. expect(params).toEqual({
  102. page: '3',
  103. })
  104. })
  105. test('handles errors gracefully', () => {
  106. Object.defineProperty(window, 'location', {
  107. get: () => {
  108. throw new Error('Location access denied')
  109. },
  110. configurable: true,
  111. })
  112. const consoleSpy = jest.spyOn(console, 'warn').mockImplementation()
  113. const params = extractQueryParams(['page', 'limit'])
  114. expect(params).toEqual({})
  115. expect(consoleSpy).toHaveBeenCalledWith('Failed to extract query parameters:', expect.any(Error))
  116. consoleSpy.mockRestore()
  117. })
  118. })
  119. describe('createNavigationPathWithParams', () => {
  120. test('creates path with specified parameters', () => {
  121. const path = createNavigationPathWithParams('/datasets/123/documents', {
  122. page: 1,
  123. limit: 25,
  124. keyword: 'search term',
  125. })
  126. expect(path).toBe('/datasets/123/documents?page=1&limit=25&keyword=search+term')
  127. })
  128. test('filters out empty values', () => {
  129. const path = createNavigationPathWithParams('/datasets/123/documents', {
  130. page: 1,
  131. limit: '',
  132. keyword: 'test',
  133. empty: null,
  134. undefined,
  135. })
  136. expect(path).toBe('/datasets/123/documents?page=1&keyword=test')
  137. })
  138. test('handles errors gracefully', () => {
  139. // Mock URLSearchParams to throw an error
  140. const originalURLSearchParams = globalThis.URLSearchParams
  141. globalThis.URLSearchParams = jest.fn(() => {
  142. throw new Error('URLSearchParams error')
  143. }) as any
  144. const consoleSpy = jest.spyOn(console, 'warn').mockImplementation()
  145. const path = createNavigationPathWithParams('/datasets/123/documents', { page: 1 })
  146. expect(path).toBe('/datasets/123/documents')
  147. expect(consoleSpy).toHaveBeenCalledWith('Failed to create navigation path with params:', expect.any(Error))
  148. consoleSpy.mockRestore()
  149. globalThis.URLSearchParams = originalURLSearchParams
  150. })
  151. })
  152. describe('mergeQueryParams', () => {
  153. test('merges new params with existing ones', () => {
  154. Object.defineProperty(window, 'location', {
  155. value: { search: '?page=3&limit=10' },
  156. writable: true,
  157. })
  158. const merged = mergeQueryParams({ keyword: 'test', page: '1' })
  159. const result = merged.toString()
  160. expect(result).toContain('page=1') // overridden
  161. expect(result).toContain('limit=10') // preserved
  162. expect(result).toContain('keyword=test') // added
  163. })
  164. test('removes parameters when value is null', () => {
  165. Object.defineProperty(window, 'location', {
  166. value: { search: '?page=3&limit=10&keyword=test' },
  167. writable: true,
  168. })
  169. const merged = mergeQueryParams({ keyword: null, filter: 'active' })
  170. const result = merged.toString()
  171. expect(result).toContain('page=3')
  172. expect(result).toContain('limit=10')
  173. expect(result).not.toContain('keyword')
  174. expect(result).toContain('filter=active')
  175. })
  176. test('creates fresh params when preserveExisting is false', () => {
  177. Object.defineProperty(window, 'location', {
  178. value: { search: '?page=3&limit=10' },
  179. writable: true,
  180. })
  181. const merged = mergeQueryParams({ keyword: 'test' }, false)
  182. const result = merged.toString()
  183. expect(result).toBe('keyword=test')
  184. })
  185. })
  186. describe('datasetNavigation', () => {
  187. test('backToDocuments creates correct navigation function', () => {
  188. Object.defineProperty(window, 'location', {
  189. value: { search: '?page=2&limit=25' },
  190. writable: true,
  191. })
  192. const backFn = datasetNavigation.backToDocuments(mockRouter, 'dataset-123')
  193. backFn()
  194. expect(mockPush).toHaveBeenCalledWith('/datasets/dataset-123/documents?page=2&limit=25')
  195. })
  196. test('toDocumentDetail creates correct navigation function', () => {
  197. const detailFn = datasetNavigation.toDocumentDetail(mockRouter, 'dataset-123', 'doc-456')
  198. detailFn()
  199. expect(mockPush).toHaveBeenCalledWith('/datasets/dataset-123/documents/doc-456')
  200. })
  201. test('toDocumentSettings creates correct navigation function', () => {
  202. const settingsFn = datasetNavigation.toDocumentSettings(mockRouter, 'dataset-123', 'doc-456')
  203. settingsFn()
  204. expect(mockPush).toHaveBeenCalledWith('/datasets/dataset-123/documents/doc-456/settings')
  205. })
  206. })
  207. describe('Real-world Integration Scenarios', () => {
  208. test('complete user workflow: list -> detail -> back', () => {
  209. // User starts on page 3 with search
  210. Object.defineProperty(window, 'location', {
  211. value: { search: '?page=3&keyword=API&limit=25' },
  212. writable: true,
  213. })
  214. // Create back navigation function (as would be done in detail component)
  215. const backToDocuments = datasetNavigation.backToDocuments(mockRouter, 'main-dataset')
  216. // User clicks back
  217. backToDocuments()
  218. // Should return to exact same list state
  219. expect(mockPush).toHaveBeenCalledWith('/datasets/main-dataset/documents?page=3&keyword=API&limit=25')
  220. })
  221. test('user applies filters then views document', () => {
  222. // Complex filter state
  223. Object.defineProperty(window, 'location', {
  224. value: { search: '?page=1&limit=50&status=active&type=pdf&sort=created_at&order=desc' },
  225. writable: true,
  226. })
  227. const backFn = createBackNavigation(mockRouter, '/datasets/filtered-set/documents')
  228. backFn()
  229. expect(mockPush).toHaveBeenCalledWith('/datasets/filtered-set/documents?page=1&limit=50&status=active&type=pdf&sort=created_at&order=desc')
  230. })
  231. })
  232. })