panel.spec.ts 3.8 KB

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