aboutsummaryrefslogtreecommitdiff
path: root/test/unit/specs/components/emoji_input.spec.js
blob: 045b47fdde998e514b74fd1170de31680ccf0dab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { shallowMount, createLocalVue } from '@vue/test-utils'
import EmojiInput from 'src/components/emoji_input/emoji_input.vue'

const generateInput = (value, padEmoji = true) => {
  const localVue = createLocalVue()
  localVue.directive('click-outside', () => {})
  const wrapper = shallowMount(EmojiInput, {
    propsData: {
      suggest: () => [],
      enableEmojiPicker: true,
      value
    },
    mocks: {
      $store: {
        getters: {
          mergedConfig: {
            padEmoji
          }
        }
      }
    },
    slots: {
      default: '<input />'
    },
    localVue
  })
  return [wrapper, localVue]
}

describe('EmojiInput', () => {
  describe('insertion mechanism', () => {
    it('inserts string at the end with trailing space', () => {
      const initialString = 'Testing'
      const [wrapper] = generateInput(initialString)
      const input = wrapper.find('input')
      input.setValue(initialString)
      wrapper.setData({ caret: initialString.length })
      wrapper.vm.insert({ insertion: '(test)', keepOpen: false })
      const inputEvents = wrapper.emitted().input
      expect(inputEvents[inputEvents.length - 1][0]).to.eql('Testing (test) ')
    })

    it('inserts string at the end with trailing space (source has a trailing space)', () => {
      const initialString = 'Testing '
      const [wrapper] = generateInput(initialString)
      const input = wrapper.find('input')
      input.setValue(initialString)
      wrapper.setData({ caret: initialString.length })
      wrapper.vm.insert({ insertion: '(test)', keepOpen: false })
      const inputEvents = wrapper.emitted().input
      expect(inputEvents[inputEvents.length - 1][0]).to.eql('Testing (test) ')
    })

    it('inserts string at the begginning without leading space', () => {
      const initialString = 'Testing'
      const [wrapper] = generateInput(initialString)
      const input = wrapper.find('input')
      input.setValue(initialString)
      wrapper.setData({ caret: 0 })
      wrapper.vm.insert({ insertion: '(test)', keepOpen: false })
      const inputEvents = wrapper.emitted().input
      expect(inputEvents[inputEvents.length - 1][0]).to.eql('(test) Testing')
    })

    it('inserts string between words without creating extra spaces', () => {
      const initialString = 'Spurdo Sparde'
      const [wrapper] = generateInput(initialString)
      const input = wrapper.find('input')
      input.setValue(initialString)
      wrapper.setData({ caret: 6 })
      wrapper.vm.insert({ insertion: ':ebin:', keepOpen: false })
      const inputEvents = wrapper.emitted().input
      expect(inputEvents[inputEvents.length - 1][0]).to.eql('Spurdo :ebin: Sparde')
    })

    it('inserts string between words without creating extra spaces (other caret)', () => {
      const initialString = 'Spurdo Sparde'
      const [wrapper] = generateInput(initialString)
      const input = wrapper.find('input')
      input.setValue(initialString)
      wrapper.setData({ caret: 7 })
      wrapper.vm.insert({ insertion: ':ebin:', keepOpen: false })
      const inputEvents = wrapper.emitted().input
      expect(inputEvents[inputEvents.length - 1][0]).to.eql('Spurdo :ebin: Sparde')
    })

    it('inserts string without any padding if padEmoji setting is set to false', () => {
      const initialString = 'Eat some spam!'
      const [wrapper] = generateInput(initialString, false)
      const input = wrapper.find('input')
      input.setValue(initialString)
      wrapper.setData({ caret: initialString.length, keepOpen: false })
      wrapper.vm.insert({ insertion: ':spam:' })
      const inputEvents = wrapper.emitted().input
      expect(inputEvents[inputEvents.length - 1][0]).to.eql('Eat some spam!:spam:')
    })

    it('correctly sets caret after insertion at beginning', (done) => {
      const initialString = '1234'
      const [wrapper, vue] = generateInput(initialString)
      const input = wrapper.find('input')
      input.setValue(initialString)
      wrapper.setData({ caret: 0 })
      wrapper.vm.insert({ insertion: '1234', keepOpen: false })
      vue.nextTick(() => {
        expect(wrapper.vm.caret).to.eql(5)
        done()
      })
    })

    it('correctly sets caret after insertion at end', (done) => {
      const initialString = '1234'
      const [wrapper, vue] = generateInput(initialString)
      const input = wrapper.find('input')
      input.setValue(initialString)
      wrapper.setData({ caret: initialString.length })
      wrapper.vm.insert({ insertion: '1234', keepOpen: false })
      vue.nextTick(() => {
        expect(wrapper.vm.caret).to.eql(10)
        done()
      })
    })

    it('correctly sets caret after insertion if padEmoji setting is set to false', (done) => {
      const initialString = '1234'
      const [wrapper, vue] = generateInput(initialString, false)
      const input = wrapper.find('input')
      input.setValue(initialString)
      wrapper.setData({ caret: initialString.length })
      wrapper.vm.insert({ insertion: '1234', keepOpen: false })
      vue.nextTick(() => {
        expect(wrapper.vm.caret).to.eql(8)
        done()
      })
    })
  })
})