Design system.
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.
 
 
 

176 lines
3.9 KiB

  1. import * as React from 'react';
  2. import {
  3. render,
  4. screen
  5. } from '@testing-library/react';
  6. import userEvent from '@testing-library/user-event';
  7. import * as ButtonBase from '@tesseract-design/web-base-button';
  8. import * as CheckControlBase from '@tesseract-design/web-base-checkcontrol';
  9. import { vi } from 'vitest';
  10. import { RadioButton } from '.';
  11. vi.mock('@tesseract-design/web-base-button');
  12. vi.mock('@tesseract-design/web-base-checkcontrol');
  13. describe('RadioButton', () => {
  14. it('renders a radio button', () => {
  15. render(
  16. <RadioButton />
  17. );
  18. const checkbox = screen.getByRole('radio');
  19. expect(checkbox).toBeInTheDocument();
  20. });
  21. it('renders a subtext', () => {
  22. render(
  23. <RadioButton
  24. subtext="subtext"
  25. />
  26. );
  27. const subtext: HTMLElement = screen.getByTestId('subtext');
  28. expect(subtext).toBeInTheDocument();
  29. });
  30. it('renders a badge', () => {
  31. render(
  32. <RadioButton
  33. badge="badge"
  34. />
  35. );
  36. const badge: HTMLElement = screen.getByTestId('badge');
  37. expect(badge).toBeInTheDocument();
  38. });
  39. it('handles click events', () => {
  40. const onClick = vi.fn().mockImplementationOnce((e) => { e.preventDefault(); });
  41. render(
  42. <RadioButton
  43. onClick={onClick}
  44. />
  45. );
  46. const button: HTMLInputElement = screen.getByRole('radio');
  47. userEvent.click(button);
  48. expect(onClick).toBeCalled();
  49. });
  50. it('renders a compact button', () => {
  51. render(
  52. <RadioButton
  53. compact
  54. />
  55. );
  56. expect(ButtonBase.Button).toBeCalledWith(expect.objectContaining({
  57. compact: true,
  58. }));
  59. expect(ButtonBase.Label).toBeCalledWith(expect.objectContaining({
  60. compact: true,
  61. }));
  62. });
  63. describe.each(Object.values(ButtonBase.ButtonSize))('on %s size', (size) => {
  64. it('renders button styles', () => {
  65. render(
  66. <RadioButton
  67. size={size}
  68. />
  69. );
  70. expect(ButtonBase.Button).toBeCalledWith(expect.objectContaining({
  71. size,
  72. }));
  73. });
  74. it('renders badge styles', () => {
  75. render(
  76. <RadioButton
  77. size={size}
  78. badge="badge"
  79. />
  80. );
  81. expect(ButtonBase.BadgeContainer).toBeCalledWith(expect.objectContaining({
  82. size,
  83. }));
  84. });
  85. });
  86. it.each(Object.values(ButtonBase.ButtonVariant))('renders a button with variant %s', (variant) => {
  87. render(
  88. <RadioButton
  89. variant={variant}
  90. />
  91. );
  92. expect(ButtonBase.Button).toBeCalledWith(expect.objectContaining({
  93. variant,
  94. }));
  95. });
  96. it('renders a bordered button', () => {
  97. render(
  98. <RadioButton
  99. border
  100. />
  101. );
  102. expect(ButtonBase.Border).toBeCalledWith(expect.objectContaining({
  103. border: true,
  104. }));
  105. });
  106. it('renders a block button', () => {
  107. render(
  108. <RadioButton
  109. block
  110. />
  111. );
  112. expect(ButtonBase.Button).toBeCalledWith(expect.objectContaining({
  113. block: true,
  114. }));
  115. expect(ButtonBase.Border).toBeCalledWith(expect.objectContaining({
  116. block: true,
  117. }));
  118. expect(CheckControlBase.ClickAreaWrapper).toBeCalledWith(expect.objectContaining({
  119. block: true,
  120. }));
  121. });
  122. it('renders children', () => {
  123. render(
  124. <RadioButton>
  125. Foo
  126. </RadioButton>
  127. );
  128. const children: HTMLElement = screen.getByTestId('children');
  129. expect(children).toHaveTextContent('Foo');
  130. });
  131. it('renders a disabled button', () => {
  132. render(
  133. <RadioButton
  134. disabled
  135. />
  136. );
  137. const radio: HTMLButtonElement = screen.getByRole('radio');
  138. expect(radio).toBeDisabled();
  139. });
  140. it('handles change events', () => {
  141. const onChange = vi.fn().mockImplementationOnce((e) => { e.preventDefault(); })
  142. render(
  143. <RadioButton
  144. onChange={onChange}
  145. />
  146. );
  147. const radio: HTMLInputElement = screen.getByRole('radio');
  148. userEvent.click(radio);
  149. expect(onChange).toBeCalled();
  150. });
  151. });