range.spec.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import SelectionRange from 'handsontable/selection/range';
  2. import { CellCoords, CellRange } from 'walkontable';
  3. describe('SelectionRange', () => {
  4. let selectionRange;
  5. beforeEach(() => {
  6. selectionRange = new SelectionRange();
  7. });
  8. afterEach(() => {
  9. selectionRange = null;
  10. });
  11. describe('.constructor', () => {
  12. it('should initiate `ranges` as an ampty array', () => {
  13. expect(selectionRange.ranges.length).toBe(0);
  14. });
  15. });
  16. describe('.isEmpty', () => {
  17. it('should return `true` if the size of the ranges is equal to zero', () => {
  18. selectionRange.ranges.length = 0;
  19. expect(selectionRange.isEmpty()).toBe(true);
  20. });
  21. it('should return `false` if the size of the ranges is not equal to zero', () => {
  22. selectionRange.ranges.push('x');
  23. expect(selectionRange.isEmpty()).toBe(false);
  24. });
  25. });
  26. describe('.set', () => {
  27. it('should reset an array of cell ranges and append new CellRange to this', () => {
  28. selectionRange.ranges.push(
  29. new CellRange(new CellCoords(4, 4))
  30. );
  31. selectionRange.set(new CellCoords(0, 0));
  32. selectionRange.set(new CellCoords(1, 2));
  33. expect(selectionRange.ranges.length).toBe(1);
  34. expect(selectionRange.ranges[0].toObject()).toEqual({
  35. from: { col: 2, row: 1 },
  36. to: { col: 2, row: 1 },
  37. });
  38. });
  39. });
  40. describe('.add', () => {
  41. it('should append new CellRange to the ranges array', () => {
  42. selectionRange.add(new CellCoords(0, 0));
  43. selectionRange.add(new CellCoords(0, 0));
  44. selectionRange.add(new CellCoords(1, 2));
  45. expect(selectionRange.ranges.length).toBe(3);
  46. expect(selectionRange.ranges[0].toObject()).toEqual({
  47. from: { col: 0, row: 0 },
  48. to: { col: 0, row: 0 },
  49. });
  50. expect(selectionRange.ranges[1].toObject()).toEqual({
  51. from: { col: 0, row: 0 },
  52. to: { col: 0, row: 0 },
  53. });
  54. expect(selectionRange.ranges[2].toObject()).toEqual({
  55. from: { col: 2, row: 1 },
  56. to: { col: 2, row: 1 },
  57. });
  58. });
  59. });
  60. describe('.current', () => {
  61. it('should return `undefined` when an array of ranges is empty', () => {
  62. expect(selectionRange.current()).not.toBeDefined();
  63. });
  64. it('should return recently added cell range', () => {
  65. selectionRange.ranges.push(
  66. new CellRange(new CellCoords(4, 4)),
  67. new CellRange(new CellCoords(0, 0)),
  68. new CellRange(new CellCoords(1, 2))
  69. );
  70. expect(selectionRange.current().toObject()).toEqual({
  71. from: { col: 2, row: 1 },
  72. to: { col: 2, row: 1 },
  73. });
  74. });
  75. });
  76. describe('.previous', () => {
  77. it('should return `undefined` when an array of ranges is empty', () => {
  78. expect(selectionRange.previous()).not.toBeDefined();
  79. });
  80. it('should return previously added cell range', () => {
  81. selectionRange.ranges.push(
  82. new CellRange(new CellCoords(4, 4)),
  83. new CellRange(new CellCoords(0, 0)),
  84. new CellRange(new CellCoords(1, 2))
  85. );
  86. expect(selectionRange.previous().toObject()).toEqual({
  87. from: { col: 0, row: 0 },
  88. to: { col: 0, row: 0 },
  89. });
  90. });
  91. });
  92. describe('.includes', () => {
  93. it('should return `true` if the coords match the selection range', () => {
  94. selectionRange.ranges.push(
  95. new CellRange(new CellCoords(1, 1), new CellCoords(1, 1), new CellCoords(3, 3)),
  96. new CellRange(new CellCoords(11, 11), new CellCoords(11, 11), new CellCoords(11, 11))
  97. );
  98. expect(selectionRange.includes(new CellCoords(1, 2))).toBe(true);
  99. expect(selectionRange.includes(new CellCoords(1, 3))).toBe(true);
  100. expect(selectionRange.includes(new CellCoords(11, 11))).toBe(true);
  101. });
  102. it('should return `false` if the coords doesn\'t match the selection range', () => {
  103. selectionRange.ranges.push(
  104. new CellRange(new CellCoords(1, 1), new CellCoords(1, 1), new CellCoords(3, 3)),
  105. new CellRange(new CellCoords(11, 11), new CellCoords(11, 11), new CellCoords(11, 11))
  106. );
  107. expect(selectionRange.includes(new CellCoords(1, 4))).toBe(false);
  108. expect(selectionRange.includes(new CellCoords(0, 0))).toBe(false);
  109. expect(selectionRange.includes(new CellCoords(11, 12))).toBe(false);
  110. });
  111. });
  112. describe('.clear', () => {
  113. it('should reset the ranges collection', () => {
  114. selectionRange.ranges.push(
  115. new CellRange(new CellCoords(4, 4)),
  116. new CellRange(new CellCoords(0, 0)),
  117. new CellRange(new CellCoords(1, 2))
  118. );
  119. selectionRange.clear();
  120. expect(selectionRange.ranges.length).toBe(0);
  121. });
  122. });
  123. describe('.size', () => {
  124. it('should return the length/size of the collected ranges', () => {
  125. selectionRange.ranges.push(
  126. new CellRange(new CellCoords(4, 4)),
  127. new CellRange(new CellCoords(0, 0)),
  128. new CellRange(new CellCoords(1, 2))
  129. );
  130. expect(selectionRange.size()).toBe(3);
  131. });
  132. });
  133. describe('.peekByIndex', () => {
  134. it('should return the CellRange object from the end based on the offset argument passed to the method', () => {
  135. selectionRange.ranges.push(
  136. new CellRange(new CellCoords(4, 4)),
  137. new CellRange(new CellCoords(0, 0)),
  138. new CellRange(new CellCoords(1, 2))
  139. );
  140. expect(selectionRange.peekByIndex().toObject()).toEqual({
  141. from: { col: 2, row: 1 },
  142. to: { col: 2, row: 1 },
  143. });
  144. expect(selectionRange.peekByIndex(-1).toObject()).toEqual({
  145. from: { col: 0, row: 0 },
  146. to: { col: 0, row: 0 },
  147. });
  148. expect(selectionRange.peekByIndex(-2).toObject()).toEqual({
  149. from: { col: 4, row: 4 },
  150. to: { col: 4, row: 4 },
  151. });
  152. expect(selectionRange.peekByIndex(-4)).not.toBeDefined();
  153. expect(selectionRange.peekByIndex(-5)).not.toBeDefined();
  154. });
  155. });
  156. it('should have implemented iterator protocol', () => {
  157. selectionRange.ranges.push(
  158. new CellRange(new CellCoords(4, 4)),
  159. new CellRange(new CellCoords(0, 0)),
  160. new CellRange(new CellCoords(1, 2))
  161. );
  162. expect(selectionRange[Symbol.iterator]).toBeDefined();
  163. const ranges = Array.from(selectionRange);
  164. expect(ranges.length).toBe(3);
  165. expect(ranges[0].toObject()).toEqual({
  166. from: { col: 4, row: 4 },
  167. to: { col: 4, row: 4 },
  168. });
  169. expect(ranges[1].toObject()).toEqual({
  170. from: { col: 0, row: 0 },
  171. to: { col: 0, row: 0 },
  172. });
  173. expect(ranges[2].toObject()).toEqual({
  174. from: { col: 2, row: 1 },
  175. to: { col: 2, row: 1 },
  176. });
  177. });
  178. });