|
| 1 | +import vscode, { window } from 'vscode'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +import extractRangeToNewNote from './extractRangeToNewNote'; |
| 5 | +import { getWorkspaceFolder } from '../utils'; |
| 6 | +import { |
| 7 | + closeEditorsAndCleanWorkspace, |
| 8 | + rndName, |
| 9 | + createFile, |
| 10 | + openTextDocument, |
| 11 | +} from '../test/testUtils'; |
| 12 | + |
| 13 | +describe('extractRangeToNewNote command', () => { |
| 14 | + beforeEach(closeEditorsAndCleanWorkspace); |
| 15 | + |
| 16 | + afterEach(closeEditorsAndCleanWorkspace); |
| 17 | + |
| 18 | + it('should extract range to a new note', async () => { |
| 19 | + const name0 = rndName(); |
| 20 | + const name1 = rndName(); |
| 21 | + |
| 22 | + await createFile(`${name0}.md`, 'Hello world.'); |
| 23 | + |
| 24 | + const doc = await openTextDocument(`${name0}.md`); |
| 25 | + |
| 26 | + const targetPathInputBoxSpy = jest.spyOn(vscode.window, 'showInputBox'); |
| 27 | + |
| 28 | + targetPathInputBoxSpy.mockReturnValue( |
| 29 | + Promise.resolve(path.join(getWorkspaceFolder()!, `${name1}.md`)), |
| 30 | + ); |
| 31 | + |
| 32 | + await extractRangeToNewNote(doc, new vscode.Range(0, 0, 0, 12)); |
| 33 | + |
| 34 | + expect(await doc.getText()).toBe(''); |
| 35 | + |
| 36 | + const newDoc = await openTextDocument(`${name1}.md`); |
| 37 | + |
| 38 | + expect(await newDoc.getText()).toBe('Hello world.'); |
| 39 | + |
| 40 | + targetPathInputBoxSpy.mockRestore(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should extract a multiline range to a new note', async () => { |
| 44 | + const name0 = rndName(); |
| 45 | + const name1 = rndName(); |
| 46 | + |
| 47 | + await createFile( |
| 48 | + `${name0}.md`, |
| 49 | + `Multiline |
| 50 | + Hello world.`, |
| 51 | + ); |
| 52 | + |
| 53 | + const doc = await openTextDocument(`${name0}.md`); |
| 54 | + |
| 55 | + const targetPathInputBoxSpy = jest.spyOn(vscode.window, 'showInputBox'); |
| 56 | + |
| 57 | + targetPathInputBoxSpy.mockReturnValue( |
| 58 | + Promise.resolve(path.join(getWorkspaceFolder()!, `${name1}.md`)), |
| 59 | + ); |
| 60 | + |
| 61 | + await extractRangeToNewNote(doc, new vscode.Range(0, 0, 1, 16)); |
| 62 | + |
| 63 | + expect(await doc.getText()).toBe(''); |
| 64 | + |
| 65 | + const newDoc = await openTextDocument(`${name1}.md`); |
| 66 | + |
| 67 | + expect(await newDoc.getText()).toMatchInlineSnapshot(` |
| 68 | + "Multiline |
| 69 | + Hello world." |
| 70 | + `); |
| 71 | + |
| 72 | + targetPathInputBoxSpy.mockRestore(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should extract range from active markdown file', async () => { |
| 76 | + const name0 = rndName(); |
| 77 | + const name1 = rndName(); |
| 78 | + |
| 79 | + await createFile(`${name0}.md`, 'Hello world.'); |
| 80 | + |
| 81 | + const doc = await openTextDocument(`${name0}.md`); |
| 82 | + const editor = await window.showTextDocument(doc); |
| 83 | + |
| 84 | + editor.selection = new vscode.Selection(0, 0, 0, 12); |
| 85 | + |
| 86 | + const targetPathInputBoxSpy = jest.spyOn(vscode.window, 'showInputBox'); |
| 87 | + |
| 88 | + targetPathInputBoxSpy.mockReturnValue( |
| 89 | + Promise.resolve(path.join(getWorkspaceFolder()!, `${name1}.md`)), |
| 90 | + ); |
| 91 | + |
| 92 | + await extractRangeToNewNote(); |
| 93 | + |
| 94 | + expect(await doc.getText()).toBe(''); |
| 95 | + |
| 96 | + const newDoc = await openTextDocument(`${name1}.md`); |
| 97 | + |
| 98 | + expect(await newDoc.getText()).toBe('Hello world.'); |
| 99 | + |
| 100 | + targetPathInputBoxSpy.mockRestore(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should not extract anything from unknown file format', async () => { |
| 104 | + const name0 = rndName(); |
| 105 | + |
| 106 | + await createFile(`${name0}.txt`, 'Hello world.'); |
| 107 | + |
| 108 | + const doc = await openTextDocument(`${name0}.txt`); |
| 109 | + const editor = await window.showTextDocument(doc); |
| 110 | + |
| 111 | + editor.selection = new vscode.Selection(0, 0, 0, 12); |
| 112 | + |
| 113 | + const targetPathInputBoxSpy = jest.spyOn(vscode.window, 'showInputBox'); |
| 114 | + |
| 115 | + await extractRangeToNewNote(); |
| 116 | + |
| 117 | + expect(await doc.getText()).toBe('Hello world.'); |
| 118 | + |
| 119 | + expect(targetPathInputBoxSpy).not.toBeCalled(); |
| 120 | + |
| 121 | + targetPathInputBoxSpy.mockRestore(); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should fail when target path is outside of the workspace', async () => { |
| 125 | + const name0 = rndName(); |
| 126 | + |
| 127 | + await createFile(`${name0}.md`, 'Hello world.'); |
| 128 | + |
| 129 | + const doc = await openTextDocument(`${name0}.md`); |
| 130 | + |
| 131 | + const targetPathInputBoxSpy = jest.spyOn(vscode.window, 'showInputBox'); |
| 132 | + |
| 133 | + targetPathInputBoxSpy.mockReturnValue(Promise.resolve('/random-path/file.md')); |
| 134 | + |
| 135 | + expect(extractRangeToNewNote(doc, new vscode.Range(0, 0, 0, 12))).rejects.toThrowError( |
| 136 | + 'should be within the current workspace', |
| 137 | + ); |
| 138 | + |
| 139 | + targetPathInputBoxSpy.mockRestore(); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should fail when entered file already exists', async () => { |
| 143 | + const name0 = rndName(); |
| 144 | + const name1 = rndName(); |
| 145 | + |
| 146 | + await createFile(`${name0}.md`, 'Hello world.'); |
| 147 | + await createFile(`${name1}.md`); |
| 148 | + |
| 149 | + const doc = await openTextDocument(`${name0}.md`); |
| 150 | + |
| 151 | + const targetPathInputBoxSpy = jest.spyOn(vscode.window, 'showInputBox'); |
| 152 | + |
| 153 | + targetPathInputBoxSpy.mockReturnValue( |
| 154 | + Promise.resolve(path.join(getWorkspaceFolder()!, `${name1}.md`)), |
| 155 | + ); |
| 156 | + |
| 157 | + expect(extractRangeToNewNote(doc, new vscode.Range(0, 0, 0, 12))).rejects.toThrowError( |
| 158 | + 'Such file or directory already exists. Please use unique filename instead.', |
| 159 | + ); |
| 160 | + |
| 161 | + targetPathInputBoxSpy.mockRestore(); |
| 162 | + }); |
| 163 | +}); |
0 commit comments