helpers.test.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import { FuncDef, FuncDefs, FuncInstance } from '../gfunc';
  2. import { EditableParam } from './FunctionParamEditor';
  3. import { mapFuncDefsToSelectables, mapFuncInstanceToParams } from './helpers';
  4. function createFunctionInstance(funcDef: FuncDef, currentParams: string[]): FuncInstance {
  5. let funcInstance: FuncInstance = new FuncInstance(funcDef);
  6. funcInstance.params = currentParams;
  7. return funcInstance;
  8. }
  9. describe('Graphite components helpers', () => {
  10. it('converts function definitions to selectable options', function () {
  11. const functionDefs: FuncDefs = {
  12. functionA1: { name: 'functionA1', category: 'A', params: [], defaultParams: [] },
  13. functionB1: { name: 'functionB1', category: 'B', params: [], defaultParams: [] },
  14. functionA2: { name: 'functionA2', category: 'A', params: [], defaultParams: [] },
  15. functionB2: { name: 'functionB2', category: 'B', params: [], defaultParams: [] },
  16. };
  17. const options = mapFuncDefsToSelectables(functionDefs);
  18. expect(options).toMatchObject([
  19. {
  20. label: 'A',
  21. options: [
  22. { label: 'functionA1', value: 'functionA1' },
  23. { label: 'functionA2', value: 'functionA2' },
  24. ],
  25. },
  26. {
  27. label: 'B',
  28. options: [
  29. { label: 'functionB1', value: 'functionB1' },
  30. { label: 'functionB2', value: 'functionB2' },
  31. ],
  32. },
  33. ]);
  34. });
  35. describe('mapFuncInstanceToParams', () => {
  36. let funcDef: FuncDef;
  37. function assertFunctionInstance(definition: FuncDef, params: string[], expected: EditableParam[]): void {
  38. expect(mapFuncInstanceToParams(createFunctionInstance(definition, params))).toMatchObject(expected);
  39. }
  40. it('converts param options to selectable options', () => {
  41. const funcDef = {
  42. name: 'functionA1',
  43. category: 'A',
  44. params: [{ name: 'foo', type: 'any', optional: false, multiple: false, options: ['foo', 2] }],
  45. defaultParams: [],
  46. };
  47. assertFunctionInstance(
  48. funcDef,
  49. [],
  50. [
  51. {
  52. name: 'foo',
  53. multiple: false,
  54. optional: false,
  55. options: [
  56. { label: 'foo', value: 'foo' },
  57. { label: '2', value: '2' },
  58. ],
  59. value: '',
  60. },
  61. ]
  62. );
  63. });
  64. describe('when all parameters are required and no multiple values are allowed', () => {
  65. beforeEach(() => {
  66. funcDef = {
  67. name: 'allRequiredNoMultiple',
  68. category: 'A',
  69. params: [
  70. { name: 'a', type: 'any', optional: false, multiple: false },
  71. { name: 'b', type: 'any', optional: false, multiple: false },
  72. ],
  73. defaultParams: [],
  74. };
  75. });
  76. it('creates required params', () => {
  77. assertFunctionInstance(
  78. funcDef,
  79. [],
  80. [
  81. { name: 'a', multiple: false, optional: false, options: [], value: '' },
  82. { name: 'b', multiple: false, optional: false, options: [], value: '' },
  83. ]
  84. );
  85. });
  86. it('fills in provided parameters', () => {
  87. assertFunctionInstance(
  88. funcDef,
  89. ['a', 'b'],
  90. [
  91. { name: 'a', multiple: false, optional: false, options: [], value: 'a' },
  92. { name: 'b', multiple: false, optional: false, options: [], value: 'b' },
  93. ]
  94. );
  95. });
  96. });
  97. describe('when all parameters are required and multiple values are allowed', () => {
  98. beforeEach(() => {
  99. funcDef = {
  100. name: 'allRequiredWithMultiple',
  101. category: 'A',
  102. params: [
  103. { name: 'a', type: 'any', optional: false, multiple: false },
  104. { name: 'b', type: 'any', optional: false, multiple: true },
  105. ],
  106. defaultParams: [],
  107. };
  108. });
  109. it('does not add extra param to add multiple values if not all params are filled in', () => {
  110. assertFunctionInstance(
  111. funcDef,
  112. [],
  113. [
  114. { name: 'a', multiple: false, optional: false, options: [], value: '' },
  115. { name: 'b', multiple: true, optional: false, options: [], value: '' },
  116. ]
  117. );
  118. assertFunctionInstance(
  119. funcDef,
  120. ['a'],
  121. [
  122. { name: 'a', multiple: false, optional: false, options: [], value: 'a' },
  123. { name: 'b', multiple: true, optional: false, options: [], value: '' },
  124. ]
  125. );
  126. });
  127. it('marks additional params as optional (only first one is required)', () => {
  128. assertFunctionInstance(
  129. funcDef,
  130. ['a', 'b', 'b2'],
  131. [
  132. { name: 'a', multiple: false, optional: false, options: [], value: 'a' },
  133. { name: 'b', multiple: true, optional: false, options: [], value: 'b' },
  134. { name: 'b', multiple: true, optional: true, options: [], value: 'b2' },
  135. { name: 'b', multiple: true, optional: true, options: [], value: '' },
  136. ]
  137. );
  138. });
  139. it('adds an extra param to allo adding multiple values if all params are filled in', () => {
  140. assertFunctionInstance(
  141. funcDef,
  142. ['a', 'b'],
  143. [
  144. { name: 'a', multiple: false, optional: false, options: [], value: 'a' },
  145. { name: 'b', multiple: true, optional: false, options: [], value: 'b' },
  146. { name: 'b', multiple: true, optional: true, options: [], value: '' },
  147. ]
  148. );
  149. });
  150. });
  151. describe('when there are optional parameters but no multiple values are allowed', () => {
  152. beforeEach(() => {
  153. funcDef = {
  154. name: 'twoOptionalNoMultiple',
  155. category: 'A',
  156. params: [
  157. { name: 'a', type: 'any', optional: false, multiple: false },
  158. { name: 'b', type: 'any', optional: false, multiple: false },
  159. { name: 'c', type: 'any', optional: true, multiple: false },
  160. { name: 'd', type: 'any', optional: true, multiple: false },
  161. ],
  162. defaultParams: [],
  163. };
  164. });
  165. it('creates non-required parameters', () => {
  166. assertFunctionInstance(
  167. funcDef,
  168. [],
  169. [
  170. { name: 'a', multiple: false, optional: false, options: [], value: '' },
  171. { name: 'b', multiple: false, optional: false, options: [], value: '' },
  172. { name: 'c', multiple: false, optional: true, options: [], value: '' },
  173. { name: 'd', multiple: false, optional: true, options: [], value: '' },
  174. ]
  175. );
  176. });
  177. it('fills in provided parameters', () => {
  178. assertFunctionInstance(
  179. funcDef,
  180. ['a', 'b', 'c', 'd'],
  181. [
  182. { name: 'a', multiple: false, optional: false, options: [], value: 'a' },
  183. { name: 'b', multiple: false, optional: false, options: [], value: 'b' },
  184. { name: 'c', multiple: false, optional: true, options: [], value: 'c' },
  185. { name: 'd', multiple: false, optional: true, options: [], value: 'd' },
  186. ]
  187. );
  188. });
  189. });
  190. describe('when there are optional parameters and multiple values are allowed', () => {
  191. beforeEach(() => {
  192. funcDef = {
  193. name: 'twoOptionalWithMultiple',
  194. category: 'A',
  195. params: [
  196. { name: 'a', type: 'any', optional: false, multiple: false },
  197. { name: 'b', type: 'any', optional: false, multiple: false },
  198. { name: 'c', type: 'any', optional: true, multiple: false },
  199. { name: 'd', type: 'any', optional: true, multiple: true },
  200. ],
  201. defaultParams: [],
  202. };
  203. });
  204. it('does not add extra param to add multiple values if not all params are filled in', () => {
  205. assertFunctionInstance(
  206. funcDef,
  207. ['a', 'b', 'c'],
  208. [
  209. { name: 'a', multiple: false, optional: false, options: [], value: 'a' },
  210. { name: 'b', multiple: false, optional: false, options: [], value: 'b' },
  211. { name: 'c', multiple: false, optional: true, options: [], value: 'c' },
  212. { name: 'd', multiple: true, optional: true, options: [], value: '' },
  213. ]
  214. );
  215. });
  216. it('adds an extra param to add multiple values if all params are filled in', () => {
  217. assertFunctionInstance(
  218. funcDef,
  219. ['a', 'b', 'c', 'd'],
  220. [
  221. { name: 'a', multiple: false, optional: false, options: [], value: 'a' },
  222. { name: 'b', multiple: false, optional: false, options: [], value: 'b' },
  223. { name: 'c', multiple: false, optional: true, options: [], value: 'c' },
  224. { name: 'd', multiple: true, optional: true, options: [], value: 'd' },
  225. { name: 'd', multiple: true, optional: true, options: [], value: '' },
  226. ]
  227. );
  228. });
  229. });
  230. });
  231. });