pleroma-fe/test/unit/specs/components/emoji_input.spec.js

155 lines
5.4 KiB
JavaScript
Raw Normal View History

2022-03-22 18:14:56 +02:00
import { shallowMount } from '@vue/test-utils'
import vClickOutside from 'click-outside-vue3'
2026-01-06 16:23:17 +02:00
import { h } from 'vue'
2026-01-08 17:26:52 +02:00
import EmojiInput from 'src/components/emoji_input/emoji_input.vue'
2019-09-25 20:54:07 +03:00
const generateInput = (value, padEmoji = true) => {
const wrapper = shallowMount(EmojiInput, {
2022-03-22 18:14:56 +02:00
global: {
renderStubDefaultSlot: true,
mocks: {
$store: {
getters: {
mergedConfig: {
2026-01-06 16:22:52 +02:00
padEmoji,
},
},
2023-01-21 22:42:53 -05:00
},
2026-01-06 16:22:52 +02:00
$t: (msg) => msg,
2022-03-22 18:14:56 +02:00
},
stubs: {
2025-02-27 22:54:23 -05:00
FAIcon: true,
Popover: {
template: `<div><slot trigger /></div>`,
methods: {
2026-01-06 17:32:22 +02:00
updateStyles() {
/* no-op */
},
2026-01-06 16:22:52 +02:00
},
},
2022-03-22 18:14:56 +02:00
},
directives: {
2026-01-06 16:22:52 +02:00
'click-outside': vClickOutside,
},
2019-09-25 20:54:07 +03:00
},
2022-03-22 18:14:56 +02:00
props: {
suggest: () => [],
enableEmojiPicker: true,
2026-01-06 16:22:52 +02:00
modelValue: value,
},
2022-03-22 18:14:56 +02:00
slots: {
2026-01-06 16:22:52 +02:00
default: () => h('input', ''),
},
})
2022-03-22 18:14:56 +02:00
return wrapper
}
describe('EmojiInput', () => {
describe('insertion mechanism', () => {
it('inserts string at the end with trailing space', () => {
const initialString = 'Testing'
2022-03-22 18:14:56 +02:00
const wrapper = generateInput(initialString)
const input = wrapper.find('input')
input.setValue(initialString)
wrapper.setData({ caret: initialString.length })
wrapper.vm.insert({ insertion: '(test)', keepOpen: false })
2022-03-22 18:14:56 +02:00
const inputEvents = wrapper.emitted()['update:modelValue']
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 '
2022-03-22 18:14:56 +02:00
const wrapper = generateInput(initialString)
const input = wrapper.find('input')
input.setValue(initialString)
wrapper.setData({ caret: initialString.length })
wrapper.vm.insert({ insertion: '(test)', keepOpen: false })
2022-03-22 18:14:56 +02:00
const inputEvents = wrapper.emitted()['update:modelValue']
expect(inputEvents[inputEvents.length - 1][0]).to.eql('Testing (test) ')
})
it('inserts string at the begginning without leading space', () => {
const initialString = 'Testing'
2022-03-22 18:14:56 +02:00
const wrapper = generateInput(initialString)
const input = wrapper.find('input')
input.setValue(initialString)
wrapper.setData({ caret: 0 })
wrapper.vm.insert({ insertion: '(test)', keepOpen: false })
2022-03-22 18:14:56 +02:00
const inputEvents = wrapper.emitted()['update:modelValue']
expect(inputEvents[inputEvents.length - 1][0]).to.eql('(test) Testing')
})
it('inserts string between words without creating extra spaces', () => {
const initialString = 'Spurdo Sparde'
2022-03-22 18:14:56 +02:00
const wrapper = generateInput(initialString)
const input = wrapper.find('input')
input.setValue(initialString)
wrapper.setData({ caret: 6 })
wrapper.vm.insert({ insertion: ':ebin:', keepOpen: false })
2022-03-22 18:14:56 +02:00
const inputEvents = wrapper.emitted()['update:modelValue']
2026-01-06 16:22:52 +02:00
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'
2022-03-22 18:14:56 +02:00
const wrapper = generateInput(initialString)
const input = wrapper.find('input')
input.setValue(initialString)
wrapper.setData({ caret: 7 })
wrapper.vm.insert({ insertion: ':ebin:', keepOpen: false })
2022-03-22 18:14:56 +02:00
const inputEvents = wrapper.emitted()['update:modelValue']
2026-01-06 16:22:52 +02:00
expect(inputEvents[inputEvents.length - 1][0]).to.eql(
'Spurdo :ebin: Sparde',
)
})
2019-09-25 20:54:07 +03:00
it('inserts string without any padding if padEmoji setting is set to false', () => {
const initialString = 'Eat some spam!'
2022-03-22 18:14:56 +02:00
const wrapper = generateInput(initialString, false)
const input = wrapper.find('input')
input.setValue(initialString)
2019-09-25 20:54:07 +03:00
wrapper.setData({ caret: initialString.length, keepOpen: false })
wrapper.vm.insert({ insertion: ':spam:' })
2022-03-22 18:14:56 +02:00
const inputEvents = wrapper.emitted()['update:modelValue']
2026-01-06 16:22:52 +02:00
expect(inputEvents[inputEvents.length - 1][0]).to.eql(
'Eat some spam!:spam:',
)
})
2025-02-27 22:54:23 -05:00
it('correctly sets caret after insertion at beginning', async () => {
const initialString = '1234'
2022-03-22 18:14:56 +02:00
const wrapper = generateInput(initialString)
const input = wrapper.find('input')
input.setValue(initialString)
wrapper.setData({ caret: 0 })
wrapper.vm.insert({ insertion: '1234', keepOpen: false })
2025-02-27 22:54:23 -05:00
await wrapper.vm.$nextTick()
expect(wrapper.vm.caret).to.eql(5)
})
2025-02-27 22:54:23 -05:00
it('correctly sets caret after insertion at end', async () => {
const initialString = '1234'
2022-03-22 18:14:56 +02:00
const wrapper = generateInput(initialString)
const input = wrapper.find('input')
input.setValue(initialString)
wrapper.setData({ caret: initialString.length })
wrapper.vm.insert({ insertion: '1234', keepOpen: false })
2025-02-27 22:54:23 -05:00
await wrapper.vm.$nextTick()
expect(wrapper.vm.caret).to.eql(10)
})
2025-02-27 22:54:23 -05:00
it('correctly sets caret after insertion if padEmoji setting is set to false', async () => {
const initialString = '1234'
2022-03-22 18:14:56 +02:00
const wrapper = generateInput(initialString, false)
const input = wrapper.find('input')
input.setValue(initialString)
wrapper.setData({ caret: initialString.length })
2019-09-25 20:54:07 +03:00
wrapper.vm.insert({ insertion: '1234', keepOpen: false })
2025-02-27 22:54:23 -05:00
await wrapper.vm.$nextTick()
expect(wrapper.vm.caret).to.eql(8)
})
})
})