You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

code.test.ts 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import state from 'state'
  2. import { generateFromCode } from 'state/code/generate'
  3. import * as json from './__mocks__/document.json'
  4. import tld from 'utils/tld'
  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(tld.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(tld.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(tld.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(tld.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(tld.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(tld.getShapes(state.data)).toMatchSnapshot(
  174. 'generated ellipse from code'
  175. )
  176. })
  177. it('generates a draw shape', async () => {
  178. state.send('CLEARED_PAGE')
  179. const code = `
  180. const ellipse = new Draw({
  181. id: 'test-draw',
  182. name: 'Test draw',
  183. points: [[100, 100], [200,200], [300,300]],
  184. style: {
  185. size: SizeStyle.Medium,
  186. color: ColorStyle.Red,
  187. dash: DashStyle.Dotted,
  188. },
  189. })
  190. `
  191. const { controls, shapes } = await generateFromCode(state.data, code)
  192. state.send('GENERATED_FROM_CODE', { controls, shapes })
  193. expect(tld.getShapes(state.data)).toMatchSnapshot(
  194. 'generated draw from code'
  195. )
  196. })
  197. it('generates an arrow shape', async () => {
  198. state.send('CLEARED_PAGE')
  199. const code = `
  200. const draw = new Arrow({
  201. id: 'test-draw',
  202. name: 'Test draw',
  203. points: [[100, 100], [200,200], [300,300]],
  204. style: {
  205. size: SizeStyle.Medium,
  206. color: ColorStyle.Red,
  207. dash: DashStyle.Dotted,
  208. },
  209. })
  210. `
  211. const { controls, shapes } = await generateFromCode(state.data, code)
  212. state.send('GENERATED_FROM_CODE', { controls, shapes })
  213. expect(tld.getShapes(state.data)).toMatchSnapshot(
  214. 'generated draw from code'
  215. )
  216. })
  217. it('generates a text shape', async () => {
  218. state.send('CLEARED_PAGE')
  219. const code = `
  220. const text = new Text({
  221. id: 'test-text',
  222. name: 'Test text',
  223. point: [100, 100],
  224. text: 'Hello world!',
  225. style: {
  226. size: SizeStyle.Large,
  227. color: ColorStyle.Red,
  228. dash: DashStyle.Dotted,
  229. },
  230. })
  231. `
  232. const { controls, shapes } = await generateFromCode(state.data, code)
  233. state.send('GENERATED_FROM_CODE', { controls, shapes })
  234. expect(tld.getShapes(state.data)).toMatchSnapshot(
  235. 'generated draw from code'
  236. )
  237. })
  238. })