outputmodel.spec.ts 2.7 KB

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