|
| 1 | +import any from '@travi/any'; |
| 2 | +import {assert} from 'chai'; |
| 3 | +import mergeNewlines from './merge-newlines-in-paragraph'; |
| 4 | + |
| 5 | +suite('merge newlines in a paragraph', () => { |
| 6 | + const originalChildren = any.listOf(any.simpleObject); |
| 7 | + const index = any.integer({max: originalChildren.length - 1}); |
| 8 | + |
| 9 | + test('that a newline node is removed if its direct sibling is also a newline', () => { |
| 10 | + const node = {...any.simpleObject(), type: 'text', value: '\n'}; |
| 11 | + const parent = { |
| 12 | + ...any.simpleObject(), |
| 13 | + type: 'paragraph', |
| 14 | + children: [ |
| 15 | + ...originalChildren.slice(0, index), |
| 16 | + {type: 'text', value: '\n'}, |
| 17 | + {type: 'text', value: '\n'}, |
| 18 | + ...originalChildren.slice(index) |
| 19 | + ] |
| 20 | + }; |
| 21 | + |
| 22 | + mergeNewlines(node, index, parent); |
| 23 | + |
| 24 | + assert.deepEqual( |
| 25 | + parent.children, |
| 26 | + [ |
| 27 | + ...originalChildren.slice(0, index), |
| 28 | + {type: 'text', value: '\n'}, |
| 29 | + ...originalChildren.slice(index) |
| 30 | + ] |
| 31 | + ); |
| 32 | + }); |
| 33 | + |
| 34 | + test('that a newline node is removed if node is the last child of the parent', () => { |
| 35 | + const node = {...any.simpleObject(), type: 'text', value: '\n'}; |
| 36 | + const parent = { |
| 37 | + ...any.simpleObject(), |
| 38 | + type: 'paragraph', |
| 39 | + children: [ |
| 40 | + ...originalChildren, |
| 41 | + {type: 'text', value: '\n'} |
| 42 | + ] |
| 43 | + }; |
| 44 | + |
| 45 | + mergeNewlines(node, originalChildren.length, parent); |
| 46 | + |
| 47 | + assert.deepEqual(parent.children, originalChildren); |
| 48 | + }); |
| 49 | + |
| 50 | + test('that the children of the parent are unchanged if the parent is not a `paragraph`', () => { |
| 51 | + const node = {...any.simpleObject(), type: 'text', value: '\n'}; |
| 52 | + const parent = {...any.simpleObject(), children: [...originalChildren]}; |
| 53 | + |
| 54 | + mergeNewlines(node, index, parent); |
| 55 | + |
| 56 | + assert.deepEqual(parent.children, originalChildren); |
| 57 | + }); |
| 58 | + |
| 59 | + test('that the children of the parent are unchanged if the node is not a `text` node', () => { |
| 60 | + const node = {...any.simpleObject(), type: any.word(), value: '\n'}; |
| 61 | + const parent = {...any.simpleObject(), children: [...originalChildren], type: 'paragraph'}; |
| 62 | + |
| 63 | + mergeNewlines(node, index, parent); |
| 64 | + |
| 65 | + assert.deepEqual(parent.children, originalChildren); |
| 66 | + }); |
| 67 | + |
| 68 | + test('that the children of the parent are unchanged if the node is not a newline', () => { |
| 69 | + const node = {...any.simpleObject(), type: 'text', value: any.word()}; |
| 70 | + const parent = {...any.simpleObject(), children: [...originalChildren], type: 'paragraph'}; |
| 71 | + |
| 72 | + mergeNewlines(node, index, parent); |
| 73 | + |
| 74 | + assert.deepEqual(parent.children, originalChildren); |
| 75 | + }); |
| 76 | +}); |
0 commit comments