Skip to content

Commit

Permalink
test(fluentui/react): fix initial global leaks that were causing OOM …
Browse files Browse the repository at this point in the history
…issues (#27661)

* test(fluentui/react): fix initial global leaks that were causing OOM issues
  • Loading branch information
Hotell authored Apr 26, 2023
1 parent 557ffca commit d67287c
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,16 @@ function customColumnDivider(

describe('DetailsList', () => {
let spy: jest.SpyInstance;
beforeAll(() => {
beforeEach(() => {
/* eslint-disable-next-line @typescript-eslint/no-empty-function */
spy = jest.spyOn(window, 'scrollTo').mockImplementation(() => {});
resetIds();
});

afterAll(() => {
spy.mockRestore();
});

beforeEach(() => {
resetIds();
});

afterEach(() => {
if ((setTimeout as unknown as jest.Mock).mock) {
jest.runOnlyPendingTimers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ const groupProps: IDetailsGroupRenderProps = {
*/
describe('DetailsListV2', () => {
let spy: jest.SpyInstance;
beforeAll(() => {

beforeEach(() => {
/* eslint-disable-next-line @typescript-eslint/no-empty-function */
spy = jest.spyOn(window, 'scrollTo').mockImplementation(() => {});
});
Expand Down
20 changes: 10 additions & 10 deletions packages/react/src/components/Dialog/Dialog.deprecated.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ import { resetIds, setWarningCallback } from '../../Utilities';
import { DialogBase } from './Dialog.base';

describe('Dialog deprecated props', () => {
beforeEach(() => {
resetIds();
beforeAll(() => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
const noop = () => {};
// Prevent warn deprecations from failing test
setWarningCallback(noop);
});

afterAll(() => {
beforeEach(() => {
resetIds();
});

beforeAll(() => {
// Prevent warn deprecations from failing test
setWarningCallback(() => {
/* no-op */
});
(ReactDOM.createPortal as any) = jest.fn((element, node) => {
return element;
});
});

afterAll(() => {
setWarningCallback();
});

afterEach(() => {
(ReactDOM.createPortal as any).mockClear();
jest.useRealTimers();
Expand Down
30 changes: 20 additions & 10 deletions packages/react/src/components/Dialog/Dialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ describe('Dialog', () => {
resetIds();
});

afterEach(() => {
setWarningCallback();
jest.useRealTimers();
});

afterAll(() => {
resetIds();
});
Expand Down Expand Up @@ -86,11 +81,14 @@ describe('Dialog', () => {

expect(queryByRole('dialog')).toBeFalsy();
expect(onDismissed).toHaveBeenCalledTimes(1);

jest.useRealTimers();
});

it('deprecated isOpen controls open state of the dialog', () => {
// suppress deprecation warning as error
setWarningCallback(() => undefined);
// eslint-disable-next-line @typescript-eslint/no-empty-function
setWarningCallback(() => {});
jest.useFakeTimers();
const onDismissed = jest.fn();

Expand All @@ -106,6 +104,9 @@ describe('Dialog', () => {

expect(queryByRole('dialog')).toBeFalsy();
expect(onDismissed).toHaveBeenCalledTimes(1);

jest.useRealTimers();
setWarningCallback();
});

it('Properly attaches auto-generated aria attributes IDs', () => {
Expand Down Expand Up @@ -154,7 +155,7 @@ describe('Dialog', () => {
<DialogBase
hidden={false}
modalProps={{
titleAriaId: titleAriaId,
titleAriaId,
}}
dialogContentProps={{
title: 'sample title',
Expand All @@ -170,7 +171,8 @@ describe('Dialog', () => {

it('deprecated titleId prop should be used if titleProps.id is not passed', () => {
// Prevent warn deprecations from failing test
setWarningCallback(() => undefined);
// eslint-disable-next-line @typescript-eslint/no-empty-function
setWarningCallback(() => {});

const titleId = 'title_id';
render(
Expand All @@ -185,10 +187,13 @@ describe('Dialog', () => {

const dialogTitle = document.getElementById(titleId);
expect(dialogTitle).toBeTruthy();

setWarningCallback();
});

it('deprecated titleId prop should not be used if titleProps.id is undefined', () => {
setWarningCallback(() => undefined);
// eslint-disable-next-line @typescript-eslint/no-empty-function
setWarningCallback(() => {});

const titleId = 'title_id';
render(
Expand All @@ -204,10 +209,13 @@ describe('Dialog', () => {

const dialogTitle = document.getElementById(titleId);
expect(dialogTitle).toBeFalsy();

setWarningCallback();
});

it('titleProps.id should be used if deprecated titleId is also passed', () => {
setWarningCallback(() => undefined);
// eslint-disable-next-line @typescript-eslint/no-empty-function
setWarningCallback(() => {});

const titleId = 'title_id';
render(
Expand All @@ -223,6 +231,8 @@ describe('Dialog', () => {

const dialogTitle = document.getElementById(titleId);
expect(dialogTitle).toBeTruthy();

setWarningCallback();
});

describe('enableAriaHiddenSiblings', () => {
Expand Down
17 changes: 14 additions & 3 deletions packages/style-utilities/src/styles/theme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,35 @@ describe('registerOnThemeChangeCallback', () => {
expect(callback.mock.calls.length).toBe(0);
});

it('calls the previously registered callback', () => {
it('calls previously registered callback', () => {
registerOnThemeChangeCallback(callback);
loadTheme({});
expect(callback.mock.calls.length).toBe(1);
});

it('calls the previously registered callback (again)', () => {
registerOnThemeChangeCallback(callback);
loadTheme({});
loadTheme({});
expect(callback.mock.calls.length).toBe(2);
});

it('unregisters the callback, and doesnt call it again', () => {
registerOnThemeChangeCallback(callback);
loadTheme({});
loadTheme({});
removeOnThemeChangeCallback(callback);

expect(callback.mock.calls.length).toBe(2);
loadTheme({});
expect(callback.mock.calls.length).toBe(2);
});

it('didnt pass null to the callback', () => {
registerOnThemeChangeCallback(callback);
loadTheme({});
loadTheme({});

expect(callback.mock.calls[0][0]).toBeTruthy();
expect(callback.mock.calls[1][0]).toBeTruthy();
});
Expand All @@ -37,7 +48,7 @@ describe('loadTheme', () => {
describe('specify defaultFontStyle', () => {
it('applies defaultFontStyle to fonts and retains all other default values', () => {
const defaultFontStyle: IRawStyle = { fontFamily: 'Segoe UI' };
const userTheme = { defaultFontStyle: defaultFontStyle };
const userTheme = { defaultFontStyle };
loadTheme(userTheme);
const newTheme = getTheme();

Expand All @@ -58,7 +69,7 @@ describe('loadTheme', () => {
});
it('applies defaultFontStyle and fonts to theme and retains all other default values', () => {
const defaultFontStyle: IRawStyle = { fontFamily: 'Foo', fontSize: '10px' };
const userTheme = { defaultFontStyle: defaultFontStyle, fonts: { small: { fontSize: '20px' } } };
const userTheme = { defaultFontStyle, fonts: { small: { fontSize: '20px' } } };
loadTheme(userTheme);
const newTheme = getTheme();

Expand Down
2 changes: 2 additions & 0 deletions scripts/jest/src/jest.preset.v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ const createConfig = (customConfig = {}) => {
url: 'http://localhost',
},
testEnvironment: 'jsdom',
restoreMocks: true,
clearMocks: true,

watchPlugins: ['jest-watch-typeahead/filename', 'jest-watch-typeahead/testname'],
};
Expand Down
2 changes: 2 additions & 0 deletions scripts/jest/src/jest.preset.v8.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ describe(`v8 preset`, () => {
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
restoreMocks: true,
clearMocks: true,
transformIgnorePatterns: ['/node_modules/', '/lib-commonjs/', '\\.js$'],
watchPlugins: ['jest-watch-typeahead/filename', 'jest-watch-typeahead/testname'],
}),
Expand Down

0 comments on commit d67287c

Please sign in to comment.