Преглед изворни кода

Adds more tests for code

main
Steve Ruiz пре 3 година
родитељ
комит
75e60d5eb2
6 измењених фајлова са 128 додато и 31 уклоњено
  1. 70
    1
      __tests__/__snapshots__/code.test.ts.snap
  2. 44
    24
      __tests__/code.test.ts
  3. 2
    0
      state/code/generate.ts
  4. 10
    2
      state/code/text.ts
  5. 2
    3
      state/shape-utils/text.tsx
  6. 0
    1
      types.ts

+ 70
- 1
__tests__/__snapshots__/code.test.ts.snap Прегледај датотеку

@@ -1,6 +1,16 @@
1 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 15
 exports[`selection generates a draw shape: generated draw from code 1`] = `
6 16
 Array [
@@ -75,6 +85,35 @@ 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 117
 exports[`selection generates an arrow shape: generated draw from code 1`] = `
79 118
 Array [
80 119
   Object {
@@ -230,3 +269,33 @@ 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 Прегледај датотеку

@@ -1,6 +1,6 @@
1 1
 import state from 'state'
2 2
 import { generateFromCode } from 'state/code/generate'
3
-import { getShapes } from 'utils'
3
+import { getShape, getShapes } from 'utils'
4 4
 import * as json from './__mocks__/document.json'
5 5
 
6 6
 jest.useRealTimers()
@@ -56,16 +56,9 @@ describe('selection', () => {
56 56
 
57 57
   it('creates a code control', async () => {
58 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,18 +73,6 @@ describe('selection', () => {
80 73
 
81 74
   it('updates a code control', async () => {
82 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 76
     new NumberControl({
96 77
       id: "test-number-control",
97 78
       label: "x"
@@ -101,6 +82,18 @@ describe('selection', () => {
101 82
       id: "test-vector-control",
102 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 99
     const { controls, shapes } = await generateFromCode(state.data, code)
@@ -112,6 +105,10 @@ describe('selection', () => {
112 105
     expect(state.data.codeControls).toMatchSnapshot(
113 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 114
   /* -------------------- Readonly -------------------- */
@@ -251,7 +248,7 @@ describe('selection', () => {
251 248
   it('generates an arrow shape', async () => {
252 249
     state.send('CLEARED_PAGE')
253 250
     const code = `
254
-    const ellipse = new Arrow({
251
+    const draw = new Arrow({
255 252
       id: 'test-draw',
256 253
       name: 'Test draw',
257 254
       points: [[100, 100], [200,200], [300,300]],
@@ -269,4 +266,27 @@ describe('selection', () => {
269 266
 
270 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 Прегледај датотеку

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

+ 10
- 2
state/code/text.ts Прегледај датотеку

@@ -1,7 +1,8 @@
1 1
 import CodeShape from './index'
2 2
 import { uniqueId } from 'utils'
3
-import { TextShape, ShapeProps, ShapeType, FontSize } from 'types'
3
+import { TextShape, ShapeProps, ShapeType } from 'types'
4 4
 import { defaultStyle } from 'state/shape-styles'
5
+import { getShapeUtils } from 'state/shape-utils'
5 6
 
6 7
 /* ----------------- Start Copy Here ---------------- */
7 8
 
@@ -22,7 +23,6 @@ export default class Text extends CodeShape<TextShape> {
22 23
       isHidden: false,
23 24
       text: 'Text',
24 25
       scale: 1,
25
-      fontSize: FontSize.Medium,
26 26
       ...props,
27 27
       style: {
28 28
         ...defaultStyle,
@@ -30,4 +30,12 @@ export default class Text extends CodeShape<TextShape> {
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 Прегледај датотеку

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

+ 0
- 1
types.ts Прегледај датотеку

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

Loading…
Откажи
Сачувај