feat(calculateCollision): добавлена функция вычисления состояния пересечения двух объектов

This commit is contained in:
Ilia Mashkov
2026-07-16 12:37:56 +03:00
parent ccff02d29b
commit 4627fe4f76
2 changed files with 181 additions and 0 deletions
@@ -0,0 +1,130 @@
import { describe, expect, it } from 'vitest';
import { calculateCollision } from './calculateCollision';
describe('calculateCollision', () => {
it('Корректно обрабатывает невозможные кейсы (левая координата больше правой)', () => {
const firstObject = {
left: 0,
right: 20,
top: 0,
bottom: 20,
};
const secondObject = {
left: 10,
right: 0,
top: 0,
bottom: 10,
};
expect(calculateCollision(firstObject, secondObject)).toBeNull();
});
it('Корректно обрабатывает неверный формат данных', () => {
const firstObject = {
left: 'wrong',
right: 20,
top: 0,
bottom: 20,
};
const secondObject = {
left: 0,
right: 10,
top: 0,
bottom: 10,
};
expect(calculateCollision(firstObject, secondObject)).toBeNull();
});
it('Корректно обрабатывает отсутствие пересечения по X', () => {
const firstObject = {
left: 0,
right: 20,
top: 0,
bottom: 20,
};
const secondObject = {
left: 25,
right: 30,
top: 0,
bottom: 10,
};
expect(calculateCollision(firstObject, secondObject)).toBe(false);
});
it('Корректно обрабатывает отсутствие пересечения по Y', () => {
const firstObject = {
left: 0,
right: 20,
top: 0,
bottom: 20,
};
const secondObject = {
left: 0,
right: 30,
top: 25,
bottom: 30,
};
expect(calculateCollision(firstObject, secondObject)).toBe(false);
});
it('Корректно обрабатывает отсутствие пересечения по X и Y', () => {
const firstObject = {
left: 0,
right: 20,
top: 0,
bottom: 20,
};
const secondObject = {
left: 25,
right: 30,
top: 25,
bottom: 30,
};
expect(calculateCollision(firstObject, secondObject)).toBe(false);
});
it('Корректно обрабатывает пересечение', () => {
const firstObject = {
left: 0,
right: 20,
top: 0,
bottom: 20,
};
const secondObject = {
left: 10,
right: 30,
top: 10,
bottom: 30,
};
expect(calculateCollision(firstObject, secondObject)).toBe(true);
});
it('Корректно обрабатывает вхождение', () => {
const firstObject = {
left: 0,
right: 20,
top: 0,
bottom: 20,
};
const secondObject = {
left: 10,
right: 15,
top: 10,
bottom: 15,
};
expect(calculateCollision(firstObject, secondObject)).toBe(true);
});
});