index.spec.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import {Item, TabList, TabPanels, Tabs} from 'sentry/components/tabs';
  3. const TABS = [
  4. {key: 'details', label: 'Details', content: 'So by colonel hearted ferrars.'},
  5. {
  6. key: 'activity',
  7. label: 'Activity',
  8. content:
  9. 'Draw from upon here gone add one. He in sportsman household otherwise it perceived instantly.',
  10. },
  11. {
  12. key: 'user-feedback',
  13. label: 'User Feedback',
  14. content: 'Is inquiry no he several excited am.',
  15. },
  16. {
  17. key: 'attachments',
  18. label: 'Attachments',
  19. content: 'Called though excuse length ye needed it he having.',
  20. },
  21. ];
  22. describe('Tabs', () => {
  23. it('renders tabs list', () => {
  24. render(
  25. <Tabs>
  26. <TabList>
  27. {TABS.map(tab => (
  28. <Item key={tab.key}>{tab.label}</Item>
  29. ))}
  30. </TabList>
  31. <TabPanels>
  32. {TABS.map(tab => (
  33. <Item key={tab.key}>{tab.content}</Item>
  34. ))}
  35. </TabPanels>
  36. </Tabs>
  37. );
  38. // The full tabs list is rendered
  39. expect(screen.getByRole('tablist')).toHaveAttribute('aria-orientation', 'horizontal');
  40. expect(screen.getAllByRole('tab')).toHaveLength(TABS.length);
  41. TABS.forEach(tab => {
  42. expect(screen.getByRole('tab', {name: tab.label})).toBeInTheDocument();
  43. });
  44. // The first tab item is selected and its content visible
  45. expect(screen.getByRole('tab', {name: TABS[0].label})).toHaveAttribute(
  46. 'aria-selected',
  47. 'true'
  48. );
  49. expect(screen.getByText(TABS[0].content)).toBeInTheDocument();
  50. });
  51. it('renders tabs list when disabled', () => {
  52. render(
  53. <Tabs disabled>
  54. <TabList>
  55. {TABS.map(tab => (
  56. <Item key={tab.key}>{tab.label}</Item>
  57. ))}
  58. </TabList>
  59. <TabPanels>
  60. {TABS.map(tab => (
  61. <Item key={tab.key}>{tab.content}</Item>
  62. ))}
  63. </TabPanels>
  64. </Tabs>
  65. );
  66. // The first tab item is selected and its content visible
  67. expect(screen.getByRole('tab', {name: TABS[0].label})).toHaveAttribute(
  68. 'aria-selected',
  69. 'true'
  70. );
  71. expect(screen.getByText(TABS[0].content)).toBeInTheDocument();
  72. // All tabs are marked as disabled
  73. TABS.forEach(tab => {
  74. expect(screen.getByRole('tab', {name: tab.label})).toHaveAttribute(
  75. 'aria-disabled',
  76. 'true'
  77. );
  78. });
  79. });
  80. it('changes tabs on click', () => {
  81. render(
  82. <Tabs>
  83. <TabList>
  84. {TABS.map(tab => (
  85. <Item key={tab.key}>{tab.label}</Item>
  86. ))}
  87. </TabList>
  88. <TabPanels>
  89. {TABS.map(tab => (
  90. <Item key={tab.key}>{tab.content}</Item>
  91. ))}
  92. </TabPanels>
  93. </Tabs>
  94. );
  95. // Click on the Activity tab
  96. userEvent.click(screen.getByRole('tab', {name: 'Activity'}));
  97. // The Activity tab is selected and its content rendered
  98. expect(screen.getByRole('tab', {name: 'Activity'})).toHaveAttribute(
  99. 'aria-selected',
  100. 'true'
  101. );
  102. expect(screen.getByText(TABS[1].content)).toBeInTheDocument();
  103. });
  104. it('changes tabs on key press', () => {
  105. render(
  106. <Tabs>
  107. <TabList>
  108. {TABS.map(tab => (
  109. <Item key={tab.key}>{tab.label}</Item>
  110. ))}
  111. </TabList>
  112. <TabPanels>
  113. {TABS.map(tab => (
  114. <Item key={tab.key}>{tab.content}</Item>
  115. ))}
  116. </TabPanels>
  117. </Tabs>
  118. );
  119. // Focus on tab list
  120. userEvent.tab();
  121. expect(screen.getByRole('tab', {name: 'Details'})).toHaveFocus();
  122. // Press Arrow Right to select the next tab to the right (Activity)
  123. userEvent.keyboard('{arrowRight}');
  124. // The Activity tab is selected and its contents rendered
  125. expect(screen.getByRole('tab', {name: 'Activity'})).toHaveAttribute(
  126. 'aria-selected',
  127. 'true'
  128. );
  129. expect(screen.getByText(TABS[1].content)).toBeInTheDocument();
  130. });
  131. it('changes tabs on key press in vertical orientation', () => {
  132. render(
  133. <Tabs orientation="vertical">
  134. <TabList>
  135. {TABS.map(tab => (
  136. <Item key={tab.key}>{tab.label}</Item>
  137. ))}
  138. </TabList>
  139. <TabPanels>
  140. {TABS.map(tab => (
  141. <Item key={tab.key}>{tab.content}</Item>
  142. ))}
  143. </TabPanels>
  144. </Tabs>
  145. );
  146. // Focus on tab list
  147. userEvent.tab();
  148. expect(screen.getByRole('tab', {name: 'Details'})).toHaveFocus();
  149. // Press Arrow Right to select the next tab below (Activity)
  150. userEvent.keyboard('{arrowDown}');
  151. // The Activity tab should now be selected and its contents rendered
  152. expect(screen.getByRole('tab', {name: 'Activity'})).toHaveAttribute(
  153. 'aria-selected',
  154. 'true'
  155. );
  156. expect(screen.getByText(TABS[1].content)).toBeInTheDocument();
  157. });
  158. it('renders disabled tabs', () => {
  159. render(
  160. <Tabs>
  161. <TabList>
  162. {TABS.map(tab => (
  163. <Item key={tab.key} disabled>
  164. {tab.label}
  165. </Item>
  166. ))}
  167. </TabList>
  168. <TabPanels>
  169. {TABS.map(tab => (
  170. <Item key={tab.key}>{tab.content}</Item>
  171. ))}
  172. </TabPanels>
  173. </Tabs>
  174. );
  175. TABS.forEach(tab => {
  176. expect(screen.getByRole('tab', {name: tab.label})).toHaveAttribute(
  177. 'aria-disabled',
  178. 'true'
  179. );
  180. });
  181. });
  182. });