@window-splitter/state 0.4.0 → 0.4.1

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.
@@ -20,6 +20,7 @@ import {
20
20
  import { Actor, createActor } from "xstate";
21
21
  import { spring } from "framer-motion";
22
22
  import Big from "big.js";
23
+ import Cookies from "universal-cookie";
23
24
 
24
25
  function getTemplate(actor: Actor<typeof groupMachine>) {
25
26
  return buildTemplate(actor.getSnapshot().context);
@@ -177,7 +178,7 @@ describe("constraints", () => {
177
178
  });
178
179
 
179
180
  expect(getTemplate(actor)).toMatchInlineSnapshot(
180
- `"minmax(0px, min(calc(0.52040816326530612245 * (100% - 10px)), 100%)) 10px minmax(0px, min(calc(0.47959183673469387755 * (100% - 10px)), 100%))"`
181
+ `"minmax(0px, 1fr) 10px minmax(0px, min(calc(0.47959183673469387755 * (100% - 10px)), 100%))"`
181
182
  );
182
183
 
183
184
  // Drag the resizer to the left
@@ -188,7 +189,7 @@ describe("constraints", () => {
188
189
  });
189
190
 
190
191
  expect(getTemplate(actor)).toMatchInlineSnapshot(
191
- `"minmax(0px, min(calc(0.47959183673469387755 * (100% - 10px)), 100%)) 10px minmax(0px, min(calc(0.52040816326530612245 * (100% - 10px)), 100%))"`
192
+ `"minmax(0px, 1fr) 10px minmax(0px, min(calc(0.52040816326530612245 * (100% - 10px)), 100%))"`
192
193
  );
193
194
  });
194
195
 
@@ -235,7 +236,7 @@ describe("constraints", () => {
235
236
  });
236
237
 
237
238
  expect(getTemplate(actor)).toMatchInlineSnapshot(
238
- `"minmax(0px, min(calc(0.46610169491525423729 * (100% - 10px)), 100%)) 10px minmax(0px, min(calc(0.53389830508474576271 * (100% - 10px)), 100%))"`
239
+ `"minmax(0px, 1fr) 10px minmax(0px, min(calc(0.53389830508474576271 * (100% - 10px)), 100%))"`
239
240
  );
240
241
  });
241
242
 
@@ -268,7 +269,7 @@ describe("constraints", () => {
268
269
  });
269
270
 
270
271
  expect(getTemplate(actor)).toMatchInlineSnapshot(
271
- `"minmax(0px, min(calc(0.40816326530612244898 * (100% - 10px)), 40%)) 10px minmax(0px, min(calc(0.59183673469387755102 * (100% - 10px)), 100%))"`
272
+ `"minmax(0px, min(calc(0.40816326530612244898 * (100% - 10px)), 40%)) 10px minmax(0px, 1fr)"`
272
273
  );
273
274
  });
274
275
 
@@ -303,7 +304,7 @@ describe("constraints", () => {
303
304
  });
304
305
 
305
306
  expect(getTemplate(actor)).toMatchInlineSnapshot(
306
- `"minmax(0px, min(calc(0.52040816326530612245 * (100% - 10px)), 100%)) 10px minmax(0px, min(calc(0.47959183673469387755 * (100% - 10px)), 100%))"`
307
+ `"minmax(0px, 1fr) 10px minmax(0px, min(calc(0.47959183673469387755 * (100% - 10px)), 100%))"`
307
308
  );
308
309
 
309
310
  // Drag the resizer to the up
@@ -318,7 +319,7 @@ describe("constraints", () => {
318
319
  });
319
320
 
320
321
  expect(getTemplate(actor)).toMatchInlineSnapshot(
321
- `"minmax(0px, min(calc(0.47959183673469387755 * (100% - 10px)), 100%)) 10px minmax(0px, min(calc(0.52040816326530612245 * (100% - 10px)), 100%))"`
322
+ `"minmax(0px, 1fr) 10px minmax(0px, min(calc(0.52040816326530612245 * (100% - 10px)), 100%))"`
322
323
  );
323
324
  });
324
325
 
@@ -375,8 +376,8 @@ describe("constraints", () => {
375
376
 
376
377
  initializeSizes(actor, { width: 500, height: 200 });
377
378
  expect(spy).toHaveBeenCalledWith({
378
- percentage: 0.5,
379
- pixel: 250,
379
+ percentage: 0.49,
380
+ pixel: 245,
380
381
  });
381
382
 
382
383
  capturePixelValues(actor, () => {
@@ -581,7 +582,7 @@ describe("constraints", () => {
581
582
  initializeSizes(actor, { width: 500, height: 200 });
582
583
 
583
584
  expect(getTemplate(actor)).toMatchInlineSnapshot(
584
- `"minmax(0px, min(calc(0.38775510204081632653 * (100% - 10px)), 100%)) 10px minmax(0px, min(calc(0.61224489795918367347 * (100% - 10px)), 100%))"`
585
+ `"minmax(0px, 1fr) 10px 300px"`
585
586
  );
586
587
 
587
588
  capturePixelValues(actor, () => {
@@ -747,9 +748,7 @@ describe("collapsible panel", () => {
747
748
  expect(getTemplate(actor)).toBe("490px 10px 0px");
748
749
  });
749
750
 
750
- expect(getTemplate(actor)).toBe(
751
- "minmax(0px, min(calc(1 * (100% - 10px)), 100%)) 10px 0px"
752
- );
751
+ expect(getTemplate(actor)).toBe("minmax(0px, 1fr) 10px 0px");
753
752
 
754
753
  // Test dragging to expand the panel
755
754
  capturePixelValues(actor, () => {
@@ -1332,15 +1331,8 @@ describe("collapsible panel", () => {
1332
1331
  "minmax(200px, 1fr) 10px minmax(300px, 1fr)"
1333
1332
  );
1334
1333
  initializeSizes(actor, { width: 400, height: 200 });
1335
- actor.send({
1336
- type: "setSize",
1337
- size: { width: 400, height: 200 },
1338
- handleOverflow: true,
1339
- });
1340
1334
  // There wasn't enough space to render the panel so the last one is collapsed
1341
- expect(getTemplate(actor)).toBe(
1342
- "minmax(200px, min(calc(1 * (100% - 70px)), 100%)) 10px 60px"
1343
- );
1335
+ expect(getTemplate(actor)).toBe("minmax(200px, 1fr) 10px 60px");
1344
1336
  });
1345
1337
 
1346
1338
  test("on resize", async () => {
@@ -1371,20 +1363,20 @@ describe("collapsible panel", () => {
1371
1363
  expect(getTemplate(actor)).toBe(
1372
1364
  "minmax(200px, 1fr) 10px minmax(300px, 1fr)"
1373
1365
  );
1374
- initializeSizes(actor, { width: 500, height: 200 });
1375
- expect(getTemplate(actor)).toBe(
1376
- "minmax(200px, min(calc(0.40816326530612244898 * (100% - 10px)), 100%)) 10px minmax(300px, min(calc(0.61224489795918367347 * (100% - 10px)), 100%))"
1377
- );
1366
+ initializeSizes(actor, { width: 510, height: 200 });
1378
1367
 
1379
- actor.send({
1380
- type: "setSize",
1381
- size: { width: 400, height: 200 },
1382
- handleOverflow: true,
1368
+ capturePixelValues(actor, () => {
1369
+ dragHandle(actor, { id: "resizer-1", delta: 10 });
1383
1370
  });
1384
- // There wasn't enough space to render the panel so the last one is collapsed
1371
+
1385
1372
  expect(getTemplate(actor)).toBe(
1386
- "minmax(200px, min(calc(1 * (100% - 70px)), 100%)) 10px 60px"
1373
+ "minmax(200px, 1fr) 10px minmax(300px, min(calc(0.6 * (100% - 10px)), 100%))"
1387
1374
  );
1375
+
1376
+ initializeSizes(actor, { width: 400, height: 200 });
1377
+
1378
+ // There wasn't enough space to render the panel so the last one is collapsed
1379
+ expect(getTemplate(actor)).toBe("minmax(200px, 1fr) 10px 60px");
1388
1380
  });
1389
1381
 
1390
1382
  test("cannot expand panel via event", async () => {
@@ -1422,9 +1414,7 @@ describe("collapsible panel", () => {
1422
1414
  handleOverflow: true,
1423
1415
  });
1424
1416
  // There wasn't enough space to render the panel so the last one is collapsed
1425
- expect(getTemplate(actor)).toBe(
1426
- "minmax(200px, min(calc(1 * (100% - 70px)), 100%)) 10px 60px"
1427
- );
1417
+ expect(getTemplate(actor)).toBe("minmax(200px, 1fr) 10px 60px");
1428
1418
 
1429
1419
  actor.send({
1430
1420
  type: "expandPanel",
@@ -1433,9 +1423,7 @@ describe("collapsible panel", () => {
1433
1423
  await waitForIdle(actor);
1434
1424
 
1435
1425
  // There wasn't enough space to render the panel so the last one is collapsed
1436
- expect(getTemplate(actor)).toBe(
1437
- "minmax(200px, min(calc(1 * (100% - 70px)), 100%)) 10px 60px"
1438
- );
1426
+ expect(getTemplate(actor)).toBe("minmax(200px, 1fr) 10px 60px");
1439
1427
  });
1440
1428
 
1441
1429
  test("cannot expand panel via drag", async () => {
@@ -1467,18 +1455,16 @@ describe("collapsible panel", () => {
1467
1455
  "minmax(200px, 1fr) 10px minmax(300px, 1fr)"
1468
1456
  );
1469
1457
  initializeSizes(actor, { width: 400, height: 200 });
1470
- actor.send({
1471
- type: "setSize",
1472
- size: { width: 400, height: 200 },
1473
- handleOverflow: true,
1474
- });
1458
+
1475
1459
  // There wasn't enough space to render the panel so the last one is collapsed
1476
- expect(getTemplate(actor)).toBe(
1477
- "minmax(200px, min(calc(1 * (100% - 70px)), 100%)) 10px 60px"
1478
- );
1460
+ expect(getTemplate(actor)).toBe("minmax(200px, 1fr) 10px 60px");
1461
+
1462
+ // update the latest sizes to the new ones
1463
+ initializeSizes(actor, { width: 400, height: 200 });
1479
1464
 
1480
1465
  // Drag the resizer to the right
1481
1466
  capturePixelValues(actor, () => {
1467
+ expect(getTemplate(actor)).toBe("330px 10px 60px");
1482
1468
  dragHandle(actor, { id: "resizer-1", delta: -400 });
1483
1469
  expect(getTemplate(actor)).toBe("330px 10px 60px");
1484
1470
  });
@@ -1520,7 +1506,7 @@ describe("conditional panel", () => {
1520
1506
 
1521
1507
  capturePixelValues(actor, () => {
1522
1508
  expect(getTemplate(actor)).toMatchInlineSnapshot(
1523
- `"240px 10px 140px 10px 100px"`
1509
+ `"245px 10px 135px 10px 100px"`
1524
1510
  );
1525
1511
  });
1526
1512
 
@@ -1530,7 +1516,7 @@ describe("conditional panel", () => {
1530
1516
  ]);
1531
1517
 
1532
1518
  capturePixelValues(actor, () => {
1533
- expect(getTemplate(actor)).toMatchInlineSnapshot(`"240px 10px 250px"`);
1519
+ expect(getTemplate(actor)).toMatchInlineSnapshot(`"245px 10px 245px"`);
1534
1520
  });
1535
1521
  });
1536
1522
 
@@ -1575,7 +1561,7 @@ describe("conditional panel", () => {
1575
1561
 
1576
1562
  capturePixelValues(actor, () => {
1577
1563
  expect(getTemplate(actor)).toMatchInlineSnapshot(
1578
- `"240px 10px 240px 10px 0px"`
1564
+ `"245px 10px 235px 10px 0px"`
1579
1565
  );
1580
1566
  });
1581
1567
 
@@ -1585,7 +1571,7 @@ describe("conditional panel", () => {
1585
1571
  ]);
1586
1572
 
1587
1573
  capturePixelValues(actor, () => {
1588
- expect(getTemplate(actor)).toMatchInlineSnapshot(`"240px 10px 250px"`);
1574
+ expect(getTemplate(actor)).toMatchInlineSnapshot(`"245px 10px 245px"`);
1589
1575
  });
1590
1576
  });
1591
1577
 
@@ -1628,7 +1614,7 @@ describe("conditional panel", () => {
1628
1614
 
1629
1615
  capturePixelValues(actor, () => {
1630
1616
  expect(getTemplate(actor)).toMatchInlineSnapshot(
1631
- `"240px 10px 115px 10px 125px"`
1617
+ `"245px 10px 110px 10px 125px"`
1632
1618
  );
1633
1619
  });
1634
1620
 
@@ -1638,7 +1624,7 @@ describe("conditional panel", () => {
1638
1624
  ]);
1639
1625
 
1640
1626
  capturePixelValues(actor, () => {
1641
- expect(getTemplate(actor)).toMatchInlineSnapshot(`"240px 10px 250px"`);
1627
+ expect(getTemplate(actor)).toMatchInlineSnapshot(`"245px 10px 245px"`);
1642
1628
  });
1643
1629
  });
1644
1630
 
@@ -1678,7 +1664,7 @@ describe("conditional panel", () => {
1678
1664
 
1679
1665
  capturePixelValues(actor, () => {
1680
1666
  expect(getTemplate(actor)).toMatchInlineSnapshot(
1681
- `"240px 10px 100px 10px 140px"`
1667
+ `"245px 10px 100px 10px 135px"`
1682
1668
  );
1683
1669
  });
1684
1670
 
@@ -1688,7 +1674,7 @@ describe("conditional panel", () => {
1688
1674
  ]);
1689
1675
 
1690
1676
  capturePixelValues(actor, () => {
1691
- expect(getTemplate(actor)).toMatchInlineSnapshot(`"240px 10px 250px"`);
1677
+ expect(getTemplate(actor)).toMatchInlineSnapshot(`"245px 10px 245px"`);
1692
1678
  });
1693
1679
 
1694
1680
  sendAll(actor, [
@@ -1697,7 +1683,7 @@ describe("conditional panel", () => {
1697
1683
  ]);
1698
1684
 
1699
1685
  capturePixelValues(actor, () => {
1700
- expect(getTemplate(actor)).toMatchInlineSnapshot(`"240px 10px 250px"`);
1686
+ expect(getTemplate(actor)).toMatchInlineSnapshot(`"245px 10px 245px"`);
1701
1687
  });
1702
1688
  });
1703
1689
 
@@ -1925,9 +1911,19 @@ describe("utils", async () => {
1925
1911
 
1926
1912
  initializeSizes(actor, { width: 500, height: 200 });
1927
1913
 
1928
- const oldSnapshot = actor.getSnapshot();
1929
- const serialized = JSON.stringify(oldSnapshot);
1930
- const newSnapshot = prepareSnapshot(JSON.parse(serialized));
1914
+ let oldSnapshot = actor.getSnapshot();
1915
+ let serialized = JSON.stringify(oldSnapshot);
1916
+ let newSnapshot = prepareSnapshot(JSON.parse(serialized));
1917
+
1918
+ expect(newSnapshot).toMatchSnapshot();
1919
+
1920
+ capturePixelValues(actor, () => {
1921
+ dragHandle(actor, { id: "resizer-1", delta: 100 });
1922
+ });
1923
+
1924
+ oldSnapshot = actor.getSnapshot();
1925
+ serialized = JSON.stringify(oldSnapshot);
1926
+ newSnapshot = prepareSnapshot(JSON.parse(serialized));
1931
1927
 
1932
1928
  expect(newSnapshot).toMatchSnapshot();
1933
1929
  });
@@ -1956,7 +1952,7 @@ describe("static at rest", () => {
1956
1952
  initializeSizes(actor, { width: 500, height: 200 });
1957
1953
 
1958
1954
  expect(getTemplate(actor)).toMatchInlineSnapshot(
1959
- `"clamp(20px, 200px, 200px) 10px minmax(50px, min(calc(0.5 * (100% - 220px)), 100%)) 10px minmax(50px, min(calc(0.5 * (100% - 220px)), 100%))"`
1955
+ `"minmax(20px, 200px) 10px minmax(50px, 1fr) 10px minmax(50px, 1fr)"`
1960
1956
  );
1961
1957
 
1962
1958
  capturePixelValues(actor, () => {
@@ -1964,7 +1960,97 @@ describe("static at rest", () => {
1964
1960
  });
1965
1961
 
1966
1962
  expect(getTemplate(actor)).toMatchInlineSnapshot(
1967
- `"clamp(20px, 190px, 200px) 10px minmax(50px, min(calc(0.51724137931034482759 * (100% - 210px)), 100%)) 10px minmax(50px, min(calc(0.48275862068965517241 * (100% - 210px)), 100%))"`
1963
+ `"clamp(20px, 190px, 200px) 10px minmax(50px, 1fr) 10px minmax(50px, min(calc(0.48275862068965517241 * (100% - 210px)), 100%))"`
1964
+ );
1965
+ });
1966
+ });
1967
+
1968
+ describe("autosave", () => {
1969
+ test("localStorage", async () => {
1970
+ localStorage.removeItem("group");
1971
+
1972
+ const actor = createActor(groupMachine, {
1973
+ input: {
1974
+ groupId: "group",
1975
+ autosaveStrategy: "localStorage",
1976
+ initialItems: [
1977
+ initializePanel({
1978
+ id: "panel-1",
1979
+ min: "20px",
1980
+ max: "200px",
1981
+ isStaticAtRest: true,
1982
+ }),
1983
+ initializePanelHandleData({ id: "resizer-1", size: "10px" }),
1984
+ initializePanel({ id: "panel-2", min: "50px" }),
1985
+ initializePanelHandleData({ id: "resizer-2", size: "10px" }),
1986
+ initializePanel({ id: "panel-3", min: "50px" }),
1987
+ ],
1988
+ },
1989
+ }).start();
1990
+
1991
+ initializeSizes(actor, { width: 500, height: 200 });
1992
+
1993
+ capturePixelValues(actor, () => {
1994
+ dragHandle(actor, { id: "resizer-1", delta: -10 });
1995
+ });
1996
+
1997
+ await waitForIdle(actor);
1998
+
1999
+ const snapshot = prepareSnapshot(
2000
+ JSON.parse(localStorage.getItem("group")!)
2001
+ );
2002
+ const actor2 = createActor(groupMachine, {
2003
+ // @ts-expect-error For tests
2004
+ input: {},
2005
+ snapshot,
2006
+ }).start();
2007
+
2008
+ expect(buildTemplate(actor2.getSnapshot().context)).toMatchInlineSnapshot(
2009
+ `"clamp(20px, 190px, 200px) 10px minmax(50px, 1fr) 10px minmax(50px, min(calc(0.48275862068965517241 * (100% - 210px)), 100%))"`
2010
+ );
2011
+ });
2012
+
2013
+ test("cookie", async () => {
2014
+ localStorage.removeItem("group");
2015
+
2016
+ const actor = createActor(groupMachine, {
2017
+ input: {
2018
+ groupId: "group-2",
2019
+ autosaveStrategy: "cookie",
2020
+ initialItems: [
2021
+ initializePanel({
2022
+ id: "panel-1",
2023
+ min: "20px",
2024
+ max: "200px",
2025
+ isStaticAtRest: true,
2026
+ }),
2027
+ initializePanelHandleData({ id: "resizer-1", size: "10px" }),
2028
+ initializePanel({ id: "panel-2", min: "50px" }),
2029
+ initializePanelHandleData({ id: "resizer-2", size: "10px" }),
2030
+ initializePanel({ id: "panel-3", min: "50px" }),
2031
+ ],
2032
+ },
2033
+ }).start();
2034
+
2035
+ initializeSizes(actor, { width: 500, height: 200 });
2036
+
2037
+ capturePixelValues(actor, () => {
2038
+ dragHandle(actor, { id: "resizer-1", delta: -10 });
2039
+ });
2040
+
2041
+ await waitForIdle(actor);
2042
+
2043
+ const ActualClass = (Cookies as any).default || Cookies;
2044
+ const cookies = new ActualClass(null, { path: "/" });
2045
+ const snapshot = prepareSnapshot(cookies.get("group-2")!);
2046
+ const actor2 = createActor(groupMachine, {
2047
+ // @ts-expect-error For tests
2048
+ input: {},
2049
+ snapshot,
2050
+ }).start();
2051
+
2052
+ expect(buildTemplate(actor2.getSnapshot().context)).toMatchInlineSnapshot(
2053
+ `"clamp(20px, 190px, 200px) 10px minmax(50px, 1fr) 10px minmax(50px, min(calc(0.48275862068965517241 * (100% - 210px)), 100%))"`
1968
2054
  );
1969
2055
  });
1970
2056
  });