metricsChartPalette.spec.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import {getCachedChartPalette} from 'sentry/views/ddm/utils/metricsChartPalette';
  2. describe('getQuerySymbol', () => {
  3. it('should cache palettes', () => {
  4. const cache: Record<string, string>[] = [];
  5. const abcPalette = getCachedChartPalette(cache, ['a', 'b', 'c']);
  6. expect(cache.length).toBe(1);
  7. // As a, b is a subset of a, b, c, we should get the same palette
  8. const baPalette = getCachedChartPalette(cache, ['b', 'a']);
  9. expect(cache.length).toBe(1);
  10. expect(baPalette).toBe(abcPalette);
  11. // As a, b, z is not a subset of a, b, c we should get a new palette
  12. const azbPalette = getCachedChartPalette(cache, ['a', 'z', 'b']);
  13. expect(cache.length).toBe(2);
  14. // a will still be the same as it is the first entry in both arrays
  15. expect(azbPalette).not.toBe(abcPalette);
  16. });
  17. it('should not cache single series palettes', () => {
  18. const cache: Record<string, string>[] = [];
  19. const aPalette = getCachedChartPalette(cache, ['a']);
  20. expect(cache.length).toBe(0);
  21. const bPalette = getCachedChartPalette(cache, ['b']);
  22. expect(cache.length).toBe(0);
  23. expect(aPalette).not.toBe(bPalette);
  24. });
  25. it('should not cache more than CACHE_SIZE (20) palettes', () => {
  26. const cache: Record<string, string>[] = Array.from({length: 20}).map(() => ({
  27. z: '#123123',
  28. }));
  29. getCachedChartPalette(cache, ['a', 'b', 'c']);
  30. expect(cache.length).toBe(20);
  31. // Ensure it removes more than 1 cache entry
  32. const cache2: Record<string, string>[] = Array.from({length: 100}).map(() => ({
  33. z: '#123123',
  34. }));
  35. getCachedChartPalette(cache2, ['a', 'b', 'c']);
  36. expect(cache2.length).toBe(20);
  37. });
  38. });