|
| 1 | +import { EditorState } from 'prosemirror-state'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | +import { cellAround } from '../src/util'; |
| 4 | +import { c, c11, cCursor, p, table, tr, selectionFor } from './build'; |
| 5 | + |
| 6 | +describe('cellAround', () => { |
| 7 | + it('cursor is in text', () => { |
| 8 | + const doc = table( |
| 9 | + tr(cCursor, c11, c11), |
| 10 | + ); |
| 11 | + const state = EditorState.create({ doc, selection: selectionFor(doc) }); |
| 12 | + |
| 13 | + expect(cellAround(state.doc.resolve(state.selection.anchor))).not.toBeNull(); |
| 14 | + expect(cellAround(state.doc.resolve(doc.content.size))).toBeNull(); |
| 15 | + }); |
| 16 | + // Test finding a cell in a regular cell |
| 17 | + it('finds a regular cell', () => { |
| 18 | + // Create a simple table with cursor in the middle cell |
| 19 | + const doc = table( |
| 20 | + tr(c11, c11, c11), |
| 21 | + tr(c11, cCursor, c11), |
| 22 | + tr(c11, c11, c11), |
| 23 | + ); |
| 24 | + const state = EditorState.create({ doc, selection: selectionFor(doc) }); |
| 25 | + const $pos = state.doc.resolve(state.selection.anchor); |
| 26 | + const $cell = cellAround($pos); |
| 27 | + |
| 28 | + // Verify that the cell was found |
| 29 | + expect($cell).not.toBeNull(); |
| 30 | + // Verify that the found cell is the expected merged cell |
| 31 | + const cellNode = $cell!.nodeAfter; |
| 32 | + expect(cellNode!.attrs.colspan).toBe(1); |
| 33 | + expect(cellNode!.attrs.rowspan).toBe(1); |
| 34 | + }); |
| 35 | + |
| 36 | + // Test finding a cell when cursor is inside a merged cell |
| 37 | + it('finds a merged cell when cursor is inside', () => { |
| 38 | + // Create a table with a merged cell, cursor inside the merged cell |
| 39 | + const doc = table( |
| 40 | + tr(c11, c(2, 2, 'x<cursor>'), c11), |
| 41 | + tr(c11, c11), |
| 42 | + tr(c11, c11, c11), |
| 43 | + ); |
| 44 | + |
| 45 | + const state = EditorState.create({ doc, selection: selectionFor(doc) }); |
| 46 | + const $pos = state.doc.resolve(state.selection.anchor); |
| 47 | + const $cell = cellAround($pos); |
| 48 | + |
| 49 | + // Verify that the cell was found |
| 50 | + expect($cell).not.toBeNull(); |
| 51 | + // Verify that the found cell is a merged cell |
| 52 | + const cellNode = $cell!.nodeAfter; |
| 53 | + expect(cellNode!.attrs.colspan).toBe(2); |
| 54 | + expect(cellNode!.attrs.rowspan).toBe(2); |
| 55 | + }); |
| 56 | + |
| 57 | + |
| 58 | +}); |
0 commit comments