From 054eb94e3b20bee182acbc4389fa7736fad72eca Mon Sep 17 00:00:00 2001 From: Ulrich-Matthias Schäfer Date: Sat, 11 Apr 2020 13:15:08 +1000 Subject: added tests for pointed.js and poly.js --- spec/checkForAllTests.js | 25 +++++++++ spec/spec/modules/core/pointed.js | 63 ++++++++++++++++++++++ spec/spec/modules/core/poly.js | 109 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 197 insertions(+) create mode 100644 spec/checkForAllTests.js create mode 100644 spec/spec/modules/core/pointed.js create mode 100644 spec/spec/modules/core/poly.js (limited to 'spec') diff --git a/spec/checkForAllTests.js b/spec/checkForAllTests.js new file mode 100644 index 0000000..99c648e --- /dev/null +++ b/spec/checkForAllTests.js @@ -0,0 +1,25 @@ +const glob = require('glob') +const path = require('path') + +glob('./spec/*/**/*.js', (err, tests) => { + + if (err) { + throw err + } + + glob('./src/**/*.js', (err, files) => { + if (err) { + throw err + } + + files = files.map((e) => path.basename(e)) + tests = tests.map((e) => path.basename(e)) + const difference = files.filter(x => !tests.includes(x)) + + if (difference.length) { + console.error('The following files dont have a test file:\n\t' + difference.join('\n\t')) + } else { + console.info('All src files are covered by tests') + } + }) +}) diff --git a/spec/spec/modules/core/pointed.js b/spec/spec/modules/core/pointed.js new file mode 100644 index 0000000..bbe2475 --- /dev/null +++ b/spec/spec/modules/core/pointed.js @@ -0,0 +1,63 @@ +/* globals describe, expect, it, beforeEach, container */ + +import { SVG } from '../../../../src/main.js' + +describe('pointed.js', () => { + let canvas, lines + + beforeEach(() => { + canvas = SVG().addTo(container) + const line = canvas.line(1, 2, 3, 4) + const polygon = canvas.polygon([ 1, 2, 3, 4 ]) + const polyline = canvas.polyline([ 1, 2, 3, 4 ]) + lines = { line, polygon, polyline } + }) + + ;[ 'line', 'polygon', 'polyline' ].forEach((l) => { + describe('for ' + l, () => { + describe('x()', () => { + it('sets the x value of the ' + l + 'and returns itself', () => { + expect(lines[l].x(50)).toBe(lines[l]) + expect(lines[l].bbox().x).toBe(50) + }) + + it('gets the x value of the ' + l, () => { + expect(lines[l].x(50).x()).toBe(50) + }) + }) + + describe('y()', () => { + it('sets the y value of the ' + l + 'and returns itself', () => { + expect(lines[l].y(50)).toBe(lines[l]) + expect(lines[l].bbox().y).toBe(50) + }) + + it('gets the y value of the ' + l, () => { + expect(lines[l].y(50).y()).toBe(50) + }) + }) + + describe('width()', () => { + it('sets the width of the ' + l + 'and returns itself', () => { + expect(lines[l].width(50)).toBe(lines[l]) + expect(lines[l].bbox().width).toBe(50) + }) + + it('gets the width of the ' + l, () => { + expect(lines[l].width(50).width()).toBe(50) + }) + }) + + describe('height()', () => { + it('sets the height of the ' + l + 'and returns itself', () => { + expect(lines[l].height(50)).toBe(lines[l]) + expect(lines[l].bbox().height).toBe(50) + }) + + it('gets the height of the ' + l, () => { + expect(lines[l].height(50).height()).toBe(50) + }) + }) + }) + }) +}) diff --git a/spec/spec/modules/core/poly.js b/spec/spec/modules/core/poly.js new file mode 100644 index 0000000..c44bebc --- /dev/null +++ b/spec/spec/modules/core/poly.js @@ -0,0 +1,109 @@ +/* globals describe, expect, beforeEach, it, spyOn, jasmine, container */ + +import { Polygon, SVG, PointArray } from '../../../../src/main.js' + +const { any, objectContaining } = jasmine + +describe('Polygon.js', () => { + let poly + + beforeEach(() => { + poly = new Polygon() + }) + + describe('array()', () => { + it('returns the underlying PointArray', () => { + const array = poly.plot('1 2 3 4').array() + expect(array).toEqual(any(PointArray)) + expect(array).toEqual([ [ 1, 2 ], [ 3, 4 ] ]) + }) + }) + + describe('clear()', () => { + it('clears the array cache and returns itself', () => { + const array = poly.plot('1 2 3 4').array() + expect(poly.clear()).toBe(poly) + expect(array).not.toBe(poly._array) + }) + }) + + describe('move()', () => { + it('returns itself', () => { + expect(poly.move(0, 0)).toBe(poly) + }) + + it('moves the poly along x and y axis', () => { + const canvas = SVG().addTo(container) + const poly = canvas.polygon('0 0 50 50') + poly.move(50, 50) + expect(poly.bbox()).toEqual(objectContaining({ + x: 50, y: 50, width: 50, height: 50 + })) + }) + }) + + describe('plot()', () => { + it('relays to array() as getter', () => { + const spy = spyOn(poly, 'array') + poly.plot() + expect(spy).toHaveBeenCalled() + }) + + it('works by passing a string', () => { + const spy = spyOn(poly, 'attr') + poly.plot('1 2 3 4') + expect(spy).toHaveBeenCalledWith('points', '1 2 3 4') + }) + + it('works with flat array', () => { + const spy = spyOn(poly, 'attr') + poly.plot([ 1, 2, 3, 4 ]) + expect(spy).toHaveBeenCalledWith('points', [ [ 1, 2 ], [ 3, 4 ] ]) + }) + + it('works with multi array', () => { + const spy = spyOn(poly, 'attr') + poly.plot([ [ 1, 2 ], [ 3, 4 ] ]) + expect(spy).toHaveBeenCalledWith('points', [ [ 1, 2 ], [ 3, 4 ] ]) + }) + + it('works with PointArray', () => { + const spy = spyOn(poly, 'attr') + poly.plot(new PointArray([ [ 1, 2 ], [ 3, 4 ] ])) + expect(spy).toHaveBeenCalledWith('points', [ [ 1, 2 ], [ 3, 4 ] ]) + }) + }) + + describe('size()', () => { + it('returns itself', () => { + expect(poly.size(50, 50)).toBe(poly) + }) + + it('sets the size of the poly', () => { + const canvas = SVG().addTo(container) + const poly = canvas.polygon('0 0 50 50') + poly.size(100, 100) + expect(poly.bbox()).toEqual(objectContaining({ + width: 100, height: 100, x: 0, y: 0 + })) + }) + + it('changes height proportionally', () => { + const canvas = SVG().addTo(container) + const poly = canvas.polygon('0 0 50 50') + poly.size(100, null) + expect(poly.bbox()).toEqual(objectContaining({ + width: 100, height: 100, x: 0, y: 0 + })) + }) + + it('changes width proportionally', () => { + const canvas = SVG().addTo(container) + const poly = canvas.polygon('0 0 50 50') + poly.size(null, 100) + expect(poly.bbox()).toEqual(objectContaining({ + width: 100, height: 100, x: 0, y: 0 + })) + }) + }) +}) -- cgit v1.2.3