mimedocument.spec.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. Context,
  5. DocumentRegistry,
  6. MimeContent,
  7. MimeDocument,
  8. MimeDocumentFactory
  9. } from '@jupyterlab/docregistry';
  10. import { IRenderMime, RenderedText } from '@jupyterlab/rendermime';
  11. import { defaultRenderMime, testEmission } from '@jupyterlab/testutils';
  12. import * as Mock from '@jupyterlab/testutils/lib/mock';
  13. import { Message } from '@lumino/messaging';
  14. import { BoxLayout } from '@lumino/widgets';
  15. const RENDERMIME = defaultRenderMime();
  16. class LogRenderer extends MimeContent {
  17. methods: string[] = [];
  18. protected onAfterAttach(msg: Message): void {
  19. super.onAfterAttach(msg);
  20. this.methods.push('onAfterAttach');
  21. }
  22. protected onUpdateRequest(msg: Message): void {
  23. super.onUpdateRequest(msg);
  24. this.methods.push('onUpdateRequest');
  25. }
  26. }
  27. class FooText extends RenderedText {
  28. async render(model: IRenderMime.IMimeModel): Promise<void> {
  29. await super.render(model);
  30. model.setData({ data: { 'text/foo': 'bar' } });
  31. }
  32. }
  33. const fooFactory: IRenderMime.IRendererFactory = {
  34. mimeTypes: ['text/foo'],
  35. safe: true,
  36. createRenderer: options => new FooText(options)
  37. };
  38. describe('docregistry/mimedocument', () => {
  39. let dContext: Context<DocumentRegistry.IModel>;
  40. beforeEach(async () => {
  41. dContext = await Mock.createFileContext();
  42. });
  43. afterEach(() => {
  44. dContext.dispose();
  45. });
  46. describe('MimeDocumentFactory', () => {
  47. describe('#createNew()', () => {
  48. it('should require a context parameter', () => {
  49. const widgetFactory = new MimeDocumentFactory({
  50. name: 'markdown',
  51. fileTypes: ['markdown'],
  52. rendermime: RENDERMIME,
  53. primaryFileType: DocumentRegistry.getDefaultTextFileType()
  54. });
  55. expect(widgetFactory.createNew(dContext)).toBeInstanceOf(MimeDocument);
  56. });
  57. });
  58. });
  59. describe('MimeContent', () => {
  60. describe('#constructor()', () => {
  61. it('should require options', () => {
  62. const renderer = RENDERMIME.createRenderer('text/markdown');
  63. const widget = new MimeContent({
  64. context: dContext,
  65. renderer,
  66. mimeType: 'text/markdown',
  67. renderTimeout: 1000,
  68. dataType: 'string'
  69. });
  70. expect(widget).toBeInstanceOf(MimeContent);
  71. });
  72. });
  73. describe('#ready', () => {
  74. it('should resolve when the widget is ready', async () => {
  75. const renderer = RENDERMIME.createRenderer('text/markdown');
  76. const widget = new LogRenderer({
  77. context: dContext,
  78. renderer,
  79. mimeType: 'text/markdown',
  80. renderTimeout: 1000,
  81. dataType: 'string'
  82. });
  83. await widget.ready;
  84. const layout = widget.layout as BoxLayout;
  85. expect(layout.widgets.length).toBe(1);
  86. });
  87. });
  88. describe('contents changed', () => {
  89. it('should change the document contents', async () => {
  90. RENDERMIME.addFactory(fooFactory);
  91. const emission = testEmission(dContext.model.contentChanged, {
  92. test: () => {
  93. expect(dContext.model.toString()).toBe('bar');
  94. }
  95. });
  96. const renderer = RENDERMIME.createRenderer('text/foo');
  97. const widget = new LogRenderer({
  98. context: dContext,
  99. renderer,
  100. mimeType: 'text/foo',
  101. renderTimeout: 1000,
  102. dataType: 'string'
  103. });
  104. await widget.ready;
  105. await emission;
  106. });
  107. });
  108. });
  109. });