-
Notifications
You must be signed in to change notification settings - Fork 0
β‘ Optimize useDataTableUrlState with memoization #182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
|
|
||
| import { performance } from 'perf_hooks'; | ||
| import { dataTableRouterParsers } from '../src/remix-hook-form/data-table-router-parsers'; | ||
|
|
||
| const ITERATIONS = 10000; | ||
|
|
||
| // Create a complex filter state serialized to JSON | ||
| const complexFilters = [ | ||
| { columnId: 'name', type: 'text', operator: 'contains', values: ['John'] }, | ||
| { columnId: 'age', type: 'number', operator: 'between', values: [20, 30] }, | ||
| { columnId: 'status', type: 'select', operator: 'isAnyOf', values: ['active', 'pending'] }, | ||
| { columnId: 'role', type: 'text', operator: 'equals', values: ['admin'] }, | ||
| { columnId: 'department', type: 'text', operator: 'contains', values: ['engineering'] }, | ||
| ]; | ||
|
|
||
| const serializedFilters = JSON.stringify(complexFilters); | ||
|
|
||
| function runBenchmark() { | ||
| console.log('Running benchmark...'); | ||
| const start = performance.now(); | ||
|
|
||
| for (let i = 0; i < ITERATIONS; i++) { | ||
| // Simulate the parsing operation that happens in the hook | ||
| dataTableRouterParsers.filters.parse(serializedFilters); | ||
| } | ||
|
|
||
| const end = performance.now(); | ||
| const totalTime = end - start; | ||
| const avgTime = totalTime / ITERATIONS; | ||
|
|
||
| console.log(`Total time for ${ITERATIONS} iterations: ${totalTime.toFixed(2)}ms`); | ||
| console.log(`Average time per parse: ${avgTime.toFixed(4)}ms`); | ||
| } | ||
|
|
||
| runBenchmark(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { renderHook, act } from '@testing-library/react'; | ||
| import { MemoryRouter, useLocation } from 'react-router'; | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { useDataTableUrlState } from './use-data-table-url-state'; | ||
| import React from 'react'; | ||
|
|
||
| describe('useDataTableUrlState', () => { | ||
| it('should parse initial state from URL', () => { | ||
| const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
| <MemoryRouter initialEntries={['/?search=initial&page=2&pageSize=20']}> | ||
| {children} | ||
| </MemoryRouter> | ||
| ); | ||
|
|
||
| const { result } = renderHook(() => useDataTableUrlState(), { wrapper }); | ||
|
|
||
| expect(result.current.urlState.search).toBe('initial'); | ||
| expect(result.current.urlState.page).toBe(2); | ||
| expect(result.current.urlState.pageSize).toBe(20); | ||
| }); | ||
|
|
||
| it('should update URL state', () => { | ||
| let testLocation: any; | ||
|
|
||
| const LocationSpy = () => { | ||
| testLocation = useLocation(); | ||
| return null; | ||
| }; | ||
|
|
||
| const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
| <MemoryRouter initialEntries={['/']}> | ||
| <LocationSpy /> | ||
| {children} | ||
| </MemoryRouter> | ||
| ); | ||
|
|
||
| const { result } = renderHook(() => useDataTableUrlState(), { wrapper }); | ||
|
|
||
| act(() => { | ||
| result.current.setUrlState({ search: 'updated' }); | ||
| }); | ||
|
|
||
| expect(result.current.urlState.search).toBe('updated'); | ||
| expect(testLocation.search).toContain('search=updated'); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| import '@testing-library/jest-dom'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,9 @@ export default defineConfig({ | |
| .sync('src/**/*.{ts,tsx}', { | ||
| ignore: [ | ||
| 'src/**/*.d.ts', | ||
| 'src/**/*.test.tsx', | ||
| 'src/**/*.test.ts', | ||
| 'src/**/test/**', | ||
|
Comment on lines
+31
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π οΈ Refactor suggestion | π Major Good addition β also align the The glob ignore list correctly excludes test files from build entries. However, the Align dts excludes dts({
include: ['src'],
- exclude: ['**/*.stories.tsx', '**/*.test.tsx'],
+ exclude: ['**/*.stories.tsx', '**/*.test.tsx', '**/*.test.ts', '**/test/**'],
}),π€ Prompt for AI Agents |
||
| 'src/**/core/types.ts', // Exclude type-only files to avoid empty chunks | ||
| ], | ||
| }) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| /// <reference types="vitest" /> | ||
| import { defineConfig } from 'vite'; | ||
| import react from '@vitejs/plugin-react'; | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [react()], | ||
| test: { | ||
| globals: true, | ||
| environment: 'jsdom', | ||
| setupFiles: ['./src/test/setup.ts'], | ||
| }, | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.