Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

US157225 - Enhance rect calculations #159

Merged
merged 6 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/browser/vdiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,29 @@ mocha.setup({
});
/* eslint-enable */

function findTarget(elem) {
if (!elem.shadowRoot) return elem;
const nestedTarget = elem.shadowRoot.querySelector('.vdiff-target');
if (!nestedTarget) return elem;
return findTarget(nestedTarget);
function findTargets(elem) {
if (!elem.shadowRoot) return [elem];
const nestedTargets = elem.shadowRoot.querySelectorAll('.vdiff-target');
if (nestedTargets.length === 0) return [elem];
return Array.from(nestedTargets).reduce((acc, target) => [...acc, ...findTargets(target)], []);
}

function findLargestRect(elems) {
let largestRect = { left: Number.MAX_SAFE_INTEGER, top: Number.MAX_SAFE_INTEGER, right: 0, bottom: 0 };
elems.forEach(elem => {
const targets = findTargets(elem);
targets.forEach(target => {
const targetRect = target.getBoundingClientRect();
largestRect = {
left: Math.floor(Math.min(largestRect.left, targetRect.left)),
top: Math.floor(Math.min(largestRect.top, targetRect.top)),
right: Math.ceil(Math.max(largestRect.right, targetRect.right)),
bottom: Math.ceil(Math.max(largestRect.bottom, targetRect.bottom))
};
});
});

return { x: largestRect.left, y: largestRect.top, width: largestRect.right - largestRect.left, height: largestRect.bottom - largestRect.top };
}

async function ScreenshotAndCompare(opts) {
Expand All @@ -36,8 +54,8 @@ async function ScreenshotAndCompare(opts) {
const name = this.test.fullTitle();
let rect = null;
if (this.elem !== document) {
const target = findTarget(this.elem);
rect = target.getBoundingClientRect();
const elemsToInclude = [this.elem, ...this.elem.querySelectorAll('.vdiff-include')];
rect = findLargestRect(elemsToInclude);
}
const slowDuration = this.test.slow();
let result = await executeServerCommand('brightspace-visual-diff-compare', { name, rect, slowDuration, opts });
Expand Down
38 changes: 37 additions & 1 deletion test/browser/element.vdiff.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { css, html, LitElement } from 'lit';
import { css, html, LitElement, nothing } from 'lit';
import { defineCE, expect, fixture, hoverElem } from '../../src/browser/index.js';
import { executeServerCommand } from '@web/test-runner-commands';
import { unsafeHTML } from 'lit/directives/unsafe-html.js';
Expand Down Expand Up @@ -40,8 +40,30 @@ const elementTag = defineCE(
}
);

const absoluteElementTag = defineCE(
class extends LitElement {
static get styles() {
return css`
div {
border: 1px solid blue;
position: absolute;
bottom: 250px;
}
`;
}
render() {
return html`
<div class="vdiff-target">Absolutely Positioned</div>
`;
}
}
);

const fixedElementTag = defineCE(
class extends LitElement {
static get properties() {
return { multipleTargets: { type: Boolean, attribute: 'multiple-targets' } };
}
static get styles() {
return css`
div {
Expand All @@ -59,6 +81,9 @@ const fixedElementTag = defineCE(
<div class="vdiff-target">
<slot></slot>
</div>
${this.multipleTargets ? unsafeHTML(`
<${absoluteElementTag} class="vdiff-target"></${absoluteElementTag}>
`) : nothing}
`;
}
}
Expand All @@ -69,6 +94,7 @@ const nestedElementTag = defineCE(
render() {
return html`${unsafeHTML(`
<${fixedElementTag} class="vdiff-target"><${elementTag} text="Visual Difference"></${elementTag}></${fixedElementTag}>
<slot></slot>
`)}`;
}
}
Expand Down Expand Up @@ -103,6 +129,16 @@ describe('element-matches', () => {
const elem = await fixture(`<${nestedElementTag}></${nestedElementTag}>`, { viewport: { width: 500, height: 500 } });
await expect(elem).to.be.golden();
});

it('multiple targets', async() => {
const elem = await fixture(`<${fixedElementTag} multiple-targets><${elementTag} text="Visual Difference"></${fixedElementTag}>`, { viewport: { width: 500, height: 500 } });
await expect(elem).to.be.golden();
});

it('multiple to include', async() => {
const elem = await fixture(`<${nestedElementTag}><${absoluteElementTag} class="vdiff-include"></${absoluteElementTag}></${nestedElementTag}>`, { viewport: { width: 500, height: 500 } });
await expect(elem).to.be.golden();
});
});

describe('element-different', () => {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.