|
1 | 1 | import { GridItem } from "../../src";
|
2 | 2 | import { JustifiedGrid } from "../../src/grids/JustifiedGrid";
|
| 3 | +import { SIZES } from "./utils/consts"; |
3 | 4 | import {
|
4 | 5 | appendElements, cleanup, expectItemsPosition,
|
5 | 6 | getRowCount, getRowPoses, sandbox, waitEvent,
|
@@ -505,4 +506,187 @@ describe("test JustifiedGrid", () => {
|
505 | 506 | });
|
506 | 507 | });
|
507 | 508 | });
|
| 509 | + describe("test stretch option", () => { |
| 510 | + it(`should check whether cost is reflected in stretch (no stretch)`, async () => { |
| 511 | + // Given |
| 512 | + container!.style.cssText = "width: 1000px;"; |
| 513 | + |
| 514 | + grid = new JustifiedGrid(container!, { |
| 515 | + gap: 5, |
| 516 | + horizontal: false, |
| 517 | + sizeRange: [150, 300], |
| 518 | + stretch: true, |
| 519 | + stretchRange: [144, 320], |
| 520 | + passUnstretchRow: false, |
| 521 | + }); |
| 522 | + |
| 523 | + appendElements(container!, 5); |
| 524 | + |
| 525 | + // When |
| 526 | + grid.renderItems(); |
| 527 | + |
| 528 | + await waitEvent(grid, "renderComplete"); |
| 529 | + |
| 530 | + const items = grid.getItems(); |
| 531 | + const rowCount = getRowCount(items); |
| 532 | + // [518, 517], |
| 533 | + // [550, 825], |
| 534 | + // [640, 640], |
| 535 | + // [364, 520], |
| 536 | + // [710, 1020], |
| 537 | + // [600, 819], |
| 538 | + // [486, 729], |
| 539 | + // [544, 784], |
| 540 | + // [720, 720], |
| 541 | + // [381, 555], |
| 542 | + // [521, 775], |
| 543 | + |
| 544 | + // Then |
| 545 | + expect(rowCount).to.be.equal(1); |
| 546 | + |
| 547 | + // NO STRETCH |
| 548 | + items.forEach((item, i) => { |
| 549 | + expect(item.cssInlineSize).to.be.closeTo(SIZES[i][0] / SIZES[i][1] * 241.1, 0.1); |
| 550 | + expect(item.cssContentSize).to.be.closeTo(241.1, 0.1); |
| 551 | + }); |
| 552 | + }); |
| 553 | + it(`should check whether cost is reflected in stretch (stretch)`, async () => { |
| 554 | + // Given |
| 555 | + container!.style.cssText = "width: 1000px;"; |
| 556 | + |
| 557 | + grid = new JustifiedGrid(container!, { |
| 558 | + gap: 0, |
| 559 | + horizontal: false, |
| 560 | + sizeRange: [150, 220], |
| 561 | + stretch: true, |
| 562 | + stretchRange: [144, 300], |
| 563 | + passUnstretchRow: false, |
| 564 | + }); |
| 565 | + |
| 566 | + appendElements(container!, 5); |
| 567 | + |
| 568 | + // When |
| 569 | + grid.renderItems(); |
| 570 | + |
| 571 | + await waitEvent(grid, "renderComplete"); |
| 572 | + |
| 573 | + const items = grid.getItems(); |
| 574 | + const rowCount = getRowCount(items); |
| 575 | + |
| 576 | + // Then |
| 577 | + expect(rowCount).to.be.equal(1); |
| 578 | + |
| 579 | + // NO STRETCH |
| 580 | + const expectedHeight = 220; |
| 581 | + const sum = SIZES.slice(0, 5).reduce((v1, v2) => v1 + v2[0] / v2[1] * expectedHeight, 0); |
| 582 | + const scale = grid.getContainerInlineSize() / sum; |
| 583 | + |
| 584 | + items.forEach((item, i) => { |
| 585 | + expect(item.cssContentSize).to.be.closeTo(expectedHeight, 0.1); |
| 586 | + expect(item.cssInlineSize).to.be.not.closeTo(SIZES[i][0] / SIZES[i][1] * expectedHeight, 0.1); |
| 587 | + expect(item.cssInlineSize).to.be.closeTo(SIZES[i][0] / SIZES[i][1] * scale * expectedHeight, 0.1, `expect ${i}`); |
| 588 | + }); |
| 589 | + }); |
| 590 | + it(`should check whether cost is reflected in stretch (stretch, 2line)`, async () => { |
| 591 | + // Given |
| 592 | + container!.style.cssText = "width: 1000px;"; |
| 593 | + |
| 594 | + grid = new JustifiedGrid(container!, { |
| 595 | + gap: 0, |
| 596 | + horizontal: false, |
| 597 | + sizeRange: [220, 220], |
| 598 | + stretch: true, |
| 599 | + stretchRange: [144, 300], |
| 600 | + passUnstretchRow: false, |
| 601 | + }); |
| 602 | + |
| 603 | + appendElements(container!, 8); |
| 604 | + |
| 605 | + // When |
| 606 | + grid.renderItems(); |
| 607 | + |
| 608 | + await waitEvent(grid, "renderComplete"); |
| 609 | + |
| 610 | + |
| 611 | + const items = grid.getItems(); |
| 612 | + const rowCount = getRowCount(items); |
| 613 | + |
| 614 | + // Then |
| 615 | + expect(rowCount).to.be.equal(2); |
| 616 | + |
| 617 | + // NO STRETCH |
| 618 | + const expectedHeight = 220; |
| 619 | + |
| 620 | + |
| 621 | + // 0 4 |
| 622 | + // 4 8 |
| 623 | + const scale1 = grid.getContainerInlineSize() |
| 624 | + / SIZES.slice(0, 4).reduce((v1, v2) => v1 + v2[0] / v2[1] * expectedHeight, 0); |
| 625 | + const scale2 = grid.getContainerInlineSize() |
| 626 | + / SIZES.slice(4, 8).reduce((v1, v2) => v1 + v2[0] / v2[1] * expectedHeight, 0); |
| 627 | + |
| 628 | + items.slice(0, 4).forEach((item, i) => { |
| 629 | + expect(item.cssContentSize).to.be.closeTo(expectedHeight, 0.1); |
| 630 | + expect(item.cssInlineSize).to.be.not.closeTo(SIZES[i][0] / SIZES[i][1] * expectedHeight, 0.1); |
| 631 | + expect(item.cssInlineSize).to.be.closeTo(SIZES[i][0] / SIZES[i][1] * scale1 * expectedHeight, 0.1, `expect ${i}`); |
| 632 | + }); |
| 633 | + items.slice(4, 8).forEach((item, i) => { |
| 634 | + const size = SIZES[i + 4]; |
| 635 | + expect(item.cssContentSize).to.be.closeTo(expectedHeight, 0.1); |
| 636 | + expect(item.cssInlineSize).to.be.not.closeTo(size[0] / size[1] * expectedHeight, 0.1); |
| 637 | + expect(item.cssInlineSize).to.be.closeTo(size[0] / size[1] * scale2 * expectedHeight, 0.1, `expect ${i + 4}`); |
| 638 | + }); |
| 639 | + }); |
| 640 | + it(`should check whether cost is reflected in stretch (stretch, 2line, pass last line)`, async () => { |
| 641 | + // Given |
| 642 | + container!.style.cssText = "width: 1000px;"; |
| 643 | + |
| 644 | + grid = new JustifiedGrid(container!, { |
| 645 | + gap: 0, |
| 646 | + horizontal: false, |
| 647 | + sizeRange: [220, 220], |
| 648 | + stretch: true, |
| 649 | + stretchRange: [130, 300], |
| 650 | + passUnstretchRow: true, |
| 651 | + }); |
| 652 | + |
| 653 | + appendElements(container!, 8); |
| 654 | + |
| 655 | + // When |
| 656 | + grid.renderItems(); |
| 657 | + |
| 658 | + await waitEvent(grid, "renderComplete"); |
| 659 | + |
| 660 | + const passedItems = grid.getOutlines().passedItems; |
| 661 | + const items = grid.getItems(); |
| 662 | + const rowCount = getRowCount(items); |
| 663 | + |
| 664 | + // Then |
| 665 | + |
| 666 | + expect(passedItems?.length).to.be.equal(2); |
| 667 | + expect(rowCount).to.be.equal(2); |
| 668 | + |
| 669 | + // NO STRETCH |
| 670 | + const expectedHeight = 220; |
| 671 | + |
| 672 | + // 0 4 |
| 673 | + // 4 8 |
| 674 | + const scale1 = grid.getContainerInlineSize() |
| 675 | + / SIZES.slice(0, 6).reduce((v1, v2) => v1 + v2[0] / v2[1] * expectedHeight, 0); |
| 676 | + |
| 677 | + items.slice(0, 6).forEach((item, i) => { |
| 678 | + const size = SIZES[i]; |
| 679 | + expect(item.cssContentPos).to.be.equals(0); |
| 680 | + expect(item.cssContentSize).to.be.closeTo(expectedHeight, 0.1); |
| 681 | + expect(item.cssInlineSize).to.be.not.closeTo(size[0] / size[1] * expectedHeight, 0.1); |
| 682 | + expect(item.cssInlineSize).to.be.closeTo(size[0] / size[1] * scale1 * expectedHeight, 0.1, `expect ${i}`); |
| 683 | + }); |
| 684 | + items.slice(6, 8).forEach((item, i) => { |
| 685 | + const size = SIZES[i + 4]; |
| 686 | + |
| 687 | + expect(item.cssContentSize).to.be.closeTo(expectedHeight, 0.1); |
| 688 | + expect(item.cssInlineSize).to.be.not.closeTo(size[0] / size[1] * expectedHeight, 0.1); |
| 689 | + }); |
| 690 | + }); |
| 691 | + }); |
508 | 692 | });
|
0 commit comments