8
0

model.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../src/model/model';
  6. import Batch from '../../src/model/batch';
  7. describe( 'Model', () => {
  8. let model;
  9. let changes = '';
  10. beforeEach( () => {
  11. model = new Model();
  12. changes = '';
  13. } );
  14. describe( 'change & enqueueChange', () => {
  15. it( 'should execute changes immediately', () => {
  16. model.change( () => {
  17. changes += 'A';
  18. } );
  19. expect( changes ).to.equal( 'A' );
  20. } );
  21. it( 'should pass returned value', () => {
  22. const ret = model.change( () => {
  23. changes += 'A';
  24. return 'B';
  25. } );
  26. changes += ret;
  27. expect( changes ).to.equal( 'AB' );
  28. } );
  29. it( 'should not mixed the order when nested change is called', () => {
  30. const ret = model.change( () => {
  31. changes += 'A';
  32. nested();
  33. return 'D';
  34. } );
  35. changes += ret;
  36. expect( changes ).to.equal( 'ABCD' );
  37. function nested() {
  38. const ret = model.change( () => {
  39. changes += 'B';
  40. return 'C';
  41. } );
  42. changes += ret;
  43. }
  44. } );
  45. it( 'should execute enqueueChanges immediately if its the first block', () => {
  46. model.enqueueChange( new Batch(), () => {
  47. changes += 'A';
  48. nested();
  49. } );
  50. expect( changes ).to.equal( 'ABC' );
  51. function nested() {
  52. const ret = model.change( () => {
  53. changes += 'B';
  54. return 'C';
  55. } );
  56. changes += ret;
  57. }
  58. } );
  59. it( 'should be possible to enqueueChanges immediately if its the first block', () => {
  60. model.enqueueChange( new Batch(), () => {
  61. changes += 'A';
  62. nested();
  63. } );
  64. expect( changes ).to.equal( 'AB' );
  65. function nested() {
  66. const ret = model.change( () => {
  67. changes += 'B';
  68. } );
  69. }
  70. } );
  71. it( 'should be possible to nest change in enqueueChanges', () => {
  72. model.enqueueChange( new Batch(), () => {
  73. changes += 'A';
  74. nested();
  75. changes += 'D';
  76. } );
  77. expect( changes ).to.equal( 'ABCD' );
  78. function nested() {
  79. const ret = model.change( () => {
  80. changes += 'B';
  81. return 'C';
  82. } );
  83. changes += ret;
  84. }
  85. } );
  86. it( 'should be possible to nest enqueueChanges in enqueueChanges', () => {
  87. model.enqueueChange( new Batch(), () => {
  88. changes += 'A';
  89. nestedEnqueue();
  90. changes += 'B';
  91. } );
  92. expect( changes ).to.equal( 'ABC' );
  93. function nestedEnqueue() {
  94. model.enqueueChange( new Batch(), () => {
  95. changes += 'C';
  96. } );
  97. }
  98. } );
  99. it( 'should be possible to nest enqueueChanges in changes', () => {
  100. const ret = model.change( () => {
  101. changes += 'A';
  102. nestedEnqueue();
  103. changes += 'B';
  104. return 'D';
  105. } );
  106. changes += ret;
  107. expect( changes ).to.equal( 'ABCD' );
  108. function nestedEnqueue() {
  109. model.enqueueChange( new Batch(), () => {
  110. changes += 'C';
  111. } );
  112. }
  113. } );
  114. } );
  115. } );