|
| 1 | +import dayjs from "dayjs" |
| 2 | +import utc from "dayjs/plugin/utc" |
| 3 | +import * as TypesGen from "../../api/typesGenerated" |
| 4 | +import * as Mocks from "../../testHelpers/entities" |
| 5 | +import { |
| 6 | + deadlineMinusDisabled, |
| 7 | + deadlinePlusDisabled, |
| 8 | + shouldDisplayPlusMinus, |
| 9 | +} from "./WorkspaceSchedule" |
| 10 | + |
| 11 | +dayjs.extend(utc) |
| 12 | +const now = dayjs() |
| 13 | + |
| 14 | +describe("WorkspaceSchedule", () => { |
| 15 | + describe("shouldDisplayPlusMinus", () => { |
| 16 | + it("should not display if the workspace is not running", () => { |
| 17 | + // Given: a stopped workspace |
| 18 | + const workspace: TypesGen.Workspace = Mocks.MockStoppedWorkspace |
| 19 | + |
| 20 | + // Then: shouldDisplayPlusMinus should be false |
| 21 | + expect(shouldDisplayPlusMinus(workspace)).toBeFalsy() |
| 22 | + }) |
| 23 | + |
| 24 | + it("should display if the workspace is running", () => { |
| 25 | + // Given: a stopped workspace |
| 26 | + const workspace: TypesGen.Workspace = Mocks.MockWorkspace |
| 27 | + |
| 28 | + // Then: shouldDisplayPlusMinus should be false |
| 29 | + expect(shouldDisplayPlusMinus(workspace)).toBeTruthy() |
| 30 | + }) |
| 31 | + }) |
| 32 | + |
| 33 | + describe("deadlineMinusDisabled", () => { |
| 34 | + it("should be false if the deadline is more than 30 minutes in the future", () => { |
| 35 | + // Given: a workspace with a deadline set to 31 minutes in the future |
| 36 | + const workspace: TypesGen.Workspace = { |
| 37 | + ...Mocks.MockWorkspace, |
| 38 | + latest_build: { |
| 39 | + ...Mocks.MockWorkspaceBuild, |
| 40 | + deadline: now.add(31, "minutes").utc().format(), |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + // Then: deadlineMinusDisabled should be falsy |
| 45 | + expect(deadlineMinusDisabled(workspace, now)).toBeFalsy() |
| 46 | + }) |
| 47 | + |
| 48 | + it("should be true if the deadline is 30 minutes or less in the future", () => { |
| 49 | + // Given: a workspace with a deadline set to 30 minutes in the future |
| 50 | + const workspace: TypesGen.Workspace = { |
| 51 | + ...Mocks.MockWorkspace, |
| 52 | + latest_build: { |
| 53 | + ...Mocks.MockWorkspaceBuild, |
| 54 | + deadline: now.add(30, "minutes").utc().format(), |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + // Then: deadlineMinusDisabled should be truthy |
| 59 | + expect(deadlineMinusDisabled(workspace, now)).toBeTruthy() |
| 60 | + }) |
| 61 | + |
| 62 | + it("should be true if the deadline is in the past", () => { |
| 63 | + // Given: a workspace with a deadline set to 1 minute in the past |
| 64 | + const workspace: TypesGen.Workspace = { |
| 65 | + ...Mocks.MockWorkspace, |
| 66 | + latest_build: { |
| 67 | + ...Mocks.MockWorkspaceBuild, |
| 68 | + deadline: now.add(-1, "minutes").utc().format(), |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + // Then: deadlineMinusDisabled should be truthy |
| 73 | + expect(deadlineMinusDisabled(workspace, now)).toBeTruthy() |
| 74 | + }) |
| 75 | + }) |
| 76 | + |
| 77 | + describe("deadlinePlusDisabled", () => { |
| 78 | + it("should be false if the deadline is less than 24 hours in the future", () => { |
| 79 | + // Given: a workspace with a deadline set to 23 hours in the future |
| 80 | + const workspace: TypesGen.Workspace = { |
| 81 | + ...Mocks.MockWorkspace, |
| 82 | + latest_build: { |
| 83 | + ...Mocks.MockWorkspaceBuild, |
| 84 | + deadline: now.add(23, "hours").utc().format(), |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + // Then: deadlinePlusDisabled should be falsy |
| 89 | + expect(deadlinePlusDisabled(workspace, now)).toBeFalsy() |
| 90 | + }) |
| 91 | + |
| 92 | + it("should be true if the deadline is 24 hours or more in the future", () => { |
| 93 | + // Given: a workspace with a deadline set to 25 hours in the future |
| 94 | + const workspace: TypesGen.Workspace = { |
| 95 | + ...Mocks.MockWorkspace, |
| 96 | + latest_build: { |
| 97 | + ...Mocks.MockWorkspaceBuild, |
| 98 | + deadline: now.add(25, "hours").utc().format(), |
| 99 | + }, |
| 100 | + } |
| 101 | + |
| 102 | + // Then: deadlinePlusDisabled should be truthy |
| 103 | + expect(deadlinePlusDisabled(workspace, now)).toBeTruthy() |
| 104 | + }) |
| 105 | + |
| 106 | + it("should be false if the deadline is in the past", () => { |
| 107 | + // Given: a workspace with a deadline set to 1 minute in the past |
| 108 | + const workspace: TypesGen.Workspace = { |
| 109 | + ...Mocks.MockWorkspace, |
| 110 | + latest_build: { |
| 111 | + ...Mocks.MockWorkspaceBuild, |
| 112 | + deadline: now.add(-1, "minute").utc().format(), |
| 113 | + }, |
| 114 | + } |
| 115 | + |
| 116 | + // Then: deadlinePlusDisabled should be falsy |
| 117 | + expect(deadlinePlusDisabled(workspace, now)).toBeFalsy() |
| 118 | + }) |
| 119 | + }) |
| 120 | +}) |
0 commit comments