Skip to content

Commit

Permalink
Browser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot committed Oct 9, 2024
1 parent 9e3c284 commit a105b2a
Show file tree
Hide file tree
Showing 21 changed files with 480 additions and 115 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
"@types/yargs": "^17.0.33",
"@typescript-eslint/eslint-plugin": "^7.18.0",
"@typescript-eslint/parser": "^7.18.0",
"@vitest/browser": "^2.1.2",
"@vitest/coverage-v8": "^2.1.2",
"babel-loader": "^9.2.1",
"babel-plugin-istanbul": "^7.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-joy/src/styles/CssVarsProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('[Joy] CssVarsProvider', () => {
beforeEach(() => {
originalMatchmedia = window.matchMedia;
// Create mocks of localStorage getItem and setItem functions
Object.defineProperty(global, 'localStorage', {
Object.defineProperty(globalThis, 'localStorage', {
value: {
getItem: (key: string) => storage[key],
setItem: (key: string, value: string) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-joy/src/styles/extendTheme.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ describe('extendTheme', () => {
beforeEach(() => {
originalMatchmedia = window.matchMedia;
// Create mocks of localStorage getItem and setItem functions
Object.defineProperty(global, 'localStorage', {
Object.defineProperty(globalThis, 'localStorage', {
value: {
getItem: (key) => storage[key],
setItem: (key, value) => {
Expand Down
10 changes: 2 additions & 8 deletions packages/mui-material/src/Button/Button.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,14 +624,8 @@ describe('<Button />', () => {
expect(button.querySelector('.pulsate-focus-visible')).to.equal(null);
});

describe('server-side', () => {
before(function beforeHook() {
// Only run the test on node.
if (!/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}
});

// eslint-disable-next-line no-undef
describeSkipIf(!/jsdom/.test(window.navigator.userAgent))('server-side', () => {
it('should server-side render', () => {
const { container } = renderToString(<Button>Hello World</Button>);
expect(container.firstChild).to.have.text('Hello World');
Expand Down
10 changes: 2 additions & 8 deletions packages/mui-material/src/Fab/Fab.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,8 @@ describe('<Fab />', () => {
expect(renderedIconChild).to.have.class(childClassName);
});

describe('server-side', () => {
before(function beforeHook() {
// Only run the test on node.
if (!/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}
});

// eslint-disable-next-line no-undef
describeSkipIf(!/jsdom/.test(window.navigator.userAgent))('server-side', () => {
it('should server-side render', () => {
const { container } = renderToString(<Fab>Fab</Fab>);
expect(container.firstChild).to.have.text('Fab');
Expand Down
11 changes: 3 additions & 8 deletions packages/mui-material/src/Portal/Portal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@ import Portal, { PortalProps } from '@mui/material/Portal';
describe('<Portal />', () => {
const { render, renderToString } = createRenderer();

describe('server-side', () => {
before(function beforeHook() {
// Only run the test on node.
if (!/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}
});

// @ts-expect-error
// eslint-disable-next-line no-undef
describeSkipIf(!/jsdom/.test(window.navigator.userAgent))('server-side', () => {
it('render nothing on the server', () => {
const { container } = renderToString(
<Portal>
Expand Down
8 changes: 2 additions & 6 deletions packages/mui-material/src/Tabs/Tabs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,15 +368,11 @@ describe('<Tabs />', () => {
]);
});

describe('hidden tab / tabs', () => {
// eslint-disable-next-line no-undef
describeSkipIf(!/jsdom/.test(window.navigator.userAgent))('hidden tab / tabs', () => {
let nodeEnv;

before(function test() {
if (!/jsdom/.test(window.navigator.userAgent)) {
this.skip();
return;
}

nodeEnv = process.env.NODE_ENV;
// We can't use a regular assignment, because it causes a syntax error in Karma
Object.defineProperty(process.env, 'NODE_ENV', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ describe('<TextareaAutosize />', () => {
expect(parseInt(input.style.height, 10)).to.be.within(15, 17);
});

describe('layout', () => {
// @ts-expect-error
// eslint-disable-next-line no-undef
describeSkipIf(!/jsdom/.test(window.navigator.userAgent))('layout', () => {
const getComputedStyleStub = new Map<Element, Partial<CSSStyleDeclaration>>();
function setLayout(
input: HTMLTextAreaElement,
Expand Down Expand Up @@ -166,11 +168,6 @@ describe('<TextareaAutosize />', () => {
}

before(function beforeHook() {
// Only run the test on node.
if (!/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}

stub(window, 'getComputedStyle').value(
(node: Element) => getComputedStyleStub.get(node) || {},
);
Expand Down
10 changes: 2 additions & 8 deletions packages/mui-material/src/ToggleButton/ToggleButton.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,8 @@ describe('<ToggleButton />', () => {
});
});

describe('server-side', () => {
before(function beforeHook() {
// Only run the test on node.
if (!/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}
});

// eslint-disable-next-line no-undef
describeSkipIf(!/jsdom/.test(window.navigator.userAgent))('server-side', () => {
it('should server-side render', () => {
const { container } = renderToString(<ToggleButton value="hello">Hello World</ToggleButton>);
expect(container.firstChild).to.have.text('Hello World');
Expand Down
4 changes: 2 additions & 2 deletions packages/mui-material/src/internal/SwitchBase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ describe('<SwitchBase />', () => {

describe('check transitioning between controlled states throws errors', () => {
it('should error when uncontrolled and changed to controlled', function test() {
if (global.didWarnControlledToUncontrolled) {
if (globalThis.didWarnControlledToUncontrolled) {
this.skip();
}

Expand All @@ -421,7 +421,7 @@ describe('<SwitchBase />', () => {
});

it('should error when controlled and changed to uncontrolled', function test() {
if (global.didWarnControlledToUncontrolled) {
if (globalThis.didWarnControlledToUncontrolled) {
this.skip();
}

Expand Down
2 changes: 1 addition & 1 deletion packages/mui-material/src/styles/ThemeProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('ThemeProvider', () => {
originalMatchmedia = window.matchMedia;
// Create mocks of localStorage getItem and setItem functions
storage = {};
Object.defineProperty(global, 'localStorage', {
Object.defineProperty(globalThis, 'localStorage', {
value: {
getItem: (key: string) => storage[key],
setItem: (key: string, value: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('[Material UI] ThemeProviderWithVars', () => {
beforeEach(() => {
originalMatchmedia = window.matchMedia;
// Create mocks of localStorage getItem and setItem functions
Object.defineProperty(global, 'localStorage', {
Object.defineProperty(globalThis, 'localStorage', {
value: {
getItem: (key) => storage[key],
setItem: (key, value) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-material/src/styles/extendTheme.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('extendTheme', () => {
beforeEach(() => {
originalMatchmedia = window.matchMedia;
// Create mocks of localStorage getItem and setItem functions
Object.defineProperty(global, 'localStorage', {
Object.defineProperty(globalThis, 'localStorage', {
value: {
getItem: (key) => storage[key],
setItem: (key, value) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ describe('useScrollTrigger', () => {
});
});

describe('scroll', () => {
// eslint-disable-next-line no-undef
describeSkipIf(!/jsdom/.test(window.navigator.userAgent))('scroll', () => {
const triggerRef = React.createRef();
const containerRef = React.createRef(); // Get the scroll container's parent
const getContainer = () => containerRef.current.children[0]; // Get the scroll container
Expand All @@ -75,13 +76,6 @@ describe('useScrollTrigger', () => {
customContainer: PropTypes.bool,
};

before(function beforeHook() {
// Only run the test on node.
if (!/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}
});

function dispatchScroll(offset, element = window) {
act(() => {
element.pageYOffset = offset;
Expand Down
10 changes: 7 additions & 3 deletions packages/mui-material/test/integration/Menu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,18 +308,22 @@ describe('<Menu /> integration', () => {
);
});

it('closes the menu when Tabbing while the list is active', async () => {
render(<ButtonMenu />);
it.only('closes the menu when Tabbing while the list is active', async () => {
const { user } = render(<ButtonMenu />);

const trigger = screen.getByRole('button');
await act(async () => {
trigger.focus();
});

await act(async () => {
trigger.click();
});

// react-transition-group uses one commit per state transition so we need to wait a bit
fireEvent.keyDown(screen.getAllByRole('menuitem')[0], { key: 'Tab' });
clock.tick(0);

// await user.keyboard('[Tab]');

expect(screen.getByRole('menu', { hidden: true })).toBeInaccessible();
});
Expand Down
11 changes: 11 additions & 0 deletions packages/mui-material/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,18 @@ export default defineConfig({
// We use performance.now in the codebase
toFake: [...configDefaults.fakeTimers.toFake, 'performance'],
},
browser: {
enabled: false, // enabled through CLI
name: 'chromium',
provider: 'playwright',
headless: !!process.env.CI,
viewport: {
width: 1024,
height: 896,
},
},
},

resolve: {
alias: {
'@mui/internal-test-utils': path.resolve(MONOREPO_ROOT, './packages-internal/test-utils/src'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('InitColorSchemeScript', () => {

beforeEach(() => {
// Create mocks of localStorage getItem and setItem functions
Object.defineProperty(global, 'localStorage', {
Object.defineProperty(globalThis, 'localStorage', {
value: {
getItem: (key) => storage[key],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('createCssVarsProvider', () => {
originalMatchmedia = window.matchMedia;

// Create mocks of localStorage getItem and setItem functions
Object.defineProperty(global, 'localStorage', {
Object.defineProperty(globalThis, 'localStorage', {
value: {
getItem: spy((key) => storage[key]),
setItem: spy((key, value) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('useCurrentColorScheme', () => {
// clear the localstorage
storage = {};
// Create mocks of localStorage getItem and setItem functions
Object.defineProperty(global, 'localStorage', {
Object.defineProperty(globalThis, 'localStorage', {
value: {
getItem: spy((key) => storage[key]),
setItem: spy((key, value) => {
Expand Down
Loading

0 comments on commit a105b2a

Please sign in to comment.