Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jassmith committed Jun 21, 2022
1 parent 85056c9 commit dae3954
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
13 changes: 9 additions & 4 deletions packages/core/src/data-editor/use-column-sizer.test.tsx
Expand Up @@ -80,7 +80,7 @@ const abortController = new AbortController();
describe("use-column-sizer", () => {
it("Measures a simple cell", async () => {
const { result } = renderHook(() =>
useColumnSizer(COLUMNS, 1000, getShortCellsForSelection, 20, 500, theme, abortController)
useColumnSizer(COLUMNS, 1000, getShortCellsForSelection, 400, 20, 500, theme, abortController)
);

const columnA = result.current.find(col => col.title === "A");
Expand All @@ -95,7 +95,9 @@ describe("use-column-sizer", () => {
});

it("Measures the last row", async () => {
renderHook(() => useColumnSizer(COLUMNS, 1000, getShortCellsForSelection, 20, 500, theme, abortController));
renderHook(() =>
useColumnSizer(COLUMNS, 1000, getShortCellsForSelection, 400, 20, 500, theme, abortController)
);

expect(getShortCellsForSelection).toBeCalledTimes(2);
expect(getShortCellsForSelection).toHaveBeenNthCalledWith(
Expand All @@ -122,7 +124,7 @@ describe("use-column-sizer", () => {
it("Measures new columns when they arrive, doesn't re-measure existing ones", async () => {
const { result, rerender } = renderHook(
({ getCellsForSelection, columns }) =>
useColumnSizer(columns, 1000, getCellsForSelection, 20, 500, theme, abortController),
useColumnSizer(columns, 1000, getCellsForSelection, 400, 20, 500, theme, abortController),
{
initialProps: {
getCellsForSelection: getShortCellsForSelection,
Expand Down Expand Up @@ -164,7 +166,9 @@ describe("use-column-sizer", () => {
});

it("Returns the default sizes if getCellsForSelection is not provided", async () => {
const { result } = renderHook(() => useColumnSizer(COLUMNS, 1000, undefined, 20, 500, theme, abortController));
const { result } = renderHook(() =>
useColumnSizer(COLUMNS, 1000, undefined, 400, 20, 500, theme, abortController)
);

const columnA = result.current.find(col => col.title === "A");
const columnB = result.current.find(col => col.title === "B");
Expand All @@ -183,6 +187,7 @@ describe("use-column-sizer", () => {
A_BUNCH_OF_COLUMNS_THAT_ALREADY_HAVE_SIZES_WE_DONT_WANT_TO_MEASURE_THESE,
1000,
undefined,
400,
50,
500,
theme,
Expand Down
22 changes: 12 additions & 10 deletions packages/core/src/data-editor/use-column-sizer.ts
Expand Up @@ -122,9 +122,9 @@ export function useColumnSizer(
}, [abortController.signal, columns]);

return React.useMemo(() => {
const getRaw = (): SizedGridColumn[] => {
const getRaw = () => {
if (columns.every(isSizedGridColumn)) {
return [...columns];
return columns;
}

if (ctx === null) {
Expand Down Expand Up @@ -163,33 +163,35 @@ export function useColumnSizer(
});
};

const raw = getRaw();
let result = getRaw();
let totalWidth = 0;
let totalGrow = 0;
const distribute: number[] = [];
for (let i = 0; i < raw.length; i++) {
const c = raw[i];
for (let i = 0; i < result.length; i++) {
const c = result[i];
totalWidth += c.width;
if (c.grow !== undefined && c.grow > 0) {
totalGrow += c.grow;
distribute.push(i);
}
}
if (totalWidth < clientWidth && distribute.length > 0) {
const writeable = [...result];
const extra = clientWidth - totalWidth;
let remaining = extra;
for (let di = 0; di < distribute.length; di++) {
const i = distribute[di];
const weighted = (raw[i].grow ?? 0) / totalGrow;
const weighted = (result[i].grow ?? 0) / totalGrow;
const toAdd =
di === distribute.length - 1 ? remaining : Math.min(remaining, Math.floor(extra * weighted));
raw[i] = {
...raw[i],
width: raw[i].width + toAdd,
writeable[i] = {
...result[i],
width: result[i].width + toAdd,
};
remaining -= toAdd;
}
result = writeable;
}
return raw;
return result;
}, [columns, ctx, clientWidth, maxColumnWidth, minColumnWidth, selectedData]);
}

0 comments on commit dae3954

Please sign in to comment.