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

Fix <AppBar> title disappears on locale change #8846

Merged
merged 2 commits into from
Apr 23, 2023
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
35 changes: 33 additions & 2 deletions packages/ra-ui-materialui/src/button/LocalesMenuButton.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import * as React from 'react';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { Basic } from './LocalesMenuButton.stories';
import {
fireEvent,
render,
screen,
waitFor,
within,
} from '@testing-library/react';
import { Basic, FullApp } from './LocalesMenuButton.stories';

describe('LocalesMenuButton', () => {
it('should allow to change language', async () => {
Expand All @@ -16,5 +22,30 @@ describe('LocalesMenuButton', () => {
await waitFor(() => {
expect(screen.queryByText('Tableau de bord')).not.toBeNull();
});

fireEvent.click(screen.getAllByText('Français')[0]);
fireEvent.click(screen.getByText('English'));
await waitFor(() => {
expect(screen.queryByText('Dashboard')).not.toBeNull();
});
});

it('should not make the title disappear', async () => {
const { container } = render(<FullApp />);

await screen.findByText('War and Peace');
let title = container.querySelector(
'#react-admin-title'
) as HTMLElement;
expect(within(title).queryByText('Books')).not.toBeNull();

fireEvent.click(screen.getAllByText('English')[0]);
expect(screen.queryAllByText('English').length).toEqual(2);
expect(screen.queryByText('Français')).not.toBeNull();
fireEvent.click(screen.getByText('Français'));

await screen.findAllByText('Livres');
title = container.querySelector('#react-admin-title') as HTMLElement;
expect(within(title).queryByText('Livres')).not.toBeNull();
});
});
5 changes: 4 additions & 1 deletion packages/ra-ui-materialui/src/layout/Title.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export const Title = (props: TitleProps) => {
// on first mount, we don't have the container yet, so we wait for it
useEffect(() => {
setContainer(container => {
if (container) return container;
const isInTheDom =
typeof document !== 'undefined' &&
document.body.contains(container);
if (container && isInTheDom) return container;
return typeof document !== 'undefined'
? document.getElementById('react-admin-title')
: null;
Expand Down