Design system.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

172 строки
3.9 KiB

  1. import * as React from 'react';
  2. import {
  3. cleanup,
  4. render,
  5. screen,
  6. } from '@testing-library/react';
  7. import userEvent from '@testing-library/user-event';
  8. import {
  9. vi,
  10. expect,
  11. describe,
  12. it,
  13. afterEach,
  14. } from 'vitest';
  15. import matchers from '@testing-library/jest-dom/matchers';
  16. import { PluginAPI } from 'tailwindcss/types/config';
  17. import {
  18. ToggleSwitch,
  19. ToggleSwitchDerivedElement,
  20. toggleSwitchPlugin,
  21. } from '.';
  22. expect.extend(matchers);
  23. describe('toggleSwitchPlugin', () => {
  24. it('adds component styles', () => {
  25. const pluginApi = {
  26. addComponents: vi.fn(),
  27. } as unknown as PluginAPI;
  28. toggleSwitchPlugin(pluginApi);
  29. expect(pluginApi.addComponents).toBeCalledTimes(1);
  30. });
  31. });
  32. describe('ToggleSwitch', () => {
  33. afterEach(() => {
  34. cleanup();
  35. });
  36. it('renders a checkbox', () => {
  37. render(
  38. <ToggleSwitch />,
  39. );
  40. const checkbox = screen.getByRole('checkbox');
  41. expect(checkbox).toBeInTheDocument();
  42. });
  43. it('renders a block switch', () => {
  44. render(
  45. <ToggleSwitch
  46. block
  47. />,
  48. );
  49. const base = screen.getByTestId('base');
  50. expect(base).toHaveClass('flex');
  51. });
  52. it('renders a label when the component is unchecked', () => {
  53. render(
  54. <ToggleSwitch
  55. uncheckedLabel="label"
  56. />,
  57. );
  58. const uncheckedLabel = screen.getByTestId('uncheckedLabel');
  59. expect(uncheckedLabel).toBeInTheDocument();
  60. });
  61. describe('on subtext', () => {
  62. it('renders without an unchecked label', () => {
  63. render(
  64. <ToggleSwitch
  65. subtext="subtext"
  66. />,
  67. );
  68. const subtext = screen.getByTestId('subtext');
  69. expect(subtext).toBeInTheDocument();
  70. expect(subtext).toHaveClass('pl-16');
  71. });
  72. it('renders with an unchecked label', () => {
  73. render(
  74. <ToggleSwitch
  75. subtext="subtext"
  76. uncheckedLabel="label"
  77. />,
  78. );
  79. const subtext = screen.getByTestId('subtext');
  80. expect(subtext).toBeInTheDocument();
  81. expect(subtext).toHaveClass('pt-2');
  82. });
  83. });
  84. describe('on indeterminate', () => {
  85. it('renders an indeterminate checkbox', () => {
  86. render(
  87. <ToggleSwitch
  88. indeterminate
  89. />,
  90. );
  91. const checkbox = screen.getByRole('checkbox');
  92. expect(checkbox).toHaveProperty('indeterminate', true);
  93. });
  94. it('acknowledges passed ref object', () => {
  95. const ref = React.createRef<ToggleSwitchDerivedElement>();
  96. render(
  97. <ToggleSwitch
  98. indeterminate
  99. ref={ref}
  100. />,
  101. );
  102. expect(ref.current).toHaveProperty('indeterminate', true);
  103. });
  104. it('acknowledges passed legacy ref', () => {
  105. let refElement = null as null | ToggleSwitchDerivedElement;
  106. const ref = (element: ToggleSwitchDerivedElement) => {
  107. refElement = element;
  108. };
  109. render(
  110. <ToggleSwitch
  111. indeterminate
  112. ref={ref}
  113. />,
  114. );
  115. const checkbox = screen.getByRole('checkbox');
  116. expect(checkbox).toHaveProperty('indeterminate', true);
  117. expect(refElement).toBe(checkbox);
  118. });
  119. });
  120. it('handles click events', async () => {
  121. const onClick = vi.fn().mockImplementationOnce(
  122. (e: React.MouseEvent<ToggleSwitchDerivedElement>) => {
  123. e.preventDefault();
  124. },
  125. );
  126. render(
  127. <ToggleSwitch
  128. onClick={onClick}
  129. />,
  130. );
  131. const checkbox: HTMLInputElement = screen.getByRole('checkbox');
  132. await userEvent.click(checkbox);
  133. expect(onClick).toBeCalled();
  134. });
  135. it('handles change events', async () => {
  136. const onChange = vi.fn().mockImplementationOnce(
  137. (e: React.ChangeEvent<ToggleSwitchDerivedElement>) => {
  138. e.preventDefault();
  139. },
  140. );
  141. render(
  142. <ToggleSwitch
  143. onChange={onChange}
  144. />,
  145. );
  146. const checkbox: HTMLInputElement = screen.getByRole('checkbox');
  147. await userEvent.click(checkbox);
  148. expect(onChange).toBeCalled();
  149. });
  150. });