checkbox.spec.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { mount } from '@vue/test-utils';
  2. import Checkbox from '../index.vue';
  3. import { reactive, toRefs } from 'vue';
  4. test('basic usage', () => {
  5. const wrapper = mount(Checkbox, {
  6. props: {
  7. modelValue: '',
  8. label: '复选框'
  9. }
  10. });
  11. expect(wrapper.html()).toMatchSnapshot();
  12. });
  13. test('checkbox disabled test', async () => {
  14. const wrapper = mount({
  15. components: {
  16. 'nut-checkbox': Checkbox
  17. },
  18. template: `
  19. <template>
  20. <nut-checkbox v-model="checkbox1" disabled>未选时禁用状态</nut-checkbox>
  21. </template>
  22. `,
  23. setup() {
  24. const state = reactive({
  25. checkbox1: true
  26. });
  27. return { ...toRefs(state) };
  28. }
  29. });
  30. const items = wrapper.findAll('.nut-checkbox');
  31. await items[0].trigger('click');
  32. expect(wrapper.vm.checkbox1).toEqual(true);
  33. expect(wrapper.find('.nut-checkbox__icon--disable')).toBeTruthy();
  34. });
  35. test('checkbox text-position test', () => {
  36. const wrapper = mount({
  37. components: {
  38. 'nut-checkbox': Checkbox
  39. },
  40. template: `
  41. <template>
  42. <nut-checkbox v-model="checkbox1">right</nut-checkbox>
  43. <nut-checkbox v-model="checkbox2" text-position="left">left</nut-checkbox>
  44. </template>
  45. `,
  46. setup() {
  47. const state = reactive({
  48. checkbox1: true,
  49. checkbox2: true
  50. });
  51. return { ...toRefs(state) };
  52. }
  53. });
  54. const items = wrapper.findAll('.nut-checkbox');
  55. expect((items[1] as any).find('.nut-checkbox--reverse')).toBeTruthy();
  56. });
  57. test('checkbox icon-size test', () => {
  58. const wrapper = mount({
  59. components: {
  60. 'nut-checkbox': Checkbox
  61. },
  62. template: `
  63. <template>
  64. <nut-checkbox v-model="checkbox5" icon-size="25">自定义尺寸25</nut-checkbox>
  65. <nut-checkbox v-model="checkbox6" icon-size="10">自定义尺寸10</nut-checkbox>
  66. </template>
  67. `,
  68. setup() {
  69. const state = reactive({
  70. checkbox5: true,
  71. checkbox6: true
  72. });
  73. return { ...toRefs(state) };
  74. }
  75. });
  76. const items = wrapper.findAll('.nut-checkbox');
  77. expect((items[0].findAll('i') as any)[0].element.style.fontSize).toEqual('25px');
  78. expect((items[1].findAll('i') as any)[0].element.style.fontSize).toEqual('10px');
  79. });
  80. test('checkbox icon-name test', () => {
  81. const wrapper = mount({
  82. components: {
  83. 'nut-checkbox': Checkbox
  84. },
  85. template: `
  86. <template>
  87. <nut-checkbox v-model="checkbox5" icon-name="checklist" icon-active-name="checklist">自定义图标</nut-checkbox>
  88. <nut-checkbox v-model="checkbox6">auxiliary</nut-checkbox>
  89. </template>
  90. `,
  91. setup() {
  92. const state = reactive({
  93. checkbox5: true,
  94. checkbox6: true
  95. });
  96. return { ...toRefs(state) };
  97. }
  98. });
  99. const items = wrapper.findAll('.nut-checkbox');
  100. expect((items[0].findAll('i') as any)[0].classes()).toContain('nut-icon-checklist');
  101. });
  102. test('checkbox label test', () => {
  103. const wrapper = mount({
  104. components: {
  105. 'nut-checkbox': Checkbox
  106. },
  107. template: `
  108. <template>
  109. <nut-checkbox v-model="checkbox5" label="testlabel"></nut-checkbox>
  110. <nut-checkbox v-model="checkbox6">auxiliary</nut-checkbox>
  111. </template>
  112. `,
  113. setup() {
  114. const state = reactive({
  115. checkbox5: true,
  116. checkbox6: true
  117. });
  118. return { ...toRefs(state) };
  119. }
  120. });
  121. const items = wrapper.findAll('.nut-checkbox');
  122. expect((items[0].findAll('i') as any)[0].find('testlabel')).toBeTruthy();
  123. });
  124. test('should emit "update:modelValue" event when checkbox is clicked', async () => {
  125. const wrapper = mount(Checkbox);
  126. wrapper.trigger('click');
  127. expect(wrapper.emitted('update:modelValue')![0]).toEqual([true]);
  128. await wrapper.setProps({ modelValue: true });
  129. wrapper.trigger('click');
  130. expect(wrapper.emitted('update:modelValue')![1]).toEqual([false]);
  131. });
  132. test('checkbox shape test', async () => {
  133. const wrapper = mount({
  134. components: {
  135. 'nut-checkbox': Checkbox
  136. },
  137. template: `
  138. <template>
  139. <nut-checkbox shape="button"></nut-checkbox>
  140. <nut-checkbox></nut-checkbox>
  141. </template>
  142. `
  143. });
  144. const items = wrapper.findAll('.nut-checkbox');
  145. expect((items[0] as any).find('.nut-checkbox__button').exists()).toBeTruthy();
  146. expect((items[1] as any).find('.nut-checkbox__button').exists()).toBeFalsy();
  147. });