outputmodel.spec.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import 'jest';
  4. import * as nbformat from '@jupyterlab/nbformat';
  5. import { OutputModel } from '../src';
  6. import { NBTestUtils } from '@jupyterlab/testutils';
  7. const DEFAULT_EXECUTE: nbformat.IOutput = {
  8. output_type: 'execute_result',
  9. execution_count: 1,
  10. data: { 'text/plain': 'foo' },
  11. metadata: {
  12. foo: 1,
  13. bar: 'baz'
  14. }
  15. };
  16. const DEFAULT_STREAM: nbformat.IOutput = {
  17. name: 'stderr',
  18. output_type: 'stream',
  19. text: ['output to stderr\n']
  20. };
  21. describe('rendermime/outputmodel', () => {
  22. describe('OutputModel', () => {
  23. describe('#constructor()', () => {
  24. it('should create a new output model', () => {
  25. const value = DEFAULT_EXECUTE;
  26. let model = new OutputModel({ value });
  27. expect(model).toBeInstanceOf(OutputModel);
  28. model = new OutputModel({ value, trusted: true });
  29. expect(model).toBeInstanceOf(OutputModel);
  30. });
  31. });
  32. describe('#type', () => {
  33. it('should be the output type', () => {
  34. let model = new OutputModel({ value: DEFAULT_EXECUTE });
  35. expect(model.type).toBe(DEFAULT_EXECUTE.output_type);
  36. model = new OutputModel({ value: DEFAULT_STREAM });
  37. expect(model.type).toBe(DEFAULT_STREAM.output_type);
  38. });
  39. });
  40. describe('#executionCount', () => {
  41. it('should be the execution count of an execution result', () => {
  42. const model = new OutputModel({ value: DEFAULT_EXECUTE });
  43. expect(model.executionCount).toBe(1);
  44. });
  45. it('should be null for non-execution results', () => {
  46. const model = new OutputModel({ value: DEFAULT_STREAM });
  47. expect(model.executionCount).toBeNull();
  48. });
  49. });
  50. describe('#toJSON()', () => {
  51. it('should yield the raw value', () => {
  52. const model = new OutputModel({ value: DEFAULT_EXECUTE });
  53. expect(model.toJSON()).toEqual(DEFAULT_EXECUTE);
  54. });
  55. });
  56. describe('.getData()', () => {
  57. it('should handle all bundle types', () => {
  58. for (let i = 0; i < NBTestUtils.DEFAULT_OUTPUTS.length; i++) {
  59. const output = NBTestUtils.DEFAULT_OUTPUTS[i];
  60. const bundle = OutputModel.getData(output);
  61. expect(Object.keys(bundle).length).not.toBe(0);
  62. }
  63. });
  64. });
  65. describe('.getMetadata()', () => {
  66. it('should get the metadata from the bundle', () => {
  67. const metadata = OutputModel.getMetadata(DEFAULT_EXECUTE);
  68. expect(metadata['foo']).toBe(1);
  69. expect(metadata['bar']).toBe('baz');
  70. });
  71. it('should handle output with no metadata field', () => {
  72. const metadata = OutputModel.getMetadata(DEFAULT_STREAM);
  73. expect(Object.keys(metadata).length).toBe(0);
  74. });
  75. });
  76. });
  77. });