Skip to content

Commit 9b921a4

Browse files
committed
Do not crash on lack of resize observer
1 parent a76fa8a commit 9b921a4

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

.eslintrc.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ module.exports = {
5050
browser: true,
5151
},
5252
rules: {
53-
'@typescript-eslint/ban-ts-comment': ['error', { 'ts-expect-error': 'allow-with-description' }],
53+
'@typescript-eslint/ban-ts-comment': [
54+
'error',
55+
{ 'ts-expect-error': 'allow-with-description', 'ts-ignore': 'allow-with-description' },
56+
],
5457
'@typescript-eslint/camelcase': 'off', // Allow two levels of separation, e. g. ProductWizard_SidebarConfig_ListWithHeader_Component_Args
5558
'@typescript-eslint/class-name-casing': 'off', // Allow two levels of separation, e. g. ProductWizard_SidebarConfig_ListWithHeader_Component_Args
5659
'@typescript-eslint/no-empty-function': 'off', // Noop is a valid technique.

addon/-private/modifier.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ export default class ElementQueryModifier extends Modifier<Args> {
238238
// -------------------
239239

240240
didInstall(): void {
241+
// @ts-ignore https://github.com/Microsoft/TypeScript/issues/28502#issuecomment-609607344
242+
if (!ResizeObserver) return;
243+
241244
if (!this.element) throw new Error('Expected this.element to be available');
242245

243246
this._element = this.element;
@@ -246,10 +249,16 @@ export default class ElementQueryModifier extends Modifier<Args> {
246249
}
247250

248251
didUpdateArguments(): void {
252+
// @ts-ignore https://github.com/Microsoft/TypeScript/issues/28502#issuecomment-609607344
253+
if (!ResizeObserver) return;
254+
249255
this.didResizeHandler();
250256
}
251257

252258
willRemove(): void {
259+
// @ts-ignore https://github.com/Microsoft/TypeScript/issues/28502#issuecomment-609607344
260+
if (!ResizeObserver) return;
261+
253262
if (this.teardownResizeObserver) this.teardownResizeObserver();
254263
}
255264
}

tests/integration/modifier-test.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import sinon, { SinonSpy } from 'sinon';
66
import { TestContext } from 'ember-test-helpers';
77
import { Sizes } from 'ember-element-query/-private/modifier';
88
import pause from '../helpers/pause';
9+
import { setupWindowMock } from 'ember-window-mock';
910

1011
interface TestContextCustom extends TestContext {
1112
callback?: SinonSpy;
@@ -17,11 +18,35 @@ interface TestContextCustom extends TestContext {
1718
}
1819

1920
module('Integration | Modifier | element-query', function (hooks) {
21+
let m;
22+
2023
setupRenderingTest(hooks);
24+
setupWindowMock(hooks);
25+
26+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
27+
test('do not crash on missing resize observer', async function (this: TestContextCustom, assert) {
28+
// @ts-ignore https://github.com/Microsoft/TypeScript/issues/28502#issuecomment-609607344
29+
const resizeObserver = window.ResizeObserver as unknown;
30+
// @ts-ignore https://github.com/Microsoft/TypeScript/issues/28502#issuecomment-609607344
31+
window.ResizeObserver = undefined;
32+
33+
await render(hbs`
34+
{{! template-lint-disable no-inline-styles }}
35+
<div
36+
{{element-query}}
37+
>
38+
</div>
39+
`);
40+
41+
m = 'Element is rendered';
42+
assert.ok(true, m);
43+
44+
// @ts-ignore https://github.com/Microsoft/TypeScript/issues/28502#issuecomment-609607344
45+
window.ResizeObserver = resizeObserver;
46+
});
2147

2248
// eslint-disable-next-line @typescript-eslint/no-misused-promises
2349
test('calls the onResize callback', async function (this: TestContextCustom, assert) {
24-
let m;
2550
let callCount = 0;
2651
this.callback = sinon.spy(() => callCount++);
2752

@@ -79,7 +104,6 @@ module('Integration | Modifier | element-query', function (hooks) {
79104

80105
// eslint-disable-next-line @typescript-eslint/no-misused-promises
81106
test('does not call the onResize when isDisabled is initially set', async function (this: TestContextCustom, assert) {
82-
let m;
83107
let callCount = 0;
84108
this.callback = sinon.spy(() => callCount++);
85109

@@ -110,7 +134,6 @@ module('Integration | Modifier | element-query', function (hooks) {
110134

111135
// eslint-disable-next-line @typescript-eslint/no-misused-promises
112136
test('stops calling the onResize callback after updating isDisabled after rendering', async function (this: TestContextCustom, assert) {
113-
let m;
114137
let callCount = 0;
115138
this.callback = sinon.spy(() => callCount++);
116139
this.set('isDisabled', false);
@@ -163,7 +186,6 @@ module('Integration | Modifier | element-query', function (hooks) {
163186

164187
// eslint-disable-next-line @typescript-eslint/no-misused-promises
165188
test('attributes smoke test + update on arguments change test', async function (this: TestContextCustom, assert) {
166-
let m;
167189
this.set('sizes', { small: 0, large: 300 });
168190

169191
await render(hbs`
@@ -199,8 +221,6 @@ module('Integration | Modifier | element-query', function (hooks) {
199221

200222
// eslint-disable-next-line @typescript-eslint/no-misused-promises
201223
test('usning multiple modifiers on the same element with custom sizes', async function (this: TestContextCustom, assert) {
202-
let m;
203-
204224
await render(hbs`
205225
{{! template-lint-disable no-inline-styles }}
206226
<div
@@ -233,8 +253,6 @@ module('Integration | Modifier | element-query', function (hooks) {
233253

234254
// eslint-disable-next-line @typescript-eslint/no-misused-promises
235255
test('usning multiple modifiers on the same element with custom prefixes', async function (this: TestContextCustom, assert) {
236-
let m;
237-
238256
await render(hbs`
239257
{{! template-lint-disable no-inline-styles }}
240258
<div
@@ -314,7 +332,6 @@ module('Integration | Modifier | element-query', function (hooks) {
314332
cases.forEach(({ actualWidth, expectedAttributes, prefix }) => {
315333
// eslint-disable-next-line @typescript-eslint/no-misused-promises, @typescript-eslint/restrict-template-expressions
316334
test(`width ${actualWidth}, prefix ${prefix}`, async function (this: TestContextCustom, assert) {
317-
let m;
318335
this.actualWidth = actualWidth;
319336
this.prefix = prefix;
320337

@@ -396,7 +413,6 @@ module('Integration | Modifier | element-query', function (hooks) {
396413
cases.forEach(({ actualWidth, expectedAttributes, prefix }) => {
397414
// eslint-disable-next-line @typescript-eslint/no-misused-promises, @typescript-eslint/restrict-template-expressions
398415
test(`width ${actualWidth}, prefix: ${prefix}`, async function (this: TestContextCustom, assert) {
399-
let m;
400416
this.actualWidth = actualWidth;
401417
this.sizes = sizes;
402418
this.prefix = prefix;
@@ -492,7 +508,6 @@ module('Integration | Modifier | element-query', function (hooks) {
492508
cases.forEach(({ actualHeight, expectedAttributes, prefix }) => {
493509
// eslint-disable-next-line @typescript-eslint/no-misused-promises, @typescript-eslint/restrict-template-expressions
494510
test(`height ${actualHeight}, prefix ${prefix}`, async function (this: TestContextCustom, assert) {
495-
let m;
496511
this.actualHeight = actualHeight;
497512
this.prefix = prefix;
498513

@@ -574,7 +589,6 @@ module('Integration | Modifier | element-query', function (hooks) {
574589
cases.forEach(({ actualHeight, expectedAttributes, prefix }) => {
575590
// eslint-disable-next-line @typescript-eslint/no-misused-promises, @typescript-eslint/restrict-template-expressions
576591
test(`height ${actualHeight}, prefix: ${prefix}`, async function (this: TestContextCustom, assert) {
577-
let m;
578592
this.actualHeight = actualHeight;
579593
this.sizes = sizes;
580594
this.prefix = prefix;

0 commit comments

Comments
 (0)