Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/.vitepress/tableconfig-signatures.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
{
"name": "onReset",
"signature": "onReset?: (context: TableContext) => Promise<void>",
"comment": "/** Reset hook */"
"comment": "/** Hook called after reset completes (after goToFirst, cache clear, and autoInit). */"
},
{
"name": "strategies",
Expand Down
2 changes: 1 addition & 1 deletion docs/.vitepress/tableresult-signatures.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
{
"name": "reset",
"signature": "reset: () => Promise<void>",
"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",
Expand Down
4 changes: 2 additions & 2 deletions src/typeContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ export interface TableConfig<T = any> {
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<void>;
/** All interaction strategies */
strategies?: TableStrategies;
Expand Down Expand Up @@ -915,7 +915,7 @@ export interface TableResult<T = any> extends AsyncIterable<{ row: SmartRow<T>;


/**
* 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<void>;

Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ export interface TableConfig<T = any> {
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<void>;
/** All interaction strategies */
strategies?: TableStrategies;
Expand Down Expand Up @@ -915,7 +915,7 @@ export interface TableResult<T = any> extends AsyncIterable<{ row: SmartRow<T>;


/**
* 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<void>;

Expand Down
3 changes: 1 addition & 2 deletions src/useTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,6 @@ export const useTable = <T = any>(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());
Expand All @@ -458,6 +456,7 @@ export const useTable = <T = any>(rootLocator: Locator, configOptions: TableConf
tableMapper.clear();
log("Table reset complete. Calling autoInit to restore state.");
await _autoInit();
await config.onReset(createStrategyContext());
Comment thread
coderabbitai[bot] marked this conversation as resolved.
},

revalidate: async () => {
Expand Down
44 changes: 44 additions & 0 deletions tests/edge-cases.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
<table id="t">
<thead><tr><th>ID</th></tr></thead>
<tbody id="tbody">
<tr><td>1</td></tr>
<tr><td>2</td></tr>
</tbody>
<tfoot><tr><td><button id="first">First</button><button id="next">Next</button></td></tr></tfoot>
</table>
<div id="page">1</div>
<script>
let p = 1;
function render() {
if (p === 1) {
document.getElementById('tbody').innerHTML = '<tr><td>1</td></tr><tr><td>2</td></tr>';
document.getElementById('page').textContent = '1';
} else {
document.getElementById('tbody').innerHTML = '<tr><td>3</td></tr><tr><td>4</td></tr>';
document.getElementById('page').textContent = '2';
}
}
document.getElementById('next').onclick = () => { if (p === 1) { p = 2; render(); } };
document.getElementById('first').onclick = () => { if (p === 2) { p = 1; render(); } };
</script>
`);

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(`
<table id="t"><thead><tr><th>Name</th></tr></thead><tbody><tr><td>Alice</td></tr></tbody></table>
Expand Down
Loading