Skip to content

Commit 1434cc1

Browse files
committed
Add stories to test lists themes.
1 parent 7027adc commit 1434cc1

File tree

6 files changed

+156
-5
lines changed

6 files changed

+156
-5
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as React from 'react';
2+
import expect from 'expect';
3+
import { render, screen } from '@testing-library/react';
4+
5+
import { Themed } from './List.stories';
6+
7+
describe('<InfiniteList />', () => {
8+
it('should be customized by a theme', async () => {
9+
render(<Themed />);
10+
expect(screen.queryByTestId('themed-list').classList).toContain(
11+
'custom-class'
12+
);
13+
});
14+
});

packages/ra-ui-materialui/src/list/InfiniteList.stories.tsx

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
useInfinitePaginationContext,
99
TestMemoryRouter,
1010
} from 'ra-core';
11-
import { Box, Button, Card, Typography } from '@mui/material';
11+
import { Box, Button, Card, ThemeOptions, Typography } from '@mui/material';
1212

1313
import { InfiniteList } from './InfiniteList';
1414
import { SimpleList } from './SimpleList';
@@ -24,6 +24,8 @@ import { SearchInput } from '../input';
2424
import { BulkDeleteButton, SelectAllButton, SortButton } from '../button';
2525
import { TopToolbar, Layout } from '../layout';
2626
import { BulkActionsToolbar } from './BulkActionsToolbar';
27+
import { deepmerge } from '@mui/utils';
28+
import { defaultLightTheme } from '../theme';
2729

2830
export default {
2931
title: 'ra-ui-materialui/list/InfiniteList',
@@ -89,11 +91,12 @@ const dataProvider = fakeRestProvider(
8991
500
9092
);
9193

92-
const Admin = ({ children, dataProvider, layout }: any) => (
94+
const Admin = ({ children, dataProvider, layout, ...props }: any) => (
9395
<TestMemoryRouter>
9496
<AdminContext
9597
dataProvider={dataProvider}
9698
i18nProvider={polyglotI18nProvider(() => defaultMessages, 'en')}
99+
{...props}
97100
>
98101
<AdminUI layout={layout}>{children}</AdminUI>
99102
</AdminContext>
@@ -437,3 +440,31 @@ export const PartialPagination = () => (
437440
/>
438441
</Admin>
439442
);
443+
444+
export const Themed = () => (
445+
<Admin
446+
dataProvider={dataProvider}
447+
theme={deepmerge(defaultLightTheme, {
448+
components: {
449+
RaInfiniteList: {
450+
defaultProps: {
451+
className: 'custom-class',
452+
perPage: 5,
453+
},
454+
},
455+
},
456+
} as ThemeOptions)}
457+
>
458+
<Resource
459+
name="books"
460+
list={() => (
461+
<InfiniteList data-testid={'themed-list'}>
462+
<SimpleList
463+
primaryText="%{title}"
464+
secondaryText="%{author}"
465+
/>
466+
</InfiniteList>
467+
)}
468+
/>
469+
</Admin>
470+
);

packages/ra-ui-materialui/src/list/List.spec.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from 'ra-core';
1010
import { createTheme, ThemeProvider } from '@mui/material/styles';
1111

12-
import { defaultTheme } from '../theme/defaultTheme';
12+
import { defaultTheme } from '../theme';
1313
import { List } from './List';
1414
import { Filter } from './filter';
1515
import { TextInput } from '../input';
@@ -22,6 +22,7 @@ import {
2222
PartialPagination,
2323
Default,
2424
SelectAllLimit,
25+
Themed,
2526
} from './List.stories';
2627

2728
const theme = createTheme(defaultTheme);
@@ -112,6 +113,13 @@ describe('<List />', () => {
112113
expect(screen.queryAllByText('Hello')).toHaveLength(1);
113114
});
114115

116+
it('should be customized by a theme', async () => {
117+
render(<Themed />);
118+
expect(screen.queryByTestId('themed-list').classList).toContain(
119+
'custom-class'
120+
);
121+
});
122+
115123
describe('empty', () => {
116124
it('should render an invite when the list is empty', async () => {
117125
const Dummy = () => {

packages/ra-ui-materialui/src/list/List.stories.tsx

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ import {
88
DataProvider,
99
} from 'ra-core';
1010
import fakeRestDataProvider from 'ra-data-fakerest';
11-
import { Box, Card, Typography, Button, Link as MuiLink } from '@mui/material';
11+
import {
12+
Box,
13+
Card,
14+
Typography,
15+
Button,
16+
Link as MuiLink,
17+
ThemeOptions,
18+
} from '@mui/material';
1219

1320
import { List } from './List';
1421
import { SimpleList } from './SimpleList';
@@ -22,6 +29,8 @@ import { BulkDeleteButton, ListButton, SelectAllButton } from '../button';
2229
import { ShowGuesser } from '../detail';
2330
import TopToolbar from '../layout/TopToolbar';
2431
import { BulkActionsToolbar } from './BulkActionsToolbar';
32+
import { deepmerge } from '@mui/utils';
33+
import { defaultLightTheme, RaThemeOptions } from '../theme';
2534

2635
export default { title: 'ra-ui-materialui/list/List' };
2736

@@ -801,3 +810,39 @@ export const ResponseMetadata = () => (
801810
</Admin>
802811
</TestMemoryRouter>
803812
);
813+
814+
export const Themed = () => (
815+
<TestMemoryRouter initialEntries={['/books']}>
816+
<Admin
817+
dataProvider={defaultDataProvider}
818+
theme={deepmerge(defaultLightTheme, {
819+
components: {
820+
RaList: {
821+
defaultProps: {
822+
className: 'custom-class',
823+
},
824+
styleOverrides: {
825+
root: {
826+
background: 'pink',
827+
828+
['& .MuiListItemText-primary']: {
829+
color: 'hotpink',
830+
fontWeight: 'bold',
831+
},
832+
},
833+
},
834+
},
835+
},
836+
} as ThemeOptions)}
837+
>
838+
<Resource
839+
name="books"
840+
list={() => (
841+
<List data-testid={'themed-list'}>
842+
<BookList />
843+
</List>
844+
)}
845+
/>
846+
</Admin>
847+
</TestMemoryRouter>
848+
);

packages/ra-ui-materialui/src/list/SimpleList/SimpleList.spec.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
RowClick,
2323
Standalone,
2424
StandaloneEmpty,
25+
Themed,
2526
} from './SimpleList.stories';
2627
import { Basic } from '../filter/FilterButton.stories';
2728

@@ -259,6 +260,13 @@ describe('<SimpleList />', () => {
259260
await screen.findByText('War and Peace');
260261
});
261262

263+
it('should be customized by a theme', async () => {
264+
render(<Themed />);
265+
expect(screen.queryByTestId('themed-list').classList).toContain(
266+
'custom-class'
267+
);
268+
});
269+
262270
describe('standalone', () => {
263271
it('should work without a ListContext', async () => {
264272
render(<Standalone />);

packages/ra-ui-materialui/src/list/SimpleList/SimpleList.stories.tsx

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ import {
1111
} from 'ra-core';
1212
import defaultMessages from 'ra-language-english';
1313
import polyglotI18nProvider from 'ra-i18n-polyglot';
14-
import { Alert, Box, FormControlLabel, FormGroup, Switch } from '@mui/material';
14+
import {
15+
Alert,
16+
Box,
17+
FormControlLabel,
18+
FormGroup,
19+
Switch,
20+
ThemeOptions,
21+
} from '@mui/material';
1522
import { Location } from 'react-router';
1623

1724
import { AdminUI } from '../../AdminUI';
@@ -21,6 +28,8 @@ import { List, ListProps } from '../List';
2128
import { RowClickFunction } from '../types';
2229
import { SimpleList } from './SimpleList';
2330
import { FunctionLinkType } from './SimpleListItem';
31+
import { deepmerge } from '@mui/utils';
32+
import { defaultLightTheme } from '../../theme';
2433

2534
export default { title: 'ra-ui-materialui/list/SimpleList' };
2635

@@ -430,3 +439,39 @@ export const StandaloneEmpty = () => (
430439
</AdminContext>
431440
</TestMemoryRouter>
432441
);
442+
443+
export const Themed = () => (
444+
<TestMemoryRouter>
445+
<AdminContext
446+
theme={deepmerge(defaultLightTheme, {
447+
components: {
448+
RaSimpleList: {
449+
defaultProps: {
450+
className: 'custom-class',
451+
},
452+
styleOverrides: {
453+
root: {
454+
background: 'pink',
455+
456+
['& .MuiListItemText-primary']: {
457+
color: 'hotpink',
458+
fontWeight: 'bold',
459+
},
460+
},
461+
},
462+
},
463+
},
464+
} as ThemeOptions)}
465+
>
466+
<ResourceContextProvider value="books">
467+
<SimpleList
468+
data-testid={'themed-list'}
469+
data={data.books}
470+
primaryText={record => record.title}
471+
secondaryText={record => record.author}
472+
tertiaryText={record => record.year}
473+
/>
474+
</ResourceContextProvider>
475+
</AdminContext>
476+
</TestMemoryRouter>
477+
);

0 commit comments

Comments
 (0)