exporter.jest.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. jest.mock('app/core/store', () => {
  2. return {
  3. getBool: jest.fn(),
  4. };
  5. });
  6. import _ from 'lodash';
  7. import config from 'app/core/config';
  8. import { DashboardExporter } from '../export/exporter';
  9. import { DashboardModel } from '../dashboard_model';
  10. describe('given dashboard with repeated panels', () => {
  11. var dash, exported;
  12. beforeEach(done => {
  13. dash = {
  14. templating: {
  15. list: [
  16. {
  17. name: 'apps',
  18. type: 'query',
  19. datasource: 'gfdb',
  20. current: { value: 'Asd', text: 'Asd' },
  21. options: [{ value: 'Asd', text: 'Asd' }],
  22. },
  23. {
  24. name: 'prefix',
  25. type: 'constant',
  26. current: { value: 'collectd', text: 'collectd' },
  27. options: [],
  28. },
  29. {
  30. name: 'ds',
  31. type: 'datasource',
  32. query: 'testdb',
  33. current: { value: 'prod', text: 'prod' },
  34. options: [],
  35. },
  36. ],
  37. },
  38. annotations: {
  39. list: [
  40. {
  41. name: 'logs',
  42. datasource: 'gfdb',
  43. },
  44. ],
  45. },
  46. panels: [
  47. { id: 6, datasource: 'gfdb', type: 'graph' },
  48. { id: 7 },
  49. {
  50. id: 8,
  51. datasource: '-- Mixed --',
  52. targets: [{ datasource: 'other' }],
  53. },
  54. { id: 9, datasource: '$ds' },
  55. {
  56. id: 2,
  57. repeat: 'apps',
  58. datasource: 'gfdb',
  59. type: 'graph',
  60. },
  61. { id: 3, repeat: null, repeatPanelId: 2 },
  62. ],
  63. };
  64. config.buildInfo = {
  65. version: '3.0.2',
  66. };
  67. //Stubs test function calls
  68. var datasourceSrvStub = { get: jest.fn(arg => getStub(arg)) };
  69. config.panels['graph'] = {
  70. id: 'graph',
  71. name: 'Graph',
  72. info: { version: '1.1.0' },
  73. };
  74. dash = new DashboardModel(dash, {});
  75. var exporter = new DashboardExporter(datasourceSrvStub);
  76. exporter.makeExportable(dash).then(clean => {
  77. exported = clean;
  78. done();
  79. });
  80. });
  81. it('should replace datasource refs', () => {
  82. var panel = exported.panels[0];
  83. expect(panel.datasource).toBe('${DS_GFDB}');
  84. });
  85. it('should replace datasource in variable query', () => {
  86. expect(exported.templating.list[0].datasource).toBe('${DS_GFDB}');
  87. expect(exported.templating.list[0].options.length).toBe(0);
  88. expect(exported.templating.list[0].current.value).toBe(undefined);
  89. expect(exported.templating.list[0].current.text).toBe(undefined);
  90. });
  91. it('should replace datasource in annotation query', () => {
  92. expect(exported.annotations.list[1].datasource).toBe('${DS_GFDB}');
  93. });
  94. it('should add datasource as input', () => {
  95. expect(exported.__inputs[0].name).toBe('DS_GFDB');
  96. expect(exported.__inputs[0].pluginId).toBe('testdb');
  97. expect(exported.__inputs[0].type).toBe('datasource');
  98. });
  99. it('should add datasource to required', () => {
  100. var require = _.find(exported.__requires, { name: 'TestDB' });
  101. expect(require.name).toBe('TestDB');
  102. expect(require.id).toBe('testdb');
  103. expect(require.type).toBe('datasource');
  104. expect(require.version).toBe('1.2.1');
  105. });
  106. it('should not add built in datasources to required', () => {
  107. var require = _.find(exported.__requires, { name: 'Mixed' });
  108. expect(require).toBe(undefined);
  109. });
  110. it('should add datasources used in mixed mode', () => {
  111. var require = _.find(exported.__requires, { name: 'OtherDB' });
  112. expect(require).not.toBe(undefined);
  113. });
  114. it('should add panel to required', () => {
  115. var require = _.find(exported.__requires, { name: 'Graph' });
  116. expect(require.name).toBe('Graph');
  117. expect(require.id).toBe('graph');
  118. expect(require.version).toBe('1.1.0');
  119. });
  120. it('should add grafana version', () => {
  121. var require = _.find(exported.__requires, { name: 'Grafana' });
  122. expect(require.type).toBe('grafana');
  123. expect(require.id).toBe('grafana');
  124. expect(require.version).toBe('3.0.2');
  125. });
  126. it('should add constant template variables as inputs', () => {
  127. var input = _.find(exported.__inputs, { name: 'VAR_PREFIX' });
  128. expect(input.type).toBe('constant');
  129. expect(input.label).toBe('prefix');
  130. expect(input.value).toBe('collectd');
  131. });
  132. it('should templatize constant variables', () => {
  133. var variable = _.find(exported.templating.list, { name: 'prefix' });
  134. expect(variable.query).toBe('${VAR_PREFIX}');
  135. expect(variable.current.text).toBe('${VAR_PREFIX}');
  136. expect(variable.current.value).toBe('${VAR_PREFIX}');
  137. expect(variable.options[0].text).toBe('${VAR_PREFIX}');
  138. expect(variable.options[0].value).toBe('${VAR_PREFIX}');
  139. });
  140. });
  141. // Stub responses
  142. var stubs = [];
  143. stubs['gfdb'] = {
  144. name: 'gfdb',
  145. meta: { id: 'testdb', info: { version: '1.2.1' }, name: 'TestDB' },
  146. };
  147. stubs['other'] = {
  148. name: 'other',
  149. meta: { id: 'other', info: { version: '1.2.1' }, name: 'OtherDB' },
  150. };
  151. stubs['-- Mixed --'] = {
  152. name: 'mixed',
  153. meta: {
  154. id: 'mixed',
  155. info: { version: '1.2.1' },
  156. name: 'Mixed',
  157. builtIn: true,
  158. },
  159. };
  160. stubs['-- Grafana --'] = {
  161. name: '-- Grafana --',
  162. meta: {
  163. id: 'grafana',
  164. info: { version: '1.2.1' },
  165. name: 'grafana',
  166. builtIn: true,
  167. },
  168. };
  169. function getStub(arg) {
  170. return Promise.resolve(stubs[arg]);
  171. }