Skip to content

Commit 45b1270

Browse files
committed
Add deprecation warning for scaledWidth/scaledHeight accessors
1 parent e9c8ef0 commit 45b1270

2 files changed

Lines changed: 96 additions & 2 deletions

File tree

src/AutoSizer.test.tsx

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ describe("AutoSizer", () => {
7575
beforeEach(() => {
7676
// @ts-ignore
7777
global.IS_REACT_ACT_ENVIRONMENT = true;
78+
79+
jest.resetAllMocks();
7880
});
7981

8082
function renderHelper(
@@ -353,6 +355,81 @@ describe("AutoSizer", () => {
353355
expect(ChildComponent).toHaveBeenCalledTimes(1);
354356
expect(onResize).toHaveBeenCalledTimes(2);
355357
});
358+
359+
it("should warn if deprecated scaledHeight accessor is used", async () => {
360+
const consoleWarnSpy = jest
361+
.spyOn(console, "warn")
362+
.mockImplementation(() => {});
363+
364+
// Destructure syntax should warn too
365+
const onResize = jest.fn(({ height, scaledHeight }) => {
366+
expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
367+
expect(consoleWarnSpy).toHaveBeenCalledWith(
368+
"scaledWidth and scaledHeight parameters have been deprecated; use width and height instead"
369+
);
370+
371+
expect(height).toBe(100.5);
372+
expect(scaledHeight).toBe(100.5);
373+
});
374+
375+
const ChildComponent = jest
376+
.fn()
377+
.mockImplementation(DefaultChildComponent);
378+
379+
renderHelper(
380+
{
381+
ChildComponent,
382+
height: 100.5,
383+
width: 200.5,
384+
},
385+
{
386+
disableHeight: true,
387+
onResize,
388+
}
389+
);
390+
391+
expect(onResize).toHaveBeenCalledTimes(1);
392+
});
393+
394+
it("should warn if deprecated scaledWidth accessor is used", async () => {
395+
const consoleWarnSpy = jest
396+
.spyOn(console, "warn")
397+
.mockImplementation(() => {});
398+
399+
const onResize = jest.fn((params) => {
400+
expect(params.width).toBe(200.5);
401+
expect(consoleWarnSpy).not.toHaveBeenCalled();
402+
403+
// Should be called when first accessed
404+
expect(params.scaledWidth).toBe(200.5);
405+
expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
406+
expect(consoleWarnSpy).toHaveBeenCalledWith(
407+
"scaledWidth and scaledHeight parameters have been deprecated; use width and height instead"
408+
);
409+
410+
// Should not be called again
411+
expect(params.scaledWidth).toBe(200.5);
412+
expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
413+
});
414+
415+
const ChildComponent = jest
416+
.fn()
417+
.mockImplementation(DefaultChildComponent);
418+
419+
renderHelper(
420+
{
421+
ChildComponent,
422+
height: 100.5,
423+
width: 200.5,
424+
},
425+
{
426+
disableHeight: true,
427+
onResize,
428+
}
429+
);
430+
431+
expect(onResize).toHaveBeenCalledTimes(1);
432+
});
356433
});
357434

358435
describe("HTML attributes", () => {

src/AutoSizer.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export class AutoSizer extends Component<Props, State> {
1919

2020
_autoSizer: HTMLElement | null = null;
2121
_detectElementResize: DetectElementResize | null = null;
22+
_didLogDeprecationWarning = false;
2223
_parentNode: HTMLElement | null = null;
2324
_resizeObserver: ResizeObserver | null = null;
2425
_timeoutId: number | null = null;
@@ -178,14 +179,30 @@ export class AutoSizer extends Component<Props, State> {
178179
width,
179180
});
180181

182+
const maybeLogDeprecationWarning = () => {
183+
if (!this._didLogDeprecationWarning) {
184+
this._didLogDeprecationWarning = true;
185+
186+
console.warn(
187+
"scaledWidth and scaledHeight parameters have been deprecated; use width and height instead"
188+
);
189+
}
190+
};
191+
181192
if (typeof onResize === "function") {
182193
onResize({
183194
height,
184195
width,
185196

186197
// TODO Remove these params in the next major release
187-
scaledHeight: height,
188-
scaledWidth: width,
198+
get scaledHeight() {
199+
maybeLogDeprecationWarning();
200+
return height;
201+
},
202+
get scaledWidth() {
203+
maybeLogDeprecationWarning();
204+
return width;
205+
},
189206
});
190207
}
191208
}

0 commit comments

Comments
 (0)