Skip to content

Commit c811b8f

Browse files
committed
test: test stretchItemSize
1 parent 64ef6a0 commit c811b8f

File tree

3 files changed

+145
-2
lines changed

3 files changed

+145
-2
lines changed

src/grids/MasonryGrid.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,14 @@ export class MasonryGrid extends Grid<MasonryGridOptions> {
266266
const [inline, content] = stretchItemSize[0].split(":").map((value) => parseFloat(value));
267267

268268
minStretchSize = item.computedInlineSize * content / inline;
269-
} else {
269+
} else if (stretchItemSize[0]) {
270270
minStretchSize = stretchItemSize[0];
271271
}
272272
if (isString(stretchItemSize[1])) {
273273
const [inline, content] = stretchItemSize[1].split(":").map((value) => parseFloat(value));
274274

275275
maxStretchSize = item.computedInlineSize * content / inline;
276-
} else {
276+
} else if (stretchItemSize[1]) {
277277
maxStretchSize = stretchItemSize[1];
278278
}
279279
const nextContentSize = between(contentSize, minStretchSize, maxStretchSize);

test/manual/stretchItemSize.html

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<style>
2+
html, body {
3+
position: relative;
4+
height: 100%;
5+
padding: 0!important;
6+
margin: 0!important;
7+
}
8+
9+
10+
.container {
11+
display: flex;
12+
gap: 12px;
13+
align-items: flex-start;
14+
flex-wrap: wrap;
15+
overflow: hidden;
16+
margin: 16px;
17+
}
18+
19+
20+
.result {
21+
text-align: center;
22+
padding: 10px;
23+
font-weight: bold;
24+
box-sizing: border-box;
25+
font-size: 14px;
26+
}
27+
28+
.button {
29+
position: relative;
30+
display: block;
31+
margin: 10px auto;
32+
padding: 10px 20px;
33+
background: white;
34+
border: 1px solid #ccc;
35+
appearance: none;
36+
font-weight: bold;
37+
width: 150px;
38+
text-align: center;
39+
box-sizing: border-box;
40+
font-size: 14px;
41+
}
42+
43+
.image {
44+
position: relative;
45+
display: flex;
46+
width: calc(50% - 12px);
47+
color: black;
48+
overflow: hidden;
49+
border-radius: 16px;
50+
min-height: 10px;
51+
}
52+
53+
.image>img {
54+
width: 100%;
55+
height: fit-content;
56+
}
57+
58+
.title {
59+
position: absolute;
60+
bottom: 12px;
61+
left: 12px;
62+
right: 12px;
63+
height: 16px;
64+
line-height: 16px;
65+
font-weight: bold;
66+
font-size: 13px;
67+
color: #fff;
68+
display: flex;
69+
align-items: center;
70+
gap: 4px;
71+
}
72+
.profile {
73+
border-radius: 50%;
74+
}
75+
76+
</style>
77+
<div class="container">
78+
79+
</div>
80+
<script src="../../dist/grid.js"></script>
81+
<script>
82+
const items = [];
83+
84+
for (let i = 0; i < 6; ++i) {
85+
items.push(`<div class="image" data-grid-content-offset="40">
86+
<img src="https://naver.github.io/egjs-infinitegrid/assets/image/${i * 6 + 5}.jpg" alt="image1" data-grid-lazy="true"/>
87+
<div class="title">
88+
<img class="profile" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewbox='0 0 16 16' width='16' height='16'%3E%3Crect width='16' height='16' fill='%232fdea8'/%3E%3Ctext x='8' y='8' dominant-baseline='middle' text-anchor='middle' fill='%232fdea8' style='filter: invert(100%);font-size: 6px;'%3E16x16%3C/text%3E%3C/svg%3E" loading="lazy" style="visibility: visible;">
89+
<span>이건 아이템${i}</span>
90+
</div>
91+
</div>`);
92+
}
93+
94+
const html = items.join("");
95+
96+
document.querySelector(".container").innerHTML = html;
97+
const grid = new Grid.MasonryGrid(".container", {
98+
column: 2,
99+
columnSizeRatio: true,
100+
isConstantSize: true,
101+
stretchItemSize: [0, "1:1"],
102+
gap: 12,
103+
}).on("renderComplete", () => {
104+
console.log("?");
105+
});
106+
grid.renderItems();
107+
</script>

test/unit/MasonryGrid.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,42 @@ describe("test MasonryGrid", () => {
406406
]);
407407
expect(container!.style.height).to.be.deep.equals("460px");
408408
});
409+
it(`should check the size of the items must be stretched,`, async () => {
410+
// Given
411+
container!.style.cssText = "width: 600px; height: 600px;";
412+
container!.innerHTML = `
413+
<div style="position: absolute;width: 200px; height: 400px;"></div>
414+
<div style="position: absolute;width: 150px; height: 150px;"></div>
415+
<div style="position: absolute;width: 300px; height: 600px;"></div>
416+
<div style="position: absolute;width: 250px; height: 250px;"></div>
417+
`;
418+
grid = new MasonryGrid(container!, {
419+
align: "end",
420+
gap: 10,
421+
// stretch item size
422+
stretchItemSize: [0, "1:1"],
423+
});
424+
425+
// When
426+
grid.renderItems();
427+
428+
await waitEvent(grid, "renderComplete");
429+
430+
// Then
431+
expect(grid.getOutlines()).to.be.deep.equals({
432+
start: [0, 0],
433+
end: [470, 470],
434+
});
435+
436+
437+
expect(grid.getItems().map((item) => item.cssRect)).to.be.deep.equals([
438+
{ left: 190, top: 0, height: 200 },
439+
{ left: 400, top: 0 },
440+
{ left: 400, top: 160, height: 300 },
441+
{ left: 190, top: 210 },
442+
]);
443+
expect(container!.style.height).to.be.deep.equals("460px");
444+
});
409445
it(`should check if it is aligned with end`, async () => {
410446
// Given
411447
container!.style.cssText = "width: 600px; height: 600px;";

0 commit comments

Comments
 (0)