emoji_input.spec.js 5.0 KB

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