Adds more tests for code

This commit is contained in:
Steve Ruiz 2021-06-25 13:21:33 +01:00
parent 776c0b5f0e
commit 75e60d5eb2
6 changed files with 128 additions and 31 deletions

View file

@ -1,6 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`selection creates a code control: generated code controls from code 1`] = `Object {}`; exports[`selection creates a code control: generated code controls from code 1`] = `
Object {
"test-number-control": Object {
"id": "test-number-control",
"label": "x",
"step": 1,
"type": "number",
"value": 0,
},
}
`;
exports[`selection generates a draw shape: generated draw from code 1`] = ` exports[`selection generates a draw shape: generated draw from code 1`] = `
Array [ Array [
@ -75,6 +85,35 @@ Array [
] ]
`; `;
exports[`selection generates a text shape: generated draw from code 1`] = `
Array [
Object {
"childIndex": 1,
"id": "test-text",
"isAspectRatioLocked": false,
"isGenerated": true,
"isHidden": false,
"isLocked": false,
"name": "Test text",
"parentId": "page1",
"point": Array [
100,
100,
],
"rotation": 0,
"scale": 1,
"style": Object {
"color": "Red",
"dash": "Dotted",
"isFilled": false,
"size": "Large",
},
"text": "Hello world!",
"type": "text",
},
]
`;
exports[`selection generates an arrow shape: generated draw from code 1`] = ` exports[`selection generates an arrow shape: generated draw from code 1`] = `
Array [ Array [
Object { Object {
@ -230,3 +269,33 @@ Object {
}, },
} }
`; `;
exports[`selection updates a code control: rectangle in state after changing code control 1`] = `
Object {
"childIndex": 1,
"id": "test-rectangle",
"isAspectRatioLocked": false,
"isGenerated": true,
"isHidden": false,
"isLocked": false,
"name": "Test Rectangle",
"parentId": "page1",
"point": Array [
0,
100,
],
"radius": 2,
"rotation": 0,
"size": Array [
0,
0,
],
"style": Object {
"color": "Red",
"dash": "Dotted",
"isFilled": false,
"size": "Medium",
},
"type": "rectangle",
}
`;

View file

@ -1,6 +1,6 @@
import state from 'state' import state from 'state'
import { generateFromCode } from 'state/code/generate' import { generateFromCode } from 'state/code/generate'
import { getShapes } from 'utils' import { getShape, getShapes } from 'utils'
import * as json from './__mocks__/document.json' import * as json from './__mocks__/document.json'
jest.useRealTimers() jest.useRealTimers()
@ -56,16 +56,9 @@ describe('selection', () => {
it('creates a code control', async () => { it('creates a code control', async () => {
const code = ` const code = `
const rectangle = new Rectangle({ new NumberControl({
id: "test-rectangle", id: "test-number-control",
name: 'Test Rectangle', label: "x"
point: [100, 100],
size: [200, 200],
style: {
size: SizeStyle.Medium,
color: ColorStyle.Red,
dash: DashStyle.Dotted,
},
}) })
` `
@ -80,18 +73,6 @@ describe('selection', () => {
it('updates a code control', async () => { it('updates a code control', async () => {
const code = ` const code = `
const rectangle = new Rectangle({
id: "test-rectangle",
name: 'Test Rectangle',
point: [100, 100],
size: [200, 200],
style: {
size: SizeStyle.Medium,
color: ColorStyle.Red,
dash: DashStyle.Dotted,
},
})
new NumberControl({ new NumberControl({
id: "test-number-control", id: "test-number-control",
label: "x" label: "x"
@ -101,6 +82,18 @@ describe('selection', () => {
id: "test-vector-control", id: "test-vector-control",
label: "size" label: "size"
}) })
const rectangle = new Rectangle({
id: "test-rectangle",
name: 'Test Rectangle',
point: [controls.x, 100],
size: controls.size,
style: {
size: SizeStyle.Medium,
color: ColorStyle.Red,
dash: DashStyle.Dotted,
},
})
` `
const { controls, shapes } = await generateFromCode(state.data, code) const { controls, shapes } = await generateFromCode(state.data, code)
@ -112,6 +105,10 @@ describe('selection', () => {
expect(state.data.codeControls).toMatchSnapshot( expect(state.data.codeControls).toMatchSnapshot(
'data in state after changing control' 'data in state after changing control'
) )
expect(getShape(state.data, 'test-rectangle')).toMatchSnapshot(
'rectangle in state after changing code control'
)
}) })
/* -------------------- Readonly -------------------- */ /* -------------------- Readonly -------------------- */
@ -251,7 +248,7 @@ describe('selection', () => {
it('generates an arrow shape', async () => { it('generates an arrow shape', async () => {
state.send('CLEARED_PAGE') state.send('CLEARED_PAGE')
const code = ` const code = `
const ellipse = new Arrow({ const draw = new Arrow({
id: 'test-draw', id: 'test-draw',
name: 'Test draw', name: 'Test draw',
points: [[100, 100], [200,200], [300,300]], points: [[100, 100], [200,200], [300,300]],
@ -269,4 +266,27 @@ describe('selection', () => {
expect(getShapes(state.data)).toMatchSnapshot('generated draw from code') expect(getShapes(state.data)).toMatchSnapshot('generated draw from code')
}) })
it('generates a text shape', async () => {
state.send('CLEARED_PAGE')
const code = `
const text = new Text({
id: 'test-text',
name: 'Test text',
point: [100, 100],
text: 'Hello world!',
style: {
size: SizeStyle.Large,
color: ColorStyle.Red,
dash: DashStyle.Dotted,
},
})
`
const { controls, shapes } = await generateFromCode(state.data, code)
state.send('GENERATED_FROM_CODE', { controls, shapes })
expect(getShapes(state.data)).toMatchSnapshot('generated draw from code')
})
}) })

View file

@ -17,6 +17,7 @@ import {
Shape, Shape,
DashStyle, DashStyle,
ColorStyle, ColorStyle,
FontSize,
SizeStyle, SizeStyle,
} from 'types' } from 'types'
import { getPage, getShapes } from 'utils' import { getPage, getShapes } from 'utils'
@ -39,6 +40,7 @@ const baseScope = {
DashStyle, DashStyle,
ColorStyle, ColorStyle,
SizeStyle, SizeStyle,
FontSize,
} }
/** /**

View file

@ -1,7 +1,8 @@
import CodeShape from './index' import CodeShape from './index'
import { uniqueId } from 'utils' import { uniqueId } from 'utils'
import { TextShape, ShapeProps, ShapeType, FontSize } from 'types' import { TextShape, ShapeProps, ShapeType } from 'types'
import { defaultStyle } from 'state/shape-styles' import { defaultStyle } from 'state/shape-styles'
import { getShapeUtils } from 'state/shape-utils'
/* ----------------- Start Copy Here ---------------- */ /* ----------------- Start Copy Here ---------------- */
@ -22,7 +23,6 @@ export default class Text extends CodeShape<TextShape> {
isHidden: false, isHidden: false,
text: 'Text', text: 'Text',
scale: 1, scale: 1,
fontSize: FontSize.Medium,
...props, ...props,
style: { style: {
...defaultStyle, ...defaultStyle,
@ -30,4 +30,12 @@ export default class Text extends CodeShape<TextShape> {
}, },
}) })
} }
get scale(): number {
return this.shape.scale
}
set scale(scale: number) {
getShapeUtils(this.shape).setProperty(this.shape, 'scale', scale)
}
} }

View file

@ -1,6 +1,6 @@
import { uniqueId, isMobile } from 'utils' import { uniqueId, isMobile } from 'utils'
import vec from 'utils/vec' import vec from 'utils/vec'
import { TextShape, ShapeType, FontSize } from 'types' import { TextShape, ShapeType } from 'types'
import { import {
defaultStyle, defaultStyle,
getFontSize, getFontSize,
@ -53,7 +53,6 @@ const text = registerShapeUtils<TextShape>({
create(props) { create(props) {
return { return {
id: uniqueId(), id: uniqueId(),
type: ShapeType.Text, type: ShapeType.Text,
isGenerated: false, isGenerated: false,
name: 'Text', name: 'Text',
@ -67,7 +66,6 @@ const text = registerShapeUtils<TextShape>({
style: defaultStyle, style: defaultStyle,
text: '', text: '',
scale: 1, scale: 1,
fontSize: FontSize.Medium,
...props, ...props,
} }
}, },
@ -118,6 +116,7 @@ const text = registerShapeUtils<TextShape>({
fontSize={fontSize} fontSize={fontSize}
width={bounds.width} width={bounds.width}
height={bounds.height} height={bounds.height}
fill={styles.stroke}
dominantBaseline="hanging" dominantBaseline="hanging"
> >
{str} {str}

View file

@ -182,7 +182,6 @@ export interface TextShape extends BaseShape {
type: ShapeType.Text type: ShapeType.Text
text: string text: string
scale: number scale: number
fontSize: FontSize
} }
export interface GroupShape extends BaseShape { export interface GroupShape extends BaseShape {