Bladeren bron

Add baseline tests for toc

Martha Cryan 4 jaren geleden
bovenliggende
commit
515293f93d

+ 1 - 0
packages/toc/babel.config.js

@@ -0,0 +1 @@
+module.exports = require('@jupyterlab/testutils/lib/babel.config');

+ 2 - 0
packages/toc/jest.config.js

@@ -0,0 +1,2 @@
+const func = require('@jupyterlab/testutils/lib/jest-config');
+module.exports = func(__dirname);

+ 15 - 5
packages/toc/package.json

@@ -27,12 +27,16 @@
   "types": "lib/index.d.ts",
   "style": "style/index.css",
   "scripts": {
-    "build": "tsc",
-    "clean": "rimraf lib",
-    "precommit": "lint-staged",
+    "build": "tsc -b",
+    "build:test": "tsc --build tsconfig.test.json",
+    "clean": "rimraf lib && rimraf tsconfig.tsbuildinfo",
+    "docs": "typedoc src",
     "prepublishOnly": "npm run build",
-    "prettier": "prettier --write '**/*{.ts,.tsx,.js,.jsx,.css,.json,.md}'",
-    "watch": "tsc -w"
+    "test": "jest",
+    "test:cov": "jest --collect-coverage",
+    "test:debug": "node --inspect-brk node_modules/.bin/jest --runInBand",
+    "test:debug:watch": "node --inspect-brk node_modules/.bin/jest --runInBand --watch",
+    "watch": "tsc -b --watch"
   },
   "lint-staged": {
     "**/*{.ts,.tsx,.css,.json,.md}": [
@@ -59,11 +63,17 @@
     "react-dom": "~16.9.0"
   },
   "devDependencies": {
+    "@babel/core": "^7.5.0",
+    "@babel/preset-env": "^7.7.6",
+    "@types/jest": "^24.0.23",
     "@types/react": "~16.9.16",
     "@types/react-dom": "~16.9.4",
+    "@jupyterlab/testutils": "^3.0.0-alpha.4",
+    "jest": "^25.2.3",
     "lint-staged": "^8.2.1",
     "prettier": "^1.19.1",
     "rimraf": "~3.0.0",
+    "ts-jest": "^25.2.1",
     "tslint": "^5.20.1",
     "tslint-config-prettier": "^1.18.0",
     "tslint-plugin-prettier": "^2.1.0",

+ 117 - 0
packages/toc/test/toc.spec.ts

@@ -0,0 +1,117 @@
+// Copyright (c) Jupyter Development Team.
+// Distributed under the terms of the Modified BSD License.
+
+import 'jest';
+
+import {
+  NotebookPanel,
+  NotebookTracker,
+  NotebookWidgetFactory,
+  NotebookModelFactory
+} from '@jupyterlab/notebook';
+import { DocumentManager } from '@jupyterlab/docmanager';
+import * as ToC from '@jupyterlab/toc';
+import { RenderMimeRegistry } from '@jupyterlab/rendermime';
+import { ServiceManager } from '@jupyterlab/services';
+import { DocumentRegistry, TextModelFactory } from '@jupyterlab/docregistry';
+import { UUID } from '@lumino/coreutils';
+
+import { NBTestUtils, Mock, defaultRenderMime } from '@jupyterlab/testutils';
+
+let manager: DocumentManager;
+let widget: ToC.TableOfContents;
+let registry: DocumentRegistry;
+let services: ServiceManager.IManager;
+let factory: TextModelFactory;
+
+beforeAll(async () => {
+  jest.setTimeout(20000);
+  const opener: DocumentManager.IWidgetOpener = {
+    open: widget => {
+      // no-op
+    }
+  };
+  factory = new TextModelFactory();
+  registry = new DocumentRegistry({
+    textModelFactory: factory
+  });
+  const contentFactory = NBTestUtils.createNotebookPanelFactory();
+  const notebookFactory = new NotebookModelFactory({});
+  registry.addModelFactory(notebookFactory);
+  registry.addWidgetFactory(
+    new NotebookWidgetFactory({
+      modelName: 'notebook',
+      contentFactory,
+      fileTypes: ['notebook'],
+      rendermime: defaultRenderMime(),
+      mimeTypeService: NBTestUtils.mimeTypeService,
+      name: 'notebook'
+    })
+  );
+  services = new Mock.ServiceManagerMock();
+  manager = new DocumentManager({
+    registry,
+    opener,
+    manager: services
+  });
+});
+
+describe('@jupyterlab/toc', () => {
+  describe('TableOfContents', () => {
+    describe('#constructor', () => {
+      it('should construct a new ToC widget', () => {
+        widget = new ToC.TableOfContents({
+          docmanager: manager,
+          rendermime: new RenderMimeRegistry()
+        });
+        expect(widget).toBeInstanceOf(ToC.TableOfContents);
+      });
+    });
+  });
+
+  describe('TableOfContentsRegistry', () => {
+    let registry: ToC.TableOfContentsRegistry;
+
+    beforeAll(() => {
+      registry = new ToC.TableOfContentsRegistry();
+    });
+
+    describe('IGenerator<NotebookPanel>', () => {
+      let notebookTracker: NotebookTracker;
+      let notebookGenerator: ToC.TableOfContentsRegistry.IGenerator<NotebookPanel>;
+      let notebookWidget: NotebookPanel;
+
+      it('should create a notebook generator', () => {
+        notebookTracker = new NotebookTracker({
+          namespace: 'notebook'
+        });
+        notebookGenerator = ToC.createNotebookGenerator(
+          notebookTracker,
+          widget,
+          NBTestUtils.defaultRenderMime().sanitizer
+        );
+      });
+
+      it('should add a notebook generator to the registry', () => {
+        registry.add(notebookGenerator);
+      });
+
+      it('should find the notebook generator', async () => {
+        const path = UUID.uuid4() + '.ipynb';
+        const newNotebookWidget = manager.createNew(path, 'notebook');
+        expect(newNotebookWidget).toBeInstanceOf(NotebookPanel);
+        notebookWidget = newNotebookWidget as NotebookPanel;
+        await notebookTracker.add(notebookWidget);
+        const foundNotebookGenerator = registry.find(notebookWidget);
+        expect(foundNotebookGenerator).toBeDefined();
+      });
+
+      it('should change current', async () => {
+        widget.current = {
+          widget: notebookWidget,
+          generator: notebookGenerator
+        };
+      });
+    });
+  });
+});

+ 72 - 0
packages/toc/tsconfig.test.json

@@ -0,0 +1,72 @@
+{
+  "extends": "../../tsconfigbase.test",
+  "include": ["src/*", "test/*"],
+  "references": [
+    {
+      "path": "../apputils"
+    },
+    {
+      "path": "../cells"
+    },
+    {
+      "path": "../coreutils"
+    },
+    {
+      "path": "../docmanager"
+    },
+    {
+      "path": "../docregistry"
+    },
+    {
+      "path": "../fileeditor"
+    },
+    {
+      "path": "../markdownviewer"
+    },
+    {
+      "path": "../notebook"
+    },
+    {
+      "path": "../rendermime"
+    },
+    {
+      "path": "../ui-components"
+    },
+    {
+      "path": "."
+    },
+    {
+      "path": "../../testutils"
+    },
+    {
+      "path": "../apputils"
+    },
+    {
+      "path": "../cells"
+    },
+    {
+      "path": "../coreutils"
+    },
+    {
+      "path": "../docmanager"
+    },
+    {
+      "path": "../docregistry"
+    },
+    {
+      "path": "../fileeditor"
+    },
+    {
+      "path": "../markdownviewer"
+    },
+    {
+      "path": "../notebook"
+    },
+    {
+      "path": "../rendermime"
+    },
+    {
+      "path": "../ui-components"
+    }
+  ]
+}