vdom.spec.ts 3.3 KB

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