github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ui/src/redux/timewindow.spec.ts (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 { assert } from "chai"; 12 import * as timewindow from "./timewindow"; 13 import moment from "moment"; 14 15 describe("time window reducer", function() { 16 describe("actions", function() { 17 it("should create the correct action to set the current time window", function() { 18 const start = moment(); 19 const end = start.add(10, "s"); 20 const expectedSetting = { 21 type: timewindow.SET_WINDOW, 22 payload: { 23 start, 24 end, 25 }, 26 }; 27 assert.deepEqual( 28 timewindow.setTimeWindow({ start, end }), 29 expectedSetting); 30 }); 31 32 it("should create the correct action to set time window settings", function() { 33 const payload: timewindow.TimeScale = { 34 windowSize: moment.duration(10, "s"), 35 windowValid: moment.duration(10, "s"), 36 sampleSize: moment.duration(10, "s"), 37 }; 38 assert.deepEqual( 39 timewindow.setTimeScale(payload), 40 { type: timewindow.SET_SCALE, payload }, 41 ); 42 }); 43 }); 44 45 describe("reducer", () => { 46 it("should have the correct default value.", () => { 47 assert.deepEqual( 48 timewindow.timeWindowReducer(undefined, { type: "unknown" }), 49 new timewindow.TimeWindowState(), 50 ); 51 assert.deepEqual( 52 (new timewindow.TimeWindowState()).scale, 53 timewindow.availableTimeScales["Past 10 Minutes"], 54 ); 55 }); 56 57 describe("setTimeWindow", () => { 58 const start = moment(); 59 const end = start.add(10, "s"); 60 it("should correctly overwrite previous value", () => { 61 const expected = new timewindow.TimeWindowState(); 62 expected.currentWindow = { 63 start, 64 end, 65 }; 66 expected.scaleChanged = false; 67 assert.deepEqual( 68 timewindow.timeWindowReducer(undefined, timewindow.setTimeWindow({ start, end })), 69 expected, 70 ); 71 }); 72 }); 73 74 describe("setTimeWindowSettings", () => { 75 const newSize = moment.duration(1, "h"); 76 const newValid = moment.duration(1, "m"); 77 const newSample = moment.duration(1, "m"); 78 it("should correctly overwrite previous value", () => { 79 const expected = new timewindow.TimeWindowState(); 80 expected.scale = { 81 windowSize: newSize, 82 windowValid: newValid, 83 sampleSize: newSample, 84 }; 85 expected.scaleChanged = true; 86 assert.deepEqual( 87 timewindow.timeWindowReducer(undefined, timewindow.setTimeScale({ 88 windowSize: newSize, 89 windowValid: newValid, 90 sampleSize: newSample, 91 })), 92 expected, 93 ); 94 }); 95 }); 96 describe("findClosestTimeScale", () => { 97 it("should found correctly time scale", () => { 98 assert.deepEqual(timewindow.findClosestTimeScale(15), { ...timewindow.availableTimeScales["Past 10 Minutes"], key: "Custom" }); 99 assert.deepEqual(timewindow.findClosestTimeScale(moment.duration(10, "minutes").asSeconds()), { ...timewindow.availableTimeScales["Past 10 Minutes"], key: "Past 10 Minutes"}); 100 assert.deepEqual(timewindow.findClosestTimeScale(moment.duration(14, "days").asSeconds()), { ...timewindow.availableTimeScales["Past 2 Weeks"], key: "Past 2 Weeks" }); 101 assert.deepEqual(timewindow.findClosestTimeScale(moment.duration(moment().daysInMonth() * 5, "days").asSeconds()), { ...timewindow.availableTimeScales["Past 2 Months"], key: "Custom" }); 102 }); 103 }); 104 }); 105 });