Skip to content

Commit

Permalink
test: extract assertFormLayoutGrid test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
vursen committed Feb 25, 2025
1 parent 72d3d93 commit 482c5e9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 56 deletions.
25 changes: 1 addition & 24 deletions packages/form-layout/test/form-layout-auto-responsive.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,7 @@ import { fixtureSync, nextFrame, nextResize } from '@vaadin/testing-helpers';
import '../src/vaadin-form-layout.js';
import '../src/vaadin-form-item.js';
import '../src/vaadin-form-row.js';

function assertFormLayoutGrid(layout, { columns, rows }) {
const children = [...layout.children];
expect(new Set(children.map((child) => child.offsetLeft)).size).to.equal(columns, `expected ${columns} columns`);
expect(new Set(children.map((child) => child.offsetTop)).size).to.equal(rows, `expected ${rows} rows`);
}

function assertFormLayoutLabelPosition(layout, { position }) {
const columnWidth = parseFloat(layout.columnWidth);
const labelWidth = parseFloat(getComputedStyle(layout).getPropertyValue('--vaadin-form-layout-label-width'));
const labelSpacing = parseFloat(getComputedStyle(layout).getPropertyValue('--vaadin-form-layout-label-spacing'));

[...layout.children].forEach((child) => {
if (child.localName !== 'vaadin-form-item') {
return;
}

if (position === 'aside') {
expect(child.offsetWidth).to.equal(columnWidth + labelWidth + labelSpacing, 'expected labels to be aside');
} else {
expect(child.offsetWidth).to.equal(columnWidth, 'expected labels to be above');
}
});
}
import { assertFormLayoutGrid, assertFormLayoutLabelPosition } from './helpers.js';

describe('form-layout auto responsive', () => {
let container, layout, fields;
Expand Down
31 changes: 31 additions & 0 deletions packages/form-layout/test/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { expect } from '@vaadin/chai-plugins';

/**
* Asserts that the given form layout has the specified number of columns and rows.
*/
export function assertFormLayoutGrid(layout, { columns, rows }) {
const children = [...layout.children];
expect(new Set(children.map((child) => child.offsetLeft)).size).to.equal(columns, `expected ${columns} columns`);
expect(new Set(children.map((child) => child.offsetTop)).size).to.equal(rows, `expected ${rows} rows`);
}

/**
* Asserts that the given form layout has the specified label position.
*/
export function assertFormLayoutLabelPosition(layout, { position }) {
const columnWidth = parseFloat(layout.columnWidth);
const labelWidth = parseFloat(getComputedStyle(layout).getPropertyValue('--vaadin-form-layout-label-width'));
const labelSpacing = parseFloat(getComputedStyle(layout).getPropertyValue('--vaadin-form-layout-label-spacing'));

[...layout.children].forEach((child) => {
if (child.localName !== 'vaadin-form-item') {
return;
}

if (position === 'aside') {
expect(child.offsetWidth).to.equal(columnWidth + labelWidth + labelSpacing, 'expected labels to be aside');
} else {
expect(child.offsetWidth).to.equal(columnWidth, 'expected labels to be above');
}
});
}
26 changes: 1 addition & 25 deletions test/integration/dialog-form-layout.test.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
import { expect } from '@vaadin/chai-plugins';
import { setViewport } from '@vaadin/test-runner-commands';
import { fixtureSync, nextFrame, nextRender, nextResize } from '@vaadin/testing-helpers';
import './not-animated-styles.js';
import '@vaadin/form-layout';
import '@vaadin/form-layout/vaadin-form-item.js';
import '@vaadin/dialog';

function assertFormLayoutGrid(layout, { columns, rows }) {
const children = [...layout.children];
expect(new Set(children.map((child) => child.offsetLeft)).size).to.equal(columns, `expected ${columns} columns`);
expect(new Set(children.map((child) => child.offsetTop)).size).to.equal(rows, `expected ${rows} rows`);
}

function assertFormLayoutLabelPosition(layout, { position }) {
const columnWidth = parseFloat(layout.columnWidth);
const labelWidth = parseFloat(getComputedStyle(layout).getPropertyValue('--vaadin-form-layout-label-width'));
const labelSpacing = parseFloat(getComputedStyle(layout).getPropertyValue('--vaadin-form-layout-label-spacing'));

[...layout.children].forEach((child) => {
if (child.localName !== 'vaadin-form-item') {
return;
}

if (position === 'aside') {
expect(child.offsetWidth).to.equal(columnWidth + labelWidth + labelSpacing, 'expected labels to be aside');
} else {
expect(child.offsetWidth).to.equal(columnWidth, 'expected labels to be above');
}
});
}
import { assertFormLayoutGrid, assertFormLayoutLabelPosition } from '@vaadin/form-layout/test/helpers.js';

describe('form-layout in dialog', () => {
let dialog, layout;
Expand Down
8 changes: 1 addition & 7 deletions test/integration/horizontal-layout-form-layout.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { expect } from '@vaadin/chai-plugins';
import { setViewport } from '@vaadin/test-runner-commands';
import { fixtureSync, nextFrame } from '@vaadin/testing-helpers';
import './not-animated-styles.js';
import '@vaadin/form-layout';
import '@vaadin/horizontal-layout';

function assertFormLayoutGrid(layout, { columns, rows }) {
const children = [...layout.children];
expect(new Set(children.map((child) => child.offsetLeft)).size).to.equal(columns);
expect(new Set(children.map((child) => child.offsetTop)).size).to.equal(rows);
}
import { assertFormLayoutGrid } from '@vaadin/form-layout/test/helpers.js';

describe('form-layouts in horizontal-layout', () => {
let horizontalLayout, formLayouts;
Expand Down

0 comments on commit 482c5e9

Please sign in to comment.