From 373b78a10b3255104e221c64494e3139d11d058b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ulrich-Matthias=20Sch=C3=A4fer?= Date: Fri, 10 Apr 2020 15:04:42 +1000 Subject: [PATCH] added tests for Line --- spec/spec/elements/Line.js | 104 +++++++++++++++++++++++++++++++++++ spec/spec/elements/Marker.js | 4 +- src/elements/Line.js | 2 +- 3 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 spec/spec/elements/Line.js diff --git a/spec/spec/elements/Line.js b/spec/spec/elements/Line.js new file mode 100644 index 0000000..9fe1871 --- /dev/null +++ b/spec/spec/elements/Line.js @@ -0,0 +1,104 @@ +/* globals describe, expect, it, beforeEach, spyOn, jasmine, container */ + +import { Line, PointArray, SVG, G } from '../../../src/main.js' + +const { any, objectContaining } = jasmine + +describe('Line.js', () => { + let line + + beforeEach(() => { + line = new Line() + }) + + describe('()', () => { + it('creates a new object of type Line', () => { + expect(new Line()).toEqual(any(Line)) + }) + + it('sets passed attributes on the element', () => { + expect(new Line({ id: 'foo' }).id()).toBe('foo') + }) + }) + + describe('array()', () => { + it('returns a PointArray containing the points of the line', () => { + const array = line.plot(1, 2, 3, 4).array() + expect(array).toEqual(any(PointArray)) + expect(array).toEqual([ [ 1, 2 ], [ 3, 4 ] ]) + }) + }) + + describe('plot()', () => { + it('relays to array() as getter', () => { + const spy = spyOn(line, 'array') + line.plot() + expect(spy).toHaveBeenCalled() + }) + + it('calls attr with line attributes when 4 parameters given', () => { + const spy = spyOn(line, 'attr') + line.plot(1, 2, 3, 4) + expect(spy).toHaveBeenCalledWith({ x1: 1, y1: 2, x2: 3, y2: 4 }) + }) + + it('calls attr with line attributes when array given', () => { + const spy = spyOn(line, 'attr') + line.plot([ 1, 2, 3, 4 ]) + expect(spy).toHaveBeenCalledWith({ x1: 1, y1: 2, x2: 3, y2: 4 }) + }) + + it('calls attr with line attributes when multi array given', () => { + const spy = spyOn(line, 'attr') + line.plot([ [ 1, 2 ], [ 3, 4 ] ]) + expect(spy).toHaveBeenCalledWith({ x1: 1, y1: 2, x2: 3, y2: 4 }) + }) + + it('calls attr with line attributes when PointArray given', () => { + const spy = spyOn(line, 'attr') + line.plot(new PointArray([ [ 1, 2 ], [ 3, 4 ] ])) + expect(spy).toHaveBeenCalledWith({ x1: 1, y1: 2, x2: 3, y2: 4 }) + }) + }) + + describe('move()', () => { + it('returns itself', () => { + expect(line.move(0, 0)).toBe(line) + }) + + it('moves the line along x and y axis', () => { + const canvas = SVG().addTo(container) + const line = canvas.line(1, 2, 3, 4) + line.move(50, 50) + expect(line.bbox()).toEqual(objectContaining({ + x: 50, y: 50, width: 2, height: 2 + })) + }) + }) + + describe('size()', () => { + it('returns itself', () => { + expect(line.size(50, 50)).toBe(line) + }) + + it('sets the size of the line', () => { + const canvas = SVG().addTo(container) + const line = canvas.line(1, 2, 3, 4) + line.size(50, 50) + expect(line.bbox()).toEqual(objectContaining({ + width: 50, height: 50, x: 1, y: 2 + })) + }) + }) + + describe('Container', () => { + describe('line()', () => { + it('creates a line with given points', () => { + const group = new G() + const line = group.line(1, 2, 3, 4) + expect(line.array()).toEqual([ [ 1, 2 ], [ 3, 4 ] ]) + expect(line).toEqual(any(Line)) + }) + }) + }) +}) diff --git a/spec/spec/elements/Marker.js b/spec/spec/elements/Marker.js index 67be0bd..129bbc2 100644 --- a/spec/spec/elements/Marker.js +++ b/spec/spec/elements/Marker.js @@ -32,8 +32,8 @@ describe('Marker.js', function () { describe('orient()', () => { it('sets the orient attribute', () => { - const marker = new Marker().orient('start') - expect(marker.attr('orient')).toBe('start') + const marker = new Marker().orient('auto') + expect(marker.attr('orient')).toBe('auto') }) }) diff --git a/src/elements/Line.js b/src/elements/Line.js index 146aa45..b9982f4 100644 --- a/src/elements/Line.js +++ b/src/elements/Line.js @@ -29,7 +29,7 @@ export default class Line extends Shape { if (x1 == null) { return this.array() } else if (typeof y1 !== 'undefined') { - x1 = { x1: x1, y1: y1, x2: x2, y2: y2 } + x1 = { x1, y1, x2, y2 } } else { x1 = new PointArray(x1).toLine() } -- 2.39.5