chartUtils.spec.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import {fitToValueRect, isInRect} from 'sentry/views/ddm/chartUtils';
  2. describe('isInRect', () => {
  3. const rect = {
  4. top: 0,
  5. left: 0,
  6. right: 10,
  7. bottom: 10,
  8. x: 0,
  9. y: 0,
  10. width: 10,
  11. height: 10,
  12. toJSON: () => {},
  13. };
  14. it('should return false if rect is undefined', () => {
  15. expect(isInRect(1, 2, undefined)).toBe(false);
  16. });
  17. it('should return true if point is within the rect', () => {
  18. expect(isInRect(5, 5, rect)).toBe(true);
  19. });
  20. it('should return false if point is outside the rect', () => {
  21. expect(isInRect(11, 11, rect)).toBe(false);
  22. });
  23. it('should return true if point is exactly on the border of the rect', () => {
  24. expect(isInRect(0, 0, rect)).toBe(true);
  25. expect(isInRect(10, 10, rect)).toBe(true);
  26. });
  27. });
  28. describe('fitToValueRect', () => {
  29. it('should return original x and y if rect is undefined', () => {
  30. const x = 5;
  31. const y = 10;
  32. const rect = undefined;
  33. const result = fitToValueRect(x, y, rect);
  34. expect(result).toEqual([x, y]);
  35. });
  36. it('should return original x and y if they are within the value rect', () => {
  37. const x = 5;
  38. const y = 10;
  39. const rect = {xMin: 0, xMax: 10, yMin: 0, yMax: 20};
  40. const result = fitToValueRect(x, y, rect);
  41. expect(result).toEqual([x, y]);
  42. });
  43. it('should return x as xMin if it is below the minimum xValue', () => {
  44. const x = -5;
  45. const y = 10;
  46. const rect = {xMin: 0, xMax: 10, yMin: 0, yMax: 20};
  47. const result = fitToValueRect(x, y, rect);
  48. expect(result).toEqual([rect.xMin, y]);
  49. });
  50. it('should return x as xMax if it is above the maximum xValue', () => {
  51. const x = 15;
  52. const y = 10;
  53. const rect = {xMin: 0, xMax: 10, yMin: 0, yMax: 20};
  54. const result = fitToValueRect(x, y, rect);
  55. expect(result).toEqual([rect.xMax, y]);
  56. });
  57. it('should return y as yMin if it is below the minimum yValue', () => {
  58. const x = 5;
  59. const y = -5;
  60. const rect = {xMin: 0, xMax: 10, yMin: 0, yMax: 20};
  61. const result = fitToValueRect(x, y, rect);
  62. expect(result).toEqual([x, rect.yMin]);
  63. });
  64. it('should return y as yMax if it is above the maximum yValue', () => {
  65. const x = 5;
  66. const y = 25;
  67. const rect = {xMin: 0, xMax: 10, yMin: 0, yMax: 20};
  68. const result = fitToValueRect(x, y, rect);
  69. expect(result).toEqual([x, rect.yMax]);
  70. });
  71. });