Skip to content

Commit 64dc090

Browse files
committed
fix hook
1 parent c46fbaf commit 64dc090

3 files changed

Lines changed: 89 additions & 8 deletions

File tree

packages/jsapi-components/src/useSetPaddedViewportCallback.test.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import useSetPaddedViewportCallback from './useSetPaddedViewportCallback';
66

77
let table: dh.Table;
88
let viewportOptions: dh.ViewportSubscriptionOptions;
9+
let viewportOptionsMissingRows: Partial<dh.ViewportSubscriptionOptions>;
10+
let viewportOptionsMissingColumns: Partial<dh.ViewportSubscriptionOptions>;
11+
let viewportOptionsMissingBoth: Partial<dh.ViewportSubscriptionOptions>;
912
const viewportSize = 10;
1013
const viewportPadding = 4;
1114

@@ -19,6 +22,16 @@ beforeEach(() => {
1922
},
2023
columns: table.columns,
2124
};
25+
viewportOptionsMissingRows = {
26+
columns: table.columns,
27+
};
28+
viewportOptionsMissingColumns = {
29+
rows: {
30+
first: 5,
31+
last: 15,
32+
},
33+
};
34+
viewportOptionsMissingBoth = {};
2235
});
2336

2437
it('should create a callback that sets a padded viewport', () => {
@@ -89,6 +102,68 @@ it('should use TableViewportSubscription if viewport options are provided', () =
89102
expect(table.setViewport).not.toHaveBeenCalled();
90103
});
91104

105+
it('should fill missing rows and columns when creating a subscription', () => {
106+
jest.spyOn(TableUtils, 'isTreeTable').mockReturnValue(false);
107+
108+
const mockSubscription = {
109+
update: jest.fn(),
110+
close: jest.fn(),
111+
};
112+
113+
(table.createViewportSubscription as jest.Mock).mockReturnValue(
114+
mockSubscription
115+
);
116+
117+
const { result, rerender } = renderHook(
118+
options =>
119+
useSetPaddedViewportCallback(
120+
table,
121+
viewportSize,
122+
viewportPadding,
123+
options
124+
),
125+
{ initialProps: viewportOptionsMissingRows }
126+
);
127+
128+
const firstRow = 30;
129+
result.current(firstRow);
130+
131+
const expectedRows = {
132+
first: firstRow - viewportPadding,
133+
last: firstRow + viewportSize + viewportPadding - 1,
134+
};
135+
136+
expect(table.createViewportSubscription).toHaveBeenCalledWith({
137+
columns: table.columns,
138+
rows: expectedRows,
139+
});
140+
141+
jest.clearAllMocks();
142+
143+
rerender(viewportOptionsMissingColumns);
144+
result.current(firstRow + 5);
145+
146+
expect(table.createViewportSubscription).toHaveBeenCalledWith({
147+
rows: viewportOptionsMissingColumns.rows,
148+
columns: table.columns,
149+
});
150+
151+
jest.clearAllMocks();
152+
153+
rerender(viewportOptionsMissingBoth);
154+
result.current(firstRow + 10);
155+
156+
const expectedRows2 = {
157+
first: firstRow + 10 - viewportPadding,
158+
last: firstRow + 10 + viewportSize + viewportPadding - 1,
159+
};
160+
161+
expect(table.createViewportSubscription).toHaveBeenCalledWith({
162+
rows: expectedRows2,
163+
columns: table.columns,
164+
});
165+
});
166+
92167
it('should use setViewport if provided a tree table', () => {
93168
jest.spyOn(TableUtils, 'isTreeTable').mockReturnValue(true);
94169

packages/jsapi-components/src/useSetPaddedViewportCallback.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ import {
1616
* @param viewportPadding The padding to add before and after the viewport.
1717
* @param viewportSubscriptionOptions The viewport subscription options to use. If provided and
1818
* the table is not a `TreeTable`, the data will be requested using a `TableViewportSubscription`.
19+
* Rows and columns are filled in when the subscription is created if they are missing.
1920
* @returns A callback function for setting the viewport.
2021
*/
2122
export function useSetPaddedViewportCallback(
2223
table: dh.Table | dh.TreeTable | null,
2324
viewportSize: number,
2425
viewportPadding: number,
25-
viewportSubscriptionOptions: dh.ViewportSubscriptionOptions | null = null
26+
viewportSubscriptionOptions: Partial<dh.ViewportSubscriptionOptions> | null = null
2627
): (firstRow: number) => void {
2728
const subscriptionRef = useRef<dh.TableViewportSubscription | null>(null);
2829
const prevTableRef = useRef<dh.Table | dh.TreeTable | null>(null);
29-
const prevViewportOptionsRef = useRef<dh.ViewportSubscriptionOptions | null>(
30-
null
31-
);
30+
const prevViewportOptionsRef =
31+
useRef<Partial<dh.ViewportSubscriptionOptions> | null>(null);
3232

3333
const cleanupSubscription = () => {
3434
if (subscriptionRef.current) {
@@ -66,9 +66,14 @@ export function useSetPaddedViewportCallback(
6666
viewportSubscriptionOptions != null &&
6767
!TableUtils.isTreeTable(table)
6868
) {
69-
subscriptionRef.current = table.createViewportSubscription(
70-
viewportSubscriptionOptions
71-
);
69+
const subscriptionOptions: dh.ViewportSubscriptionOptions = {
70+
...viewportSubscriptionOptions,
71+
rows: viewportSubscriptionOptions.rows ?? { first, last },
72+
columns: viewportSubscriptionOptions.columns ?? table.columns,
73+
};
74+
75+
subscriptionRef.current =
76+
table.createViewportSubscription(subscriptionOptions);
7277
}
7378

7479
if (subscriptionRef.current == null) {

packages/jsapi-components/src/useViewportData.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export interface UseViewportDataProps<
3333
viewportPadding?: number;
3434
viewportSize?: number;
3535
deserializeRow?: RowDeserializer<TItem>;
36-
viewportSubscriptionOptions?: dh.ViewportSubscriptionOptions | null;
36+
viewportSubscriptionOptions?: Partial<dh.ViewportSubscriptionOptions> | null;
3737
}
3838

3939
export interface UseViewportDataResult<
@@ -71,6 +71,7 @@ export interface UseViewportDataResult<
7171
* @param reuseItemsOnTableResize If true, existing items will be re-used when
7272
* @param viewportSubscriptionOptions The viewport subscription options to use. If provided and
7373
* the table is not a `TreeTable`, the data will be requested using a `TableViewportSubscription`.
74+
* Rows and columns are filled in when the subscription is created if they are missing.
7475
* @returns An object for managing Table viewport state.
7576
*/
7677
export function useViewportData<TItem, TTable extends dh.Table | dh.TreeTable>({

0 commit comments

Comments
 (0)