drag-utils.test.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import {indexForPositionOnList} from '../../../src/lib/drag-utils';
  2. const box = (top, right, bottom, left) => ({top, right, bottom, left});
  3. describe('indexForPositionOnList', () => {
  4. test('returns null when not given any boxes', () => {
  5. expect(indexForPositionOnList({x: 0, y: 0}, [])).toEqual(null);
  6. });
  7. test('wrapped list with incomplete last row', () => {
  8. const boxes = [
  9. box(0, 100, 100, 0), // index: 0
  10. box(0, 200, 100, 100), // index: 1
  11. box(0, 300, 100, 200), // index: 2
  12. box(100, 100, 200, 0), // index: 3 (second row)
  13. box(100, 200, 200, 100) // index: 4 (second row, left incomplete intentionally)
  14. ];
  15. // Inside the second box.
  16. expect(indexForPositionOnList({x: 150, y: 50}, boxes)).toEqual(1);
  17. // On the border edge of the first and second box. Given to the first box.
  18. expect(indexForPositionOnList({x: 100, y: 50}, boxes)).toEqual(0);
  19. // Off the top/left edge.
  20. expect(indexForPositionOnList({x: -100, y: -100}, boxes)).toEqual(0);
  21. // Off the left edge, in the second row.
  22. expect(indexForPositionOnList({x: -100, y: 175}, boxes)).toEqual(3);
  23. // Off the right edge, in the first row.
  24. expect(indexForPositionOnList({x: 400, y: 75}, boxes)).toEqual(2);
  25. // Off the top edge, middle of second item.
  26. expect(indexForPositionOnList({x: 150, y: -75}, boxes)).toEqual(1);
  27. // Within the right edge bounds, but on the second (incomplete) row.
  28. // This tests that wrapped lists with incomplete final rows work correctly.
  29. expect(indexForPositionOnList({x: 375, y: 175}, boxes)).toEqual(4);
  30. });
  31. });