diff --git a/docs/.vitepress/tableconfig-signatures.json b/docs/.vitepress/tableconfig-signatures.json index 7a572dcc..7ed2ddc9 100644 --- a/docs/.vitepress/tableconfig-signatures.json +++ b/docs/.vitepress/tableconfig-signatures.json @@ -42,7 +42,7 @@ { "name": "onReset", "signature": "onReset?: (context: TableContext) => Promise", - "comment": "/** Reset hook */" + "comment": "/** Hook called after reset completes (after goToFirst, cache clear, and autoInit). */" }, { "name": "strategies", diff --git a/docs/.vitepress/tableresult-signatures.json b/docs/.vitepress/tableresult-signatures.json index 811ff814..4c6bc541 100644 --- a/docs/.vitepress/tableresult-signatures.json +++ b/docs/.vitepress/tableresult-signatures.json @@ -77,7 +77,7 @@ { "name": "reset", "signature": "reset: () => Promise", - "comment": "/**\n* Resets the table state (clears cache, flags) and invokes the onReset strategy.\n*/" + "comment": "/**\n* Resets the table: calls goToFirst (if configured), clears cache, re-inits headers, then calls onReset.\n*/" }, { "name": "revalidate", diff --git a/src/typeContext.ts b/src/typeContext.ts index 2c48185a..3a21f32f 100644 --- a/src/typeContext.ts +++ b/src/typeContext.ts @@ -685,7 +685,7 @@ export interface TableConfig { autoScroll?: boolean; /** Debug options for development and troubleshooting */ debug?: DebugConfig; - /** Reset hook */ + /** Hook called after reset completes (after goToFirst, cache clear, and autoInit). */ onReset?: (context: TableContext) => Promise; /** All interaction strategies */ strategies?: TableStrategies; @@ -915,7 +915,7 @@ export interface TableResult extends AsyncIterable<{ row: SmartRow; /** - * Resets the table state (clears cache, flags) and invokes the onReset strategy. + * Resets the table: calls goToFirst (if configured), clears cache, re-inits headers, then calls onReset. */ reset: () => Promise; diff --git a/src/types.ts b/src/types.ts index a158d8b8..02627462 100644 --- a/src/types.ts +++ b/src/types.ts @@ -685,7 +685,7 @@ export interface TableConfig { autoScroll?: boolean; /** Debug options for development and troubleshooting */ debug?: DebugConfig; - /** Reset hook */ + /** Hook called after reset completes (after goToFirst, cache clear, and autoInit). */ onReset?: (context: TableContext) => Promise; /** All interaction strategies */ strategies?: TableStrategies; @@ -915,7 +915,7 @@ export interface TableResult extends AsyncIterable<{ row: SmartRow; /** - * Resets the table state (clears cache, flags) and invokes the onReset strategy. + * Resets the table: calls goToFirst (if configured), clears cache, re-inits headers, then calls onReset. */ reset: () => Promise; diff --git a/src/useTable.ts b/src/useTable.ts index bf619d1d..75a0c082 100644 --- a/src/useTable.ts +++ b/src/useTable.ts @@ -444,8 +444,6 @@ export const useTable = (rootLocator: Locator, configOptions: TableConf reset: async () => { log("Resetting table..."); - await config.onReset(createStrategyContext()); - if (config.strategies.pagination?.goToFirst) { log("Auto-navigating to first page..."); await config.strategies.pagination.goToFirst(createStrategyContext()); @@ -458,6 +456,7 @@ export const useTable = (rootLocator: Locator, configOptions: TableConf tableMapper.clear(); log("Table reset complete. Calling autoInit to restore state."); await _autoInit(); + await config.onReset(createStrategyContext()); }, revalidate: async () => { diff --git a/tests/edge-cases.spec.ts b/tests/edge-cases.spec.ts index 900fb989..22e4d7f8 100644 --- a/tests/edge-cases.spec.ts +++ b/tests/edge-cases.spec.ts @@ -251,6 +251,50 @@ test.describe('Edge cases and missing coverage', () => { await expect(page.locator('#page')).toHaveText('1'); }); + test('onReset runs after goToFirst and autoInit (#404)', async ({ page }) => { + await page.setContent(` + + + + + + + +
ID
1
2
+
1
+ + `); + + let pageTextDuringOnReset = ''; + const table = useTable(page.locator('#t'), { + strategies: { pagination: Strategies.Pagination.click({ next: '#next', first: '#first' }) }, + maxPages: 2, + onReset: async () => { + pageTextDuringOnReset = await page.locator('#page').textContent() ?? ''; + }, + }); + await table.init(); + await table.findRows({}); + expect(table.currentPageIndex).toBe(1); + await expect(page.locator('#page')).toHaveText('2'); + + await table.reset(); + expect(pageTextDuringOnReset).toBe('1'); + }); + test('sorting.getState throws when no sorting strategy configured', async ({ page }) => { await page.setContent(`
Name
Alice