variable.test.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { containsVariable, assignModelProperties } from '../variable';
  2. describe('containsVariable', () => {
  3. describe('when checking if a string contains a variable', () => {
  4. it('should find it with $const syntax', () => {
  5. const contains = containsVariable('this.$test.filters', 'test');
  6. expect(contains).toBe(true);
  7. });
  8. it('should not find it if only part matches with $const syntax', () => {
  9. const contains = containsVariable('this.$serverDomain.filters', 'server');
  10. expect(contains).toBe(false);
  11. });
  12. it('should find it if it ends with variable and passing multiple test strings', () => {
  13. const contains = containsVariable('show field keys from $pgmetric', 'test string2', 'pgmetric');
  14. expect(contains).toBe(true);
  15. });
  16. it('should find it with [[var]] syntax', () => {
  17. const contains = containsVariable('this.[[test]].filters', 'test');
  18. expect(contains).toBe(true);
  19. });
  20. it('should find it with [[var:option]] syntax', () => {
  21. const contains = containsVariable('this.[[test:csv]].filters', 'test');
  22. expect(contains).toBe(true);
  23. });
  24. it('should find it when part of segment', () => {
  25. const contains = containsVariable('metrics.$env.$group-*', 'group');
  26. expect(contains).toBe(true);
  27. });
  28. it('should find it its the only thing', () => {
  29. const contains = containsVariable('$env', 'env');
  30. expect(contains).toBe(true);
  31. });
  32. it('should be able to pass in multiple test strings', () => {
  33. const contains = containsVariable('asd', 'asd2.$env', 'env');
  34. expect(contains).toBe(true);
  35. });
  36. it('should find it with ${var} syntax', () => {
  37. const contains = containsVariable('this.${test}.filters', 'test');
  38. expect(contains).toBe(true);
  39. });
  40. it('should find it with ${var:option} syntax', () => {
  41. const contains = containsVariable('this.${test:csv}.filters', 'test');
  42. expect(contains).toBe(true);
  43. });
  44. });
  45. });
  46. describe('assignModelProperties', () => {
  47. it('only set properties defined in defaults', () => {
  48. const target: any = { test: 'asd' };
  49. assignModelProperties(target, { propA: 1, propB: 2 }, { propB: 0 });
  50. expect(target.propB).toBe(2);
  51. expect(target.test).toBe('asd');
  52. });
  53. it('use default value if not found on source', () => {
  54. const target: any = { test: 'asd' };
  55. assignModelProperties(target, { propA: 1, propB: 2 }, { propC: 10 });
  56. expect(target.propC).toBe(10);
  57. });
  58. });