model.js 2.7 KB

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