emoji_input.spec.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { h } from 'vue'
  2. import { shallowMount } from '@vue/test-utils'
  3. import EmojiInput from 'src/components/emoji_input/emoji_input.vue'
  4. import vClickOutside from 'click-outside-vue3'
  5. const generateInput = (value, padEmoji = true) => {
  6. const wrapper = shallowMount(EmojiInput, {
  7. global: {
  8. renderStubDefaultSlot: true,
  9. mocks: {
  10. $store: {
  11. getters: {
  12. mergedConfig: {
  13. padEmoji
  14. }
  15. }
  16. }
  17. },
  18. stubs: {
  19. FAIcon: true
  20. },
  21. directives: {
  22. 'click-outside': vClickOutside
  23. }
  24. },
  25. props: {
  26. suggest: () => [],
  27. enableEmojiPicker: true,
  28. modelValue: value
  29. },
  30. slots: {
  31. default: () => h('input', '')
  32. }
  33. })
  34. return wrapper
  35. }
  36. describe('EmojiInput', () => {
  37. describe('insertion mechanism', () => {
  38. it('inserts string at the end with trailing space', () => {
  39. const initialString = 'Testing'
  40. const wrapper = generateInput(initialString)
  41. const input = wrapper.find('input')
  42. input.setValue(initialString)
  43. wrapper.setData({ caret: initialString.length })
  44. wrapper.vm.insert({ insertion: '(test)', keepOpen: false })
  45. const inputEvents = wrapper.emitted()['update:modelValue']
  46. expect(inputEvents[inputEvents.length - 1][0]).to.eql('Testing (test) ')
  47. })
  48. it('inserts string at the end with trailing space (source has a trailing space)', () => {
  49. const initialString = 'Testing '
  50. const wrapper = generateInput(initialString)
  51. const input = wrapper.find('input')
  52. input.setValue(initialString)
  53. wrapper.setData({ caret: initialString.length })
  54. wrapper.vm.insert({ insertion: '(test)', keepOpen: false })
  55. const inputEvents = wrapper.emitted()['update:modelValue']
  56. expect(inputEvents[inputEvents.length - 1][0]).to.eql('Testing (test) ')
  57. })
  58. it('inserts string at the begginning without leading space', () => {
  59. const initialString = 'Testing'
  60. const wrapper = generateInput(initialString)
  61. const input = wrapper.find('input')
  62. input.setValue(initialString)
  63. wrapper.setData({ caret: 0 })
  64. wrapper.vm.insert({ insertion: '(test)', keepOpen: false })
  65. const inputEvents = wrapper.emitted()['update:modelValue']
  66. expect(inputEvents[inputEvents.length - 1][0]).to.eql('(test) Testing')
  67. })
  68. it('inserts string between words without creating extra spaces', () => {
  69. const initialString = 'Spurdo Sparde'
  70. const wrapper = generateInput(initialString)
  71. const input = wrapper.find('input')
  72. input.setValue(initialString)
  73. wrapper.setData({ caret: 6 })
  74. wrapper.vm.insert({ insertion: ':ebin:', keepOpen: false })
  75. const inputEvents = wrapper.emitted()['update:modelValue']
  76. expect(inputEvents[inputEvents.length - 1][0]).to.eql('Spurdo :ebin: Sparde')
  77. })
  78. it('inserts string between words without creating extra spaces (other caret)', () => {
  79. const initialString = 'Spurdo Sparde'
  80. const wrapper = generateInput(initialString)
  81. const input = wrapper.find('input')
  82. input.setValue(initialString)
  83. wrapper.setData({ caret: 7 })
  84. wrapper.vm.insert({ insertion: ':ebin:', keepOpen: false })
  85. const inputEvents = wrapper.emitted()['update:modelValue']
  86. expect(inputEvents[inputEvents.length - 1][0]).to.eql('Spurdo :ebin: Sparde')
  87. })
  88. it('inserts string without any padding if padEmoji setting is set to false', () => {
  89. const initialString = 'Eat some spam!'
  90. const wrapper = generateInput(initialString, false)
  91. const input = wrapper.find('input')
  92. input.setValue(initialString)
  93. wrapper.setData({ caret: initialString.length, keepOpen: false })
  94. wrapper.vm.insert({ insertion: ':spam:' })
  95. const inputEvents = wrapper.emitted()['update:modelValue']
  96. expect(inputEvents[inputEvents.length - 1][0]).to.eql('Eat some spam!:spam:')
  97. })
  98. it('correctly sets caret after insertion at beginning', (done) => {
  99. const initialString = '1234'
  100. const wrapper = generateInput(initialString)
  101. const input = wrapper.find('input')
  102. input.setValue(initialString)
  103. wrapper.setData({ caret: 0 })
  104. wrapper.vm.insert({ insertion: '1234', keepOpen: false })
  105. wrapper.vm.$nextTick(() => {
  106. expect(wrapper.vm.caret).to.eql(5)
  107. done()
  108. })
  109. })
  110. it('correctly sets caret after insertion at end', (done) => {
  111. const initialString = '1234'
  112. const wrapper = generateInput(initialString)
  113. const input = wrapper.find('input')
  114. input.setValue(initialString)
  115. wrapper.setData({ caret: initialString.length })
  116. wrapper.vm.insert({ insertion: '1234', keepOpen: false })
  117. wrapper.vm.$nextTick(() => {
  118. expect(wrapper.vm.caret).to.eql(10)
  119. done()
  120. })
  121. })
  122. it('correctly sets caret after insertion if padEmoji setting is set to false', (done) => {
  123. const initialString = '1234'
  124. const wrapper = generateInput(initialString, false)
  125. const input = wrapper.find('input')
  126. input.setValue(initialString)
  127. wrapper.setData({ caret: initialString.length })
  128. wrapper.vm.insert({ insertion: '1234', keepOpen: false })
  129. wrapper.vm.$nextTick(() => {
  130. expect(wrapper.vm.caret).to.eql(8)
  131. done()
  132. })
  133. })
  134. })
  135. })