Files
grafana/public/app/features/plugins/loader/cache.test.ts
Jack Westbrook 036c878843 Plugins: Improve frontend loader cache (#87488)
* do it

* set empty child version to parent version

* feat(plugins): use pluginId for loader cache keys

* feat(plugins): apply caching to all js and css files systemjs loads

* remove old code and add comment

* test(plugins): update systemjs hooks tests in line with better caching

* test(plugins): wip - comment out failing backend loader tests

* fix tests and improve comment

* Update public/app/features/plugins/loader/cache.test.ts

Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>

---------

Co-authored-by: Will Browne <will.browne@grafana.com>
Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>
2024-06-07 10:03:41 +02:00

65 lines
2.4 KiB
TypeScript

import { registerPluginInCache, invalidatePluginInCache, resolveWithCache, getPluginFromCache } from './cache';
jest.mock('./constants', () => ({
CACHE_INITIALISED_AT: 123456,
}));
describe('Cache Functions', () => {
describe('registerPluginInCache', () => {
it('should register a plugin in the cache', () => {
const plugin = { pluginId: 'plugin1', version: '1.0.0', isAngular: false };
registerPluginInCache(plugin);
expect(getPluginFromCache('plugin1')).toEqual(plugin);
});
it('should not register a plugin if it already exists in the cache', () => {
const pluginId = 'plugin2';
const plugin = { pluginId, version: '2.0.0' };
registerPluginInCache(plugin);
const plugin2 = { pluginId, version: '2.5.0' };
registerPluginInCache(plugin2);
expect(getPluginFromCache(pluginId)?.version).toBe('2.0.0');
});
});
describe('invalidatePluginInCache', () => {
it('should invalidate a plugin in the cache', () => {
const pluginId = 'plugin3';
const plugin = { pluginId, version: '3.0.0' };
registerPluginInCache(plugin);
invalidatePluginInCache(pluginId);
expect(getPluginFromCache(pluginId)).toBeUndefined();
});
it('should not throw an error if the plugin does not exist in the cache', () => {
expect(() => invalidatePluginInCache('nonExistentPlugin')).not.toThrow();
});
});
describe('resolveWithCache', () => {
it('should resolve URL with timestamp cache bust parameter if plugin is not available in the cache', () => {
const url = 'http://localhost:3000/public/plugins/plugin4/module.js';
expect(resolveWithCache(url)).toContain('_cache=123456');
});
it('should resolve URL with plugin version as cache bust parameter if available', () => {
const plugin = { pluginId: 'plugin5', version: '5.0.0' };
registerPluginInCache(plugin);
const url = 'http://localhost:3000/public/plugins/plugin5/module.js';
expect(resolveWithCache(url)).toContain('_cache=5.0.0');
});
});
describe('getPluginFromCache', () => {
it('should return plugin from cache if exists', () => {
const plugin = { pluginId: 'plugin6', version: '6.0.0' };
registerPluginInCache(plugin);
expect(getPluginFromCache('plugin6')).toEqual(plugin);
});
it('should return undefined if plugin does not exist in cache', () => {
expect(getPluginFromCache('nonExistentPlugin')).toBeUndefined();
});
});
});