(snapshot, expected)
| 7 | |
| 8 | // Pure geometry validation, also exercised with broken layouts in the unit tests. |
| 9 | export function validLayout(snapshot, expected) { |
| 10 | if ( |
| 11 | !snapshot || |
| 12 | ![snapshot.width, snapshot.height, snapshot.contentWidth, snapshot.scrollTop].every( |
| 13 | Number.isFinite |
| 14 | ) || |
| 15 | Math.abs(snapshot.width - expected.width) > 1 || |
| 16 | snapshot.height <= 0 || |
| 17 | !snapshot.rows.length || |
| 18 | snapshot.rows.length >= expected.total |
| 19 | ) { |
| 20 | return false; |
| 21 | } |
| 22 | let visible = snapshot.rows |
| 23 | .filter(row => row.top + row.height > 1 && row.top < snapshot.height - 1) |
| 24 | .sort((a, b) => a.top - b.top); |
| 25 | if ( |
| 26 | visible.length < 2 || |
| 27 | visible[0].top > 1 || |
| 28 | visible.at(-1).top + visible.at(-1).height < snapshot.height - 1 |
| 29 | ) { |
| 30 | return false; |
| 31 | } |
| 32 | if ( |
| 33 | expected.atTop && |
| 34 | (snapshot.scrollTop > 1 || visible[0].id !== 'item-0' || Math.abs(visible[0].top) > 1) |
| 35 | ) { |
| 36 | return false; |
| 37 | } |
| 38 | return ( |
| 39 | (!expected.columns || |
| 40 | (snapshot.columns?.length === expected.columns.length && |
| 41 | snapshot.columns.every((width, i) => Math.abs(width - expected.columns[i]) <= 1))) && |
| 42 | new Set(visible.map(row => Math.round(row.height))).size >= 2 && |
| 43 | visible.every( |
| 44 | (row, i) => |
| 45 | Number.isFinite(row.index) && |
| 46 | row.index >= 0 && |
| 47 | row.index < expected.total && |
| 48 | row.id === `item-${row.index}` && |
| 49 | row.height > 0 && |
| 50 | Math.abs(row.height - row.allocatedHeight) <= 1 && |
| 51 | (!expected.columns || Math.abs(row.height - row.contentHeight) <= 1) && |
| 52 | Math.abs(row.width - snapshot.contentWidth) <= 1 && |
| 53 | (!i || |
| 54 | (row.index === visible[i - 1].index + 1 && |
| 55 | Math.abs(row.top - visible[i - 1].top - visible[i - 1].height) <= 1)) |
| 56 | ) |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | function cellContentHeight(cell) { |
| 61 | let style = getComputedStyle(cell); |
no test coverage detected