emoji_input.spec.js 5.3 KB

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