import { assert } from 'chai'; import { Style } from '../../../../lib/graphics/styles/Style'; import { Shape } from '../../../../lib/graphics/shapes/Shape'; import { Line } from '../../../../lib/graphics/shapes/Line'; import { Vector2D } from '../../../../lib/graphics/Vector2D'; import { canvasRenderingContext2DSpy } from '../mock/CanvasRenderingContext2D'; describe('graphics.styles.Style', () => { describe('constructor/create', () => { it('empty', () => { const style = new Style(); assert.strictEqual(style.size, 0); }); it('attributes', () => { const style = new Style({ strokeStyle: 'black' }); assert.strictEqual(style.size, 1); assert.strictEqual(style.get('strokeStyle'), 'black'); }); it('create', () => { const style = Style.create({ strokeStyle: 'black', lineWidth: 2 }); assert.strictEqual(style.size, 2); assert.strictEqual(style.get('strokeStyle'), 'black'); assert.strictEqual(style.get('lineWidth'), 2); }); }); describe('attributes/setAttributes', () => { it('attributes/setAttributes', () => { const style = new Style(); assert.deepEqual(style.attributes(), {}); style.setAttributes({ strokeStyle: 'black', lineWidth: 2 }); assert.deepEqual(style.attributes(), { strokeStyle: 'black', lineWidth: 2 }); style.setAttributes(); assert.deepEqual(style.attributes(), {}); const attrs = Object.create({ foo: 'bar' }); attrs.strokeStyle = 'blue'; style.setAttributes(attrs); assert.deepEqual(style.attributes(), { 'strokeStyle': 'blue' }); }); }); describe('getters/setter', () => { test.each([ ['direction', 'ltr'], ['fillStyle', 'lightgray'], ['filter', 'blur(5px)'], ['font', '48px serif'], ['globalAlpha', .5], ['globalCompositeOperation', 'xor'], ['imageSmoothingEnabled', true], ['imageSmoothingQuality', 'medium'], ['lineCap', 'round'], ['lineDashOffset', 2], ['lineJoin', 'round'], ['lineWidth', 15], ['miterLimit', 10], ['shadowBlur', 10], ['shadowColor', 'black'], ['shadowOffsetX', 10], ['shadowOffsetY', 10], ['strokeStyle', 'blue'], ['textAlign', 'center'], ['textBaseline', 'middle'], ])('getters/setter#%#', (name: string, value: any) => { const style = new Style(); (style)[name] = value; assert.strictEqual((style)[name], value); }); it('getters', () => { const style = new Style({ strokeStyle: 'black', lineWidth: 2 }); assert.strictEqual(style.strokeStyle, 'black'); assert.strictEqual(style.lineWidth, 2); assert.isUndefined(style.fillStyle); assert.isUndefined(style.fillStyle); }); it('setters', () => { const style = new Style({ strokeStyle: 'black', lineWidth: 2 }); style.strokeStyle = 'yellow'; style.fillStyle = 'black'; style.lineWidth = 1; assert.strictEqual(style.strokeStyle, 'yellow'); assert.strictEqual(style.fillStyle, 'black'); assert.strictEqual(style.lineWidth, 1); }); }); describe('set/get/delete/has/clear', () => { it('set/get/delete/has/clear', () => { const style = new Style(); assert.ok(!style.has('strokeStyle')); assert.isUndefined(style.get('strokeStyle')); style.set('strokeStyle', 'yellow'); assert.ok(style.has('strokeStyle')); assert.strictEqual(style.get('strokeStyle'), 'yellow'); style.set('lineWidth', 2); assert.ok(style.has('lineWidth')); assert.strictEqual(style.get('lineWidth'), 2); style.delete('lineWidth') assert.ok(!style.has('lineWidth')); assert.isUndefined(style.get('lineWidth')); style.clear(); assert.deepEqual(style.size, 0); }); }); describe('forEach', () => { it('empty', () => { const style = new Style(); let count = 0; style.forEach(() => { count++; }); assert.strictEqual(count, 0); }); it('filled', () => { const attrs = { strokeStyle: 'black', lineWidth: 2 }; const style = new Style(attrs); let count = 0; const boundObj = { ok: true }; style.forEach(function (this: any, value, name, thisStyle) { assert.ok(name in attrs); assert.strictEqual(value, (attrs)[name]); assert.ok(thisStyle === style); assert.ok(this.ok); count++; }, boundObj); assert.strictEqual(count, Object.keys(attrs).length); }); }); describe('merged', () => { it('merged', () => { const lhs = new Style({ strokeStyle: 'yellow', lineWidth: 2 }); const rhs = new Style({ lineWidth: 1, fillStyle: 'black' }); const merged = lhs.merged(rhs); assert.deepEqual(merged.attributes(), { strokeStyle: 'yellow', lineWidth: 1, fillStyle: 'black' }); lhs.lineWidth = 3; rhs.fillStyle = 'gray'; assert.deepEqual(merged.attributes(), { strokeStyle: 'yellow', lineWidth: 1, fillStyle: 'black' }); }); }); describe('render', () => { test.each([ [ new Style({ strokeStyle: 'yellow', fillStyle: 'black' }), new Line(new Vector2D(1, 1), new Vector2D(3, 3)), [ { apply: 'save', args: [] }, { set: 'strokeStyle', value: 'yellow' }, { set: 'fillStyle', value: 'black' }, { apply: 'beginPath', args: [] }, { apply: 'moveTo', args: [1, 1] }, { apply: 'lineTo', args: [3, 3] }, { apply: 'stroke', args: [] }, { apply: 'restore', args: [] }, ] ] ])('render#%#', (style: Style, shape: Shape, result: any[]) => { const interceptions: any[] = []; const context = canvasRenderingContext2DSpy(interceptions); style.render(context, shape); assert.deepEqual(interceptions, result); }); }); });