vdom.spec.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { VDomModel, VDomRenderer } from '@jupyterlab/apputils';
  4. import { framePromise } from '@jupyterlab/testutils';
  5. import { Widget } from '@lumino/widgets';
  6. import * as React from 'react';
  7. class TestModel extends VDomModel {
  8. get value(): string {
  9. return this._value;
  10. }
  11. set value(newValue: string) {
  12. this._value = newValue;
  13. this.stateChanged.emit(void 0);
  14. }
  15. private _value = '';
  16. }
  17. class TestWidget extends VDomRenderer<TestModel> {
  18. protected render(): React.ReactElement<any> {
  19. return React.createElement('span', null, this.model.value);
  20. }
  21. }
  22. class TestWidgetNoModel extends VDomRenderer {
  23. protected render(): React.ReactElement<any> {
  24. return React.createElement('span', null, 'No model!');
  25. }
  26. }
  27. describe('@jupyterlab/apputils', () => {
  28. describe('VDomModel', () => {
  29. describe('#constructor()', () => {
  30. it('should create a VDomModel', () => {
  31. const model = new VDomModel();
  32. expect(model).toBeInstanceOf(VDomModel);
  33. });
  34. it('should create a TestModel', () => {
  35. const model = new TestModel();
  36. expect(model).toBeInstanceOf(TestModel);
  37. });
  38. it('should be properly disposed', () => {
  39. const model = new TestModel();
  40. model.dispose();
  41. expect(model.isDisposed).toBe(true);
  42. });
  43. });
  44. describe('#stateChanged()', () => {
  45. it('should fire the stateChanged signal on a change', () => {
  46. const model = new TestModel();
  47. let changed = false;
  48. model.stateChanged.connect(() => {
  49. changed = true;
  50. });
  51. model.value = 'newvalue';
  52. expect(changed).toBe(true);
  53. });
  54. });
  55. });
  56. describe('VDomRenderer', () => {
  57. describe('#constructor()', () => {
  58. it('should create a TestWidget', () => {
  59. const widget = new TestWidget(new TestModel());
  60. expect(widget).toBeInstanceOf(TestWidget);
  61. });
  62. it('should be properly disposed', () => {
  63. const widget = new TestWidget(new TestModel());
  64. widget.dispose();
  65. expect(widget.isDisposed).toBe(true);
  66. });
  67. });
  68. describe('#modelChanged()', () => {
  69. it('should fire the stateChanged signal on a change', () => {
  70. const model = new TestModel();
  71. const widget = new TestWidget(new TestModel());
  72. let changed = false;
  73. widget.modelChanged.connect(() => {
  74. changed = true;
  75. });
  76. widget.model = model;
  77. expect(changed).toBe(true);
  78. });
  79. });
  80. describe('#render()', () => {
  81. it('should render the contents after a model change', async () => {
  82. const widget = new TestWidget(new TestModel());
  83. const model = new TestModel();
  84. widget.model = model;
  85. model.value = 'foo';
  86. await framePromise();
  87. const span = widget.node.firstChild as HTMLElement;
  88. expect(span.textContent).toBe('foo');
  89. });
  90. });
  91. describe('#noModel()', () => {
  92. it('should work with a null model', async () => {
  93. const widget = new TestWidgetNoModel();
  94. Widget.attach(widget, document.body);
  95. await framePromise();
  96. const span = widget.node.firstChild as HTMLElement;
  97. expect(span.textContent).toBe('No model!');
  98. });
  99. });
  100. });
  101. });