11import React from 'react'
22import { render , screen , userEvent } from 'uiSrc/utils/test-utils'
3-
43import { rdiInstanceFactory } from 'uiSrc/mocks/rdi/RdiInstance.factory'
5-
64import RdiInstancesListCell from './RdiInstancesListCell'
75import { lastConnectionFormat } from 'uiSrc/utils'
86
@@ -19,7 +17,8 @@ jest.mock('uiSrc/utils', () => ({
1917 lastConnectionFormat : jest . fn ( ( ) => '3 min ago' ) ,
2018} ) )
2119
22- const buildRow = (
20+ const makeProps = (
21+ columnId : string ,
2322 overrides : Partial < ReturnType < typeof rdiInstanceFactory . build > > = { } ,
2423) => {
2524 const instance = rdiInstanceFactory . build ( {
@@ -28,49 +27,55 @@ const buildRow = (
2827 url : 'https://example' ,
2928 ...overrides ,
3029 } )
31- return { row : { original : instance } as any , instance }
30+ return {
31+ row : { original : instance } as any ,
32+ column : { id : columnId } as any ,
33+ instance,
34+ }
3235}
3336
3437describe ( 'RdiInstancesListCell' , ( ) => {
3538 beforeEach ( ( ) => {
3639 jest . clearAllMocks ( )
3740 } )
3841
39- it ( 'should render null when field is not provided' , ( ) => {
40- const { row } = buildRow ( )
41- const { container } = render ( < RdiInstancesListCell { ...( { row } as any ) } /> )
42+ it ( 'should render null when value is missing for the column' , ( ) => {
43+ const { row, column } = makeProps ( 'version' , { version : undefined as any } )
44+ const { container } = render (
45+ < RdiInstancesListCell { ...( { row, column } as any ) } /> ,
46+ )
4247 expect ( container . firstChild ) . toBeNull ( )
4348 } )
4449
4550 it ( 'should render text value and data-testid for a string field (name)' , ( ) => {
46- const { row, instance } = buildRow ( { id : 'cell-1' , name : 'My Endpoint' } )
51+ const { row, column, instance } = makeProps ( 'name' , {
52+ id : 'cell-1' ,
53+ name : 'My Endpoint' ,
54+ } )
4755
48- render ( < RdiInstancesListCell { ...( { row } as any ) } field = "name" /> )
56+ render ( < RdiInstancesListCell { ...( { row, column } as any ) } /> )
4957
5058 expect ( screen . getByText ( 'My Endpoint' ) ) . toBeInTheDocument ( )
51- // data-testid includes id and text
5259 expect (
5360 screen . getByTestId ( `rdi-list-cell-${ instance . id } -${ instance . name } ` ) ,
5461 ) . toBeInTheDocument ( )
5562 } )
5663
57- it ( 'should not show copy icon by default ' , ( ) => {
58- const { row } = buildRow ( )
64+ it ( 'should not show copy icon for non-url field ' , ( ) => {
65+ const { row, column } = makeProps ( 'name' )
5966
60- render ( < RdiInstancesListCell { ...( { row } as any ) } field = "url" /> )
67+ render ( < RdiInstancesListCell { ...( { row, column } as any ) } /> )
6168
6269 expect ( screen . queryByRole ( 'button' ) ) . not . toBeInTheDocument ( )
6370 } )
6471
6572 it ( 'should show copy icon and call handleCopyUrl with url text and id' , async ( ) => {
66- const { row, instance } = buildRow ( {
73+ const { row, column , instance } = makeProps ( 'url' , {
6774 id : 'cpy-1' ,
6875 url : 'https://ri.example' ,
6976 } )
7077
71- render (
72- < RdiInstancesListCell { ...( { row } as any ) } field = "url" withCopyIcon /> ,
73- )
78+ render ( < RdiInstancesListCell { ...( { row, column } as any ) } /> )
7479
7580 const btn = screen . getByRole ( 'button' )
7681 await userEvent . click ( btn , { pointerEventsCheck : 0 } )
@@ -82,27 +87,17 @@ describe('RdiInstancesListCell', () => {
8287 expect ( id ) . toBe ( instance . id )
8388 } )
8489
85- it ( 'should format lastConnection via lastConnectionFormat and pass formatted text to handler ' , async ( ) => {
90+ it ( 'should format lastConnection via lastConnectionFormat and render formatted text (no copy icon) ' , async ( ) => {
8691 const date = new Date ( '2023-01-01T00:00:00.000Z' )
87- const { row, instance } = buildRow ( { id : 'last-1' , lastConnection : date } )
88-
89- render (
90- < RdiInstancesListCell
91- { ...( { row } as any ) }
92- field = "lastConnection"
93- withCopyIcon
94- /> ,
95- )
92+ const { row, column } = makeProps ( 'lastConnection' , {
93+ id : 'last-1' ,
94+ lastConnection : date ,
95+ } )
96+
97+ render ( < RdiInstancesListCell { ...( { row, column } as any ) } /> )
9698
97- // Uses mocked formatter
9899 expect ( lastConnectionFormat ) . toHaveBeenCalledWith ( date as any )
99100 expect ( screen . getByText ( '3 min ago' ) ) . toBeInTheDocument ( )
100-
101- const btn = screen . getByRole ( 'button' )
102- await userEvent . click ( btn , { pointerEventsCheck : 0 } )
103-
104- const [ , text , id ] = mockHandleCopyUrl . mock . calls [ 0 ]
105- expect ( text ) . toBe ( '3 min ago' )
106- expect ( id ) . toBe ( instance . id )
101+ expect ( screen . queryByRole ( 'button' ) ) . not . toBeInTheDocument ( )
107102 } )
108103} )
0 commit comments