@brightspace-ui/testing 1.0.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -474,14 +474,15 @@ To capture the entire viewport, pass `document` as the target element to the ass
474
474
  await expect(document).to.be.golden();
475
475
  ```
476
476
 
477
- #### Rendering Fullscreen Fixtures
477
+ #### Turning Off Full Page Padding
478
478
 
479
- To render a fullscreen fixture that fills the viewport, pass a `fullscreen` option to `fixture()`:
479
+ By default, the page hosting vdiff fixtures has `38px` of padding. For fixtures which are meant to fill the entire page with no surrounding whitespace, the page padding can be disabled:
480
480
 
481
481
  ```javascript
482
- const elem = await fixture(html`<fullscreen-elem></fullscreen-elem>`, {
483
- fullscreen: true
482
+ await fixture(html`<fullscreen-elem></fullscreen-elem>`, {
483
+ pagePadding: false
484
484
  });
485
+ await expect(document).to.be.golden();
485
486
  ```
486
487
 
487
488
  #### Including Other Elements
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@brightspace-ui/testing",
3
3
  "description": "Utilities for testing front-end components and applications",
4
4
  "type": "module",
5
- "version": "1.0.0",
5
+ "version": "1.2.0",
6
6
  "repository": "https://github.com/BrightspaceUI/testing.git",
7
7
  "scripts": {
8
8
  "lint": "eslint . --ext .js",
@@ -24,7 +24,7 @@
24
24
  "eslint": "^8",
25
25
  "eslint-config-brightspace": "^0.23",
26
26
  "mocha": "^10",
27
- "sinon": "^15"
27
+ "sinon": "^16"
28
28
  },
29
29
  "exports": {
30
30
  ".": "./src/browser/index.js"
@@ -2,7 +2,7 @@ import { emulateMedia, sendMouse, setViewport } from '@web/test-runner-commands'
2
2
  import { getDocumentLocaleSettings } from '@brightspace-ui/intl/lib/common.js';
3
3
  import { nextFrame } from '@open-wc/testing';
4
4
 
5
- const DEFAULT_FULLSCREEN = false,
5
+ const DEFAULT_PAGE_PADDING = true,
6
6
  DEFAULT_LANG = 'en',
7
7
  DEFAULT_MATHJAX_RENDER_LATEX = false,
8
8
  DEFAULT_MEDIA = 'screen',
@@ -12,7 +12,7 @@ const DEFAULT_FULLSCREEN = false,
12
12
  const documentLocaleSettings = getDocumentLocaleSettings();
13
13
 
14
14
  let
15
- currentFullscreen = false,
15
+ currentPagePadding = true,
16
16
  currentMathjaxRenderLatex = DEFAULT_MATHJAX_RENDER_LATEX,
17
17
  currentMedia = DEFAULT_MEDIA,
18
18
  currentRtl = false,
@@ -34,7 +34,7 @@ export async function reset(opts = {}) {
34
34
  height: DEFAULT_VIEWPORT_HEIGHT,
35
35
  width: DEFAULT_VIEWPORT_WIDTH
36
36
  },
37
- fullscreen: DEFAULT_FULLSCREEN,
37
+ pagePadding: DEFAULT_PAGE_PADDING,
38
38
  media: DEFAULT_MEDIA
39
39
  };
40
40
 
@@ -46,15 +46,15 @@ export async function reset(opts = {}) {
46
46
 
47
47
  window.scroll(0, 0);
48
48
 
49
- if (opts.fullscreen !== currentFullscreen) {
50
- if (opts.fullscreen) {
51
- document.body.classList.add('fullscreen');
49
+ if (opts.pagePadding !== currentPagePadding) {
50
+ if (!opts.pagePadding) {
51
+ document.body.classList.add('no-padding');
52
52
  }
53
53
  else {
54
- document.body.classList.remove('fullscreen');
54
+ document.body.classList.remove('no-padding');
55
55
  }
56
56
  awaitNextFrame = true;
57
- currentFullscreen = opts.fullscreen;
57
+ currentPagePadding = opts.pagePadding;
58
58
  }
59
59
 
60
60
  if (shouldResetMouse) {
@@ -4,6 +4,7 @@ import { executeServerCommand } from '@web/test-runner-commands';
4
4
  // start loading fonts early
5
5
  [...document.fonts].forEach(font => font.load());
6
6
 
7
+ const DEFAULT_MARGIN = 10;
7
8
  let test;
8
9
 
9
10
  /* eslint-disable no-undef, no-invalid-this */
@@ -26,7 +27,7 @@ function findTargets(elem) {
26
27
  return Array.from(nestedTargets).reduce((acc, target) => [...acc, ...findTargets(target)], [elem]);
27
28
  }
28
29
 
29
- function findLargestRect(elems) {
30
+ function findLargestRect(elems, margin) {
30
31
  let largestRect = { left: Number.MAX_SAFE_INTEGER, top: Number.MAX_SAFE_INTEGER, right: 0, bottom: 0 };
31
32
  elems.forEach(elem => {
32
33
  const targets = findTargets(elem);
@@ -42,11 +43,29 @@ function findLargestRect(elems) {
42
43
  }
43
44
  });
44
45
  });
45
-
46
- return { x: largestRect.left, y: largestRect.top, width: largestRect.right - largestRect.left, height: largestRect.bottom - largestRect.top };
46
+ const rect = {
47
+ x: largestRect.left - margin,
48
+ y: largestRect.top - margin,
49
+ width: largestRect.right - largestRect.left + (margin * 2),
50
+ height: largestRect.bottom - largestRect.top + (margin * 2)
51
+ };
52
+ // We need to handle RTL overflow/scroll scenarios because of Playwright limitations
53
+ if (document.documentElement.getAttribute('dir') === 'rtl' && (largestRect.left < 0 || window.scrollX < 0)) {
54
+ rect.x = -1 * largestRect.left - 1;
55
+ rect.width = largestRect.right + margin;
56
+
57
+ if (largestRect.top <= 0) {
58
+ rect.y = window.scrollY;
59
+ rect.height = largestRect.bottom + margin;
60
+ }
61
+ rect.width = Math.min(rect.width, document.documentElement.clientWidth);
62
+ rect.height = Math.min(rect.height, document.documentElement.clientHeight);
63
+ return { rect, fullPage: true };
64
+ }
65
+ return { rect, fullPage: false };
47
66
  }
48
67
 
49
- async function ScreenshotAndCompare(opts) {
68
+ async function ScreenshotAndCompare(opts = {}) {
50
69
 
51
70
  if (window.d2lTest) {
52
71
  document.documentElement.classList.add('screenshot');
@@ -54,13 +73,18 @@ async function ScreenshotAndCompare(opts) {
54
73
  }
55
74
 
56
75
  const name = this.test.fullTitle();
57
- let rect = null;
58
- if (this.elem !== document) {
76
+ let fullPage = false,
77
+ rect = null;
78
+ if (this.elem === document) {
79
+ fullPage = true;
80
+ } else {
59
81
  const elemsToInclude = [this.elem, ...this.elem.querySelectorAll('.vdiff-include')];
60
- rect = findLargestRect(elemsToInclude);
82
+ const margin = opts.margin ?? DEFAULT_MARGIN;
83
+ ({ rect, fullPage } = findLargestRect(elemsToInclude, margin));
61
84
  }
62
85
  const slowDuration = this.test.slow();
63
- let result = await executeServerCommand('brightspace-visual-diff-compare', { name, rect, slowDuration, opts });
86
+
87
+ let result = await executeServerCommand('brightspace-visual-diff-compare', { name, fullPage, rect, slowDuration });
64
88
  if (result.resizeRequired) {
65
89
  this.test.timeout(0);
66
90
  result = await executeServerCommand('brightspace-visual-diff-compare-resize', { name });
@@ -5,7 +5,6 @@ import pixelmatch from 'pixelmatch';
5
5
  import { PNG } from 'pngjs';
6
6
 
7
7
  const isCI = !!env['CI'];
8
- const DEFAULT_MARGIN = 10;
9
8
  const DEFAULT_TOLERANCE = 0; // TODO: Support tolerance override?
10
9
  const METADATA_NAME = '.vdiff.json';
11
10
  const ROOT_NAME = '.vdiff';
@@ -181,21 +180,13 @@ export function visualDiff({ updateGoldens = false, runSubset = false } = {}) {
181
180
  }
182
181
  }
183
182
 
184
- const opts = { margin: DEFAULT_MARGIN, ...payload.opts };
185
183
  const screenshotOpts = {
186
184
  animations: 'disabled',
187
185
  path: updateGoldens ? goldenFileName : screenshotFileName
188
186
  };
189
- if (!payload.rect) {
190
- screenshotOpts.fullPage = true;
191
- } else {
192
- screenshotOpts.clip = {
193
- x: payload.rect.x - opts.margin,
194
- y: payload.rect.y - opts.margin,
195
- width: payload.rect.width + (opts.margin * 2),
196
- height: payload.rect.height + (opts.margin * 2)
197
- };
198
- }
187
+
188
+ if (payload.fullPage) screenshotOpts.fullPage = true;
189
+ if (payload.rect) screenshotOpts.clip = payload.rect;
199
190
 
200
191
  const page = session.browser.getPage(session.id);
201
192
  await page.screenshot(screenshotOpts);
@@ -94,11 +94,11 @@ export class WTRConfig {
94
94
  font-size: 0.95rem;
95
95
  font-weight: 400;
96
96
  line-height: 1.4rem;
97
- padding: 30px;
97
+ margin: 0;
98
+ padding: 38px;
98
99
  }
99
- body.fullscreen > #d2l-test-fixture-container {
100
- position: absolute;
101
- inset: 0;
100
+ body.no-padding {
101
+ padding: 0;
102
102
  }
103
103
  </style>
104
104
  </head>