import-folds.test.mjs (2079B)
1 import test from 'node:test';import assert from 'node:assert/strict'; 2 import {EditorState,EditorSelection,EditorView} from '../dist/codemirror.js'; 3 import {importSites,selectedImportLines,importFolds} from '../dist/import-folds.js'; 4 const hash='0123456789abcdef'.repeat(4),other='abcdef0123456789'.repeat(4); 5 test('import projection recognizes only real, complete, line-head import hashes',()=>{ 6 const source=`-- !import ${hash}\ntext = "!import ${hash}"\n|- !import ${hash} -|\nx = !import ${hash}\n!import ${hash.slice(1)} as short\n!import ${hash.toUpperCase()} as upper\n!import ${hash}x as extra\n!import ${hash} as A\n !import\t${other} as B`; 7 const sites=importSites(EditorState.create({doc:source})); 8 assert.equal(sites.length,2);assert.deepEqual(sites.map(site=>site.hash),[hash,other]); 9 for(const site of sites)assert.equal(source.slice(site.from,site.to),site.hash); 10 }); 11 test('selection exposes only cursor head lines',()=>{ 12 const doc=`-- λ🙂\n!import ${hash} as A\n!import ${other} as B\nanswer = 42`; 13 let state=EditorState.create({doc}); 14 assert.deepEqual([...selectedImportLines(state)],[1]); 15 state=state.update({selection:EditorSelection.single(doc.indexOf(' as A')+4)}).state; 16 assert.deepEqual([...selectedImportLines(state)],[2]); 17 state=state.update({selection:EditorSelection.single(state.doc.line(2).from,state.doc.line(4).from)}).state; 18 assert.deepEqual([...selectedImportLines(state)],[4]); 19 }); 20 test('projection transactions preserve canonical source and character-level ranges',()=>{ 21 const doc=`!import ${hash} as A\n!import ${other} as B\nanswer = 42`; 22 let state=EditorState.create({doc,extensions:[importFolds]}); 23 assert.equal(state.field(importFolds).size,2); 24 state=state.update({selection:EditorSelection.single(state.doc.line(3).from)}).state; 25 assert.equal(state.doc.toString(),doc);assert.deepEqual(state.facet(EditorView.atomicRanges),[]); 26 state=state.update({changes:{from:0,insert:`!import ${hash} as C\n`}}).state; 27 assert.equal(importSites(state).length,3);assert.equal(state.doc.toString(),`!import ${hash} as C\n`+doc); 28 });