Skip to content

Commit

Permalink
feat: US157225 - Enhance rect calculations (#159)
Browse files Browse the repository at this point in the history
* Handle multiple vdiff-targets and add vdiff-include functionality

* Add test for multiple inner targets, and add test for vdiff-include

* Temporarily force golden run

* Updating vdiff goldens (#160)

Co-authored-by: github-actions <[email protected]>

* Put proper testing back

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions <[email protected]>
  • Loading branch information
3 people committed Sep 5, 2023
1 parent 08e2407 commit a2720d0
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 8 deletions.
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.

0 comments on commit a2720d0

Please sign in to comment.