|
| 1 | +// @ts-check |
| 2 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 3 | +import { ListHelpers } from '../helpers/list-numbering-helpers.js'; |
| 4 | +import { decreaseListIndent } from './decreaseListIndent.js'; |
| 5 | + |
| 6 | +// Mock the helper modules used by the command |
| 7 | +vi.mock('../helpers/list-numbering-helpers.js', () => { |
| 8 | + const fns = { |
| 9 | + getCurrentListItem: vi.fn(), |
| 10 | + getParentOrderedList: vi.fn(), |
| 11 | + getParentBulletList: vi.fn(), |
| 12 | + getNewListId: vi.fn(), |
| 13 | + generateNewListDefinition: vi.fn(), |
| 14 | + }; |
| 15 | + return { ListHelpers: fns }; |
| 16 | +}); |
| 17 | + |
| 18 | +vi.mock('../helpers/index.js', () => { |
| 19 | + // The command falls back to findParentNode(...) only if the ListHelpers returns null. |
| 20 | + // We'll default to returning null so ListHelpers drive the tests. |
| 21 | + return { |
| 22 | + findParentNode: () => () => null, |
| 23 | + }; |
| 24 | +}); |
| 25 | + |
| 26 | +describe('decreaseListIndent', () => { |
| 27 | + /** @type {{ state: any }} */ |
| 28 | + let editor; |
| 29 | + /** @type {{ setNodeMarkup: ReturnType<typeof vi.fn> }} */ |
| 30 | + let tr; |
| 31 | + |
| 32 | + const OrderedListType = { name: 'orderedList' }; |
| 33 | + const BulletListType = { name: 'bulletList' }; |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + vi.clearAllMocks(); |
| 37 | + editor = { state: { selection: {} } }; |
| 38 | + tr = { setNodeMarkup: vi.fn() }; |
| 39 | + }); |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + vi.resetModules(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('returns false when no current list item is found', () => { |
| 46 | + ListHelpers.getCurrentListItem.mockReturnValue(null); |
| 47 | + ListHelpers.getParentOrderedList.mockReturnValue(null); |
| 48 | + ListHelpers.getParentBulletList.mockReturnValue(null); |
| 49 | + |
| 50 | + const result = decreaseListIndent()({ editor, tr }); |
| 51 | + expect(result).toBe(false); |
| 52 | + expect(tr.setNodeMarkup).not.toHaveBeenCalled(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('returns false when no parent list is found', () => { |
| 56 | + const currentItem = { |
| 57 | + node: { type: { name: 'listItem' }, attrs: { level: 2, numId: 123 } }, |
| 58 | + pos: 10, |
| 59 | + }; |
| 60 | + ListHelpers.getCurrentListItem.mockReturnValue(currentItem); |
| 61 | + ListHelpers.getParentOrderedList.mockReturnValue(null); |
| 62 | + ListHelpers.getParentBulletList.mockReturnValue(null); |
| 63 | + |
| 64 | + const result = decreaseListIndent()({ editor, tr }); |
| 65 | + expect(result).toBe(false); |
| 66 | + expect(tr.setNodeMarkup).not.toHaveBeenCalled(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('no-ops (returns true) at level 0 and does not mutate the doc', () => { |
| 70 | + const currentItem = { |
| 71 | + node: { type: { name: 'listItem' }, attrs: { level: 0 /* no numId */ } }, |
| 72 | + pos: 5, |
| 73 | + }; |
| 74 | + const parentList = { |
| 75 | + node: { type: OrderedListType, attrs: { listId: 777 } }, |
| 76 | + }; |
| 77 | + |
| 78 | + ListHelpers.getCurrentListItem.mockReturnValue(currentItem); |
| 79 | + ListHelpers.getParentOrderedList.mockReturnValue(parentList); |
| 80 | + ListHelpers.getParentBulletList.mockReturnValue(null); |
| 81 | + |
| 82 | + const result = decreaseListIndent()({ editor, tr }); |
| 83 | + expect(result).toBe(true); |
| 84 | + expect(tr.setNodeMarkup).not.toHaveBeenCalled(); |
| 85 | + expect(ListHelpers.generateNewListDefinition).not.toHaveBeenCalled(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('decreases level by 1 and keeps existing numId; ensures list definition', () => { |
| 89 | + const currentItem = { |
| 90 | + node: { type: { name: 'listItem' }, attrs: { level: 2, numId: 123, foo: 'bar' } }, |
| 91 | + pos: 42, |
| 92 | + }; |
| 93 | + const parentList = { |
| 94 | + node: { type: OrderedListType, attrs: { listId: 777 } }, |
| 95 | + }; |
| 96 | + |
| 97 | + ListHelpers.getCurrentListItem.mockReturnValue(currentItem); |
| 98 | + ListHelpers.getParentOrderedList.mockReturnValue(parentList); |
| 99 | + ListHelpers.getParentBulletList.mockReturnValue(null); |
| 100 | + |
| 101 | + const result = decreaseListIndent()({ editor, tr }); |
| 102 | + |
| 103 | + expect(result).toBe(true); |
| 104 | + expect(tr.setNodeMarkup).toHaveBeenCalledTimes(1); |
| 105 | + expect(tr.setNodeMarkup).toHaveBeenCalledWith(42, null, { |
| 106 | + foo: 'bar', |
| 107 | + level: 1, // 2 -> 1 |
| 108 | + numId: 123, // keeps existing |
| 109 | + }); |
| 110 | + |
| 111 | + expect(ListHelpers.generateNewListDefinition).toHaveBeenCalledTimes(1); |
| 112 | + expect(ListHelpers.generateNewListDefinition).toHaveBeenCalledWith({ |
| 113 | + numId: 123, |
| 114 | + listType: OrderedListType, |
| 115 | + editor, |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + it('uses parent list listId when current item has no numId', () => { |
| 120 | + const currentItem = { |
| 121 | + node: { type: { name: 'listItem' }, attrs: { level: 3 } }, |
| 122 | + pos: 7, |
| 123 | + }; |
| 124 | + const parentList = { |
| 125 | + node: { type: BulletListType, attrs: { listId: 888 } }, |
| 126 | + }; |
| 127 | + |
| 128 | + ListHelpers.getCurrentListItem.mockReturnValue(currentItem); |
| 129 | + ListHelpers.getParentOrderedList.mockReturnValue(null); |
| 130 | + ListHelpers.getParentBulletList.mockReturnValue(parentList); |
| 131 | + |
| 132 | + const result = decreaseListIndent()({ editor, tr }); |
| 133 | + |
| 134 | + expect(result).toBe(true); |
| 135 | + expect(tr.setNodeMarkup).toHaveBeenCalledWith(7, null, { |
| 136 | + level: 2, // 3 -> 2 |
| 137 | + numId: 888, // inherited from parent |
| 138 | + }); |
| 139 | + |
| 140 | + expect(ListHelpers.generateNewListDefinition).toHaveBeenCalledWith({ |
| 141 | + numId: 888, |
| 142 | + listType: BulletListType, |
| 143 | + editor, |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + it('falls back to ListHelpers.getNewListId when neither item nor parent have ids', () => { |
| 148 | + const currentItem = { |
| 149 | + node: { type: { name: 'listItem' }, attrs: { level: 1 } }, |
| 150 | + pos: 11, |
| 151 | + }; |
| 152 | + const parentList = { |
| 153 | + node: { |
| 154 | + type: OrderedListType, |
| 155 | + attrs: { |
| 156 | + /* no listId */ |
| 157 | + }, |
| 158 | + }, |
| 159 | + }; |
| 160 | + |
| 161 | + ListHelpers.getCurrentListItem.mockReturnValue(currentItem); |
| 162 | + ListHelpers.getParentOrderedList.mockReturnValue(parentList); |
| 163 | + ListHelpers.getParentBulletList.mockReturnValue(null); |
| 164 | + ListHelpers.getNewListId.mockReturnValue(9999); |
| 165 | + |
| 166 | + const result = decreaseListIndent()({ editor, tr }); |
| 167 | + |
| 168 | + expect(result).toBe(true); |
| 169 | + expect(ListHelpers.getNewListId).toHaveBeenCalledWith(editor); |
| 170 | + expect(tr.setNodeMarkup).toHaveBeenCalledWith(11, null, { |
| 171 | + level: 0, // 1 -> 0 |
| 172 | + numId: 9999, // fallback |
| 173 | + }); |
| 174 | + |
| 175 | + expect(ListHelpers.generateNewListDefinition).toHaveBeenCalledWith({ |
| 176 | + numId: 9999, |
| 177 | + listType: OrderedListType, |
| 178 | + editor, |
| 179 | + }); |
| 180 | + }); |
| 181 | + |
| 182 | + it('does not generate a list definition if resolved numId is null/undefined', () => { |
| 183 | + const currentItem = { |
| 184 | + node: { type: { name: 'listItem' }, attrs: { level: 2 } }, |
| 185 | + pos: 21, |
| 186 | + }; |
| 187 | + const parentList = { |
| 188 | + node: { type: OrderedListType, attrs: {} }, // no listId |
| 189 | + }; |
| 190 | + |
| 191 | + ListHelpers.getCurrentListItem.mockReturnValue(currentItem); |
| 192 | + ListHelpers.getParentOrderedList.mockReturnValue(parentList); |
| 193 | + ListHelpers.getParentBulletList.mockReturnValue(null); |
| 194 | + ListHelpers.getNewListId.mockReturnValue(null); // still no id |
| 195 | + |
| 196 | + const result = decreaseListIndent()({ editor, tr }); |
| 197 | + |
| 198 | + expect(result).toBe(true); |
| 199 | + expect(tr.setNodeMarkup).toHaveBeenCalledWith(21, null, { |
| 200 | + level: 1, |
| 201 | + numId: null, // explicit null is fine; command should still set it |
| 202 | + }); |
| 203 | + expect(ListHelpers.generateNewListDefinition).not.toHaveBeenCalled(); |
| 204 | + }); |
| 205 | +}); |
0 commit comments