|
| 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 | +}); |
0 commit comments