input.spec.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import { config, mount } from '@vue/test-utils';
  2. import Input from '../index.vue';
  3. import NutIcon from '../../icon/index.vue';
  4. beforeAll(() => {
  5. config.global.components = {
  6. NutIcon
  7. };
  8. });
  9. afterAll(() => {
  10. config.global.components = {};
  11. });
  12. test('base', () => {
  13. const wrapper = mount(Input, { props: { modelValue: 3 } });
  14. const input = wrapper.find('input');
  15. expect(input.exists()).toBe(true);
  16. expect(input.element.value).toBe('3');
  17. });
  18. test('should emit focuse event when input is focus', () => {
  19. const wrapper = mount(Input);
  20. wrapper.find('input').trigger('focus');
  21. expect(wrapper.emitted('focus')).toBeTruthy();
  22. });
  23. test('should emit blur event when input is blur', () => {
  24. const wrapper = mount(Input);
  25. wrapper.find('input').trigger('blur');
  26. expect(wrapper.emitted('blur')).toBeTruthy();
  27. });
  28. test('should emit change event when input is change', () => {
  29. const wrapper = mount(Input);
  30. wrapper.find('input').trigger('input');
  31. expect(wrapper.emitted('change')).toBeTruthy();
  32. });
  33. test('should render clear icon when using clearable prop', async () => {
  34. const wrapper = mount(Input, {
  35. props: {
  36. clearable: true,
  37. modelValue: 'test'
  38. }
  39. });
  40. const clearBtn = wrapper.find('.nut-textinput-clear');
  41. const input = wrapper.find('input');
  42. await input.trigger('focus');
  43. // expect(wrapper.find('.nut-textinput-clear').exists()).toBeTruthy();
  44. wrapper.find('.nut-textinput-clear').trigger('click');
  45. // expect((wrapper.emitted('update:modelValue') as any)[0][0]).toEqual('');
  46. // expect((wrapper.emitted('handleClear') as any)[0][0]).toBeTruthy();
  47. });
  48. test('should clear when event clear', () => {
  49. const wrapper = mount(Input, { props: { modelValue: 3 } });
  50. const input = wrapper.find('input');
  51. const clear = wrapper.find('.nut-textinput-clear');
  52. wrapper.find('input').trigger('input');
  53. clear.trigger('click');
  54. expect(clear.exists()).toBe(true);
  55. setTimeout(() => {
  56. expect(input.element.value).toBe('');
  57. });
  58. });
  59. // 测试只能是数字
  60. test('should format input value when type is number', () => {
  61. const wrapper = mount(Input, {
  62. props: {
  63. type: 'number',
  64. modelValue: ''
  65. }
  66. });
  67. const input = wrapper.find('input');
  68. input.element.value = '1';
  69. input.trigger('input');
  70. expect((wrapper.emitted('change') as any)[0][0]).toEqual('1');
  71. input.element.value = '1.2';
  72. input.trigger('input');
  73. expect((wrapper.emitted('change') as any)[1][0]).toEqual('12');
  74. // input.element.value = '111qwe';
  75. // input.trigger('input');
  76. // expect((wrapper.emitted('change') as any)[1][0]).toEqual('111');
  77. });
  78. test('should format input value when type is number', () => {
  79. const wrapper = mount(Input, {
  80. props: {
  81. type: 'number',
  82. modelValue: '123abc'
  83. }
  84. });
  85. const input = wrapper.find('input');
  86. input.trigger('blur');
  87. expect((wrapper.emitted('blur') as any)[0][0]).toEqual('');
  88. });
  89. // 测试小数
  90. test('should format input value when type is digit', () => {
  91. const wrapper = mount(Input, {
  92. props: {
  93. type: 'digit',
  94. modelValue: ''
  95. }
  96. });
  97. const input = wrapper.find('input');
  98. input.element.value = '1';
  99. input.trigger('input');
  100. expect((wrapper.emitted('change') as any)[0][0]).toEqual('1');
  101. input.element.value = '1.2';
  102. input.trigger('input');
  103. expect((wrapper.emitted('change') as any)[1][0]).toEqual('1.2');
  104. });
  105. test('should limit maxlength of input value when using maxlength prop', async () => {
  106. const wrapper = mount(Input, {
  107. props: {
  108. maxLength: 3,
  109. modelValue: '1234'
  110. }
  111. });
  112. const input = wrapper.find('input');
  113. input.trigger('input');
  114. expect((wrapper.emitted('change') as any)[0][0]).toEqual('123');
  115. input.element.value = '1234';
  116. input.trigger('input');
  117. expect((wrapper.emitted('change') as any)[0][0]).toEqual('123');
  118. });
  119. test('should no label', () => {
  120. const wrapper = mount(Input, {
  121. props: {}
  122. });
  123. const label = wrapper.find('.label-string');
  124. expect(label.exists()).toBe(false);
  125. });
  126. test('should label', () => {
  127. const wrapper = mount(Input, {
  128. props: {
  129. label: '文本'
  130. }
  131. });
  132. const label = wrapper.find('.label-string');
  133. expect(label.text()).toBe('文本');
  134. });
  135. test('should require', () => {
  136. const wrapper = mount(Input, {
  137. props: {
  138. requireShow: true
  139. }
  140. });
  141. const input = wrapper.find('.nut-input');
  142. expect(input.classes()).toContain('nut-input-require');
  143. });
  144. test('should disabled', () => {
  145. const wrapper = mount(Input, {
  146. props: {
  147. disabled: true
  148. }
  149. });
  150. const input = wrapper.find('input');
  151. expect(input.attributes('disabled')).toBe('');
  152. });
  153. test('should readonly', () => {
  154. const wrapper = mount(Input, {
  155. props: {
  156. readonly: true
  157. }
  158. });
  159. const input = wrapper.find('input');
  160. expect(input.attributes('readonly')).toBe('');
  161. });
  162. test('should text-align left', () => {
  163. const wrapper = mount(Input, {
  164. props: {
  165. textAlign: 'center'
  166. }
  167. });
  168. const input = wrapper.find('input').element;
  169. expect(input.style.textAlign).toEqual('center');
  170. });
  171. test('should render clear icon when using clearable prop', async () => {
  172. const wrapper = mount(Input, {
  173. props: {
  174. clearable: true,
  175. modelValue: 'test'
  176. }
  177. });
  178. const clearBtn = wrapper.find('.nut-textinput-clear');
  179. const input = wrapper.find('input');
  180. await input.trigger('focus');
  181. // expect(wrapper.find('.nut-textinput-clear').exists()).toBeTruthy();
  182. wrapper.find('.nut-textinput-clear').trigger('click');
  183. // expect((wrapper.emitted('update:modelValue') as any)[0][0]).toEqual('');
  184. // expect((wrapper.emitted('handleClear') as any)[0][0]).toBeTruthy();
  185. });