Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export function getMotionName(prefixCls: string, transitionName?: string, animat
}

// =============================== Offset ===============================
function getScroll(w: Window, top?: boolean): number {
function getScroll(w: Window | null | undefined, top?: boolean): number {
if (!w) {
return 0;
}
let ret = w[`page${top ? 'Y' : 'X'}Offset`];
const method = `scroll${top ? 'Top' : 'Left'}`;
if (typeof ret !== 'number') {
Expand Down
27 changes: 27 additions & 0 deletions tests/util.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,33 @@ describe('Dialog.Util', () => {
top: 1128,
});
});

it('returns zero offset when defaultView and parentWindow are missing', () => {
const element = {
ownerDocument: {},
getBoundingClientRect: () => ({ left: 0, top: 0 }),
} as any;

expect(offset(element)).toEqual({
left: 0,
top: 0,
});
});

it('returns zero offset when defaultView and parentWindow are null or undefined', () => {
const element = {
ownerDocument: {
defaultView: null,
parentWindow: undefined,
},
getBoundingClientRect: () => ({ left: 0, top: 0 }),
} as any;

expect(offset(element)).toEqual({
left: 0,
top: 0,
});
});
});

describe('getMotionName', () => {
Expand Down
Loading