@rickcedwhat/playwright-smart-table 3.2.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/types.d.ts CHANGED
@@ -1,18 +1,46 @@
1
1
  import type { Locator, Page } from '@playwright/test';
2
2
  export type Selector = string | ((root: Locator | Page) => Locator);
3
- export type SmartRow = Locator & {
3
+ /**
4
+ * Function to get a cell locator given row, column info.
5
+ * Replaces the old cellResolver.
6
+ */
7
+ export type GetCellLocatorFn = (args: {
8
+ row: Locator;
9
+ columnName: string;
10
+ columnIndex: number;
11
+ rowIndex?: number;
12
+ page: Page;
13
+ }) => Locator;
14
+ /**
15
+ * Function to get the currently active/focused cell.
16
+ * Returns null if no cell is active.
17
+ */
18
+ export type GetActiveCellFn = (args: TableContext) => Promise<{
19
+ rowIndex: number;
20
+ columnIndex: number;
21
+ columnName?: string;
22
+ locator: Locator;
23
+ } | null>;
24
+ export type SmartRow<T = any> = Locator & {
4
25
  getRequestIndex(): number | undefined;
5
26
  rowIndex?: number;
6
27
  getCell(column: string): Locator;
7
- toJSON(): Promise<Record<string, string>>;
28
+ toJSON(options?: {
29
+ columns?: string[];
30
+ }): Promise<T>;
31
+ /**
32
+ * Scrolls/paginates to bring this row into view.
33
+ * Only works if rowIndex is known.
34
+ */
35
+ bringIntoView(): Promise<void>;
8
36
  /**
9
- * Fills the row with data. Automatically detects input types (text input, select, checkbox, etc.).
37
+ * Fills the row with data. Automatically detects input types.
10
38
  */
11
- fill: (data: Record<string, any>, options?: FillOptions) => Promise<void>;
39
+ fill: (data: Partial<T> | Record<string, any>, options?: FillOptions) => Promise<void>;
12
40
  /**
13
41
  * Alias for fill() to avoid conflict with Locator.fill()
14
42
  */
15
- smartFill: (data: Record<string, any>, options?: FillOptions) => Promise<void>;
43
+ smartFill: (data: Partial<T> | Record<string, any>, options?: FillOptions) => Promise<void>;
16
44
  };
17
45
  export type StrategyContext = TableContext & {
18
46
  rowLocator?: Locator;
@@ -66,9 +94,46 @@ export type FillStrategy = (options: {
66
94
  fillOptions?: FillOptions;
67
95
  }) => Promise<void>;
68
96
  export type { HeaderStrategy } from './strategies/headers';
69
- export type { ColumnStrategy } from './strategies/columns';
97
+ export type { CellNavigationStrategy } from './strategies/columns';
70
98
  import { HeaderStrategy } from './strategies/headers';
71
- import { ColumnStrategy } from './strategies/columns';
99
+ import { CellNavigationStrategy } from './strategies/columns';
100
+ /**
101
+ * Strategy to resolve column names (string or regex) to their index.
102
+ */
103
+ export type { ColumnResolutionStrategy } from './strategies/resolution';
104
+ /**
105
+ * Strategy to filter rows based on criteria.
106
+ */
107
+ export interface FilterStrategy {
108
+ apply(options: {
109
+ rows: Locator;
110
+ filter: {
111
+ column: string;
112
+ value: string | RegExp | number;
113
+ };
114
+ colIndex: number;
115
+ tableContext: TableContext;
116
+ }): Locator;
117
+ }
118
+ /**
119
+ * Organized container for all table interaction strategies.
120
+ */
121
+ export interface TableStrategies {
122
+ /** Strategy for discovering/scanning headers */
123
+ header?: HeaderStrategy;
124
+ /** Strategy for navigating to specific cells (row + column) */
125
+ cellNavigation?: CellNavigationStrategy;
126
+ /** Strategy for filling form inputs */
127
+ fill?: FillStrategy;
128
+ /** Strategy for paginating through data */
129
+ pagination?: PaginationStrategy;
130
+ /** Strategy for sorting columns */
131
+ sorting?: SortingStrategy;
132
+ /** Function to get a cell locator */
133
+ getCellLocator?: GetCellLocatorFn;
134
+ /** Function to get the currently active/focused cell */
135
+ getActiveCell?: GetActiveCellFn;
136
+ }
72
137
  /**
73
138
  * Configuration options for useTable.
74
139
  */
@@ -79,21 +144,9 @@ export interface TableConfig {
79
144
  rowSelector?: string;
80
145
  /** Selector for the cells within a row */
81
146
  cellSelector?: string;
82
- /** Strategy for filling forms within the table */
83
- fillStrategy?: FillStrategy;
84
- /** Strategy for discovering headers */
85
- headerStrategy?: HeaderStrategy;
86
- /** Strategy for navigating to columns */
87
- columnStrategy?: ColumnStrategy;
88
147
  /** Number of pages to scan for verification */
89
148
  maxPages?: number;
90
- /** Pagination Strategy */
91
- pagination?: PaginationStrategy;
92
- /** Sorting Strategy */
93
- sorting?: SortingStrategy;
94
- /**
95
- * Hook to rename columns dynamically.
96
- */
149
+ /** Hook to rename columns dynamically */
97
150
  headerTransformer?: (args: {
98
151
  text: string;
99
152
  index: number;
@@ -105,27 +158,14 @@ export interface TableConfig {
105
158
  debug?: boolean;
106
159
  /** Reset hook */
107
160
  onReset?: (context: TableContext) => Promise<void>;
108
- /**
109
- * Custom resolver for finding a cell.
110
- * Overrides cellSelector logic if provided.
111
- * Useful for virtualized tables where nth() index doesn't match DOM index.
112
- */
113
- cellResolver?: (args: {
114
- row: Locator;
115
- columnName: string;
116
- columnIndex: number;
117
- rowIndex?: number;
118
- }) => Locator;
161
+ /** All interaction strategies */
162
+ strategies?: TableStrategies;
119
163
  }
120
164
  export interface FinalTableConfig extends TableConfig {
121
165
  headerSelector: string;
122
166
  rowSelector: string;
123
167
  cellSelector: string;
124
- fillStrategy: FillStrategy;
125
- headerStrategy: HeaderStrategy;
126
- columnStrategy: ColumnStrategy;
127
168
  maxPages: number;
128
- pagination: PaginationStrategy;
129
169
  autoScroll: boolean;
130
170
  debug: boolean;
131
171
  headerTransformer: (args: {
@@ -134,12 +174,7 @@ export interface FinalTableConfig extends TableConfig {
134
174
  locator: Locator;
135
175
  }) => string | Promise<string>;
136
176
  onReset: (context: TableContext) => Promise<void>;
137
- cellResolver?: (args: {
138
- row: Locator;
139
- columnName: string;
140
- columnIndex: number;
141
- rowIndex?: number;
142
- }) => Locator;
177
+ strategies: TableStrategies;
143
178
  }
144
179
  export interface FillOptions {
145
180
  /**
@@ -148,7 +183,7 @@ export interface FillOptions {
148
183
  */
149
184
  inputMappers?: Record<string, (cell: Locator) => Locator>;
150
185
  }
151
- export interface TableResult {
186
+ export interface TableResult<T = any> {
152
187
  /**
153
188
  * Initializes the table by resolving headers. Must be called before using sync methods.
154
189
  * @param options Optional timeout for header resolution (default: 3000ms)
@@ -159,15 +194,21 @@ export interface TableResult {
159
194
  getHeaders: () => Promise<string[]>;
160
195
  getHeaderCell: (columnName: string) => Promise<Locator>;
161
196
  /**
162
- * Finds a row on the current page only. Returns immediately (sync).
197
+ * Finds a row by filters on the current page only. Returns immediately (sync).
163
198
  * Throws error if table is not initialized.
164
199
  */
165
- getByRow: {
166
- (index: number): SmartRow;
167
- (filters: Record<string, string | RegExp | number>, options?: {
168
- exact?: boolean;
169
- }): SmartRow;
170
- };
200
+ getByRow: (filters: Record<string, string | RegExp | number>, options?: {
201
+ exact?: boolean;
202
+ }) => SmartRow;
203
+ /**
204
+ * Gets a row by 1-based index on the current page.
205
+ * Throws error if table is not initialized.
206
+ * @param index 1-based row index
207
+ * @param options Optional settings including bringIntoView
208
+ */
209
+ getByRowIndex: (index: number, options?: {
210
+ bringIntoView?: boolean;
211
+ }) => SmartRow;
171
212
  /**
172
213
  * Searches for a row across all available data using the configured strategy (pagination, scroll, etc.).
173
214
  * Auto-initializes if needed.
@@ -177,7 +218,7 @@ export interface TableResult {
177
218
  maxPages?: number;
178
219
  }) => Promise<SmartRow>;
179
220
  /**
180
- * Manually scrolls to a column using the configured ColumnStrategy.
221
+ * Navigates to a specific column using the configured CellNavigationStrategy.
181
222
  */
182
223
  scrollToColumn: (columnName: string) => Promise<void>;
183
224
  getAllCurrentRows: <T extends {
@@ -201,6 +242,11 @@ export interface TableResult {
201
242
  * Resets the table state (clears cache, flags) and invokes the onReset strategy.
202
243
  */
203
244
  reset: () => Promise<void>;
245
+ /**
246
+ * Revalidates the table's structure (headers, columns) without resetting pagination or state.
247
+ * Useful when columns change visibility or order dynamically.
248
+ */
249
+ revalidate: () => Promise<void>;
204
250
  /**
205
251
  * Scans a specific column across all pages and returns the values.
206
252
  */
@@ -262,4 +308,4 @@ export interface TableResult {
262
308
  /**
263
309
  * Restricted table result that excludes methods that shouldn't be called during iteration.
264
310
  */
265
- export type RestrictedTableResult = Omit<TableResult, 'searchForRow' | 'iterateThroughTable' | 'reset' | 'getAllRows'>;
311
+ export type RestrictedTableResult<T = any> = Omit<TableResult<T>, 'searchForRow' | 'iterateThroughTable' | 'reset' | 'getAllRows'>;
@@ -2,17 +2,20 @@ import type { Locator } from '@playwright/test';
2
2
  import { TableConfig, Selector, TableResult, PaginationStrategy } from './types';
3
3
  import { FillStrategies } from './strategies/fill';
4
4
  import { HeaderStrategies } from './strategies/headers';
5
- import { ColumnStrategies } from './strategies/columns';
5
+ import { CellNavigationStrategies, ColumnStrategies } from './strategies/columns';
6
+ import { ResolutionStrategies } from './strategies/resolution';
7
+ import { Strategies } from './strategies';
6
8
  /**
7
9
  * Main hook to interact with a table.
8
10
  */
9
- export declare const useTable: (rootLocator: Locator, configOptions?: TableConfig) => TableResult;
11
+ export declare const useTable: <T = any>(rootLocator: Locator, configOptions?: TableConfig) => TableResult<T>;
10
12
  export declare const PaginationStrategies: {
11
13
  clickNext: (nextButtonSelector: Selector, timeout?: number) => PaginationStrategy;
12
14
  clickLoadMore: (buttonSelector: Selector, timeout?: number) => PaginationStrategy;
13
15
  infiniteScroll: (timeout?: number) => PaginationStrategy;
14
16
  };
15
- export declare const TableStrategies: {
17
+ /** @deprecated Use Strategies.Pagination instead */
18
+ export declare const DeprecatedTableStrategies: {
16
19
  clickNext: (nextButtonSelector: Selector, timeout?: number) => PaginationStrategy;
17
20
  clickLoadMore: (buttonSelector: Selector, timeout?: number) => PaginationStrategy;
18
21
  infiniteScroll: (timeout?: number) => PaginationStrategy;
@@ -20,4 +23,4 @@ export declare const TableStrategies: {
20
23
  export declare const SortingStrategies: {
21
24
  AriaSort: () => import("./types").SortingStrategy;
22
25
  };
23
- export { FillStrategies, HeaderStrategies, ColumnStrategies };
26
+ export { FillStrategies, HeaderStrategies, CellNavigationStrategies, ColumnStrategies, ResolutionStrategies, Strategies };