Skip to content

Commit 293cc66

Browse files
committed
Test buttons default props.
1 parent 761914d commit 293cc66

13 files changed

+309
-12
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import * as React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import expect from 'expect';
4+
import { deepmerge } from '@mui/utils';
5+
import { ThemeOptions } from '@mui/material';
6+
import { ListContextProvider, testDataProvider } from 'ra-core';
7+
8+
import { AdminContext } from '../AdminContext';
9+
import { defaultLightTheme } from '../theme';
10+
import { BulkDeleteButton } from './BulkDeleteButton';
11+
12+
describe('<BulkDeleteButton />', () => {
13+
const dataProvider = testDataProvider({
14+
deleteMany: async () => ({ data: [{ id: 123 }] as any }),
15+
});
16+
17+
it('should be customized by a theme', () => {
18+
render(
19+
<AdminContext
20+
dataProvider={dataProvider}
21+
theme={deepmerge(defaultLightTheme, {
22+
components: {
23+
RaBulkDeleteButton: {
24+
defaultProps: {
25+
label: 'Bulk Delete',
26+
className: 'custom-class',
27+
'data-testid': 'themed-button',
28+
},
29+
},
30+
},
31+
} as ThemeOptions)}
32+
>
33+
<ListContextProvider
34+
value={
35+
{
36+
selectedIds: [123],
37+
onUnselectItems: () => {},
38+
} as any
39+
}
40+
>
41+
<BulkDeleteButton
42+
resource="books"
43+
mutationMode="pessimistic"
44+
/>
45+
</ListContextProvider>
46+
</AdminContext>
47+
);
48+
49+
const button = screen.getByTestId('themed-button');
50+
expect(button.textContent).toBe('Bulk Delete');
51+
expect(button.classList).toContain('custom-class');
52+
});
53+
});

packages/ra-ui-materialui/src/button/BulkExportButton.spec.tsx

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ import {
66
testDataProvider,
77
ListContextProvider,
88
} from 'ra-core';
9-
import { createTheme, ThemeProvider } from '@mui/material/styles';
9+
import { createTheme, ThemeProvider, ThemeOptions } from '@mui/material/styles';
10+
import { deepmerge } from '@mui/utils';
1011

1112
import { BulkExportButton } from './BulkExportButton';
1213

1314
const theme = createTheme();
1415

1516
describe('<BulkExportButton />', () => {
16-
it('should invoke dataProvider with meta', async () => {
17-
const exporter = jest.fn().mockName('exporter');
18-
const dataProvider = testDataProvider({
19-
getMany: jest.fn().mockResolvedValueOnce({ data: [], total: 0 }),
20-
});
17+
const exporter = jest.fn().mockName('exporter');
18+
const dataProvider = testDataProvider({
19+
getMany: jest.fn().mockResolvedValueOnce({ data: [], total: 0 }),
20+
});
2121

22+
it('should invoke dataProvider with meta', async () => {
2223
render(
2324
<CoreAdminContext dataProvider={dataProvider}>
2425
<ThemeProvider theme={theme}>
@@ -46,4 +47,38 @@ describe('<BulkExportButton />', () => {
4647
expect(exporter).toHaveBeenCalled();
4748
});
4849
});
50+
51+
it('should be customized by a theme', () => {
52+
render(
53+
<CoreAdminContext dataProvider={dataProvider}>
54+
<ThemeProvider
55+
theme={deepmerge(theme, {
56+
components: {
57+
RaBulkExportButton: {
58+
defaultProps: {
59+
label: 'Bulk Export',
60+
className: 'custom-class',
61+
'data-testid': 'themed-button',
62+
},
63+
},
64+
},
65+
} as ThemeOptions)}
66+
>
67+
<ListContextProvider
68+
value={{ selectedIds: ['selectedId'] }}
69+
>
70+
<BulkExportButton
71+
resource="test"
72+
exporter={exporter}
73+
meta={{ pass: 'meta' }}
74+
/>
75+
</ListContextProvider>
76+
</ThemeProvider>
77+
</CoreAdminContext>
78+
);
79+
80+
const button = screen.getByTestId('themed-button');
81+
expect(button.textContent).toBe('Bulk Export');
82+
expect(button.classList).toContain('custom-class');
83+
});
4984
});

packages/ra-ui-materialui/src/button/BulkUpdateButton.spec.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
import * as React from 'react';
22
import expect from 'expect';
33
import { render, screen } from '@testing-library/react';
4-
import { MutationMode } from './BulkUpdateButton.stories';
4+
import { MutationMode, Themed } from './BulkUpdateButton.stories';
55

66
describe('BulkUpdateButton', () => {
7+
it('should be customized by a theme', async () => {
8+
render(<Themed />);
9+
10+
const checkbox = await screen.findByRole('checkbox', {
11+
name: 'Select all',
12+
});
13+
checkbox.click();
14+
15+
const button = screen.queryByTestId('themed-button');
16+
expect(button.textContent).toBe('Bulk Update');
17+
expect(button.classList).toContain('custom-class');
18+
});
19+
720
describe('mutationMode', () => {
821
it('should ask confirmation before updating in pessimistic mode', async () => {
922
render(<MutationMode />);

packages/ra-ui-materialui/src/button/BulkUpdateButton.stories.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import { AdminContext } from '../AdminContext';
99
import { AdminUI } from '../AdminUI';
1010
import { List, Datagrid } from '../list';
1111
import { TextField, NumberField } from '../field';
12+
import { deepmerge } from '@mui/utils';
13+
import { defaultLightTheme } from '../theme';
14+
import { ThemeOptions } from '@mui/material';
1215

1316
export default { title: 'ra-ui-materialui/button/BulkUpdateButton' };
1417

@@ -89,9 +92,13 @@ const dataProvider = fakeRestDataProvider({
8992
authors: [],
9093
});
9194

92-
const Wrapper = ({ bulkActionButtons }) => (
95+
const Wrapper = ({ bulkActionButtons, theme = undefined }) => (
9396
<TestMemoryRouter initialEntries={['/books']}>
94-
<AdminContext dataProvider={dataProvider} i18nProvider={i18nProvider}>
97+
<AdminContext
98+
dataProvider={dataProvider}
99+
i18nProvider={i18nProvider}
100+
theme={theme}
101+
>
95102
<AdminUI>
96103
<Resource
97104
name="books"
@@ -160,3 +167,21 @@ export const MutationMode = () => (
160167
}
161168
/>
162169
);
170+
171+
export const Themed = () => (
172+
<Wrapper
173+
bulkActionButtons={<BulkUpdateButton data={{ reads: 0 }} />}
174+
theme={deepmerge(defaultLightTheme, {
175+
components: {
176+
RaBulkUpdateButton: {
177+
defaultProps: {
178+
label: 'Bulk Update',
179+
mutationMode: 'optimistic',
180+
className: 'custom-class',
181+
'data-testid': 'themed-button',
182+
},
183+
},
184+
},
185+
} as ThemeOptions)}
186+
/>
187+
);

packages/ra-ui-materialui/src/button/CloneButton.spec.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { render, screen } from '@testing-library/react';
44

55
import { AdminContext } from '../AdminContext';
66
import { CloneButton } from './CloneButton';
7+
import { deepmerge } from '@mui/utils';
8+
import { defaultLightTheme } from '../theme';
9+
import { ThemeOptions } from '@mui/material';
710

811
const invalidButtonDomProps = {
912
record: { id: 123, foo: 'bar' },
@@ -42,4 +45,31 @@ describe('<CloneButton />', () => {
4245

4346
spy.mockRestore();
4447
});
48+
49+
it('should be customized by a theme', () => {
50+
render(
51+
<AdminContext
52+
theme={deepmerge(defaultLightTheme, {
53+
components: {
54+
RaCloneButton: {
55+
defaultProps: {
56+
label: 'Clone',
57+
className: 'custom-class',
58+
'data-testid': 'themed-button',
59+
},
60+
},
61+
},
62+
} as ThemeOptions)}
63+
>
64+
<CloneButton
65+
resource="posts"
66+
record={{ id: 123, foo: 'bar' }}
67+
/>
68+
</AdminContext>
69+
);
70+
71+
const button = screen.getByTestId('themed-button');
72+
expect(button.textContent).toBe('Clone');
73+
expect(button.classList).toContain('custom-class');
74+
});
4575
});

packages/ra-ui-materialui/src/button/DeleteButton.spec.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
NotificationDefault,
66
NotificationTranslated,
77
FullApp,
8+
Themed,
89
} from './DeleteButton.stories';
910

1011
describe('<DeleteButton />', () => {
@@ -28,6 +29,14 @@ describe('<DeleteButton />', () => {
2829
expect(screen.queryAllByLabelText('Delete')).toHaveLength(1);
2930
});
3031
});
32+
33+
it('should be customized by a theme', async () => {
34+
render(<Themed />);
35+
const button = screen.queryByTestId('themed-button');
36+
expect(button.classList).toContain('custom-class');
37+
expect(button.textContent).toBe('Delete');
38+
});
39+
3140
describe('success notification', () => {
3241
it('should use a generic success message by default', async () => {
3342
render(<NotificationDefault />);

packages/ra-ui-materialui/src/button/DeleteButton.stories.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { colors, createTheme, Alert } from '@mui/material';
2+
import { colors, createTheme, Alert, ThemeOptions } from '@mui/material';
33
import polyglotI18nProvider from 'ra-i18n-polyglot';
44
import englishMessages from 'ra-language-english';
55
import frenchMessages from 'ra-language-french';
@@ -18,6 +18,8 @@ import { Datagrid } from '../list/datagrid/Datagrid';
1818
import { TextField } from '../field/TextField';
1919
import { AdminUI } from '../AdminUI';
2020
import { Notification } from '../layout';
21+
import { deepmerge } from '@mui/utils';
22+
import { defaultLightTheme } from '../theme';
2123

2224
const theme = createTheme({
2325
palette: {
@@ -385,3 +387,22 @@ export const SuccessMessage = () => {
385387
</AdminContext>
386388
);
387389
};
390+
391+
export const Themed = () => (
392+
<AdminContext
393+
theme={deepmerge(defaultLightTheme, {
394+
components: {
395+
RaDeleteButton: {
396+
defaultProps: {
397+
label: 'Delete',
398+
className: 'custom-class',
399+
},
400+
},
401+
},
402+
} as ThemeOptions)}
403+
>
404+
<ResourceContextProvider value="posts">
405+
<DeleteButton data-testid={'themed-button'} record={{ id: 1 }} />
406+
</ResourceContextProvider>
407+
</AdminContext>
408+
);

packages/ra-ui-materialui/src/button/ListButton.spec.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22
import { fireEvent, render, screen } from '@testing-library/react';
33
import expect from 'expect';
4-
import { Basic, AccessControl } from './ListButton.stories';
4+
import { Basic, AccessControl, Themed } from './ListButton.stories';
55

66
const invalidButtonDomProps = {
77
redirect: 'list',
@@ -30,4 +30,11 @@ describe('<ListButton />', () => {
3030
fireEvent.click(screen.getByLabelText('Allow accessing books'));
3131
await screen.findByLabelText('List');
3232
});
33+
34+
it('should be customized by a theme', async () => {
35+
render(<Themed />);
36+
const button = screen.queryByTestId('themed-button');
37+
expect(button.classList).toContain('custom-class');
38+
expect(button.textContent).toBe('List');
39+
});
3340
});

packages/ra-ui-materialui/src/button/ListButton.stories.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import { TextInput } from '../input/TextInput';
2121
import { ListButton } from './ListButton';
2222
import { Edit } from '../detail/Edit';
2323
import { TopToolbar } from '../layout';
24+
import { deepmerge } from '@mui/utils';
25+
import { defaultLightTheme } from '../theme';
26+
import { ThemeOptions } from '@mui/material';
2427

2528
export default { title: 'ra-ui-materialui/button/ListButton' };
2629

@@ -234,3 +237,29 @@ const dataProvider = fakeRestDataProvider({
234237
],
235238
authors: [],
236239
});
240+
241+
export const Themed = ({ buttonProps }: { buttonProps?: any }) => (
242+
<TestMemoryRouter>
243+
<AdminContext
244+
theme={deepmerge(defaultLightTheme, {
245+
components: {
246+
RaListButton: {
247+
defaultProps: {
248+
label: 'List',
249+
className: 'custom-class',
250+
},
251+
},
252+
},
253+
} as ThemeOptions)}
254+
>
255+
<ResourceContextProvider value="books">
256+
<RecordContextProvider value={{ id: 1 }}>
257+
<ListButton
258+
data-testid={'themed-button'}
259+
{...buttonProps}
260+
/>
261+
</RecordContextProvider>
262+
</ResourceContextProvider>
263+
</AdminContext>
264+
</TestMemoryRouter>
265+
);

packages/ra-ui-materialui/src/button/ShowButton.spec.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
33
import expect from 'expect';
4-
import { Basic, AccessControl } from './ShowButton.stories';
4+
import { Basic, AccessControl, Themed } from './ShowButton.stories';
55

66
const invalidButtonDomProps = {
77
redirect: 'list',
@@ -43,4 +43,11 @@ describe('<ShowButton />', () => {
4343
expect(screen.queryAllByLabelText('Show')).toHaveLength(1);
4444
});
4545
});
46+
47+
it('should be customized by a theme', async () => {
48+
render(<Themed />);
49+
const button = screen.queryByTestId('themed-button');
50+
expect(button.classList).toContain('custom-class');
51+
expect(button.textContent).toBe('Show');
52+
});
4653
});

0 commit comments

Comments
 (0)