variable.jest.ts 2.0 KB

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