2022-11-09 23:13:54 +08:00
import React , { useEffect } from 'react' ;
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' ,
2019-03-18 10:46:17 +08:00
title : ` ant design ` ,
2021-10-14 12:09:07 +08:00
avatar : 'https://joeschmoe.io/api/v1/random' ,
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 }
renderItem = { item = > (
< 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 }
renderItem = { item = > (
< 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 }
renderItem = { item = > (
< 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 }
renderItem = { item = > (
< 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 }
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 }
renderItem = { item = > (
< 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"
renderItem = { item = > < List.Item > { item . title } < / List.Item > }
/ > ,
) ;
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 }
rowKey = { item = > item . id }
renderItem = { item = > < List.Item > { item . title } < / List.Item > }
/ > ,
) ;
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 ` } ] }
rowKey = { item = > item . id }
renderItem = { item = > (
< 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 ] ) ;
} ) ;
2019-03-11 23:34:05 +08:00
} ) ;