panel.spec.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import 'jest';
  4. import { Message, MessageLoop } from '@lumino/messaging';
  5. import { Widget } from '@lumino/widgets';
  6. import { CodeConsole, ConsolePanel } from '../src';
  7. import { dismissDialog } from '@jupyterlab/testutils';
  8. import * as Mock from '@jupyterlab/testutils/lib/mock';
  9. import {
  10. createConsolePanelFactory,
  11. rendermime,
  12. mimeTypeService,
  13. editorFactory
  14. } from './utils';
  15. class TestPanel extends ConsolePanel {
  16. methods: string[] = [];
  17. protected onActivateRequest(msg: Message): void {
  18. super.onActivateRequest(msg);
  19. this.methods.push('onActivateRequest');
  20. }
  21. protected onCloseRequest(msg: Message): void {
  22. super.onCloseRequest(msg);
  23. this.methods.push('onCloseRequest');
  24. }
  25. }
  26. const contentFactory = createConsolePanelFactory();
  27. describe('console/panel', () => {
  28. let panel: TestPanel;
  29. const manager = new Mock.ServiceManagerMock();
  30. beforeAll(async () => {
  31. return await manager.ready;
  32. });
  33. beforeEach(() => {
  34. panel = new TestPanel({
  35. manager,
  36. contentFactory,
  37. rendermime,
  38. mimeTypeService,
  39. sessionContext: Mock.createSimpleSessionContext()
  40. });
  41. });
  42. afterEach(() => {
  43. panel.dispose();
  44. });
  45. describe('ConsolePanel', () => {
  46. describe('#constructor()', () => {
  47. it('should create a new console panel', () => {
  48. expect(panel).toBeInstanceOf(ConsolePanel);
  49. expect(Array.from(panel.node.classList)).toEqual(
  50. expect.arrayContaining(['jp-ConsolePanel'])
  51. );
  52. });
  53. });
  54. describe('#console', () => {
  55. it('should be a code console widget created at instantiation', () => {
  56. expect(panel.console).toBeInstanceOf(CodeConsole);
  57. });
  58. });
  59. describe('#session', () => {
  60. it('should be a client session object', () => {
  61. expect(panel.sessionContext.kernelChanged).toBeTruthy();
  62. });
  63. });
  64. describe('#dispose()', () => {
  65. it('should dispose of the resources held by the panel', () => {
  66. panel.dispose();
  67. expect(panel.isDisposed).toBe(true);
  68. panel.dispose();
  69. expect(panel.isDisposed).toBe(true);
  70. });
  71. });
  72. describe('#onAfterAttach()', () => {
  73. it('should start the session', async () => {
  74. Widget.attach(panel, document.body);
  75. await panel.sessionContext.ready;
  76. await panel.sessionContext.session!.kernel!.info;
  77. });
  78. });
  79. describe('#onActivateRequest()', () => {
  80. it('should give the focus to the console prompt', () => {
  81. Widget.attach(panel, document.body);
  82. MessageLoop.sendMessage(panel, Widget.Msg.ActivateRequest);
  83. expect(panel.console.promptCell!.editor.hasFocus()).toBe(true);
  84. return dismissDialog();
  85. });
  86. });
  87. describe('#onCloseRequest()', () => {
  88. it('should dispose of the panel resources after closing', () => {
  89. Widget.attach(panel, document.body);
  90. expect(panel.isDisposed).toBe(false);
  91. MessageLoop.sendMessage(panel, Widget.Msg.CloseRequest);
  92. expect(panel.isDisposed).toBe(true);
  93. });
  94. });
  95. describe('.ContentFactory', () => {
  96. describe('#constructor', () => {
  97. it('should create a new code console factory', () => {
  98. const factory = new ConsolePanel.ContentFactory({ editorFactory });
  99. expect(factory).toBeInstanceOf(ConsolePanel.ContentFactory);
  100. });
  101. });
  102. describe('#createConsole()', () => {
  103. it('should create a console widget', () => {
  104. const options = {
  105. contentFactory: contentFactory,
  106. rendermime,
  107. mimeTypeService,
  108. sessionContext: panel.sessionContext
  109. };
  110. expect(contentFactory.createConsole(options)).toBeInstanceOf(
  111. CodeConsole
  112. );
  113. });
  114. });
  115. });
  116. });
  117. });