From 4a87a12ac207c2b6e61dc695016f19fa18ead4f9 Mon Sep 17 00:00:00 2001 From: Maddy <12754347+MaddyDev@users.noreply.github.com> Date: Sun, 26 Apr 2020 23:34:53 -0700 Subject: [PATCH] added more tests (#10161) * added more tests * typo fixes * removed unused func * added an extra assert * fix typo --- .../browser/find/notebookFindModel.ts | 11 ---- .../notebookFindModel.test.ts | 65 ++++++++++++++++++- 2 files changed, 63 insertions(+), 13 deletions(-) diff --git a/src/sql/workbench/contrib/notebook/browser/find/notebookFindModel.ts b/src/sql/workbench/contrib/notebook/browser/find/notebookFindModel.ts index 70a89c31cb..0c2a2a6858 100644 --- a/src/sql/workbench/contrib/notebook/browser/find/notebookFindModel.ts +++ b/src/sql/workbench/contrib/notebook/browser/find/notebookFindModel.ts @@ -612,17 +612,6 @@ export class NotebookFindModel extends Disposable implements INotebookFindModel return cellSrc.replace(/(?:__|[*#])|\[(.*?)\]\(.*?\)/gm, '$1'); } - // remove /n's to calculate the line number to locate the correct element - cleanUpCellSource(cellValue: string | string[]): string | string[] { - let trimmedCellSrc: string[] = []; - if (cellValue instanceof Array) { - trimmedCellSrc = cellValue.filter(c => c !== '\n' && c !== '\r\n' && c.indexOf('|-') === -1); - } else { - return cellValue; - } - return trimmedCellSrc; - } - clearFind(): void { this._findArray = new Array(); this._findIndex = 0; diff --git a/src/sql/workbench/contrib/notebook/test/electron-browser/notebookFindModel.test.ts b/src/sql/workbench/contrib/notebook/test/electron-browser/notebookFindModel.test.ts index 902277d015..9402210fea 100644 --- a/src/sql/workbench/contrib/notebook/test/electron-browser/notebookFindModel.test.ts +++ b/src/sql/workbench/contrib/notebook/test/electron-browser/notebookFindModel.test.ts @@ -110,6 +110,20 @@ suite('Notebook Find Model', function (): void { await initNotebookModel(expectedNotebookContent); }); + test('Should set notebook model on initialize', async function (): Promise { + //initialize find + let notebookFindModel = new NotebookFindModel(model); + assert.equal(notebookFindModel.notebookModel, model, 'Failed to set notebook model'); + }); + + test('Should have no decorations on initialize', async function (): Promise { + //initialize find + let notebookFindModel = new NotebookFindModel(model); + assert.equal(notebookFindModel.findDecorations, undefined, 'findDecorations should be undefined on initialize'); + assert.equal(notebookFindModel.getPosition(), undefined, 'currentMatch should be undefined on initialize'); + assert.equal(notebookFindModel.getLastPosition(), undefined, 'previousMatch should be undefined on initialize'); + }); + test('Should find results in the notebook', async function (): Promise { // Need to set rendered text content for 2nd cell setRenderedTextContent(1); @@ -119,7 +133,9 @@ suite('Notebook Find Model', function (): void { await notebookFindModel.find('markdown', false, false, max_find_count); assert(notebookFindModel.findMatches, 'Find in notebook failed.'); - assert.equal(notebookFindModel.findMatches.length, 2, 'Find couldnt find all occurrences'); + assert.equal(notebookFindModel.findMatches.length, 2, 'Find could not find all occurrences'); + assert.equal(notebookFindModel.findArray.length, 2, 'Find could not find all occurrences'); + assert.equal(notebookFindModel.getFindCount(), 2, 'Find count do not match find results'); }); test('Should not find results in the notebook', async function (): Promise { @@ -144,6 +160,19 @@ suite('Notebook Find Model', function (): void { assert.deepEqual(notebookFindModel.findMatches[1].range, expectedFindRange2, 'Find in markdown range is wrong :\n' + JSON.stringify(expectedFindRange2) + '\n ' + JSON.stringify(notebookFindModel.findMatches[1].range)); }); + test('Should set selection when find matches results', async function (): Promise { + // Need to set rendered text content for 2nd cell + setRenderedTextContent(1); + + //initialize find + let notebookFindModel = new NotebookFindModel(model); + await notebookFindModel.find('markdown', false, false, max_find_count); + + notebookFindModel.setSelection(notebookFindModel.findMatches[0].range); + let expectedFindRange1 = new NotebookRange(model.cells[0], 2, 13, 2, 21); + assert.deepEqual(notebookFindModel.currentMatch, expectedFindRange1, 'Find failed to set selection on finding results'); + }); + test('Should ignore hyperlink markdown data and find correctly', async function (): Promise { let markdownContent: nb.INotebookContents = { cells: [{ @@ -245,7 +274,7 @@ suite('Notebook Find Model', function (): void { await notebookFindModel.find('insert', false, false, max_find_count); assert(notebookFindModel.findMatches, 'Find in notebook failed.'); - assert.equal(notebookFindModel.findMatches.length, 3, 'Find couldnt find all occurances'); + assert.equal(notebookFindModel.findMatches.length, 3, 'Find couldnt find all occurrences'); await notebookFindModel.find('insert', true, false, max_find_count); assert.equal(notebookFindModel.findMatches.length, 2, 'Find failed to apply match case while searching'); @@ -359,6 +388,38 @@ suite('Notebook Find Model', function (): void { assert.equal(notebookFindModel.findMatches.length, 2, 'Find failed on markdown edit'); }); + test('Find next/previous should return the correct find index', async function (): Promise { + // Need to set rendered text content for 2nd cell + setRenderedTextContent(1); + + //initialize find + let notebookFindModel = new NotebookFindModel(model); + await notebookFindModel.find('insert', false, false, max_find_count); + + assert.equal(notebookFindModel.getFindIndex(), 1, 'Failed to get the correct find index'); + + notebookFindModel.findNext(); + assert.equal(notebookFindModel.getFindIndex(), 2, 'Failed to get the correct find index'); + + notebookFindModel.findPrevious(); + assert.equal(notebookFindModel.getFindIndex(), 1, 'Failed to get the correct find index'); + }); + + test('Should clear results on clear', async function (): Promise { + // Need to set rendered text content for 2nd cell + setRenderedTextContent(1); + + //initialize find + let notebookFindModel = new NotebookFindModel(model); + await notebookFindModel.find('insert', false, false, max_find_count); + + assert.equal(notebookFindModel.findMatches.length, 3, 'Failed to find all occurrences'); + + notebookFindModel.clearFind(); + assert.equal(notebookFindModel.findMatches.length, 0, 'Failed to clear find results'); + assert.equal(notebookFindModel.findDecorations, undefined, 'Failed to clear find decorations on clear'); + }); + async function initNotebookModel(contents: nb.INotebookContents): Promise { let mockContentManager = TypeMoq.Mock.ofType(NotebookEditorContentManager);