Skip to content

Commit 4adcbb9

Browse files
authored
Fix: Cannot read properties of undefined (reading 'offsetHeight') (T1321478) (#32869)
1 parent 38878ef commit 4adcbb9

4 files changed

Lines changed: 75 additions & 3 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import {
2+
describe, expect, it,
3+
} from '@jest/globals';
4+
import $ from '@js/core/renderer';
5+
6+
import positionUtils from './m_position';
7+
8+
describe('position (setup)', () => {
9+
describe('when called with an element that does not exist in the DOM', () => {
10+
it('should return undefined without throwing for a selector that matches nothing', () => {
11+
const result = positionUtils.setup('#non-existent-element-that-was-unmounted');
12+
13+
expect(result).toBeUndefined();
14+
});
15+
16+
it('should return undefined without throwing for an empty jQuery object', () => {
17+
const $emptyElement = $([]);
18+
19+
const result = positionUtils.setup($emptyElement);
20+
21+
expect(result).toBeUndefined();
22+
});
23+
});
24+
25+
describe('when called with an existing element as a getter (no options)', () => {
26+
it('should return the offset of the element', () => {
27+
const el = document.createElement('div');
28+
document.body.appendChild(el);
29+
30+
const result = positionUtils.setup(el);
31+
32+
expect(result).toBeDefined();
33+
34+
document.body.removeChild(el);
35+
});
36+
});
37+
});

packages/devextreme/js/__internal/common/core/animation/m_position.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,10 @@ const getOffsetWithoutScale = function ($startElement, $currentElement = $startE
399399
const position = function (what, options?) {
400400
const $what = $(what);
401401

402+
if (!$what.length) {
403+
return undefined;
404+
}
405+
402406
if (!options) {
403407
return $what.offset();
404408
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {
2+
describe, expect, it,
3+
} from '@jest/globals';
4+
import $ from '@js/core/renderer';
5+
6+
import { resetPosition } from './translator';
7+
8+
describe('resetPosition', () => {
9+
describe('when called with an empty jQuery wrapper and finishTransition=true', () => {
10+
it('should not throw when element does not exist in the DOM', () => {
11+
const $emptyElement = $([]);
12+
13+
expect(() => resetPosition($emptyElement, true)).not.toThrow();
14+
});
15+
16+
it('should not throw when element selector matches nothing', () => {
17+
const $missingElement = $('#non-existent-element-that-was-unmounted');
18+
19+
expect(() => resetPosition($missingElement, true)).not.toThrow();
20+
});
21+
});
22+
23+
describe('when called with a real DOM element and finishTransition=true', () => {
24+
it('should not throw and should reset position', () => {
25+
const el = document.createElement('div');
26+
document.body.appendChild(el);
27+
const $el = $(el);
28+
29+
expect(() => resetPosition($el, true)).not.toThrow();
30+
31+
document.body.removeChild(el);
32+
});
33+
});
34+
});

packages/devextreme/js/__internal/common/core/animation/translator.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,6 @@ export const resetPosition = function (
150150
clearCache($element);
151151

152152
if (finishTransition) {
153-
// @ts-expect-error
154-
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
155-
$element.get(0).offsetHeight;
156153
$element.css('transition', originalTransition);
157154
}
158155
};

0 commit comments

Comments
 (0)