13
13
// limitations under the License.
14
14
15
15
import React from 'react' ;
16
- import { shallow } from 'enzyme' ;
16
+ import { render , screen } from '@testing-library/react' ;
17
+ import '@testing-library/jest-dom' ;
17
18
import { TraceId } from './TraceId' ;
18
19
import { getConfigValue } from '../../utils/config/get-config' ;
19
20
@@ -26,8 +27,6 @@ describe('TraceIdDisplayLength', () => {
26
27
const MOCK_TRACE_ID = '12345678901234567890' ;
27
28
const CUSTOM_CLASS = 'custom-class' ;
28
29
29
- let wrapper ;
30
-
31
30
const defaultProps = {
32
31
traceId : MOCK_TRACE_ID ,
33
32
} ;
@@ -36,26 +35,28 @@ describe('TraceIdDisplayLength', () => {
36
35
jest . clearAllMocks ( ) ;
37
36
} ) ;
38
37
39
- const createWrapper = ( props = { } ) => {
40
- return shallow ( < TraceId { ...defaultProps } { ...props } /> ) ;
38
+ const renderComponent = ( props = { } ) => {
39
+ render ( < TraceId { ...defaultProps } { ...props } /> ) ;
41
40
} ;
42
41
43
42
describe ( 'TraceIdDisplayLength Component' , ( ) => {
44
43
it ( 'renders the default traceIdLength 7' , ( ) => {
45
44
getConfigValue . mockReturnValue ( undefined ) ;
46
- wrapper = createWrapper ( ) ;
45
+ renderComponent ( ) ;
47
46
48
- const displayedText = wrapper . text ( ) ;
49
- expect ( displayedText ) . toEqual ( MOCK_TRACE_ID . slice ( 0 , DEFAULT_LENGTH ) ) ;
47
+ const displayed = MOCK_TRACE_ID . slice ( 0 , DEFAULT_LENGTH ) ;
48
+ expect ( screen . getByText ( displayed ) ) . toBeInTheDocument ( ) ;
49
+ expect ( screen . queryByText ( MOCK_TRACE_ID . slice ( 0 , DEFAULT_LENGTH + 1 ) ) ) . not . toBeInTheDocument ( ) ;
50
50
} ) ;
51
51
52
52
it ( 'renders the config length when provided' , ( ) => {
53
53
const configuredLength = 5 ;
54
54
getConfigValue . mockReturnValue ( configuredLength ) ;
55
- wrapper = createWrapper ( ) ;
55
+ renderComponent ( ) ;
56
56
57
- const displayedText = wrapper . text ( ) ;
58
- expect ( displayedText ) . toEqual ( MOCK_TRACE_ID . slice ( 0 , configuredLength ) ) ;
57
+ const displayed = MOCK_TRACE_ID . slice ( 0 , configuredLength ) ;
58
+ expect ( screen . getByText ( displayed ) ) . toBeInTheDocument ( ) ;
59
+ expect ( screen . queryByText ( MOCK_TRACE_ID . slice ( 0 , configuredLength + 1 ) ) ) . not . toBeInTheDocument ( ) ;
59
60
} ) ;
60
61
} ) ;
61
62
@@ -65,36 +66,43 @@ describe('TraceIdDisplayLength', () => {
65
66
const configuredLength = 10 ;
66
67
getConfigValue . mockReturnValue ( configuredLength ) ;
67
68
68
- wrapper = createWrapper ( { traceId : shortTraceId } ) ;
69
- expect ( wrapper . text ( ) ) . toEqual ( shortTraceId ) ;
69
+ renderComponent ( { traceId : shortTraceId } ) ;
70
+ expect ( screen . getByText ( shortTraceId ) ) . toBeInTheDocument ( ) ;
70
71
} ) ;
71
72
72
- it ( 'renders when traceId is undefiend' , ( ) => {
73
- wrapper = createWrapper ( { traceId : '' } ) ;
74
- expect ( wrapper . text ( ) ) . toEqual ( '' ) ;
73
+ it ( 'renders an empty <small> element when traceId is an empty string' , ( ) => {
74
+ const { container } = render ( < TraceId traceId = "" /> ) ;
75
+ const el = container . querySelector ( 'small' ) ;
76
+ expect ( el ) . toBeInTheDocument ( ) ;
77
+ expect ( el ) . toBeEmptyDOMElement ( ) ;
75
78
} ) ;
76
79
} ) ;
77
80
78
81
describe ( 'Style handling' , ( ) => {
79
82
it ( 'applies custom className when provided' , ( ) => {
80
- wrapper = createWrapper ( { className : CUSTOM_CLASS } ) ;
81
- expect ( wrapper . hasClass ( CUSTOM_CLASS ) ) . toBe ( true ) ;
83
+ getConfigValue . mockReturnValue ( undefined ) ;
84
+ renderComponent ( { className : CUSTOM_CLASS } ) ;
85
+
86
+ const el = screen . getByText ( MOCK_TRACE_ID . slice ( 0 , DEFAULT_LENGTH ) ) ;
87
+ expect ( el ) . toHaveClass ( CUSTOM_CLASS ) ;
82
88
} ) ;
83
89
84
90
it ( 'default classes for styling' , ( ) => {
85
- wrapper = createWrapper ( ) ;
86
- expect ( wrapper . hasClass ( 'TraceIDLength' ) ) . toBe ( true ) ;
87
- expect ( wrapper . hasClass ( 'u-tx-muted' ) ) . toBe ( true ) ;
91
+ renderComponent ( ) ;
92
+ const el = screen . getByText ( MOCK_TRACE_ID . slice ( 0 , DEFAULT_LENGTH ) ) ;
93
+
94
+ expect ( el ) . toHaveClass ( 'TraceIDLength' ) ;
95
+ expect ( el ) . toHaveClass ( 'u-tx-muted' ) ;
88
96
} ) ;
89
97
90
98
it ( 'adds a length-based class depending on the configuration' , ( ) => {
91
99
getConfigValue . mockReturnValue ( DEFAULT_LENGTH ) ;
92
- wrapper = createWrapper ( ) ;
93
- expect ( wrapper . hasClass ( 'TraceIDLength--short' ) ) . toBe ( true ) ;
100
+ renderComponent ( ) ;
101
+ expect ( screen . getByText ( MOCK_TRACE_ID . slice ( 0 , DEFAULT_LENGTH ) ) ) . toHaveClass ( 'TraceIDLength--short' ) ;
94
102
95
103
getConfigValue . mockReturnValue ( 32 ) ;
96
- wrapper = createWrapper ( ) ;
97
- expect ( wrapper . hasClass ( 'TraceIDLength--full' ) ) . toBe ( true ) ;
104
+ renderComponent ( ) ;
105
+ expect ( screen . getByText ( MOCK_TRACE_ID . slice ( 0 , 32 ) ) ) . toHaveClass ( 'TraceIDLength--full' ) ;
98
106
} ) ;
99
107
} ) ;
100
108
} ) ;
0 commit comments