Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(fluentui/react): fix initial global leaks that were causing OOM issues #27661

Merged
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
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 @@ -78,6 +78,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