Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

code.test.ts 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import state from 'state'
  2. import { generateFromCode } from 'state/code/generate'
  3. import { getShape, getShapes } from 'utils'
  4. import * as json from './__mocks__/document.json'
  5. jest.useRealTimers()
  6. state.reset()
  7. state.send('MOUNTED').send('LOADED_FROM_FILE', { json: JSON.stringify(json) })
  8. state.send('CLEARED_PAGE')
  9. describe('selection', () => {
  10. it('opens and closes the code panel', () => {
  11. expect(state.data.settings.isCodeOpen).toBe(false)
  12. state.send('TOGGLED_CODE_PANEL_OPEN')
  13. expect(state.data.settings.isCodeOpen).toBe(true)
  14. state.send('TOGGLED_CODE_PANEL_OPEN')
  15. expect(state.data.settings.isCodeOpen).toBe(false)
  16. })
  17. it('saves changes to code', () => {
  18. expect(getShapes(state.data).length).toBe(0)
  19. const code = `// hello world!`
  20. state.send('SAVED_CODE', { code })
  21. expect(state.data.document.code[state.data.currentCodeFileId].code).toBe(
  22. code
  23. )
  24. })
  25. it('generates shapes', async () => {
  26. const code = `
  27. const rectangle = new Rectangle({
  28. id: "test-rectangle",
  29. name: 'Test Rectangle',
  30. point: [100, 100],
  31. size: [200, 200],
  32. style: {
  33. size: SizeStyle.Medium,
  34. color: ColorStyle.Red,
  35. dash: DashStyle.Dotted,
  36. },
  37. })
  38. `
  39. const { controls, shapes } = await generateFromCode(state.data, code)
  40. state.send('GENERATED_FROM_CODE', { controls, shapes })
  41. expect(getShapes(state.data)).toMatchSnapshot(
  42. 'generated rectangle from code'
  43. )
  44. })
  45. it('creates a code control', async () => {
  46. const code = `
  47. new NumberControl({
  48. id: "test-number-control",
  49. label: "x"
  50. })
  51. `
  52. const { controls, shapes } = await generateFromCode(state.data, code)
  53. state.send('GENERATED_FROM_CODE', { controls, shapes })
  54. expect(state.data.codeControls).toMatchSnapshot(
  55. 'generated code controls from code'
  56. )
  57. })
  58. it('updates a code control', async () => {
  59. const code = `
  60. new NumberControl({
  61. id: "test-number-control",
  62. label: "x"
  63. })
  64. new VectorControl({
  65. id: "test-vector-control",
  66. label: "size"
  67. })
  68. const rectangle = new Rectangle({
  69. id: "test-rectangle",
  70. name: 'Test Rectangle',
  71. point: [controls.x, 100],
  72. size: controls.size,
  73. style: {
  74. size: SizeStyle.Medium,
  75. color: ColorStyle.Red,
  76. dash: DashStyle.Dotted,
  77. },
  78. })
  79. `
  80. const { controls, shapes } = await generateFromCode(state.data, code)
  81. state.send('GENERATED_FROM_CODE', { controls, shapes })
  82. state.send('CHANGED_CODE_CONTROL', { 'test-number-control': 100 })
  83. expect(state.data.codeControls).toMatchSnapshot(
  84. 'data in state after changing control'
  85. )
  86. expect(getShape(state.data, 'test-rectangle')).toMatchSnapshot(
  87. 'rectangle in state after changing code control'
  88. )
  89. })
  90. /* -------------------- Readonly -------------------- */
  91. it('does not saves changes to code when readonly', () => {
  92. state.send('CLEARED_PAGE')
  93. expect(getShapes(state.data).length).toBe(0)
  94. const code = `// hello world!`
  95. state
  96. .send('SAVED_CODE', { code })
  97. .send('TOGGLED_READ_ONLY')
  98. .send('SAVED_CODE', { code: '' })
  99. expect(state.data.document.code[state.data.currentCodeFileId].code).toBe(
  100. code
  101. )
  102. state.send('TOGGLED_READ_ONLY').send('SAVED_CODE', { code: '' })
  103. expect(state.data.document.code[state.data.currentCodeFileId].code).toBe('')
  104. })
  105. /* --------------------- Methods -------------------- */
  106. it('moves shape to front', async () => {
  107. null
  108. })
  109. it('moves shape forward', async () => {
  110. null
  111. })
  112. it('moves shape backward', async () => {
  113. null
  114. })
  115. it('moves shape to back', async () => {
  116. null
  117. })
  118. it('rotates a shape', async () => {
  119. null
  120. })
  121. it('rotates a shape by a delta', async () => {
  122. null
  123. })
  124. it('translates a shape', async () => {
  125. null
  126. })
  127. it('translates a shape by a delta', async () => {
  128. null
  129. })
  130. /* --------------------- Shapes --------------------- */
  131. it('generates a rectangle shape', async () => {
  132. state.send('CLEARED_PAGE')
  133. const code = `
  134. const rectangle = new Rectangle({
  135. id: "test-rectangle",
  136. name: 'Test Rectangle',
  137. point: [100, 100],
  138. size: [200, 200],
  139. style: {
  140. size: SizeStyle.Medium,
  141. color: ColorStyle.Red,
  142. dash: DashStyle.Dotted,
  143. },
  144. })
  145. `
  146. const { controls, shapes } = await generateFromCode(state.data, code)
  147. state.send('GENERATED_FROM_CODE', { controls, shapes })
  148. expect(getShapes(state.data)).toMatchSnapshot(
  149. 'generated rectangle from code'
  150. )
  151. })
  152. it('changes a rectangle size', async () => {
  153. null
  154. })
  155. it('generates an ellipse shape', async () => {
  156. state.send('CLEARED_PAGE')
  157. const code = `
  158. const ellipse = new Ellipse({
  159. id: 'test-ellipse',
  160. name: 'Test ellipse',
  161. point: [100, 100],
  162. radiusX: 100,
  163. radiusY: 200,
  164. style: {
  165. size: SizeStyle.Medium,
  166. color: ColorStyle.Red,
  167. dash: DashStyle.Dotted,
  168. },
  169. })
  170. `
  171. const { controls, shapes } = await generateFromCode(state.data, code)
  172. state.send('GENERATED_FROM_CODE', { controls, shapes })
  173. expect(getShapes(state.data)).toMatchSnapshot('generated ellipse from code')
  174. })
  175. it('generates a draw shape', async () => {
  176. state.send('CLEARED_PAGE')
  177. const code = `
  178. const ellipse = new Draw({
  179. id: 'test-draw',
  180. name: 'Test draw',
  181. points: [[100, 100], [200,200], [300,300]],
  182. style: {
  183. size: SizeStyle.Medium,
  184. color: ColorStyle.Red,
  185. dash: DashStyle.Dotted,
  186. },
  187. })
  188. `
  189. const { controls, shapes } = await generateFromCode(state.data, code)
  190. state.send('GENERATED_FROM_CODE', { controls, shapes })
  191. expect(getShapes(state.data)).toMatchSnapshot('generated draw from code')
  192. })
  193. it('generates an arrow shape', async () => {
  194. state.send('CLEARED_PAGE')
  195. const code = `
  196. const draw = new Arrow({
  197. id: 'test-draw',
  198. name: 'Test draw',
  199. points: [[100, 100], [200,200], [300,300]],
  200. style: {
  201. size: SizeStyle.Medium,
  202. color: ColorStyle.Red,
  203. dash: DashStyle.Dotted,
  204. },
  205. })
  206. `
  207. const { controls, shapes } = await generateFromCode(state.data, code)
  208. state.send('GENERATED_FROM_CODE', { controls, shapes })
  209. expect(getShapes(state.data)).toMatchSnapshot('generated draw from code')
  210. })
  211. it('generates a text shape', async () => {
  212. state.send('CLEARED_PAGE')
  213. const code = `
  214. const text = new Text({
  215. id: 'test-text',
  216. name: 'Test text',
  217. point: [100, 100],
  218. text: 'Hello world!',
  219. style: {
  220. size: SizeStyle.Large,
  221. color: ColorStyle.Red,
  222. dash: DashStyle.Dotted,
  223. },
  224. })
  225. `
  226. const { controls, shapes } = await generateFromCode(state.data, code)
  227. state.send('GENERATED_FROM_CODE', { controls, shapes })
  228. expect(getShapes(state.data)).toMatchSnapshot('generated draw from code')
  229. })
  230. })