github.com/thanos-io/thanos@v0.32.5/pkg/ui/react-app/src/pages/graph/GraphHelpers.test.ts (about)

     1  import { formatValue, parseValue, getOptions } from './GraphHelpers';
     2  import moment from 'moment-timezone';
     3  require('../../vendor/flot/jquery.flot'); // need for $.colors
     4  
     5  describe('GraphHelpers', () => {
     6    describe('formatValue', () => {
     7      it('formats tick values correctly', () => {
     8        [
     9          { input: null, output: 'null' },
    10          { input: 0, output: '0.00' },
    11          { input: 2e24, output: '2.00Y' },
    12          { input: 2e23, output: '200.00Z' },
    13          { input: 2e22, output: '20.00Z' },
    14          { input: 2e21, output: '2.00Z' },
    15          { input: 2e19, output: '20.00E' },
    16          { input: 2e18, output: '2.00E' },
    17          { input: 2e17, output: '200.00P' },
    18          { input: 2e16, output: '20.00P' },
    19          { input: 2e15, output: '2.00P' },
    20          { input: 1e15, output: '1.00P' },
    21          { input: 2e14, output: '200.00T' },
    22          { input: 2e13, output: '20.00T' },
    23          { input: 2e12, output: '2.00T' },
    24          { input: 2e11, output: '200.00G' },
    25          { input: 2e10, output: '20.00G' },
    26          { input: 2e9, output: '2.00G' },
    27          { input: 2e8, output: '200.00M' },
    28          { input: 2e7, output: '20.00M' },
    29          { input: 2e6, output: '2.00M' },
    30          { input: 2e5, output: '200.00k' },
    31          { input: 2e4, output: '20.00k' },
    32          { input: 2e3, output: '2.00k' },
    33          { input: 2e2, output: '200.00' },
    34          { input: 2e1, output: '20.00' },
    35          { input: 2, output: '2.00' },
    36          { input: 2e-1, output: '0.20' },
    37          { input: 2e-2, output: '0.02' },
    38          { input: 2e-3, output: '2.00m' },
    39          { input: 2e-4, output: '0.20m' },
    40          { input: 2e-5, output: '0.02m' },
    41          { input: 2e-6, output: '2.00µ' },
    42          { input: 2e-7, output: '0.20µ' },
    43          { input: 2e-8, output: '0.02µ' },
    44          { input: 2e-9, output: '2.00n' },
    45          { input: 2e-10, output: '0.20n' },
    46          { input: 2e-11, output: '0.02n' },
    47          { input: 2e-12, output: '2.00p' },
    48          { input: 2e-13, output: '0.20p' },
    49          { input: 2e-14, output: '0.02p' },
    50          { input: 2e-15, output: '2.00f' },
    51          { input: 2e-16, output: '0.20f' },
    52          { input: 2e-17, output: '0.02f' },
    53          { input: 2e-18, output: '2.00a' },
    54          { input: 2e-19, output: '0.20a' },
    55          { input: 2e-20, output: '0.02a' },
    56          { input: 1e-21, output: '1.00z' },
    57          { input: 2e-21, output: '2.00z' },
    58          { input: 2e-22, output: '0.20z' },
    59          { input: 2e-23, output: '0.02z' },
    60          { input: 2e-24, output: '2.00y' },
    61          { input: 2e-25, output: '0.20y' },
    62          { input: 2e-26, output: '0.02y' },
    63        ].map((t) => {
    64          expect(formatValue(t.input)).toBe(t.output);
    65        });
    66      });
    67      it('should throw error if no match', () => {
    68        try {
    69          formatValue(undefined as any);
    70        } catch (error) {
    71          expect((error as Error).message).toEqual("couldn't format a value, this is a bug");
    72        }
    73      });
    74    });
    75    describe('parseValue', () => {
    76      it('should parse number properly', () => {
    77        expect(parseValue('12.3e')).toEqual(12.3);
    78      });
    79      it('should return 0 if value is NaN and stacked prop is true', () => {
    80        expect(parseValue('asd')).toEqual(null);
    81      });
    82      it('should return null if value is NaN and stacked prop is false', () => {
    83        expect(parseValue('asd')).toBeNull();
    84      });
    85    });
    86    describe('Plot options', () => {
    87      it('should configure options properly if stacked prop is true', () => {
    88        expect(getOptions(true, false)).toMatchObject({
    89          series: {
    90            stack: true,
    91            lines: { lineWidth: 1, steps: false, fill: true },
    92            shadowSize: 0,
    93          },
    94        });
    95      });
    96      it('should configure options properly if stacked prop is false', () => {
    97        expect(getOptions(false, false)).toMatchObject({
    98          series: {
    99            stack: false,
   100            lines: { lineWidth: 2, steps: false, fill: false },
   101            shadowSize: 0,
   102          },
   103        });
   104      });
   105      it('should configure options properly if useLocalTime prop is true', () => {
   106        expect(getOptions(true, true)).toMatchObject({
   107          xaxis: {
   108            mode: 'time',
   109            showTicks: true,
   110            showMinorTicks: true,
   111            timeBase: 'milliseconds',
   112            timezone: 'browser',
   113          },
   114        });
   115      });
   116      it('should configure options properly if useLocalTime prop is false', () => {
   117        expect(getOptions(false, false)).toMatchObject({
   118          xaxis: {
   119            mode: 'time',
   120            showTicks: true,
   121            showMinorTicks: true,
   122            timeBase: 'milliseconds',
   123          },
   124        });
   125      });
   126      it('should return proper tooltip html from options', () => {
   127        expect(
   128          getOptions(true, false).tooltip.content('', 1572128592, 1572128592, {
   129            series: { labels: { foo: '1', bar: '2' }, color: '' },
   130          } as any)
   131        ).toEqual(`
   132              <div class="date">1970-01-19 04:42:08 +00:00</div>
   133              <div>
   134                <span class="detail-swatch" style="background-color: "></span>
   135                <span>value: <strong>1572128592</strong></span>
   136              <div>
   137              <div class="labels mt-1">
   138                <div class="mb-1"><strong>foo</strong>: 1</div><div class="mb-1"><strong>bar</strong>: 2</div>
   139              </div>
   140            `);
   141      });
   142      it('should return proper tooltip html from options with local time', () => {
   143        moment.tz.setDefault('America/New_York');
   144        expect(
   145          getOptions(true, true).tooltip.content('', 1572128592, 1572128592, {
   146            series: { labels: { foo: '1', bar: '2' }, color: '' },
   147          } as any)
   148        ).toEqual(`
   149              <div class="date">1970-01-18 23:42:08 -05:00</div>
   150              <div>
   151                <span class="detail-swatch" style="background-color: "></span>
   152                <span>value: <strong>1572128592</strong></span>
   153              <div>
   154              <div class="labels mt-1">
   155                <div class="mb-1"><strong>foo</strong>: 1</div><div class="mb-1"><strong>bar</strong>: 2</div>
   156              </div>
   157            `);
   158      });
   159      it('should render Plot with proper options', () => {
   160        expect(getOptions(true, false)).toEqual({
   161          grid: {
   162            hoverable: true,
   163            clickable: true,
   164            autoHighlight: true,
   165            mouseActiveRadius: 100,
   166          },
   167          legend: { show: false },
   168          xaxis: {
   169            mode: 'time',
   170            showTicks: true,
   171            showMinorTicks: true,
   172            timeBase: 'milliseconds',
   173          },
   174          yaxis: { tickFormatter: expect.anything() },
   175          crosshair: { mode: 'xy', color: '#bbb' },
   176          tooltip: {
   177            show: true,
   178            cssClass: 'graph-tooltip',
   179            content: expect.anything(),
   180            defaultTheme: false,
   181            lines: true,
   182          },
   183          series: {
   184            stack: true,
   185            lines: { lineWidth: 1, steps: false, fill: true },
   186            shadowSize: 0,
   187          },
   188        });
   189      });
   190    });
   191  });