pluginSettings.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { getBackendSrv } from '@grafana/runtime';
  2. import { getPluginSettings, clearPluginSettingsCache } from './pluginSettings';
  3. jest.mock('@grafana/runtime', () => ({
  4. getBackendSrv: jest.fn().mockReturnValue({
  5. get: jest.fn(),
  6. }),
  7. }));
  8. describe('PluginSettings', () => {
  9. beforeEach(() => {
  10. jest.clearAllMocks();
  11. clearPluginSettingsCache();
  12. });
  13. it('should fetch settings when cache is empty', async () => {
  14. // arrange
  15. const testPluginResponse = {
  16. name: 'TestPlugin',
  17. type: 'datasource',
  18. id: 'test-plugin',
  19. enabled: true,
  20. };
  21. getBackendSrv().get = jest.fn().mockResolvedValue(testPluginResponse);
  22. const getRequestSpy = jest.spyOn(getBackendSrv(), 'get');
  23. // act
  24. const response = await getPluginSettings('test');
  25. // assert
  26. expect(response).toEqual(testPluginResponse);
  27. expect(getRequestSpy).toHaveBeenCalledTimes(1);
  28. expect(getRequestSpy).toHaveBeenCalledWith('/api/plugins/test/settings');
  29. });
  30. it('should fetch settings from cache when it has a hit', async () => {
  31. // arrange
  32. const testPluginResponse = {
  33. name: 'TestPlugin',
  34. type: 'datasource',
  35. id: 'test-plugin',
  36. enabled: true,
  37. };
  38. getBackendSrv().get = jest.fn().mockResolvedValue(testPluginResponse);
  39. const getRequestSpy = jest.spyOn(getBackendSrv(), 'get');
  40. // act
  41. const response1 = await getPluginSettings('test');
  42. const response2 = await getPluginSettings('test');
  43. // assert
  44. expect(response1).toEqual(testPluginResponse);
  45. expect(response2).toEqual(testPluginResponse);
  46. expect(getRequestSpy).toHaveBeenCalledTimes(1);
  47. });
  48. it('should refetch from backend when cache is cleared', async () => {
  49. // arrange
  50. const testPluginResponse = {
  51. name: 'TestPlugin',
  52. type: 'datasource',
  53. id: 'test-plugin',
  54. enabled: true,
  55. };
  56. getBackendSrv().get = jest.fn().mockResolvedValue(testPluginResponse);
  57. const getRequestSpy = jest.spyOn(getBackendSrv(), 'get');
  58. // act
  59. const response1 = await getPluginSettings('test');
  60. await clearPluginSettingsCache('test');
  61. const response2 = await getPluginSettings('test');
  62. // assert
  63. expect(response1).toEqual(testPluginResponse);
  64. expect(response2).toEqual(testPluginResponse);
  65. expect(getRequestSpy).toHaveBeenCalledTimes(2);
  66. });
  67. it('should fetch from cache when it is cleared for another plugin setting', async () => {
  68. // arrange
  69. const testPluginResponse = {
  70. name: 'TestPlugin',
  71. type: 'datasource',
  72. id: 'test-plugin',
  73. enabled: true,
  74. };
  75. getBackendSrv().get = jest.fn().mockResolvedValue(testPluginResponse);
  76. const getRequestSpy = jest.spyOn(getBackendSrv(), 'get');
  77. // act
  78. const response1 = await getPluginSettings('test');
  79. await clearPluginSettingsCache('another-test');
  80. const response2 = await getPluginSettings('test');
  81. // assert
  82. expect(response1).toEqual(testPluginResponse);
  83. expect(response2).toEqual(testPluginResponse);
  84. expect(getRequestSpy).toHaveBeenCalledTimes(1);
  85. });
  86. it('should clear all cache when no plugin id is provided to the clear function', async () => {
  87. // arrange
  88. const testPluginResponse = {
  89. name: 'TestPlugin',
  90. type: 'datasource',
  91. id: 'test-plugin',
  92. enabled: true,
  93. };
  94. getBackendSrv().get = jest.fn().mockResolvedValue(testPluginResponse);
  95. const getRequestSpy = jest.spyOn(getBackendSrv(), 'get');
  96. // act
  97. const response1 = await getPluginSettings('test');
  98. await clearPluginSettingsCache();
  99. const response2 = await getPluginSettings('test');
  100. // assert
  101. expect(response1).toEqual(testPluginResponse);
  102. expect(response2).toEqual(testPluginResponse);
  103. expect(getRequestSpy).toHaveBeenCalledTimes(2);
  104. });
  105. });