123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- import { getBackendSrv } from '@grafana/runtime';
- import { getPluginSettings, clearPluginSettingsCache } from './pluginSettings';
- jest.mock('@grafana/runtime', () => ({
- getBackendSrv: jest.fn().mockReturnValue({
- get: jest.fn(),
- }),
- }));
- describe('PluginSettings', () => {
- beforeEach(() => {
- jest.clearAllMocks();
- clearPluginSettingsCache();
- });
- it('should fetch settings when cache is empty', async () => {
- // arrange
- const testPluginResponse = {
- name: 'TestPlugin',
- type: 'datasource',
- id: 'test-plugin',
- enabled: true,
- };
- getBackendSrv().get = jest.fn().mockResolvedValue(testPluginResponse);
- const getRequestSpy = jest.spyOn(getBackendSrv(), 'get');
- // act
- const response = await getPluginSettings('test');
- // assert
- expect(response).toEqual(testPluginResponse);
- expect(getRequestSpy).toHaveBeenCalledTimes(1);
- expect(getRequestSpy).toHaveBeenCalledWith('/api/plugins/test/settings');
- });
- it('should fetch settings from cache when it has a hit', async () => {
- // arrange
- const testPluginResponse = {
- name: 'TestPlugin',
- type: 'datasource',
- id: 'test-plugin',
- enabled: true,
- };
- getBackendSrv().get = jest.fn().mockResolvedValue(testPluginResponse);
- const getRequestSpy = jest.spyOn(getBackendSrv(), 'get');
- // act
- const response1 = await getPluginSettings('test');
- const response2 = await getPluginSettings('test');
- // assert
- expect(response1).toEqual(testPluginResponse);
- expect(response2).toEqual(testPluginResponse);
- expect(getRequestSpy).toHaveBeenCalledTimes(1);
- });
- it('should refetch from backend when cache is cleared', async () => {
- // arrange
- const testPluginResponse = {
- name: 'TestPlugin',
- type: 'datasource',
- id: 'test-plugin',
- enabled: true,
- };
- getBackendSrv().get = jest.fn().mockResolvedValue(testPluginResponse);
- const getRequestSpy = jest.spyOn(getBackendSrv(), 'get');
- // act
- const response1 = await getPluginSettings('test');
- await clearPluginSettingsCache('test');
- const response2 = await getPluginSettings('test');
- // assert
- expect(response1).toEqual(testPluginResponse);
- expect(response2).toEqual(testPluginResponse);
- expect(getRequestSpy).toHaveBeenCalledTimes(2);
- });
- it('should fetch from cache when it is cleared for another plugin setting', async () => {
- // arrange
- const testPluginResponse = {
- name: 'TestPlugin',
- type: 'datasource',
- id: 'test-plugin',
- enabled: true,
- };
- getBackendSrv().get = jest.fn().mockResolvedValue(testPluginResponse);
- const getRequestSpy = jest.spyOn(getBackendSrv(), 'get');
- // act
- const response1 = await getPluginSettings('test');
- await clearPluginSettingsCache('another-test');
- const response2 = await getPluginSettings('test');
- // assert
- expect(response1).toEqual(testPluginResponse);
- expect(response2).toEqual(testPluginResponse);
- expect(getRequestSpy).toHaveBeenCalledTimes(1);
- });
- it('should clear all cache when no plugin id is provided to the clear function', async () => {
- // arrange
- const testPluginResponse = {
- name: 'TestPlugin',
- type: 'datasource',
- id: 'test-plugin',
- enabled: true,
- };
- getBackendSrv().get = jest.fn().mockResolvedValue(testPluginResponse);
- const getRequestSpy = jest.spyOn(getBackendSrv(), 'get');
- // act
- const response1 = await getPluginSettings('test');
- await clearPluginSettingsCache();
- const response2 = await getPluginSettings('test');
- // assert
- expect(response1).toEqual(testPluginResponse);
- expect(response2).toEqual(testPluginResponse);
- expect(getRequestSpy).toHaveBeenCalledTimes(2);
- });
- });
|