123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- // Copyright (c) Jupyter Development Team.
- // Distributed under the terms of the Modified BSD License.
- import expect = require('expect.js');
- import {
- ClientSession
- } from '@jupyterlab/apputils';
- import {
- Kernel
- } from '@jupyterlab/services';
- import {
- Message
- } from '@phosphor/messaging';
- import {
- Widget
- } from '@phosphor/widgets';
- import {
- IOutputAreaModel, OutputAreaModel, OutputAreaWidget
- } from '@jupyterlab/outputarea';
- import {
- createClientSession, defaultRenderMime, DEFAULT_OUTPUTS
- } from '../utils';
- /**
- * The default rendermime instance to use for testing.
- */
- const rendermime = defaultRenderMime();
- class LogOutputAreaWidget extends OutputAreaWidget {
- methods: string[] = [];
- protected onUpdateRequest(msg: Message): void {
- super.onUpdateRequest(msg);
- this.methods.push('onUpdateRequest');
- }
- protected onModelChanged(sender: IOutputAreaModel, args: IOutputAreaModel.ChangedArgs) {
- super.onModelChanged(sender, args);
- this.methods.push('onModelChanged');
- }
- }
- describe('outputarea/widget', () => {
- let widget: LogOutputAreaWidget;
- let model: OutputAreaModel;
- beforeEach(() => {
- model = new OutputAreaModel({ values: DEFAULT_OUTPUTS, trusted: true });
- widget = new LogOutputAreaWidget({ rendermime, model });
- });
- afterEach(() => {
- model.dispose();
- widget.dispose();
- });
- describe('OutputAreaWidget', () => {
- describe('#constructor()', () => {
- it('should create an output area widget', () => {
- expect(widget).to.be.an(OutputAreaWidget);
- expect(widget.hasClass('jp-OutputAreaWidget')).to.be(true);
- });
- it('should take an optional contentFactory', () => {
- let contentFactory = Object.create(OutputAreaWidget.defaultContentFactory);
- let widget = new OutputAreaWidget({ rendermime, contentFactory, model });
- expect(widget.contentFactory).to.be(contentFactory);
- });
- });
- describe('#model', () => {
- it('should be the model used by the widget', () => {
- expect(widget.model).to.be(model);
- });
- });
- describe('#rendermime', () => {
- it('should be the rendermime instance used by the widget', () => {
- expect(widget.rendermime).to.be(rendermime);
- });
- });
- describe('#contentFactory', () => {
- it('should be the contentFactory used by the widget', () => {
- expect(widget.contentFactory).to.be(OutputAreaWidget.defaultContentFactory);
- });
- });
- describe('#widgets', () => {
- it('should get the child widget at the specified index', () => {
- expect(widget.widgets[0]).to.be.a(Widget);
- });
- it('should get the number of child widgets', () => {
- expect(widget.widgets.length).to.be(DEFAULT_OUTPUTS.length - 1);
- widget.model.clear();
- expect(widget.widgets.length).to.be(0);
- });
- });
- describe('#collapsed', () => {
- it('should get the collapsed state of the widget', () => {
- expect(widget.collapsed).to.be(false);
- });
- it('should set the collapsed state of the widget', () => {
- widget.collapsed = true;
- expect(widget.collapsed).to.be(true);
- });
- it('should post an update request', (done) => {
- widget.collapsed = true;
- requestAnimationFrame(() => {
- expect(widget.methods).to.contain('onUpdateRequest');
- done();
- });
- });
- });
- describe('#fixedHeight', () => {
- it('should get the fixed height state of the widget', () => {
- expect(widget.fixedHeight).to.be(false);
- });
- it('should set the fixed height state of the widget', () => {
- widget.fixedHeight = true;
- expect(widget.fixedHeight).to.be(true);
- });
- it('should post an update request', (done) => {
- widget.fixedHeight = true;
- requestAnimationFrame(() => {
- expect(widget.methods).to.contain('onUpdateRequest');
- done();
- });
- });
- });
- describe('#dispose()', () => {
- it('should dispose of the resources held by the widget', () => {
- widget.dispose();
- expect(widget.isDisposed).to.be(true);
- widget.dispose();
- expect(widget.isDisposed).to.be(true);
- });
- });
- describe('#execute()', () => {
- let session: ClientSession;
- beforeEach(() => {
- return createClientSession().then(s => {
- session = s;
- return session.initialize();
- }).then(() => {
- return session.kernel.ready;
- });
- });
- afterEach(() => {
- return session.shutdown().then(() => {
- session.dispose();
- });
- });
- it('should execute code on a kernel and send outputs to the model', () => {
- return widget.execute('print("hello")', session).then(reply => {
- expect(reply.content.execution_count).to.be.ok();
- expect(reply.content.status).to.be('ok');
- });
- });
- it('should clear existing outputs', () => {
- widget.model.fromJSON(DEFAULT_OUTPUTS);
- return widget.execute('print("hello")', session).then(reply => {
- expect(reply.content.execution_count).to.be.ok();
- expect(model.length).to.be.lessThan(2);
- });
- });
- });
- describe('#onUpdateRequest()', () => {
- it('should set the appropriate classes on the widget', (done) => {
- widget.collapsed = true;
- widget.fixedHeight = true;
- requestAnimationFrame(() => {
- expect(widget.methods).to.contain('onUpdateRequest');
- expect(widget.hasClass('jp-mod-fixedHeight')).to.be(true);
- expect(widget.hasClass('jp-mod-collapsed')).to.be(true);
- done();
- });
- });
- });
- describe('#onModelChanged()', () => {
- it('should handle an added output', () => {
- widget.model.clear();
- widget.methods = [];
- widget.model.add(DEFAULT_OUTPUTS[0]);
- expect(widget.methods).to.contain('onModelChanged');
- expect(widget.widgets.length).to.be(1);
- });
- it('should handle a clear', () => {
- widget.model.fromJSON(DEFAULT_OUTPUTS);
- widget.methods = [];
- widget.model.clear();
- expect(widget.methods).to.contain('onModelChanged');
- expect(widget.widgets.length).to.be(0);
- });
- it('should handle a set', () => {
- widget.model.clear();
- widget.model.add(DEFAULT_OUTPUTS[0]);
- widget.methods = [];
- widget.model.add(DEFAULT_OUTPUTS[0]);
- expect(widget.methods).to.contain('onModelChanged');
- expect(widget.widgets.length).to.be(1);
- });
- });
- describe('.contentFactory', () => {
- describe('#createGutter()', () => {
- it('should create a gutter widget', () => {
- let factory = new OutputAreaWidget.ContentFactory();
- expect(factory.createGutter().executionCount).to.be(null);
- });
- });
- describe('#createStdin()', () => {
- it('should create a stdin widget', () => {
- return Kernel.startNew().then(kernel => {
- let factory = new OutputAreaWidget.ContentFactory();
- let options = {
- prompt: 'hello',
- password: false,
- kernel
- };
- expect(factory.createStdin(options)).to.be.a(Widget);
- kernel.dispose();
- });
- });
- });
- });
- describe('.defaultContentFactory', () => {
- it('should be a `contentFactory` instance', () => {
- expect(OutputAreaWidget.defaultContentFactory).to.be.an(OutputAreaWidget.ContentFactory);
- });
- });
- });
- });
|