|
| 1 | +const { List, Map } = require('immutable'); |
| 2 | +const { ContentBlock, ContentState, EditorState, CharacterMetadata } = require('draft-js'); |
| 3 | +const hasSelectionInBlock = require('../hasSelectionInBlock'); |
| 4 | + |
| 5 | +const text = 'const a = "b";'; |
| 6 | +const textBlock = new ContentBlock({ |
| 7 | + key: 'texty-blockey', |
| 8 | + type: 'unstyled', |
| 9 | + text, |
| 10 | + characterList: new List(text.split('').map(char => new CharacterMetadata())), |
| 11 | + depth: 0, |
| 12 | + data: new Map(), |
| 13 | +}) |
| 14 | + |
| 15 | +const codeBlock = new ContentBlock({ |
| 16 | + key: 'codey-blockey', |
| 17 | + type: 'code-block', |
| 18 | + text, |
| 19 | + characterList: new List(text.split('').map(char => new CharacterMetadata())), |
| 20 | + depth: 0, |
| 21 | + data: new Map(), |
| 22 | +}); |
| 23 | + |
| 24 | +it('should return true if the selected block is a "code-block"', () => { |
| 25 | + const contentState = ContentState.createFromBlockArray([codeBlock]); |
| 26 | + const editorState = EditorState.createWithContent(contentState); |
| 27 | + |
| 28 | + expect(hasSelectionInBlock(editorState)).toEqual(true); |
| 29 | +}); |
| 30 | + |
| 31 | +it('should return true if there is multiple "code-block"s and one is selected', () => { |
| 32 | + const text = 'const a = "b";'; |
| 33 | + const contentState = ContentState.createFromBlockArray([codeBlock, codeBlock]); |
| 34 | + const editorState = EditorState.createWithContent(contentState); |
| 35 | + |
| 36 | + expect(hasSelectionInBlock(editorState)).toEqual(true); |
| 37 | +}); |
| 38 | + |
| 39 | +it('should return false if there is no "code-block"', () => { |
| 40 | + const text = 'const a = "b";'; |
| 41 | + const contentState = ContentState.createFromBlockArray([textBlock]); |
| 42 | + const editorState = EditorState.createWithContent(contentState); |
| 43 | + |
| 44 | + expect(hasSelectionInBlock(editorState)).toEqual(false); |
| 45 | +}); |
| 46 | + |
| 47 | +it('should return false if the selected block is not a "code-block"', () => { |
| 48 | + const text = 'const a = "b";'; |
| 49 | + const contentState = ContentState.createFromBlockArray([textBlock, codeBlock]); |
| 50 | + const editorState = EditorState.createWithContent(contentState); |
| 51 | + |
| 52 | + expect(hasSelectionInBlock(editorState)).toEqual(false); |
| 53 | +}); |
0 commit comments