range.spec.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { mount } from '@vue/test-utils';
  2. import { nextTick, reactive, toRefs } from 'vue';
  3. import Range from '../index.vue';
  4. import { trigger, triggerDrag } from '../../../utils/test/event';
  5. test('should render range when used ', async () => {
  6. const wrapper = mount(Range, {
  7. props: {
  8. modelValue: 30
  9. }
  10. });
  11. let inner = wrapper.find<HTMLElement>('.nut-range-button .number');
  12. expect(inner.text()).toBe('30');
  13. });
  14. test('should render two buttons when use range props', async () => {
  15. const wrapper = mount(Range, {
  16. props: {
  17. modelValue: [30, 70],
  18. range: true
  19. }
  20. });
  21. let left = wrapper.find<HTMLElement>('.nut-range-button-wrapper-left .number');
  22. let right = wrapper.find<HTMLElement>('.nut-range-button-wrapper-right .number');
  23. expect(left.text()).toBe('30');
  24. expect(right.text()).toBe('70');
  25. });
  26. test('should set min and max when use min and max props', async () => {
  27. const wrapper = mount(Range, {
  28. props: {
  29. modelValue: 0,
  30. max: 10,
  31. min: -10,
  32. range: true
  33. }
  34. });
  35. let min = wrapper.find<HTMLElement>('.min');
  36. let max = wrapper.find<HTMLElement>('.max');
  37. let btn = wrapper.find<HTMLElement>('.nut-range-button-wrapper-left .number');
  38. expect(min.text()).toBe('-10');
  39. expect(max.text()).toBe('10');
  40. expect(btn.text()).toBe('');
  41. });
  42. test('should change color template when use color props', async () => {
  43. const wrapper = mount(Range, {
  44. props: {
  45. modelValue: 10,
  46. inactiveColor: 'rgb(163,184,255)',
  47. buttonColor: 'rgb(52,96,250)',
  48. activeColor: 'rgb(238, 238, 238)'
  49. }
  50. });
  51. await nextTick();
  52. let root = wrapper.find<HTMLElement>('.nut-range');
  53. let bar = wrapper.find<HTMLElement>('.nut-range-bar');
  54. let btn = wrapper.find<HTMLElement>('.nut-range-button');
  55. expect(root.element.style.backgroundColor).toEqual('rgb(163, 184, 255)');
  56. expect(bar.element.style.background).toEqual('rgb(238, 238, 238)');
  57. expect(btn.element.style.borderColor).toEqual('rgb(52,96,250)');
  58. });
  59. test('should render slots template when use slots', async () => {
  60. const wrapper = mount(Range, {
  61. props: {
  62. modelValue: 10
  63. },
  64. slots: {
  65. button: '<div class="test_div">btn</div>'
  66. }
  67. });
  68. let btn = wrapper.find<HTMLElement>('.nut-range-button-wrapper');
  69. expect(btn.html()).toContain('<div class="test_div">btn</div>');
  70. });
  71. test('should not render elements when use hidden elements props', async () => {
  72. const wrapper = mount(Range, {
  73. props: {
  74. modelValue: 10,
  75. hiddenRange: true,
  76. hiddenTag: true
  77. }
  78. });
  79. expect(wrapper.find<HTMLElement>('.nut-range-button').exists()).toBe(true);
  80. expect(wrapper.find<HTMLElement>('.min').exists()).toBe(false);
  81. expect(wrapper.find<HTMLElement>('.max').exists()).toBe(false);
  82. expect(wrapper.find<HTMLElement>('.nut-range-button .number').exists()).toBe(false);
  83. });
  84. test('should not allow to click slider when disabled', async () => {
  85. const wrapper = mount(Range, {
  86. props: {
  87. modelValue: 10,
  88. disabled: true
  89. }
  90. });
  91. let range = wrapper.find('.nut-range');
  92. trigger(range, 'click', 100, 0);
  93. expect(wrapper.emitted('change')).toBeFalsy();
  94. });
  95. test('should emit "change" event after dragging button', () => {
  96. const wrapper = mount(Range, {
  97. props: {
  98. modelValue: 10
  99. }
  100. });
  101. const button = wrapper.find('.nut-range-button');
  102. triggerDrag(button, 50, 0);
  103. expect(wrapper.emitted('change')!.pop()).toEqual([100]);
  104. });