@brightspace-ui/testing 1.10.3 → 1.12.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
@@ -90,6 +90,18 @@ it('should work on small viewports', async() => {
90
90
  });
91
91
  ```
92
92
 
93
+ To change the viewport size outside of `fixture`, use the `setViewport` command:
94
+
95
+ ```javascript
96
+ import { fixture, setViewport } from '@brightspace-ui/testing';
97
+
98
+ it('should adapt when viewport changes', async() => {
99
+ const elem = await fixture(html`<my-elem></my-elem>`);
100
+ await setViewport({ height: 300, width: 200 });
101
+ // do assertions
102
+ });
103
+ ```
104
+
93
105
  #### Configuring the Language or Text Direction
94
106
 
95
107
  If the component under test has special multi-lingual or bidirectional text behavior, both `language` and `rtl` (right-to-left) options are available.
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.10.3",
5
+ "version": "1.12.0",
6
6
  "repository": "https://github.com/BrightspaceUI/testing.git",
7
7
  "scripts": {
8
8
  "lint": "eslint . --ext .js",
@@ -1,5 +1,6 @@
1
1
  import { sendKeys as cmdSendKeys, sendMouse as cmdSendMouse } from '@web/test-runner-commands';
2
2
  import { requestMouseReset } from './reset.js';
3
+ export { setViewport } from './reset.js';
3
4
 
4
5
  function getElementPosition(elem) {
5
6
  const { x, y, width, height } = elem.getBoundingClientRect();
@@ -1,6 +1,6 @@
1
1
  import './vdiff.js';
2
2
 
3
3
  export { assert, aTimeout, defineCE, expect, html, nextFrame, oneDefaultPreventedEvent, oneEvent, waitUntil } from '@open-wc/testing';
4
- export { clickAt, clickElem, focusElem, hoverAt, hoverElem, sendKeys, sendKeysElem } from './commands.js';
4
+ export { clickAt, clickElem, focusElem, hoverAt, hoverElem, sendKeys, sendKeysElem, setViewport } from './commands.js';
5
5
  export { fixture } from './fixture.js';
6
6
  export { runConstructor } from './constructor.js';
@@ -1,4 +1,4 @@
1
- import { emulateMedia, sendMouse, setViewport } from '@web/test-runner-commands';
1
+ import { setViewport as cmdSetViewport, emulateMedia, sendMouse } 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
 
@@ -6,8 +6,10 @@ const DEFAULT_PAGE_PADDING = true,
6
6
  DEFAULT_LANG = 'en',
7
7
  DEFAULT_MATHJAX_RENDER_LATEX = false,
8
8
  DEFAULT_MEDIA = 'screen',
9
- DEFAULT_VIEWPORT_HEIGHT = 800,
10
- DEFAULT_VIEWPORT_WIDTH = 800;
9
+ DEFAULT_VIEWPORT = {
10
+ height: 800,
11
+ width: 800
12
+ };
11
13
 
12
14
  const documentLocaleSettings = getDocumentLocaleSettings();
13
15
 
@@ -24,22 +26,32 @@ export function requestMouseReset() {
24
26
  shouldResetMouse = true;
25
27
  }
26
28
 
29
+ export async function setViewport(viewport) {
30
+
31
+ const newViewport = { ...DEFAULT_VIEWPORT, ...viewport };
32
+
33
+ if (newViewport.height !== currentViewportHeight || newViewport.width !== currentViewportWidth) {
34
+ currentViewportHeight = newViewport.height;
35
+ currentViewportWidth = newViewport.width;
36
+ await cmdSetViewport(newViewport).catch(() => {});
37
+ return true;
38
+ }
39
+
40
+ return false;
41
+
42
+ }
43
+
27
44
  export async function reset(opts = {}) {
28
45
 
29
46
  const defaultOpts = {
30
47
  lang: DEFAULT_LANG,
31
48
  mathjax: {},
32
49
  rtl: !!opts.lang?.startsWith('ar'),
33
- viewport: {
34
- height: DEFAULT_VIEWPORT_HEIGHT,
35
- width: DEFAULT_VIEWPORT_WIDTH
36
- },
37
50
  pagePadding: DEFAULT_PAGE_PADDING,
38
51
  media: DEFAULT_MEDIA
39
52
  };
40
53
 
41
54
  opts = { ...defaultOpts, ...opts };
42
- opts.viewport = { ...defaultOpts.viewport, ...opts.viewport };
43
55
  opts.mathjax.renderLatex = (typeof opts.mathjax.renderLatex === 'boolean') ? opts.mathjax.renderLatex : DEFAULT_MATHJAX_RENDER_LATEX;
44
56
 
45
57
  let awaitNextFrame = false;
@@ -83,11 +95,8 @@ export async function reset(opts = {}) {
83
95
  awaitNextFrame = true;
84
96
  }
85
97
 
86
- if (opts.viewport.height !== currentViewportHeight || opts.viewport.width !== currentViewportWidth) {
87
- await setViewport(opts.viewport).catch(() => {});
98
+ if (await setViewport(opts.viewport)) {
88
99
  awaitNextFrame = true;
89
- currentViewportHeight = opts.viewport.height;
90
- currentViewportWidth = opts.viewport.width;
91
100
  }
92
101
 
93
102
  if (opts.mathjax.renderLatex !== currentMathjaxRenderLatex) {