Skip to content

Commit bc04502

Browse files
committed
test(SysPlot): Added tests for main Class and utilities
1 parent 4563c77 commit bc04502

5 files changed

Lines changed: 262 additions & 0 deletions

File tree

src/SysPlot.test.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import SysPlot, { ArchimedesSpiral, VogelSpiral } from './';
2+
3+
describe('SysPlot class', () => {
4+
let sysPlot;
5+
let vectors;
6+
let positions;
7+
8+
const getShapes = () => [{ radius: 50 }, { radius: 25 }, { radius: 15 }, { radius: 10 }];
9+
const getConfig = (config) => ({
10+
algorithm: ArchimedesSpiral,
11+
padding: 10,
12+
proportional: true,
13+
spread: 0.5,
14+
...config,
15+
});
16+
17+
beforeEach(() => {
18+
sysPlot = new SysPlot()
19+
.setBounds(500, 500)
20+
.setShapes(getShapes())
21+
.setConfig(getConfig());
22+
23+
vectors = sysPlot.getVectors();
24+
positions = sysPlot.getPositions();
25+
});
26+
27+
beforeEach(() => {
28+
expect(sysPlot.vectors).not.toBe(null);
29+
expect(sysPlot.positions).not.toBe(null);
30+
});
31+
32+
describe('setConfig', () => {
33+
test('algorithm clears vectors and positions', () => {
34+
sysPlot.setConfig(getConfig({ algorithm: VogelSpiral }));
35+
36+
expect(sysPlot.config.algorithm).toBe(VogelSpiral);
37+
expect(sysPlot.vectors).toBe(null);
38+
expect(sysPlot.positions).toBe(null);
39+
});
40+
41+
test('proportional clears vectors and positions', () => {
42+
sysPlot.setConfig(getConfig({ proportional: false }));
43+
44+
expect(sysPlot.config.proportional).toBe(false);
45+
expect(sysPlot.vectors).toBe(null);
46+
expect(sysPlot.positions).toBe(null);
47+
});
48+
49+
test('spread clears vectors and positions', () => {
50+
sysPlot.setConfig(getConfig({ spread: 0.4 }));
51+
52+
expect(sysPlot.config.spread).toBe(0.4);
53+
expect(sysPlot.vectors).toBe(null);
54+
expect(sysPlot.positions).toBe(null);
55+
});
56+
57+
test('padding clears positions', () => {
58+
sysPlot.setConfig(getConfig({ padding: 5 }));
59+
60+
expect(sysPlot.config.padding).toBe(5);
61+
expect(sysPlot.vectors).not.toBe(null);
62+
expect(sysPlot.positions).toBe(null);
63+
});
64+
});
65+
66+
describe('setBounds', () => {
67+
test('sets width and height', () => {
68+
sysPlot.setBounds(100, 100);
69+
70+
expect(sysPlot.width).toBe(100);
71+
expect(sysPlot.height).toBe(100);
72+
});
73+
74+
test('clears vectors and positions when bounds have changed', () => {
75+
sysPlot.setBounds(100, 100);
76+
77+
expect(sysPlot.vectors).toBe(null);
78+
expect(sysPlot.positions).toBe(null);
79+
});
80+
});
81+
82+
describe('setShapes', () => {
83+
test('sets shapes', () => {
84+
const shapes = getShapes();
85+
86+
sysPlot.setShapes(shapes);
87+
88+
expect(sysPlot.shapes).toBe(shapes);
89+
});
90+
91+
test('clears positions', () => {
92+
sysPlot.setShapes(getShapes());
93+
94+
expect(sysPlot.positions).toBe(null);
95+
});
96+
});
97+
98+
describe('getVectors', () => {
99+
const MockAlgorithm = jest.fn();
100+
MockAlgorithm.NORMALISATION_FACTOR = 1;
101+
102+
test('returns last vectors if vectors is set', () => {
103+
expect(sysPlot.getVectors()).toBe(vectors);
104+
});
105+
106+
test('fetches new vectors if vectors are set', () => {
107+
sysPlot.vectors = null;
108+
expect(sysPlot.getVectors()).not.toBe(vectors);
109+
});
110+
});
111+
112+
describe('getPositions', () => {
113+
test('returns last positions if positions is set', () => {
114+
expect(sysPlot.getPositions()).toBe(positions);
115+
});
116+
117+
test('fetches new positions if positions are not set', () => {
118+
sysPlot.positions = null;
119+
expect(sysPlot.getPositions()).not.toBe(positions);
120+
});
121+
});
122+
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { Box, Circle, Vector } from 'sat';
2+
import hasCollided from './hasCollided';
3+
4+
describe('hasCollided', () => {
5+
describe('Circle to Circle', () => {
6+
test('collision', () => {
7+
const a = new Circle(new Vector(0, 0), 20);
8+
const b = new Circle(new Vector(10, 10), 20);
9+
10+
expect(hasCollided(a, b)).toBe(true);
11+
});
12+
13+
test('no collision', () => {
14+
const a = new Circle(new Vector(0, 0), 5);
15+
const b = new Circle(new Vector(10, 10), 5);
16+
17+
expect(hasCollided(a, b)).toBe(false);
18+
});
19+
});
20+
21+
describe('Circle to Rectangle', () => {
22+
test('collision', () => {
23+
const a = new Circle(new Vector(0, 0), 20);
24+
const b = new Box(new Vector(10, 10), 20, 20);
25+
26+
expect(hasCollided(a, b)).toBe(true);
27+
});
28+
29+
test('no collision', () => {
30+
const a = new Circle(new Vector(0, 0), 5);
31+
const b = new Box(new Vector(10, 10), 5, 5);
32+
33+
expect(hasCollided(a, b)).toBe(false);
34+
});
35+
});
36+
37+
describe('Rectangle to Circle', () => {
38+
test('collision', () => {
39+
const a = new Box(new Vector(10, 10), 20, 20);
40+
const b = new Circle(new Vector(0, 0), 20);
41+
42+
expect(hasCollided(a, b)).toBe(true);
43+
});
44+
45+
test('no collision', () => {
46+
const a = new Box(new Vector(10, 10), 5, 5);
47+
const b = new Circle(new Vector(0, 0), 5);
48+
49+
expect(hasCollided(a, b)).toBe(false);
50+
});
51+
});
52+
53+
describe('Rectangle to Rectangle', () => {
54+
test('collision', () => {
55+
const a = new Box(new Vector(0, 0), 20, 20);
56+
const b = new Box(new Vector(10, 10), 20, 20);
57+
58+
expect(hasCollided(a, b)).toBe(true);
59+
});
60+
61+
test('no collision', () => {
62+
const a = new Box(new Vector(0, 0), 5);
63+
const b = new Box(new Vector(10, 10), 5, 5);
64+
65+
expect(hasCollided(a, b)).toBe(false);
66+
});
67+
});
68+
});

src/positioning/isInBounds.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { Vector, Box, Circle } from 'sat';
2+
import isInBounds from './isInBounds';
3+
4+
describe('isInBounds', () => {
5+
describe('Circle', () => {
6+
test('is in bounds', () => {
7+
expect(isInBounds(30, 30,
8+
new Circle(new Vector(10, 10), 5))).toBe(true);
9+
});
10+
11+
test('is not in bounds', () => {
12+
expect(isInBounds(30, 30,
13+
new Circle(new Vector(10, 0), 5))).toBe(false);
14+
expect(isInBounds(30, 30,
15+
new Circle(new Vector(30, 5), 5))).toBe(false);
16+
expect(isInBounds(30, 30,
17+
new Circle(new Vector(25, 30), 5))).toBe(false);
18+
expect(isInBounds(30, 30,
19+
new Circle(new Vector(0, 25), 5))).toBe(false);
20+
});
21+
});
22+
23+
describe('Box', () => {
24+
test('is in bounds', () => {
25+
expect(isInBounds(30, 30,
26+
new Box(new Vector(0, 0), 5))).toBe(true);
27+
});
28+
29+
test('is not in bounds', () => {
30+
expect(isInBounds(30, 30,
31+
new Box(new Vector(0, -1), 10, 10))).toBe(false);
32+
expect(isInBounds(30, 30,
33+
new Box(new Vector(31, 0), 10, 10))).toBe(false);
34+
expect(isInBounds(30, 30,
35+
new Box(new Vector(20, 31), 10, 10))).toBe(false);
36+
expect(isInBounds(30, 30,
37+
new Box(new Vector(-1, 20), 10, 10))).toBe(false);
38+
});
39+
});
40+
});

src/positioning/toShape.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Box, Circle } from 'sat';
2+
import toShape from './toShape';
3+
4+
describe('toShape', () => {
5+
test('radius as Circle', () => {
6+
expect(toShape([0, 0], { radius: 10 }) instanceof Circle).toBe(true);
7+
});
8+
9+
test('width and height as Box', () => {
10+
expect(toShape([0, 0], { width: 10, height: 10 }) instanceof Box).toBe(true);
11+
});
12+
13+
test('points', () => {
14+
expect(() => toShape([0, 0], { points: [0, 0] })).toThrow();
15+
});
16+
});

src/positioning/toVectors.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Box, Circle, Vector } from 'sat';
2+
import toVectors from './toVectors';
3+
4+
describe('toVectors', () => {
5+
test('Circle', () => {
6+
expect(toVectors([ new Circle(new Vector(0, 0), 10)])).toEqual([
7+
[0, 0],
8+
]);
9+
});
10+
11+
test('Rectangle', () => {
12+
expect(toVectors([ new Box(new Vector(0, 0), 10, 10)])).toEqual([
13+
[0, 0],
14+
]);
15+
});
16+
});

0 commit comments

Comments
 (0)