Browse Source

Adds more tests for code

main
Steve Ruiz 3 years ago
parent
commit
75e60d5eb2

+ 70
- 1
__tests__/__snapshots__/code.test.ts.snap View File

1
 // Jest Snapshot v1, https://goo.gl/fbAQLP
1
 // Jest Snapshot v1, https://goo.gl/fbAQLP
2
 
2
 
3
-exports[`selection creates a code control: generated code controls from code 1`] = `Object {}`;
3
+exports[`selection creates a code control: generated code controls from code 1`] = `
4
+Object {
5
+  "test-number-control": Object {
6
+    "id": "test-number-control",
7
+    "label": "x",
8
+    "step": 1,
9
+    "type": "number",
10
+    "value": 0,
11
+  },
12
+}
13
+`;
4
 
14
 
5
 exports[`selection generates a draw shape: generated draw from code 1`] = `
15
 exports[`selection generates a draw shape: generated draw from code 1`] = `
6
 Array [
16
 Array [
75
 ]
85
 ]
76
 `;
86
 `;
77
 
87
 
88
+exports[`selection generates a text shape: generated draw from code 1`] = `
89
+Array [
90
+  Object {
91
+    "childIndex": 1,
92
+    "id": "test-text",
93
+    "isAspectRatioLocked": false,
94
+    "isGenerated": true,
95
+    "isHidden": false,
96
+    "isLocked": false,
97
+    "name": "Test text",
98
+    "parentId": "page1",
99
+    "point": Array [
100
+      100,
101
+      100,
102
+    ],
103
+    "rotation": 0,
104
+    "scale": 1,
105
+    "style": Object {
106
+      "color": "Red",
107
+      "dash": "Dotted",
108
+      "isFilled": false,
109
+      "size": "Large",
110
+    },
111
+    "text": "Hello world!",
112
+    "type": "text",
113
+  },
114
+]
115
+`;
116
+
78
 exports[`selection generates an arrow shape: generated draw from code 1`] = `
117
 exports[`selection generates an arrow shape: generated draw from code 1`] = `
79
 Array [
118
 Array [
80
   Object {
119
   Object {
230
   },
269
   },
231
 }
270
 }
232
 `;
271
 `;
272
+
273
+exports[`selection updates a code control: rectangle in state after changing code control 1`] = `
274
+Object {
275
+  "childIndex": 1,
276
+  "id": "test-rectangle",
277
+  "isAspectRatioLocked": false,
278
+  "isGenerated": true,
279
+  "isHidden": false,
280
+  "isLocked": false,
281
+  "name": "Test Rectangle",
282
+  "parentId": "page1",
283
+  "point": Array [
284
+    0,
285
+    100,
286
+  ],
287
+  "radius": 2,
288
+  "rotation": 0,
289
+  "size": Array [
290
+    0,
291
+    0,
292
+  ],
293
+  "style": Object {
294
+    "color": "Red",
295
+    "dash": "Dotted",
296
+    "isFilled": false,
297
+    "size": "Medium",
298
+  },
299
+  "type": "rectangle",
300
+}
301
+`;

+ 44
- 24
__tests__/code.test.ts View File

1
 import state from 'state'
1
 import state from 'state'
2
 import { generateFromCode } from 'state/code/generate'
2
 import { generateFromCode } from 'state/code/generate'
3
-import { getShapes } from 'utils'
3
+import { getShape, getShapes } from 'utils'
4
 import * as json from './__mocks__/document.json'
4
 import * as json from './__mocks__/document.json'
5
 
5
 
6
 jest.useRealTimers()
6
 jest.useRealTimers()
56
 
56
 
57
   it('creates a code control', async () => {
57
   it('creates a code control', async () => {
58
     const code = `
58
     const code = `
59
-    const rectangle = new Rectangle({
60
-      id: "test-rectangle",
61
-      name: 'Test Rectangle',
62
-      point: [100, 100],
63
-      size: [200, 200],
64
-      style: {
65
-        size: SizeStyle.Medium,
66
-        color: ColorStyle.Red,
67
-        dash: DashStyle.Dotted,
68
-      },
59
+    new NumberControl({
60
+      id: "test-number-control",
61
+      label: "x"
69
     })
62
     })
70
     `
63
     `
71
 
64
 
80
 
73
 
81
   it('updates a code control', async () => {
74
   it('updates a code control', async () => {
82
     const code = `
75
     const code = `
83
-    const rectangle = new Rectangle({
84
-      id: "test-rectangle",
85
-      name: 'Test Rectangle',
86
-      point: [100, 100],
87
-      size: [200, 200],
88
-      style: {
89
-        size: SizeStyle.Medium,
90
-        color: ColorStyle.Red,
91
-        dash: DashStyle.Dotted,
92
-      },
93
-    })
94
-
95
     new NumberControl({
76
     new NumberControl({
96
       id: "test-number-control",
77
       id: "test-number-control",
97
       label: "x"
78
       label: "x"
101
       id: "test-vector-control",
82
       id: "test-vector-control",
102
       label: "size"
83
       label: "size"
103
     })
84
     })
85
+
86
+    const rectangle = new Rectangle({
87
+      id: "test-rectangle",
88
+      name: 'Test Rectangle',
89
+      point: [controls.x, 100],
90
+      size: controls.size,
91
+      style: {
92
+        size: SizeStyle.Medium,
93
+        color: ColorStyle.Red,
94
+        dash: DashStyle.Dotted,
95
+      },
96
+    })
104
     `
97
     `
105
 
98
 
106
     const { controls, shapes } = await generateFromCode(state.data, code)
99
     const { controls, shapes } = await generateFromCode(state.data, code)
112
     expect(state.data.codeControls).toMatchSnapshot(
105
     expect(state.data.codeControls).toMatchSnapshot(
113
       'data in state after changing control'
106
       'data in state after changing control'
114
     )
107
     )
108
+
109
+    expect(getShape(state.data, 'test-rectangle')).toMatchSnapshot(
110
+      'rectangle in state after changing code control'
111
+    )
115
   })
112
   })
116
 
113
 
117
   /* -------------------- Readonly -------------------- */
114
   /* -------------------- Readonly -------------------- */
251
   it('generates an arrow shape', async () => {
248
   it('generates an arrow shape', async () => {
252
     state.send('CLEARED_PAGE')
249
     state.send('CLEARED_PAGE')
253
     const code = `
250
     const code = `
254
-    const ellipse = new Arrow({
251
+    const draw = new Arrow({
255
       id: 'test-draw',
252
       id: 'test-draw',
256
       name: 'Test draw',
253
       name: 'Test draw',
257
       points: [[100, 100], [200,200], [300,300]],
254
       points: [[100, 100], [200,200], [300,300]],
269
 
266
 
270
     expect(getShapes(state.data)).toMatchSnapshot('generated draw from code')
267
     expect(getShapes(state.data)).toMatchSnapshot('generated draw from code')
271
   })
268
   })
269
+
270
+  it('generates a text shape', async () => {
271
+    state.send('CLEARED_PAGE')
272
+    const code = `
273
+    const text = new Text({
274
+      id: 'test-text',
275
+      name: 'Test text',
276
+      point: [100, 100],
277
+      text: 'Hello world!',
278
+      style: {
279
+        size: SizeStyle.Large,
280
+        color: ColorStyle.Red,
281
+        dash: DashStyle.Dotted,
282
+      },
283
+    })
284
+    `
285
+
286
+    const { controls, shapes } = await generateFromCode(state.data, code)
287
+
288
+    state.send('GENERATED_FROM_CODE', { controls, shapes })
289
+
290
+    expect(getShapes(state.data)).toMatchSnapshot('generated draw from code')
291
+  })
272
 })
292
 })

+ 2
- 0
state/code/generate.ts View File

17
   Shape,
17
   Shape,
18
   DashStyle,
18
   DashStyle,
19
   ColorStyle,
19
   ColorStyle,
20
+  FontSize,
20
   SizeStyle,
21
   SizeStyle,
21
 } from 'types'
22
 } from 'types'
22
 import { getPage, getShapes } from 'utils'
23
 import { getPage, getShapes } from 'utils'
39
   DashStyle,
40
   DashStyle,
40
   ColorStyle,
41
   ColorStyle,
41
   SizeStyle,
42
   SizeStyle,
43
+  FontSize,
42
 }
44
 }
43
 
45
 
44
 /**
46
 /**

+ 10
- 2
state/code/text.ts View File

1
 import CodeShape from './index'
1
 import CodeShape from './index'
2
 import { uniqueId } from 'utils'
2
 import { uniqueId } from 'utils'
3
-import { TextShape, ShapeProps, ShapeType, FontSize } from 'types'
3
+import { TextShape, ShapeProps, ShapeType } from 'types'
4
 import { defaultStyle } from 'state/shape-styles'
4
 import { defaultStyle } from 'state/shape-styles'
5
+import { getShapeUtils } from 'state/shape-utils'
5
 
6
 
6
 /* ----------------- Start Copy Here ---------------- */
7
 /* ----------------- Start Copy Here ---------------- */
7
 
8
 
22
       isHidden: false,
23
       isHidden: false,
23
       text: 'Text',
24
       text: 'Text',
24
       scale: 1,
25
       scale: 1,
25
-      fontSize: FontSize.Medium,
26
       ...props,
26
       ...props,
27
       style: {
27
       style: {
28
         ...defaultStyle,
28
         ...defaultStyle,
30
       },
30
       },
31
     })
31
     })
32
   }
32
   }
33
+
34
+  get scale(): number {
35
+    return this.shape.scale
36
+  }
37
+
38
+  set scale(scale: number) {
39
+    getShapeUtils(this.shape).setProperty(this.shape, 'scale', scale)
40
+  }
33
 }
41
 }

+ 2
- 3
state/shape-utils/text.tsx View File

1
 import { uniqueId, isMobile } from 'utils'
1
 import { uniqueId, isMobile } from 'utils'
2
 import vec from 'utils/vec'
2
 import vec from 'utils/vec'
3
-import { TextShape, ShapeType, FontSize } from 'types'
3
+import { TextShape, ShapeType } from 'types'
4
 import {
4
 import {
5
   defaultStyle,
5
   defaultStyle,
6
   getFontSize,
6
   getFontSize,
53
   create(props) {
53
   create(props) {
54
     return {
54
     return {
55
       id: uniqueId(),
55
       id: uniqueId(),
56
-
57
       type: ShapeType.Text,
56
       type: ShapeType.Text,
58
       isGenerated: false,
57
       isGenerated: false,
59
       name: 'Text',
58
       name: 'Text',
67
       style: defaultStyle,
66
       style: defaultStyle,
68
       text: '',
67
       text: '',
69
       scale: 1,
68
       scale: 1,
70
-      fontSize: FontSize.Medium,
71
       ...props,
69
       ...props,
72
     }
70
     }
73
   },
71
   },
118
               fontSize={fontSize}
116
               fontSize={fontSize}
119
               width={bounds.width}
117
               width={bounds.width}
120
               height={bounds.height}
118
               height={bounds.height}
119
+              fill={styles.stroke}
121
               dominantBaseline="hanging"
120
               dominantBaseline="hanging"
122
             >
121
             >
123
               {str}
122
               {str}

+ 0
- 1
types.ts View File

182
   type: ShapeType.Text
182
   type: ShapeType.Text
183
   text: string
183
   text: string
184
   scale: number
184
   scale: number
185
-  fontSize: FontSize
186
 }
185
 }
187
 
186
 
188
 export interface GroupShape extends BaseShape {
187
 export interface GroupShape extends BaseShape {

Loading…
Cancel
Save