emoji_input.spec.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. state: {
  15. config: {
  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. expect(wrapper.emitted().input[0][0]).to.eql('Testing (test) ')
  38. })
  39. it('inserts string at the end with trailing space (source has a 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. expect(wrapper.emitted().input[0][0]).to.eql('Testing (test) ')
  47. })
  48. it('inserts string at the begginning without leading space', () => {
  49. const initialString = 'Testing'
  50. const [wrapper] = generateInput(initialString)
  51. const input = wrapper.find('input')
  52. input.setValue(initialString)
  53. wrapper.setData({ caret: 0 })
  54. wrapper.vm.insert({ insertion: '(test)', keepOpen: false })
  55. expect(wrapper.emitted().input[0][0]).to.eql('(test) Testing')
  56. })
  57. it('inserts string between words without creating extra spaces', () => {
  58. const initialString = 'Spurdo Sparde'
  59. const [wrapper] = generateInput(initialString)
  60. const input = wrapper.find('input')
  61. input.setValue(initialString)
  62. wrapper.setData({ caret: 6 })
  63. wrapper.vm.insert({ insertion: ':ebin:', keepOpen: false })
  64. expect(wrapper.emitted().input[0][0]).to.eql('Spurdo :ebin: Sparde')
  65. })
  66. it('inserts string between words without creating extra spaces (other caret)', () => {
  67. const initialString = 'Spurdo Sparde'
  68. const [wrapper] = generateInput(initialString)
  69. const input = wrapper.find('input')
  70. input.setValue(initialString)
  71. wrapper.setData({ caret: 7 })
  72. wrapper.vm.insert({ insertion: ':ebin:', keepOpen: false })
  73. expect(wrapper.emitted().input[0][0]).to.eql('Spurdo :ebin: Sparde')
  74. })
  75. it('inserts string without any padding if padEmoji setting is set to false', () => {
  76. const initialString = 'Eat some spam!'
  77. const [wrapper] = generateInput(initialString, false)
  78. const input = wrapper.find('input')
  79. input.setValue(initialString)
  80. wrapper.setData({ caret: initialString.length, keepOpen: false })
  81. wrapper.vm.insert({ insertion: ':spam:' })
  82. expect(wrapper.emitted().input[0][0]).to.eql('Eat some spam!:spam:')
  83. })
  84. it('correctly sets caret after insertion at beginning', (done) => {
  85. const initialString = '1234'
  86. const [wrapper, vue] = generateInput(initialString)
  87. const input = wrapper.find('input')
  88. input.setValue(initialString)
  89. wrapper.setData({ caret: 0 })
  90. wrapper.vm.insert({ insertion: '1234', keepOpen: false })
  91. vue.nextTick(() => {
  92. expect(wrapper.vm.caret).to.eql(5)
  93. done()
  94. })
  95. })
  96. it('correctly sets caret after insertion at end', (done) => {
  97. const initialString = '1234'
  98. const [wrapper, vue] = generateInput(initialString)
  99. const input = wrapper.find('input')
  100. input.setValue(initialString)
  101. wrapper.setData({ caret: initialString.length })
  102. wrapper.vm.insert({ insertion: '1234', keepOpen: false })
  103. vue.nextTick(() => {
  104. expect(wrapper.vm.caret).to.eql(10)
  105. done()
  106. })
  107. })
  108. it('correctly sets caret after insertion if padEmoji setting is set to false', (done) => {
  109. const initialString = '1234'
  110. const [wrapper, vue] = generateInput(initialString, false)
  111. const input = wrapper.find('input')
  112. input.setValue(initialString)
  113. wrapper.setData({ caret: initialString.length })
  114. wrapper.vm.insert({ insertion: '1234', keepOpen: false })
  115. vue.nextTick(() => {
  116. expect(wrapper.vm.caret).to.eql(8)
  117. done()
  118. })
  119. })
  120. })
  121. })