github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ui/src/views/app/containers/timewindow/timewindow.spec.tsx (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  import React from "react";
    12  import { assert } from "chai";
    13  import { shallow } from "enzyme";
    14  import * as sinon from "sinon";
    15  import moment from "moment";
    16  import _ from "lodash";
    17  
    18  import "src/enzymeInit";
    19  import { TimeWindowManagerUnconnected as TimeWindowManager } from "./";
    20  import * as timewindow from "src/redux/timewindow";
    21  
    22  describe("<TimeWindowManager>", function() {
    23    let spy: sinon.SinonSpy;
    24    let state: timewindow.TimeWindowState;
    25    const now = () => moment("11-12-1955 10:04PM -0800", "MM-DD-YYYY hh:mma Z");
    26  
    27    beforeEach(function() {
    28      spy = sinon.spy();
    29      state = new timewindow.TimeWindowState();
    30    });
    31  
    32    const getManager = () => shallow(<TimeWindowManager timeWindow={_.clone(state)}
    33                                                      setTimeWindow={spy}
    34                                                      now={now} />);
    35  
    36    it("resets time window immediately it is empty", function() {
    37      getManager();
    38      assert.isTrue(spy.calledOnce);
    39      assert.deepEqual(spy.firstCall.args[0], {
    40        start: now().subtract(state.scale.windowSize),
    41        end: now(),
    42      });
    43    });
    44  
    45    it("resets time window immediately if expired", function() {
    46      state.currentWindow = {
    47        start: now().subtract(state.scale.windowSize),
    48        end: now().subtract(state.scale.windowValid).subtract(1),
    49      };
    50  
    51      getManager();
    52      assert.isTrue(spy.calledOnce);
    53      assert.deepEqual(spy.firstCall.args[0], {
    54        start: now().subtract(state.scale.windowSize),
    55        end: now(),
    56      });
    57    });
    58  
    59    it("resets time window immediately if scale has changed", function() {
    60      // valid window.
    61      state.currentWindow = {
    62        start: now().subtract(state.scale.windowSize),
    63        end: now(),
    64      };
    65      state.scaleChanged = true;
    66  
    67      getManager();
    68      assert.isTrue(spy.calledOnce);
    69      assert.deepEqual(spy.firstCall.args[0], {
    70        start: now().subtract(state.scale.windowSize),
    71        end: now(),
    72      });
    73    });
    74  
    75    it("resets time window later if current window is valid", function() {
    76      state.currentWindow = {
    77        start: now().subtract(state.scale.windowSize),
    78        // 5 milliseconds until expiration.
    79        end: now().subtract(state.scale.windowValid.asMilliseconds() - 5),
    80      };
    81  
    82      getManager();
    83      assert.isTrue(spy.notCalled);
    84  
    85      // Wait 11 milliseconds, then verify that window was updated.
    86      return new Promise<void>((resolve, _reject) => {
    87        setTimeout(
    88          () => {
    89            assert.isTrue(spy.calledOnce);
    90            assert.deepEqual(spy.firstCall.args[0], {
    91              start: now().subtract(state.scale.windowSize),
    92              end: now(),
    93            });
    94            resolve();
    95          },
    96          6);
    97      });
    98    });
    99  
   100    // TODO (maxlang): Fix this test to actually change the state to catch the
   101    // issue that caused #7590. Tracked in #8595.
   102    it("has only a single timeout at a time.", function() {
   103      state.currentWindow = {
   104        start: now().subtract(state.scale.windowSize),
   105        // 5 milliseconds until expiration.
   106        end: now().subtract(state.scale.windowValid.asMilliseconds() - 5),
   107      };
   108  
   109      const manager = getManager();
   110      assert.isTrue(spy.notCalled);
   111  
   112      // Set new props on currentWindow. The previous timeout should be abandoned.
   113      state.currentWindow = {
   114        start: now().subtract(state.scale.windowSize),
   115        // 10 milliseconds until expiration.
   116        end: now().subtract(state.scale.windowValid.asMilliseconds() - 10),
   117      };
   118      manager.setProps({
   119        timeWindow: state,
   120      });
   121      assert.isTrue(spy.notCalled);
   122  
   123      // Wait 11 milliseconds, then verify that window was updated a single time.
   124      return new Promise<void>((resolve, _reject) => {
   125        setTimeout(
   126          () => {
   127            assert.isTrue(spy.calledOnce);
   128            assert.deepEqual(spy.firstCall.args[0], {
   129              start: now().subtract(state.scale.windowSize),
   130              end: now(),
   131            });
   132            resolve();
   133          },
   134          11);
   135      });
   136  
   137    });
   138  });