123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- // Copyright (c) Jupyter Development Team.
- // Distributed under the terms of the Modified BSD License.
- import expect = require('expect.js');
- import * as CodeMirror from 'codemirror';
- import {
- MockKernel
- } from 'jupyter-js-services/lib/mockkernel';
- import {
- Message
- } from 'phosphor-messaging';
- import {
- BaseCellWidget, CellModel, InputAreaWidget
- } from '../../../../lib/notebook/cells';
- import {
- CellEditorWidget
- } from '../../../../lib/notebook/cells/editor';
- const INPUT_CLASS = 'jp-InputArea';
- class LogCell extends BaseCellWidget {
- methods: string[] = [];
- messages: string[] = [];
- processMessage(msg: Message): void {
- super.processMessage(msg);
- this.messages.push(msg.type);
- }
- protected onAfterAttach(msg: Message): void {
- super.onAfterAttach(msg);
- this.methods.push('onAfterAttach');
- }
- }
- describe('jupyter-js-notebook', () => {
- describe('BaseCellWidget', () => {
- describe('.createCellEditor()', () => {
- it('should create a cell editor widget', () => {
- let editor = BaseCellWidget.createCellEditor(new CellModel());
- expect(editor).to.be.a(CellEditorWidget);
- });
- });
- describe('.createInputArea()', () => {
- it('should create an input area widget', () => {
- let editor = BaseCellWidget.createCellEditor(new CellModel());
- let input = BaseCellWidget.createInputArea(editor);
- expect(input).to.be.a(InputAreaWidget);
- });
- });
- describe('#constructor()', () => {
- it('should create a base cell widget', () => {
- let widget = new BaseCellWidget(new CellModel());
- expect(widget).to.be.a(BaseCellWidget);
- });
- });
- describe('#model', () => {
- it('should be read-only', () => {
- let widget = new BaseCellWidget(new CellModel());
- expect(() => { widget.model = null; }).to.throwError();
- });
- });
- describe('#editor', () => {
- it('should be a cell editor widget', () => {
- let widget = new BaseCellWidget(new CellModel());
- expect(widget.editor).to.be.a(CellEditorWidget);
- });
- it('should be read-only', () => {
- let widget = new BaseCellWidget(new CellModel());
- expect(() => { widget.editor = null; }).to.throwError();
- });
- });
- describe('#mimetype', () => {
- it('should be a string', () => {
- let widget = new BaseCellWidget(new CellModel());
- expect(typeof widget.mimetype).to.be('string');
- });
- it('should default to text/plain', () => {
- let widget = new BaseCellWidget(new CellModel());
- expect(widget.mimetype).to.be('text/plain');
- });
- it('should supporting being set to other types', () => {
- let widget = new BaseCellWidget(new CellModel());
- widget.mimetype = 'test/test';
- expect(widget.mimetype).to.be('test/test');
- });
- it('should not allow being set to empty or null strings', () => {
- let widget = new BaseCellWidget(new CellModel());
- widget.mimetype = null;
- expect(widget.mimetype).to.be('text/plain');
- widget.mimetype = '';
- expect(widget.mimetype).to.be('text/plain');
- });
- });
- describe('#readOnly', () => {
- it('should be a boolean', () => {
- let widget = new BaseCellWidget(new CellModel());
- expect(typeof widget.readOnly).to.be('boolean');
- });
- it('should default to false', () => {
- let widget = new BaseCellWidget(new CellModel());
- expect(widget.readOnly).to.be(false);
- });
- it('should be settable', () => {
- let widget = new BaseCellWidget(new CellModel());
- widget.readOnly = true;
- expect(widget.readOnly).to.be(true);
- });
- });
- describe('#trusted', () => {
- it('should be a boolean', () => {
- let widget = new BaseCellWidget(new CellModel());
- expect(typeof widget.trusted).to.be('boolean');
- });
- it('should default to false', () => {
- let widget = new BaseCellWidget(new CellModel());
- expect(widget.trusted).to.be(false);
- });
- it('should be settable', () => {
- let widget = new BaseCellWidget(new CellModel());
- widget.trusted = true;
- expect(widget.trusted).to.be(true);
- });
- });
- describe('#focus()', () => {
- it('should focus the cell editor', () => {
- let widget = new BaseCellWidget(new CellModel());
- widget.attach(document.body);
- expect(widget.editor.editor.hasFocus()).to.be(false);
- widget.focus();
- expect(widget.editor.editor.hasFocus()).to.be(true);
- widget.dispose();
- });
- });
- describe('#setPrompt()', () => {
- it('should not throw an error (full test in input area)', () => {
- let widget = new BaseCellWidget(new CellModel());
- expect(() => { widget.setPrompt(void 0); }).to.not.throwError();
- expect(() => { widget.setPrompt(null); }).to.not.throwError();
- expect(() => { widget.setPrompt(''); }).to.not.throwError();
- expect(() => { widget.setPrompt('null'); }).to.not.throwError();
- expect(() => { widget.setPrompt('test'); }).to.not.throwError();
- });
- });
- describe('#toggleInput()', () => {
- it('should toggle whether the input is shown', () => {
- let widget = new BaseCellWidget(new CellModel());
- let input = widget.node.getElementsByClassName(INPUT_CLASS)[0];
- widget.attach(document.body);
- expect(window.getComputedStyle(input).display).to.not.be('none');
- widget.toggleInput(false);
- expect(window.getComputedStyle(input).display).to.be('none');
- widget.toggleInput(true);
- expect(window.getComputedStyle(input).display).to.not.be('none');
- });
- });
- describe('#dispose()', () => {
- it('should dispose of the resources held by the widget', () => {
- let widget = new BaseCellWidget(new CellModel());
- widget.dispose();
- expect(widget.isDisposed).to.be(true);
- });
- it('should be safe to call multiple times', () => {
- let widget = new BaseCellWidget(new CellModel());
- widget.dispose();
- widget.dispose();
- expect(widget.isDisposed).to.be(true);
- });
- });
- describe('#onAfterAttach()', () => {
- it('should update the widget', () => {
- let widget = new LogCell(new CellModel());
- expect(widget.methods).to.not.contain('onAfterAttach');
- widget.attach(document.body);
- expect(widget.methods).to.contain('onAfterAttach');
- widget.dispose();
- });
- });
- });
- });
|