Skip to content

Commit

Permalink
Simplify controlling animations in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michaldudak committed Jan 8, 2025
1 parent 1726a1d commit 49eb462
Show file tree
Hide file tree
Showing 18 changed files with 53 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ describe('<AlertDialog.Root />', () => {
});

it('is called on close when the exit animation finishes', async () => {
(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = false;
globalThis.BASE_UI_ANIMATIONS_DISABLED = false;

let onCloseCompleteCalled = false;
function notifyonCloseComplete() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const testContext = {

describe('<Checkbox.Indicator />', () => {
beforeEach(() => {
(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = true;
globalThis.BASE_UI_ANIMATIONS_DISABLED = true;
});

const { render } = createRenderer();
Expand Down Expand Up @@ -132,7 +132,7 @@ describe('<Checkbox.Indicator />', () => {
skip();
}

(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = false;
globalThis.BASE_UI_ANIMATIONS_DISABLED = false;

let animationFinished = false;
const notifyAnimationFinished = () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/react/src/dialog/root/DialogRoot.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const isJSDOM = /jsdom/.test(window.navigator.userAgent);

describe('<Dialog.Root />', () => {
beforeEach(() => {
(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = true;
globalThis.BASE_UI_ANIMATIONS_DISABLED = true;
});

const { render } = createRenderer();
Expand Down Expand Up @@ -92,7 +92,7 @@ describe('<Dialog.Root />', () => {
skip();
}

(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = false;
globalThis.BASE_UI_ANIMATIONS_DISABLED = false;

let animationFinished = false;
const notifyAnimationFinished = () => {
Expand Down Expand Up @@ -384,7 +384,7 @@ describe('<Dialog.Root />', () => {
skip();
}

(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = false;
globalThis.BASE_UI_ANIMATIONS_DISABLED = false;

const notifyTransitionEnd = spy();

Expand Down Expand Up @@ -621,7 +621,7 @@ describe('<Dialog.Root />', () => {
});

it('is called on close when the exit animation finishes', async () => {
(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = false;
globalThis.BASE_UI_ANIMATIONS_DISABLED = false;

let onCloseCompleteCalled = false;
function notifyonCloseComplete() {
Expand Down
9 changes: 9 additions & 0 deletions packages/react/src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare global {
/**
* When `true`, disables animation-related code, even if supported by the runtime enviroment.
*/
// eslint-disable-next-line no-var, vars-on-top
var BASE_UI_ANIMATIONS_DISABLED: boolean;
}

export type {};
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { expect } from 'chai';

describe('<Menu.CheckboxItemIndicator />', () => {
beforeEach(() => {
(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = true;
globalThis.BASE_UI_ANIMATIONS_DISABLED = true;
});

const { render } = createRenderer();
Expand Down Expand Up @@ -71,7 +71,7 @@ describe('<Menu.CheckboxItemIndicator />', () => {
skip();
}

(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = false;
globalThis.BASE_UI_ANIMATIONS_DISABLED = false;

let animationFinished = false;
const notifyAnimationFinished = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('<Menu.RadioItemIndicator />', () => {
skip();
}

(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = false;
globalThis.BASE_UI_ANIMATIONS_DISABLED = false;

let animationFinished = false;
const notifyAnimationFinished = () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/menu/root/MenuRoot.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const isJSDOM = /jsdom/.test(window.navigator.userAgent);

describe('<Menu.Root />', () => {
beforeEach(() => {
(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = true;
globalThis.BASE_UI_ANIMATIONS_DISABLED = true;
});

const { render } = createRenderer();
Expand Down Expand Up @@ -757,7 +757,7 @@ describe('<Menu.Root />', () => {
skip();
}

(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = false;
globalThis.BASE_UI_ANIMATIONS_DISABLED = false;

let animationFinished = false;
const notifyAnimationFinished = () => {
Expand Down Expand Up @@ -915,7 +915,7 @@ describe('<Menu.Root />', () => {
});

it('is called on close when the exit animation finishes', async () => {
(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = false;
globalThis.BASE_UI_ANIMATIONS_DISABLED = false;

let onCloseCompleteCalled = false;
function notifyonCloseComplete() {
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/popover/root/PopoverRoot.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function Root(props: Popover.Root.Props) {

describe('<Popover.Root />', () => {
beforeEach(() => {
(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = true;
globalThis.BASE_UI_ANIMATIONS_DISABLED = true;
});

const { render, clock } = createRenderer();
Expand Down Expand Up @@ -227,7 +227,7 @@ describe('<Popover.Root />', () => {
skip();
}

(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = false;
globalThis.BASE_UI_ANIMATIONS_DISABLED = false;

let animationFinished = false;
const notifyAnimationFinished = () => {
Expand Down Expand Up @@ -574,7 +574,7 @@ describe('<Popover.Root />', () => {
});

it('is called on close when the exit animation finishes', async () => {
(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = false;
globalThis.BASE_UI_ANIMATIONS_DISABLED = false;

let onCloseCompleteCalled = false;
function notifyonCloseComplete() {
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/preview-card/root/PreviewCardRoot.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function Trigger(props: PreviewCard.Trigger.Props) {

describe('<PreviewCard.Root />', () => {
beforeEach(() => {
(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = true;
globalThis.BASE_UI_ANIMATIONS_DISABLED = true;
});

const { render, clock } = createRenderer();
Expand Down Expand Up @@ -208,7 +208,7 @@ describe('<PreviewCard.Root />', () => {
animationFinished = true;
};

(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = false;
globalThis.BASE_UI_ANIMATIONS_DISABLED = false;

function Test() {
const style = `
Expand Down Expand Up @@ -534,7 +534,7 @@ describe('<PreviewCard.Root />', () => {
});

it('is called on close when the exit animation finishes', async () => {
(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = false;
globalThis.BASE_UI_ANIMATIONS_DISABLED = false;

let onCloseCompleteCalled = false;
function notifyonCloseComplete() {
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/radio/indicator/RadioIndicator.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { RadioGroup } from '@base-ui-components/react/radio-group';

describe('<Radio.Indicator />', () => {
beforeEach(() => {
(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = true;
globalThis.BASE_UI_ANIMATIONS_DISABLED = true;
});

const { render } = createRenderer();
Expand Down Expand Up @@ -63,7 +63,7 @@ describe('<Radio.Indicator />', () => {
skip();
}

(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = false;
globalThis.BASE_UI_ANIMATIONS_DISABLED = false;

let animationFinished = false;
const notifyAnimationFinished = () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/select/root/SelectRoot.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const isJSDOM = /jsdom/.test(window.navigator.userAgent);

describe('<Select.Root />', () => {
beforeEach(() => {
(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = true;
globalThis.BASE_UI_ANIMATIONS_DISABLED = true;
});

const { render } = createRenderer();
Expand Down Expand Up @@ -252,7 +252,7 @@ describe('<Select.Root />', () => {
skip();
}

(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = false;
globalThis.BASE_UI_ANIMATIONS_DISABLED = false;

let animationFinished = false;
const notifyAnimationFinished = () => {
Expand Down Expand Up @@ -469,7 +469,7 @@ describe('<Select.Root />', () => {
});

it('is called on close when the exit animation finishes', async () => {
(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = false;
globalThis.BASE_UI_ANIMATIONS_DISABLED = false;

let onCloseCompleteCalled = false;
function notifyonCloseComplete() {
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/tooltip/root/TooltipRoot.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function Root(props: Tooltip.Root.Props) {

describe('<Tooltip.Root />', () => {
beforeEach(() => {
(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = true;
globalThis.BASE_UI_ANIMATIONS_DISABLED = true;
});

const { render, clock } = createRenderer();
Expand Down Expand Up @@ -433,7 +433,7 @@ describe('<Tooltip.Root />', () => {
});

it('is called on close when the exit animation finishes', async () => {
(globalThis as any).BASE_UI_ANIMATIONS_DISABLED = false;
globalThis.BASE_UI_ANIMATIONS_DISABLED = false;

let onCloseCompleteCalled = false;
function notifyonCloseComplete() {
Expand Down
5 changes: 1 addition & 4 deletions packages/react/src/utils/useAnimationsFinished.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ export function useAnimationsFinished(ref: React.RefObject<HTMLElement | null>)
return;
}

if (
typeof element.getAnimations !== 'function' ||
(globalThis as any).BASE_UI_ANIMATIONS_DISABLED
) {
if (typeof element.getAnimations !== 'function' || globalThis.BASE_UI_ANIMATIONS_DISABLED) {
fnToExecute();
} else {
frameRef.current = requestAnimationFrame(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"outDir": "build-tests",
"types": ["vitest/globals", "@types/chai", "@types/chai-dom"]
},
"include": ["src/**/*.spec.ts*", "src/**/*.test.ts*", "src/index.test.js", "test"],
"include": ["src/**/*.spec.ts*", "src/**/*.test.ts*", "src/index.test.js", "test", "src/global.d.ts"],
"references": [{ "path": "./tsconfig.build.json" }]
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@playwright/test": "1.49.1",
"@testing-library/dom": "^10.4.0",
"@types/chai": "^4.3.20",
"@types/chai-dom": "^1.11.3",
"@types/react": "^19.0.2",
"@types/react-dom": "^19.0.2",
"@types/sinon": "^17.0.3",
Expand Down
18 changes: 10 additions & 8 deletions test/setupVitest.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { beforeAll, afterAll, vi } from 'vitest';
/* eslint-disable no-var */
/* eslint-disable vars-on-top */
import { beforeAll, afterAll } from 'vitest';
import chai from 'chai';

import chaiDom from 'chai-dom';
import chaiPlugin from '@mui/internal-test-utils/chaiPlugin';

declare global {
var before: typeof beforeAll;
var after: typeof afterAll;
var BASE_UI_ANIMATIONS_DISABLED: boolean;
}

chai.use(chaiDom);
chai.use(chaiPlugin);

// @ts-ignore
// required for conformance tests (until everything is migrated to Vite)
globalThis.before = beforeAll;
// @ts-ignore
globalThis.after = afterAll;

// @ts-ignore
globalThis.vi = vi;

// @ts-ignore
globalThis.BASE_UI_ANIMATIONS_DISABLED = true;

const isVitestJsdom = process.env.VITEST_ENV === 'jsdom';
Expand Down
4 changes: 2 additions & 2 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"extends": "../tsconfig.base.json",
"compilerOptions": {
"module": "es2022",
"moduleResolution": "Node",
"moduleResolution": "bundler",
"types": ["vite/client", "vitest/globals"]
},
"include": ["e2e/**/*", "regressions/**/*"],
"include": ["e2e/**/*", "regressions/**/*", "./*.ts"],
"exclude": ["node_modules", "build"]
}

0 comments on commit 49eb462

Please sign in to comment.