suggestions.test.js (5773B)
1 import { schema } from 'prosemirror-schema-basic'; 2 import { triggerCharacter } from './suggestions'; 3 4 const CUR = '\u2038'; 5 6 function createPosition(text) { 7 const position = text.indexOf(CUR) + 1; 8 const stripped = text.replace(CUR, ''); 9 10 expect(position).toBeGreaterThan(0); 11 expect(position).toBeLessThanOrEqual(stripped.length + 1); 12 13 const doc = schema.node('doc', null, [ 14 schema.node('paragraph', null, [ 15 schema.text(stripped), 16 ]), 17 ]); 18 19 return doc.resolve(position); 20 } 21 22 function mention(text) { 23 const $position = createPosition(text); 24 25 return triggerCharacter('@', { allowSpaces: true })($position); 26 } 27 28 function tag(text) { 29 const $position = createPosition(text); 30 31 return triggerCharacter('#', { allowSpaces: false })($position); 32 } 33 34 describe('the triggerCharacter matcher', () => { 35 it('will match when cursor is immediately after a node', () => { 36 const doc = schema.node('doc', null, [ 37 schema.node('paragraph', null, [ 38 schema.node('hard_break'), 39 schema.text('@mention'), 40 ]), 41 ]); 42 43 const $position = doc.resolve(3); 44 const result = triggerCharacter('@', { allowSpaces: true })($position); 45 46 expect(result).toMatchObject({ 47 text: '@mention', 48 }); 49 }); 50 51 describe(`for tags`, () => { 52 it(`won't match "${CUR}#"`, () => { 53 const text = `${CUR}#`; 54 55 expect(tag(text)).toBeUndefined(); 56 }); 57 58 it(`will match "#${CUR}"`, () => { 59 const text = `#${CUR}`; 60 61 expect(tag(text)).toMatchObject({ 62 text: '#', 63 }); 64 }); 65 66 it(`won't match "${CUR}#tag"`, () => { 67 const text = `${CUR}#tag`; 68 69 expect(tag(text)).toBeUndefined(); 70 }); 71 72 it(`will match "#${CUR}tag"`, () => { 73 const text = `#${CUR}tag`; 74 75 expect(tag(text)).toMatchObject({ 76 text: '#tag', 77 range: { from: 1, to: 5 }, 78 }); 79 }); 80 81 it(`will match "#tag-with-dashes${CUR}"`, () => { 82 const text = `#tag-with-dashes${CUR}`; 83 84 expect(tag(text)).toMatchObject({ 85 text: '#tag-with-dashes', 86 }); 87 }); 88 89 it(`won't match "#tag with spaces${CUR}"`, () => { 90 const text = `#tag with spaces${CUR}`; 91 92 expect(tag(text)).toBeUndefined(); 93 }); 94 95 it(`won't match "#tag#with#hashes${CUR}"`, () => { 96 const text = `#tag#with#hashes${CUR}`; 97 98 expect(tag(text)).toBeUndefined(); 99 }); 100 101 it(`will match "#multiple${CUR} #tags #separately"`, () => { 102 const text = `#multiple${CUR} #tags #separately`; 103 104 expect(tag(text)).toMatchObject({ 105 text: '#multiple', 106 }); 107 }); 108 109 it(`will match "#multiple #tags${CUR} #separately"`, () => { 110 const text = `#multiple #tags${CUR} #separately`; 111 112 expect(tag(text)).toMatchObject({ 113 text: '#tags', 114 }); 115 }); 116 117 it(`will match "#multiple #tags #separately${CUR}"`, () => { 118 const text = `#multiple #tags #separately${CUR}`; 119 120 expect(tag(text)).toMatchObject({ 121 text: '#separately', 122 }); 123 }); 124 125 it(`won't match "#multiple ${CUR}#tags" when cursor is outside`, () => { 126 const text = `#multiple ${CUR}#tags`; 127 128 expect(tag(text)).toBeUndefined(); 129 }); 130 }); 131 132 describe('for mentions', () => { 133 it(`won't match "${CUR}@"`, () => { 134 const text = `${CUR}@`; 135 136 expect(mention(text)).toBeUndefined(); 137 }); 138 139 it(`will match "@${CUR}"`, () => { 140 const text = `@${CUR}`; 141 142 expect(mention(text)).toMatchObject({ 143 text: '@', 144 }); 145 }); 146 147 it(`won't match "${CUR}@mention"`, () => { 148 const text = `${CUR}@mention`; 149 150 expect(mention(text)).toBeUndefined(); 151 }); 152 153 it(`will match "@${CUR}mention"`, () => { 154 const text = `@${CUR}mention`; 155 156 expect(mention(text)).toMatchObject({ 157 text: '@mention', 158 range: { from: 1, to: 9 }, 159 }); 160 }); 161 162 it(`will match "@m${CUR}ention"`, () => { 163 const text = `@m${CUR}ention`; 164 165 expect(mention(text)).toMatchObject({ 166 text: '@mention', 167 }); 168 }); 169 170 it(`will match "@mention${CUR}"`, () => { 171 const text = `@mention${CUR}`; 172 173 expect(mention(text)).toMatchObject({ 174 text: '@mention', 175 }); 176 }); 177 178 it(`will match "@mention ${CUR}"`, () => { 179 const text = `@mention ${CUR}`; 180 181 expect(mention(text)).toMatchObject({ 182 text: '@mention ', 183 }); 184 }); 185 186 it(`will match "@mentions with${CUR} spaces"`, () => { 187 const text = `@mentions with${CUR} spaces`; 188 189 expect(mention(text)).toMatchObject({ 190 text: '@mentions with spaces', 191 }); 192 }); 193 194 it(`will match "@${CUR}mentions with spaces"`, () => { 195 const text = `@${CUR}mentions with spaces`; 196 197 expect(mention(text)).toMatchObject({ 198 text: '@mentions with spaces', 199 }); 200 }); 201 202 it(`will match "@multiple ${CUR}@mentions"`, () => { 203 const text = `@multiple ${CUR}@mentions`; 204 205 expect(mention(text)).toMatchObject({ 206 text: '@multiple ', 207 }); 208 }); 209 210 it(`will match "@multiple @${CUR}mentions"`, () => { 211 const text = `@multiple @${CUR}mentions`; 212 213 expect(mention(text)).toMatchObject({ 214 text: '@mentions', 215 }); 216 }); 217 218 it(`will match "mentions with text @${CUR}before them"`, () => { 219 const text = `mentions with text @${CUR}before them`; 220 221 expect(mention(text)).toMatchObject({ 222 text: '@before them', 223 }); 224 }); 225 226 it(`will match "@${CUR}mentioned@email.address"`, () => { 227 const text = `@${CUR}mentioned@email.address`; 228 229 expect(mention(text)).toMatchObject({ 230 text: '@mentioned@email.address', 231 }); 232 }); 233 234 it(`won't match "normal@${CUR}email.address"`, () => { 235 const text = `normal@${CUR}email.address`; 236 237 expect(mention(text)).toBeUndefined(); 238 }); 239 }); 240 });