2022-11-09 23:13:54 +08:00
import React , { useEffect } from 'react' ;
2024-04-08 14:04:08 +08:00
2019-03-11 23:34:05 +08:00
import List from '..' ;
2022-11-09 23:13:54 +08:00
import { pureRender , render } from '../../../tests/utils' ;
2022-06-22 14:57:09 +08:00
import ConfigProvider from '../../config-provider' ;
2019-03-11 23:34:05 +08:00
describe ( 'List Item Layout' , ( ) = > {
2019-03-18 10:46:17 +08:00
const data = [
{
key : 1 ,
2020-05-15 17:18:55 +08:00
href : 'https://ant.design' ,
2023-02-20 10:24:40 +08:00
title : 'ant design' ,
2024-01-18 17:18:40 +08:00
avatar : 'https://api.dicebear.com/7.x/miniavs/svg?seed=10' ,
2019-03-18 10:46:17 +08:00
description :
'Ant Design, a design language for background applications, is refined by Ant UED Team.' ,
content :
'We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure), to help people create their product prototypes beautifully and efficiently.' ,
extra : 'extra' ,
} ,
] ;
2019-03-11 23:34:05 +08:00
it ( 'horizontal itemLayout List which contains string nodes should not be flex container' , ( ) = > {
2022-06-01 13:45:51 +08:00
const { container : wrapper } = render (
2019-03-11 23:34:05 +08:00
< List
dataSource = { data }
2022-11-13 14:33:07 +08:00
renderItem = { ( item ) = > (
2019-03-11 23:34:05 +08:00
< List.Item key = { item . title } >
I am < span > ant < / span > design list item
< / List.Item >
) }
2019-03-18 10:46:17 +08:00
/ > ,
2019-03-11 23:34:05 +08:00
) ;
2022-06-01 13:45:51 +08:00
expect (
wrapper . querySelectorAll ( '.ant-list-item' ) [ 0 ] . classList . contains ( 'ant-list-item-no-flex' ) ,
) . toBe ( true ) ;
2019-03-11 23:34:05 +08:00
} ) ;
2022-06-01 13:45:51 +08:00
it ( 'horizontal itemLayout List should be flex container by default' , ( ) = > {
const { container : wrapper } = render (
2019-03-11 23:34:05 +08:00
< List
dataSource = { data }
2022-11-13 14:33:07 +08:00
renderItem = { ( item ) = > (
2019-03-11 23:34:05 +08:00
< List.Item key = { item . title } >
< List.Item.Meta
title = { < a href = { item . href } > { item . title } < / a > }
description = { item . description }
/ >
< / List.Item >
) }
2019-03-18 10:46:17 +08:00
/ > ,
2019-03-11 23:34:05 +08:00
) ;
2022-06-01 13:45:51 +08:00
expect (
2022-09-05 19:41:32 +08:00
wrapper . querySelector ( '.ant-list-item' ) ? . classList . contains ( 'ant-list-item-no-flex' ) ,
2022-06-01 13:45:51 +08:00
) . toBe ( false ) ;
2019-03-11 23:34:05 +08:00
} ) ;
it ( 'vertical itemLayout List should be flex container when there is extra node' , ( ) = > {
2022-06-01 13:45:51 +08:00
const { container : wrapper } = render (
2019-03-11 23:34:05 +08:00
< List
itemLayout = "vertical"
dataSource = { data }
2022-11-13 14:33:07 +08:00
renderItem = { ( item ) = > (
2019-03-11 23:34:05 +08:00
< List.Item key = { item . title } extra = { item . extra } >
< List.Item.Meta
title = { < a href = { item . href } > { item . title } < / a > }
description = { item . description }
/ >
< / List.Item >
) }
2019-03-18 10:46:17 +08:00
/ > ,
2019-03-11 23:34:05 +08:00
) ;
2022-06-01 13:45:51 +08:00
expect (
wrapper . querySelectorAll ( '.ant-list-item' ) [ 0 ] . classList . contains ( 'ant-list-item-no-flex' ) ,
) . toBe ( false ) ;
2019-03-11 23:34:05 +08:00
} ) ;
it ( 'vertical itemLayout List should not be flex container when there is not extra node' , ( ) = > {
2022-06-01 13:45:51 +08:00
const { container : wrapper } = render (
2019-03-11 23:34:05 +08:00
< List
itemLayout = "vertical"
dataSource = { data }
2022-11-13 14:33:07 +08:00
renderItem = { ( item ) = > (
2019-03-11 23:34:05 +08:00
< List.Item key = { item . title } >
< List.Item.Meta
title = { < a href = { item . href } > { item . title } < / a > }
description = { item . description }
/ >
< / List.Item >
) }
2019-03-18 10:46:17 +08:00
/ > ,
2019-03-11 23:34:05 +08:00
) ;
2022-06-01 13:45:51 +08:00
expect (
wrapper . querySelectorAll ( '.ant-list-item' ) [ 0 ] . classList . contains ( 'ant-list-item-no-flex' ) ,
) . toBe ( true ) ;
2019-03-11 23:34:05 +08:00
} ) ;
it ( 'horizontal itemLayout List should accept extra node' , ( ) = > {
2022-06-01 13:45:51 +08:00
const { container : wrapper } = render (
2019-03-11 23:34:05 +08:00
< List
dataSource = { data }
2022-11-13 14:33:07 +08:00
renderItem = { ( item ) = > (
2019-08-02 17:59:42 +08:00
< List.Item
key = { item . title }
actions = { [ < a key = "action" > Action < / a > ] }
extra = { < span > { item . extra } < / span > }
>
2019-03-11 23:34:05 +08:00
< List.Item.Meta
title = { < a href = { item . href } > { item . title } < / a > }
description = { item . description }
/ >
< / List.Item >
) }
2019-03-18 10:46:17 +08:00
/ > ,
2019-03-11 23:34:05 +08:00
) ;
2022-06-01 13:45:51 +08:00
expect ( wrapper . firstChild ) . toMatchSnapshot ( ) ;
2019-03-11 23:34:05 +08:00
} ) ;
2020-01-02 19:10:16 +08:00
it ( 'should render in RTL direction' , ( ) = > {
2022-06-01 13:45:51 +08:00
const { container : wrapper } = render (
2020-01-02 19:10:16 +08:00
< ConfigProvider direction = "rtl" >
< List
dataSource = { data }
2022-11-13 14:33:07 +08:00
renderItem = { ( item ) = > (
2020-01-02 19:10:16 +08:00
< List.Item
key = { item . title }
actions = { [ < a key = "action" > Action < / a > ] }
extra = { < span > { item . extra } < / span > }
>
< List.Item.Meta
title = { < a href = { item . href } > { item . title } < / a > }
description = { item . description }
/ >
< / List.Item >
) }
/ >
< / ConfigProvider > ,
) ;
2022-06-01 13:45:51 +08:00
expect ( wrapper . firstChild ) . toMatchSnapshot ( ) ;
2020-01-02 19:10:16 +08:00
} ) ;
2020-05-27 22:16:35 +08:00
it ( 'rowKey could be string' , ( ) = > {
const dataWithId = [
{
id : 1 ,
title : ` ant design ` ,
} ,
{
id : 2 ,
title : ` ant design ` ,
} ,
{
id : 3 ,
title : ` ant design ` ,
} ,
] ;
2022-06-01 13:45:51 +08:00
const { container : wrapper } = render (
2020-05-27 22:16:35 +08:00
< List
dataSource = { dataWithId }
rowKey = "id"
2022-11-13 14:33:07 +08:00
renderItem = { ( item ) = > < List.Item > { item . title } < / List.Item > }
2020-05-27 22:16:35 +08:00
/ > ,
) ;
2022-06-01 13:45:51 +08:00
expect ( wrapper . firstChild ) . toMatchSnapshot ( ) ;
2020-05-27 22:16:35 +08:00
} ) ;
it ( 'rowKey could be function' , ( ) = > {
const dataWithId = [
{
id : 1 ,
title : ` ant design ` ,
} ,
{
id : 2 ,
title : ` ant design ` ,
} ,
{
id : 3 ,
title : ` ant design ` ,
} ,
] ;
2022-06-01 13:45:51 +08:00
const { container : wrapper } = render (
2020-05-27 22:16:35 +08:00
< List
dataSource = { dataWithId }
2022-11-13 14:33:07 +08:00
rowKey = { ( item ) = > item . id }
renderItem = { ( item ) = > < List.Item > { item . title } < / List.Item > }
2020-05-27 22:16:35 +08:00
/ > ,
) ;
2022-06-01 13:45:51 +08:00
expect ( wrapper . firstChild ) . toMatchSnapshot ( ) ;
2020-05-27 22:16:35 +08:00
} ) ;
2022-05-07 13:34:40 +08:00
it ( 'should ref' , ( ) = > {
2022-09-05 19:41:32 +08:00
const ref = React . createRef < HTMLElement > ( ) ;
2022-05-07 13:34:40 +08:00
render ( < List.Item ref = { ref } > Item < / List.Item > ) ;
expect ( ref . current ) . toHaveClass ( 'ant-list-item' ) ;
} ) ;
it ( 'should grid ref' , ( ) = > {
2022-09-05 19:41:32 +08:00
const ref = React . createRef < HTMLElement > ( ) ;
2022-05-07 13:34:40 +08:00
render (
2022-09-05 19:41:32 +08:00
< List grid = { { } } >
2022-05-07 13:34:40 +08:00
< List.Item ref = { ref } > Item < / List.Item > ,
< / List > ,
) ;
expect ( ref . current ) . toHaveClass ( 'ant-col' ) ;
} ) ;
2022-11-09 23:13:54 +08:00
it ( 'react key' , ( ) = > {
const loadId : number [ ] = [ ] ;
const Demo = ( { id } : { id : number } ) = > {
useEffect ( ( ) = > {
loadId . push ( id ) ;
} , [ ] ) ;
return < div > { id } < / div > ;
} ;
const getDom = ( id = 1 ) = > (
< List
dataSource = { [ { id , title : ` ant design ` } ] }
2022-11-13 14:33:07 +08:00
rowKey = { ( item ) = > item . id }
renderItem = { ( item ) = > (
2022-11-09 23:13:54 +08:00
< List.Item >
< Demo id = { item . id } / >
< / List.Item >
) }
/ >
) ;
const { rerender } = pureRender ( getDom ( 1 ) ) ;
rerender ( getDom ( 3 ) ) ;
rerender ( getDom ( 5 ) ) ;
expect ( loadId ) . toEqual ( [ 1 , 3 , 5 ] ) ;
} ) ;
2023-04-07 23:39:23 +08:00
it ( 'List.Item.Meta title should have no default margin' , ( ) = > {
const { container } = render (
< List
dataSource = { [ { id : 1 , title : ` ant design ` } ] }
renderItem = { ( item ) = > (
< List.Item >
< List.Item.Meta title = { item . title } / >
< / List.Item >
) }
/ > ,
) ;
const title = container . querySelector ( '.ant-list-item-meta-title' ) ;
expect ( title && getComputedStyle ( title ) . margin ) . toEqual ( '0px 0px 4px 0px' ) ;
} ) ;
2019-03-11 23:34:05 +08:00
} ) ;