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 6.7KB

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