@@ -50,6 +50,8 @@ const mockTabs = [
50
50
) ) }
51
51
</ ul >
52
52
) ,
53
+ expectedTexts : mockCVData . keyQualifications ,
54
+ unexpectedTexts : [ "Example Company" , "University of Example" ] ,
53
55
} ,
54
56
{
55
57
id : "experience" ,
@@ -67,6 +69,12 @@ const mockTabs = [
67
69
) ) }
68
70
</ div >
69
71
) ,
72
+ expectedTexts : [
73
+ "2020-2022 - Example Company" ,
74
+ "Software Developer" ,
75
+ "Worked on various projects" ,
76
+ ] ,
77
+ unexpectedTexts : [ "Qualification 1" ] ,
70
78
} ,
71
79
{
72
80
id : "education" ,
@@ -84,72 +92,60 @@ const mockTabs = [
84
92
) ) }
85
93
</ div >
86
94
) ,
95
+ expectedTexts : [
96
+ "2016-2020 - University of Example" ,
97
+ "Bachelor in Computer Science" ,
98
+ "Studied various aspects of computer science" ,
99
+ ] ,
100
+ unexpectedTexts : [ "Qualification 1" ] ,
87
101
} ,
88
102
] ;
89
103
90
104
describe ( "Tabs" , ( ) => {
91
- it ( "renders all CV tab labels" , ( ) => {
92
- render ( < Tabs tabs = { mockTabs } /> ) ;
93
- expect (
94
- screen . getByRole ( "tab" , { name : "Nøkkelkvalifikasjoner" } )
95
- ) . toBeInTheDocument ( ) ;
96
- expect ( screen . getByRole ( "tab" , { name : "Erfaring" } ) ) . toBeInTheDocument ( ) ;
97
- expect ( screen . getByRole ( "tab" , { name : "Utdanning" } ) ) . toBeInTheDocument ( ) ;
98
- } ) ;
105
+ const renderTabs = ( ) => render ( < Tabs tabs = { mockTabs } /> ) ;
99
106
100
- it ( "renders the first tab content (Nøkkelkvalifikasjoner) by default" , ( ) => {
101
- render ( < Tabs tabs = { mockTabs } /> ) ;
102
- expect ( screen . getByText ( "Qualification 1" ) ) . toBeInTheDocument ( ) ;
103
- expect ( screen . getByText ( "Qualification 2" ) ) . toBeInTheDocument ( ) ;
104
- expect ( screen . queryByText ( "Example Company" ) ) . not . toBeInTheDocument ( ) ;
105
- expect ( screen . queryByText ( "University of Example" ) ) . not . toBeInTheDocument ( ) ;
106
- } ) ;
107
+ const expectTextsToBePresent = ( texts : string [ ] ) => {
108
+ texts . forEach ( ( text ) => {
109
+ expect ( screen . getByText ( text ) ) . toBeInTheDocument ( ) ;
110
+ } ) ;
111
+ } ;
107
112
108
- it ( "switches to Erfaring tab when clicked" , ( ) => {
109
- render ( < Tabs tabs = { mockTabs } /> ) ;
110
- fireEvent . click ( screen . getByRole ( "tab" , { name : "Erfaring" } ) ) ;
111
- expect ( screen . getByText ( "2020-2022 - Example Company" ) ) . toBeInTheDocument ( ) ;
112
- expect ( screen . getByText ( "Software Developer" ) ) . toBeInTheDocument ( ) ;
113
- expect ( screen . getByText ( "Worked on various projects" ) ) . toBeInTheDocument ( ) ;
114
- expect ( screen . queryByText ( "Qualification 1" ) ) . not . toBeInTheDocument ( ) ;
113
+ const expectTextsNotToBePresent = ( texts : string [ ] ) => {
114
+ texts . forEach ( ( text ) => {
115
+ expect ( screen . queryByText ( text ) ) . not . toBeInTheDocument ( ) ;
116
+ } ) ;
117
+ } ;
118
+
119
+ it ( "renders all CV tab labels" , ( ) => {
120
+ renderTabs ( ) ;
121
+ mockTabs . forEach ( ( tab ) => {
122
+ expect ( screen . getByRole ( "tab" , { name : tab . label } ) ) . toBeInTheDocument ( ) ;
123
+ } ) ;
115
124
} ) ;
116
125
117
- it ( "switches to Utdanning tab when clicked" , ( ) => {
118
- render ( < Tabs tabs = { mockTabs } /> ) ;
119
- fireEvent . click ( screen . getByRole ( "tab" , { name : "Utdanning" } ) ) ;
120
- expect (
121
- screen . getByText ( "2016-2020 - University of Example" )
122
- ) . toBeInTheDocument ( ) ;
123
- expect (
124
- screen . getByText ( "Bachelor in Computer Science" )
125
- ) . toBeInTheDocument ( ) ;
126
- expect (
127
- screen . getByText ( "Studied various aspects of computer science" )
128
- ) . toBeInTheDocument ( ) ;
129
- expect ( screen . queryByText ( "Qualification 1" ) ) . not . toBeInTheDocument ( ) ;
126
+ it . each ( mockTabs ) ( "renders correct content for $label tab" , ( tab ) => {
127
+ renderTabs ( ) ;
128
+ if ( tab . id !== mockTabs [ 0 ] . id ) {
129
+ fireEvent . click ( screen . getByRole ( "tab" , { name : tab . label } ) ) ;
130
+ }
131
+ expectTextsToBePresent ( tab . expectedTexts ) ;
132
+ expectTextsNotToBePresent ( tab . unexpectedTexts ) ;
130
133
} ) ;
131
134
132
135
it ( "applies correct ARIA attributes to CV tabs" , ( ) => {
133
- render ( < Tabs tabs = { mockTabs } /> ) ;
134
- const qualificationsTab = screen . getByRole ( "tab" , {
135
- name : "Nøkkelkvalifikasjoner" ,
136
+ renderTabs ( ) ;
137
+ mockTabs . forEach ( ( tab , index ) => {
138
+ const tabElement = screen . getByRole ( "tab" , { name : tab . label } ) ;
139
+ expect ( tabElement ) . toHaveAttribute (
140
+ "aria-selected" ,
141
+ index === 0 ? "true" : "false"
142
+ ) ;
143
+ expect ( tabElement ) . toHaveAttribute ( "aria-controls" , `tabpanel-${ tab . id } ` ) ;
136
144
} ) ;
137
- expect ( qualificationsTab ) . toHaveAttribute ( "aria-selected" , "true" ) ;
138
- expect ( qualificationsTab ) . toHaveAttribute (
139
- "aria-controls" ,
140
- "tabpanel-qualifications"
141
- ) ;
142
-
143
- const experienceTab = screen . getByRole ( "tab" , { name : "Erfaring" } ) ;
144
- expect ( experienceTab ) . toHaveAttribute ( "aria-selected" , "false" ) ;
145
- expect ( experienceTab ) . toHaveAttribute (
146
- "aria-controls" ,
147
- "tabpanel-experience"
148
- ) ;
149
145
} ) ;
150
146
151
147
it ( "renders in vertical orientation by default" , ( ) => {
152
- render ( < Tabs tabs = { mockTabs } /> ) ;
148
+ renderTabs ( ) ;
153
149
const tabList = screen . getByRole ( "tablist" ) ;
154
150
expect ( tabList ) . toHaveClass ( "sm:flex-col" ) ;
155
151
} ) ;
0 commit comments