Skip to content

Commit

Permalink
computePanelFlexBoxStyle uses toPrecision for default sizes as well (…
Browse files Browse the repository at this point in the history
…to avoid floating point precision errors)
  • Loading branch information
bvaughn committed Feb 13, 2024
1 parent 6d9f1b3 commit a45ab6c
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { PanelConstraints, PanelData } from "../Panel";
import { computePanelFlexBoxStyle } from "./computePanelFlexBoxStyle";

describe("computePanelFlexBoxStyle", () => {
function createPanelData(constraints: PanelConstraints = {}): PanelData {
return {
callbacks: {},
constraints,
id: "fake",
idIsFromProps: false,
order: undefined,
};
}

it("should observe a panel's default size if group layout has not yet been computed", () => {
expect(
computePanelFlexBoxStyle({
defaultSize: 0.1233456789,
dragState: null,
layout: [],
panelData: [
createPanelData({
defaultSize: 0.1233456789,
}),
createPanelData(),
],
panelIndex: 0,
precision: 2,
})
).toMatchInlineSnapshot(`
{
"flexBasis": 0,
"flexGrow": "0.12",
"flexShrink": 1,
"overflow": "hidden",
"pointerEvents": undefined,
}
`);
});

it("should always fill the full width for single-panel groups", () => {
expect(
computePanelFlexBoxStyle({
defaultSize: undefined,
dragState: null,
layout: [],
panelData: [createPanelData()],
panelIndex: 0,
precision: 2,
})
).toMatchInlineSnapshot(`
{
"flexBasis": 0,
"flexGrow": "1",
"flexShrink": 1,
"overflow": "hidden",
"pointerEvents": undefined,
}
`);
});

it("should round sizes to avoid floating point precision errors", () => {
const layout = [0.25435, 0.5758, 0.1698];
const panelData = [createPanelData(), createPanelData(), createPanelData()];

expect(
computePanelFlexBoxStyle({
defaultSize: undefined,
dragState: null,
layout,
panelData,
panelIndex: 0,
precision: 2,
})
).toMatchInlineSnapshot(`
{
"flexBasis": 0,
"flexGrow": "0.25",
"flexShrink": 1,
"overflow": "hidden",
"pointerEvents": undefined,
}
`);

expect(
computePanelFlexBoxStyle({
defaultSize: undefined,
dragState: null,
layout,
panelData,
panelIndex: 1,
precision: 2,
})
).toMatchInlineSnapshot(`
{
"flexBasis": 0,
"flexGrow": "0.58",
"flexShrink": 1,
"overflow": "hidden",
"pointerEvents": undefined,
}
`);

expect(
computePanelFlexBoxStyle({
defaultSize: undefined,
dragState: null,
layout,
panelData,
panelIndex: 2,
precision: 2,
})
).toMatchInlineSnapshot(`
{
"flexBasis": 0,
"flexGrow": "0.17",
"flexShrink": 1,
"overflow": "hidden",
"pointerEvents": undefined,
}
`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export function computePanelFlexBoxStyle({
if (size == null) {
// Initial render (before panels have registered themselves)
// In order to support server rendering, fall back to default size if provided
flexGrow = defaultSize ?? "1";
flexGrow =
defaultSize != undefined ? defaultSize.toPrecision(precision) : "1";
} else if (panelData.length === 1) {
// Special case: Single panel group should always fill full width/height
flexGrow = "1";
Expand Down

0 comments on commit a45ab6c

Please sign in to comment.