Website for showcasing all features of Tesseract Web.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

202 lines
4.3 KiB

  1. import * as React from 'react';
  2. import {
  3. render,
  4. screen,
  5. } from '@testing-library/react';
  6. import '@testing-library/jest-dom';
  7. import {
  8. ActionButton,
  9. ActionButtonType,
  10. } from '.';
  11. import userEvent from '@testing-library/user-event';
  12. import * as ButtonBase from '@tesseract-design/web-base-button';
  13. jest.mock('@tesseract-design/web-base-button');
  14. describe('ActionButton', () => {
  15. it('should render a button', () => {
  16. render(
  17. <ActionButton />
  18. );
  19. const button: HTMLButtonElement = screen.getByRole('button');
  20. expect(button).toBeInTheDocument();
  21. expect(button).toHaveProperty('type', 'button');
  22. });
  23. it('should render a subtext', () => {
  24. render(
  25. <ActionButton
  26. subtext="subtext"
  27. />
  28. );
  29. const subtext: HTMLElement = screen.getByTestId('subtext');
  30. expect(subtext).toBeInTheDocument();
  31. });
  32. it('should render a badge', () => {
  33. render(
  34. <ActionButton
  35. badge="badge"
  36. />
  37. );
  38. const badge: HTMLElement = screen.getByTestId('badge');
  39. expect(badge).toBeInTheDocument();
  40. });
  41. it('should render as a menu item', () => {
  42. render(
  43. <ActionButton
  44. menuItem
  45. />
  46. );
  47. const menuItemIndicator: HTMLElement = screen.getByTestId('menuItemIndicator');
  48. expect(menuItemIndicator).toBeInTheDocument();
  49. });
  50. it('should handle click events', () => {
  51. const onClick = jest.fn();
  52. render(
  53. <ActionButton
  54. onClick={onClick}
  55. />
  56. );
  57. const button: HTMLButtonElement = screen.getByRole('button');
  58. userEvent.click(button);
  59. expect(onClick).toBeCalled();
  60. });
  61. it('should render a compact button', () => {
  62. render(
  63. <ActionButton
  64. compact
  65. />
  66. );
  67. expect(ButtonBase.Button).toBeCalledWith(expect.objectContaining({
  68. compact: true,
  69. }));
  70. expect(ButtonBase.Label).toBeCalledWith(expect.objectContaining({
  71. compact: true,
  72. }));
  73. });
  74. describe.each([
  75. ButtonBase.ButtonSize.SMALL,
  76. ButtonBase.ButtonSize.MEDIUM,
  77. ButtonBase.ButtonSize.LARGE,
  78. ])('on %s size', (size) => {
  79. it('should render button styles', () => {
  80. render(
  81. <ActionButton
  82. size={size}
  83. />
  84. );
  85. expect(ButtonBase.Button).toBeCalledWith(expect.objectContaining({
  86. size,
  87. }));
  88. });
  89. it('should render badge styles', () => {
  90. render(
  91. <ActionButton
  92. size={size}
  93. badge="badge"
  94. />
  95. );
  96. expect(ButtonBase.BadgeContainer).toBeCalledWith(expect.objectContaining({
  97. size,
  98. }));
  99. });
  100. it('should render indicator styles', () => {
  101. render(
  102. <ActionButton
  103. size={size}
  104. menuItem
  105. />
  106. );
  107. expect(ButtonBase.IndicatorWrapper).toBeCalledWith(expect.objectContaining({
  108. size,
  109. }));
  110. });
  111. });
  112. it.each([
  113. ButtonBase.ButtonVariant.OUTLINE,
  114. ButtonBase.ButtonVariant.FILLED,
  115. ])('should render a button with variant %s', (variant) => {
  116. render(
  117. <ActionButton
  118. variant={variant}
  119. />
  120. );
  121. expect(ButtonBase.Button).toBeCalledWith(expect.objectContaining({
  122. variant,
  123. }));
  124. });
  125. it('should render a bordered button', () => {
  126. render(
  127. <ActionButton
  128. border
  129. />
  130. );
  131. expect(ButtonBase.Border).toBeCalledWith(expect.objectContaining({
  132. border: true,
  133. }));
  134. });
  135. it('should render a block button', () => {
  136. render(
  137. <ActionButton
  138. block
  139. />
  140. );
  141. expect(ButtonBase.Border).toBeCalledWith(expect.objectContaining({
  142. block: true,
  143. }));
  144. });
  145. it('should render children', () => {
  146. render(
  147. <ActionButton>
  148. Foo
  149. </ActionButton>
  150. );
  151. const children: HTMLElement = screen.getByTestId('children');
  152. expect(children).toHaveTextContent('Foo');
  153. });
  154. it.each([
  155. ActionButtonType.BUTTON,
  156. ActionButtonType.RESET,
  157. ActionButtonType.SUBMIT,
  158. ])('should render a button with type %s', (buttonType) => {
  159. render(
  160. <ActionButton
  161. type={buttonType}
  162. />
  163. );
  164. const button: HTMLButtonElement = screen.getByRole('button');
  165. expect(button).toHaveProperty('type', buttonType);
  166. });
  167. it('should render a disabled button', () => {
  168. render(
  169. <ActionButton
  170. disabled
  171. />
  172. );
  173. const button: HTMLButtonElement = screen.getByRole('button');
  174. expect(button).toBeDisabled();
  175. });
  176. });