pluginCacheBuster.test.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { invalidatePluginInCache, locateWithCache, registerPluginInCache } from '../pluginCacheBuster';
  2. import * as pluginSettings from '../pluginSettings';
  3. describe('PluginCacheBuster', () => {
  4. const now = 12345;
  5. it('should append plugin version as cache flag if plugin is registered in buster', () => {
  6. const slug = 'bubble-chart-1';
  7. const version = 'v1.0.0';
  8. const path = resolvePath(slug);
  9. const address = `http://localhost:3000/public/${path}.js`;
  10. registerPluginInCache({ path, version });
  11. const url = `${address}?_cache=${encodeURI(version)}`;
  12. expect(locateWithCache({ address }, now)).toBe(url);
  13. });
  14. it('should append Date.now as cache flag if plugin is not registered in buster', () => {
  15. const slug = 'bubble-chart-2';
  16. const address = `http://localhost:3000/public/${resolvePath(slug)}.js`;
  17. const url = `${address}?_cache=${encodeURI(String(now))}`;
  18. expect(locateWithCache({ address }, now)).toBe(url);
  19. });
  20. it('should append Date.now as cache flag if plugin is invalidated in buster', () => {
  21. const slug = 'bubble-chart-3';
  22. const version = 'v1.0.0';
  23. const path = resolvePath(slug);
  24. const address = `http://localhost:3000/public/${path}.js`;
  25. registerPluginInCache({ path, version });
  26. invalidatePluginInCache(slug);
  27. const url = `${address}?_cache=${encodeURI(String(now))}`;
  28. expect(locateWithCache({ address }, now)).toBe(url);
  29. });
  30. it('should also clear plugin settings cache', () => {
  31. const slug = 'bubble-chart-3';
  32. const version = 'v1.0.0';
  33. const path = resolvePath(slug);
  34. const clearPluginSettingsCacheSpy = jest.spyOn(pluginSettings, 'clearPluginSettingsCache');
  35. registerPluginInCache({ path, version });
  36. invalidatePluginInCache(slug);
  37. expect(clearPluginSettingsCacheSpy).toBeCalledTimes(1);
  38. expect(clearPluginSettingsCacheSpy).toBeCalledWith('bubble-chart-3');
  39. });
  40. });
  41. function resolvePath(slug: string): string {
  42. return `plugins/${slug}/module`;
  43. }