Limit suggestions list to 15, add component tests

This commit is contained in:
Matt Rider 2019-09-02 10:42:37 +02:00
parent 3e5a624147
commit cf10b650ae
2 changed files with 31 additions and 5 deletions

View File

@ -3,6 +3,7 @@ import Editor from './Editor'
import Vuex from 'vuex'
import Styleguide from '@human-connection/styleguide'
import MutationObserver from 'mutation-observer'
import Vue from 'vue'
global.MutationObserver = MutationObserver
@ -55,9 +56,11 @@ describe('Editor.vue', () => {
propsData.value = 'I am a piece of text'
})
it.skip('renders', () => {
it('renders', async () => {
wrapper = Wrapper()
expect(wrapper.find('.ProseMirror').text()).toContain('I am a piece of text')
await Vue.nextTick().then(() => {
expect(wrapper.find('.editor-content').text()).toContain(propsData.value)
})
})
})
@ -80,6 +83,16 @@ describe('Editor.vue', () => {
expect(wrapper.vm.editor.extensions.options.mention.items()).toEqual(propsData.users)
})
it('limits the suggestion list to 15 users', () => {
let manyUsersList = []
for (let i = 0; i < 25; i++) {
manyUsersList.push({ id: `user${i}` })
}
propsData.users = manyUsersList
wrapper = Wrapper()
expect(wrapper.vm.editor.extensions.options.mention.items()).toHaveLength(15)
})
it('mentions is not an option when there are no users', () => {
expect(wrapper.vm.editor.extensions.options).toEqual(
expect.not.objectContaining({
@ -98,6 +111,16 @@ describe('Editor.vue', () => {
expect(wrapper.vm.editor.extensions.options.hashtag.items()).toEqual(propsData.hashtags)
})
it('limits the suggestion list to 15 users', () => {
let manyHashtagsList = []
for (let i = 0; i < 25; i++) {
manyHashtagsList.push({ id: `hashtag${i}` })
}
propsData.hashtags = manyHashtagsList
wrapper = Wrapper()
expect(wrapper.vm.editor.extensions.options.hashtag.items()).toHaveLength(15)
})
it('hashtags is not an option when there are no hashtags', () => {
expect(wrapper.vm.editor.extensions.options).toEqual(
expect.not.objectContaining({

View File

@ -77,7 +77,7 @@ export default {
extensions.push(
new Mention({
items: () => {
return this.users
return this.users.slice(0, 15)
},
onEnter: props => this.openSuggestionList(props, MENTION),
onChange: this.updateSuggestionList,
@ -92,7 +92,7 @@ export default {
extensions.push(
new Hashtag({
items: () => {
return this.hashtags
return this.hashtags.slice(0, 15)
},
onEnter: props => this.openSuggestionList(props, HASHTAG),
onChange: this.updateSuggestionList,
@ -205,10 +205,13 @@ export default {
if (!query) {
return items
}
return items.filter(item => {
items = this.suggestionType === HASHTAG ? this.hashtags : this.users
const filteredList = items.filter(item => {
const itemString = item.slug || item.id
return itemString.toLowerCase().includes(query.toLowerCase())
})
return filteredList.slice(0, 14)
},
sanitizeQuery(query) {
if (this.suggestionType === HASHTAG) {