Add tests for Open Notebook Folder functionality. (#10056)

This commit is contained in:
Cory Rivera
2020-04-17 16:27:00 -07:00
committed by GitHub
parent 6200075624
commit 67b6f6ffcd
3 changed files with 110 additions and 85 deletions

View File

@@ -12,12 +12,13 @@ import * as rimraf from 'rimraf';
import * as os from 'os';
import * as uuid from 'uuid';
import { BookTreeViewProvider } from '../../book/bookTreeView';
import { BookTreeItem } from '../../book/bookTreeItem';
import { BookTreeItem, BookTreeItemType } from '../../book/bookTreeItem';
import { promisify } from 'util';
import { MockExtensionContext } from '../common/stubs';
import { exists } from '../../common/utils';
import { AppContext } from '../../common/appContext';
import { ApiWrapper } from '../../common/apiWrapper';
import { BookModel } from '../../book/bookModel';
import { BookTrustManager } from '../../book/bookTrustManager';
export interface IExpectedBookItem {
@@ -43,7 +44,6 @@ export function equalBookItems(book: BookTreeItem, expectedBook: IExpectedBookIt
}
describe('BookTreeViewProviderTests', function () {
describe('BookTreeViewProvider', () => {
let mockExtensionContext: vscode.ExtensionContext;
@@ -449,4 +449,110 @@ describe('BookTreeViewProviderTests', function () {
}
});
});
describe('BookTreeViewProvider.openNotebookFolder', function (): void {
let rootFolderPath: string;
let bookFolderPath: string;
let bookTitle: string;
let notebookFolderPath: string;
let tableOfContentsFile: string;
let standaloneNotebookTitle: string;
let standaloneNotebookFile: string;
let bookTreeViewProvider: BookTreeViewProvider;
let appContext: AppContext;
this.beforeAll(async () => {
rootFolderPath = path.join(os.tmpdir(), `BookFolderTest_${uuid.v4()}`);
bookFolderPath = path.join(rootFolderPath, 'BookTestData');
let dataFolderPath = path.join(bookFolderPath, '_data');
let contentFolderPath = path.join(bookFolderPath, 'content');
let configFile = path.join(bookFolderPath, '_config.yml');
tableOfContentsFile = path.join(dataFolderPath, 'toc.yml');
let bookNotebookFile = path.join(contentFolderPath, 'notebook1.ipynb');
notebookFolderPath = path.join(rootFolderPath, 'NotebookTestData');
standaloneNotebookTitle = 'notebook2';
standaloneNotebookFile = path.join(notebookFolderPath, `${standaloneNotebookTitle}.ipynb`);
await fs.mkdir(rootFolderPath);
await fs.mkdir(bookFolderPath);
await fs.mkdir(dataFolderPath);
await fs.mkdir(contentFolderPath);
await fs.mkdir(notebookFolderPath);
bookTitle = 'Test Book';
await fs.writeFile(configFile, `title: ${bookTitle}`);
await fs.writeFile(tableOfContentsFile, '- title: Notebook1\n url: /notebook1');
await fs.writeFile(bookNotebookFile, '');
await fs.writeFile(standaloneNotebookFile, '');
const mockExtensionContext = new MockExtensionContext();
appContext = new AppContext(mockExtensionContext, new ApiWrapper());
bookTreeViewProvider = new BookTreeViewProvider(appContext.apiWrapper, [], mockExtensionContext, false, 'bookTreeView');
let errorCase = new Promise((resolve, reject) => setTimeout(() => resolve(), 5000));
await Promise.race([bookTreeViewProvider.initialized, errorCase.then(() => { throw new Error('BookTreeViewProvider did not initialize in time'); })]);
appContext = new AppContext(undefined, new ApiWrapper());
});
it('should include books and notebooks when opening parent folder', async () => {
await bookTreeViewProvider.loadNotebooksInFolder(rootFolderPath);
should(bookTreeViewProvider.books.length).equal(2, 'Should have loaded a book and a notebook');
validateIsBook(bookTreeViewProvider.books[0]);
validateIsNotebook(bookTreeViewProvider.books[1]);
});
it('should include only books when opening books folder', async () => {
await bookTreeViewProvider.loadNotebooksInFolder(bookFolderPath);
should(bookTreeViewProvider.books.length).equal(1, 'Should have loaded only one book');
validateIsBook(bookTreeViewProvider.books[0]);
});
it('should include only notebooks when opening notebooks folder', async () => {
await bookTreeViewProvider.loadNotebooksInFolder(notebookFolderPath);
should(bookTreeViewProvider.books.length).equal(1, 'Should have loaded only one notebook');
validateIsNotebook(bookTreeViewProvider.books[0]);
});
this.afterEach(async function (): Promise<void> {
let bookItems = await bookTreeViewProvider.getChildren();
await Promise.all(bookItems.map(bookItem => bookTreeViewProvider.closeBook(bookItem)));
});
this.afterAll(async function (): Promise<void> {
if (await exists(rootFolderPath)) {
await promisify(rimraf)(rootFolderPath);
}
});
let validateIsBook = (book: BookModel) => {
should(book.isNotebook).be.false();
should(book.bookItems.length).equal(1);
let bookItem = book.bookItems[0];
let bookDetails = bookItem.book;
should(bookDetails.type).equal(BookTreeItemType.Book);
should(bookDetails.title).equal(bookTitle);
should(bookDetails.contentPath).equal(tableOfContentsFile.replace(/\\/g, '/'));
should(bookDetails.root).equal(bookFolderPath.replace(/\\/g, '/'));
should(bookDetails.tableOfContents.sections).not.equal(undefined);
should(bookDetails.page).not.equal(undefined);
};
let validateIsNotebook = (book: BookModel) => {
should(book.isNotebook).be.true();
should(book.bookItems.length).equal(1);
let bookItem = book.bookItems[0];
should(book.getAllNotebooks().get(vscode.Uri.file(standaloneNotebookFile).fsPath)).equal(bookItem);
let bookDetails = bookItem.book;
should(bookDetails.type).equal(BookTreeItemType.Notebook);
should(bookDetails.title).equal(standaloneNotebookTitle);
should(bookDetails.contentPath).equal(standaloneNotebookFile.replace(/\\/g, '/'));
should(bookDetails.root).equal(notebookFolderPath.replace(/\\/g, '/'));
should(bookDetails.tableOfContents.sections).equal(undefined);
should(bookDetails.page.sections).equal(undefined);
};
});
});