Skip to content

refactor(site): Refactor workspace schedule page #7069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 11, 2023
Merged
Prev Previous commit
Next Next commit
Fix tests
  • Loading branch information
BrunoQuaresma committed Apr 11, 2023
commit b338fe110c22f5922540a4f72d202e978ced8c74
5 changes: 4 additions & 1 deletion site/src/AppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ const WorkspaceBuildPage = lazy(
)
const WorkspacePage = lazy(() => import("./pages/WorkspacePage/WorkspacePage"))
const WorkspaceSchedulePage = lazy(
() => import("./pages/WorkspaceSchedulePage/WorkspaceSchedulePage"),
() =>
import(
"./pages/WorkspaceSettingsPage/WorkspaceSchedulePage/WorkspaceSchedulePage"
),
)
const TerminalPage = lazy(() => import("./pages/TerminalPage/TerminalPage"))
const TemplatePermissionsPage = lazy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import utc from "dayjs/plugin/utc"
import {
defaultSchedule,
emptySchedule,
} from "pages/WorkspaceSchedulePage/schedule"
import { emptyTTL } from "pages/WorkspaceSchedulePage/ttl"
} from "pages/WorkspaceSettingsPage/WorkspaceSchedulePage/schedule"
import { emptyTTL } from "pages/WorkspaceSettingsPage/WorkspaceSchedulePage/ttl"
import { makeMockApiError } from "testHelpers/entities"
import {
WorkspaceScheduleForm,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { FormikTouched, useFormik } from "formik"
import {
defaultSchedule,
emptySchedule,
} from "pages/WorkspaceSchedulePage/schedule"
} from "pages/WorkspaceSettingsPage/WorkspaceSchedulePage/schedule"
import { ChangeEvent, FC } from "react"
import * as Yup from "yup"
import { getFormHelpers } from "../../util/formUtils"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { renderWithAuth } from "testHelpers/renderHelpers"
import { renderWithWorkspaceSettingsLayout } from "testHelpers/renderHelpers"
import userEvent from "@testing-library/user-event"
import { screen } from "@testing-library/react"
import {
formValuesToAutostartRequest,
formValuesToTTLRequest,
} from "pages/WorkspaceSchedulePage/formToRequest"
} from "pages/WorkspaceSettingsPage/WorkspaceSchedulePage/formToRequest"
import {
Autostart,
scheduleToAutostart,
} from "pages/WorkspaceSchedulePage/schedule"
import { Autostop, ttlMsToAutostop } from "pages/WorkspaceSchedulePage/ttl"
import * as TypesGen from "../../api/typesGenerated"
} from "pages/WorkspaceSettingsPage/WorkspaceSchedulePage/schedule"
import {
Autostop,
ttlMsToAutostop,
} from "pages/WorkspaceSettingsPage/WorkspaceSchedulePage/ttl"
import * as TypesGen from "../../../api/typesGenerated"
import {
WorkspaceScheduleFormValues,
Language as FormLanguage,
Expand Down Expand Up @@ -257,7 +260,7 @@ describe("WorkspaceSchedulePage", () => {

describe("autostop change dialog", () => {
it("shows if autostop is changed", async () => {
renderWithAuth(<WorkspaceSchedulePage />, {
renderWithWorkspaceSettingsLayout(<WorkspaceSchedulePage />, {
route: `/@${MockUser.username}/${MockWorkspace.name}/schedule`,
path: "/@:username/:workspace/schedule",
})
Expand All @@ -276,7 +279,7 @@ describe("WorkspaceSchedulePage", () => {
})

it("doesn't show if autostop is not changed", async () => {
renderWithAuth(<WorkspaceSchedulePage />, {
renderWithWorkspaceSettingsLayout(<WorkspaceSchedulePage />, {
route: `/@${MockUser.username}/${MockWorkspace.name}/schedule`,
path: "/@:username/:workspace/schedule",
})
Expand Down Expand Up @@ -309,7 +312,7 @@ describe("WorkspaceSchedulePage", () => {
},
),
)
renderWithAuth(<WorkspaceSchedulePage />, {
renderWithWorkspaceSettingsLayout(<WorkspaceSchedulePage />, {
route: `/@${MockUser.username}/${MockWorkspace.name}/schedule`,
path: "/@:username/:workspace/schedule",
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog"
import { Loader } from "components/Loader/Loader"
import { PageHeader, PageHeaderTitle } from "components/PageHeader/PageHeader"
import dayjs from "dayjs"
import { scheduleToAutostart } from "pages/WorkspaceSchedulePage/schedule"
import { ttlMsToAutostop } from "pages/WorkspaceSchedulePage/ttl"
import { scheduleToAutostart } from "pages/WorkspaceSettingsPage/WorkspaceSchedulePage/schedule"
import { ttlMsToAutostop } from "pages/WorkspaceSettingsPage/WorkspaceSchedulePage/ttl"
import { useWorkspaceSettingsContext } from "pages/WorkspaceSettingsPage/WorkspaceSettingsLayout"
import { FC } from "react"
import { Helmet } from "react-helmet-async"
import { useTranslation } from "react-i18next"
import { Navigate, useNavigate, useParams } from "react-router-dom"
import { pageTitle } from "util/page"
import { scheduleChanged } from "util/schedule"
import * as TypesGen from "../../api/typesGenerated"
import { WorkspaceScheduleForm } from "../../components/WorkspaceScheduleForm/WorkspaceScheduleForm"
import { firstOrItem } from "../../util/array"
import { workspaceSchedule } from "../../xServices/workspaceSchedule/workspaceScheduleXService"
import * as TypesGen from "../../../api/typesGenerated"
import { WorkspaceScheduleForm } from "../../../components/WorkspaceScheduleForm/WorkspaceScheduleForm"
import { firstOrItem } from "../../../util/array"
import { workspaceSchedule } from "../../../xServices/workspaceSchedule/workspaceScheduleXService"
import {
formValuesToAutostartRequest,
formValuesToTTLRequest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as cronParser from "cron-parser"
import dayjs from "dayjs"
import timezone from "dayjs/plugin/timezone"
import utc from "dayjs/plugin/utc"
import { extractTimezone, stripTimezone } from "../../util/schedule"
import { extractTimezone, stripTimezone } from "../../../util/schedule"

// REMARK: timezone plugin depends on UTC
//
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import userEvent from "@testing-library/user-event"
import {
renderWithAuth,
renderWithWorkspaceSettingsLayout,
waitForLoaderToBeRemoved,
} from "testHelpers/renderHelpers"
import WorkspaceSettingsPage from "./WorkspaceSettingsPage"
Expand Down Expand Up @@ -41,7 +41,7 @@ test("Submit the workspace settings page successfully", async () => {
.mockResolvedValue(MockWorkspaceBuild)
// Setup event and rendering
const user = userEvent.setup()
renderWithAuth(<WorkspaceSettingsPage />, {
renderWithWorkspaceSettingsLayout(<WorkspaceSettingsPage />, {
route: "/@test-user/test-workspace/settings",
path: "/@:username/:workspace/settings",
// Need this because after submit the user is redirected
Expand Down
45 changes: 45 additions & 0 deletions site/src/testHelpers/renderHelpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { DashboardLayout } from "components/Dashboard/DashboardLayout"
import { createMemoryHistory } from "history"
import { i18n } from "i18n"
import { TemplateSettingsLayout } from "pages/TemplateSettingsPage/TemplateSettingsLayout"
import { WorkspaceSettingsLayout } from "pages/WorkspaceSettingsPage/WorkspaceSettingsLayout"
import { FC, ReactElement } from "react"
import { I18nextProvider } from "react-i18next"
import {
Expand Down Expand Up @@ -131,6 +132,50 @@ export function renderWithTemplateSettingsLayout(
}
}

export function renderWithWorkspaceSettingsLayout(
element: JSX.Element,
{
path = "/",
route = "/",
extraRoutes = [],
nonAuthenticatedRoutes = [],
}: RenderWithAuthOptions = {},
) {
const routes: RouteObject[] = [
{
element: <RequireAuth />,
children: [
{
element: <DashboardLayout />,
children: [
{
element: <WorkspaceSettingsLayout />,
children: [{ path, element }, ...extraRoutes],
},
],
},
],
},
...nonAuthenticatedRoutes,
]

const router = createMemoryRouter(routes, { initialEntries: [route] })

const renderResult = wrappedRender(
<I18nextProvider i18n={i18n}>
<AppProviders>
<RouterProvider router={router} />
</AppProviders>
</I18nextProvider>,
)

return {
user: MockUser,
router,
...renderResult,
}
}

export const waitForLoaderToBeRemoved = (): Promise<void> =>
// Sometimes, we have pages that are doing a lot of requests to get done, so the
// default timeout of 1_000 is not enough. We should revisit this when we unify
Expand Down
4 changes: 2 additions & 2 deletions site/src/util/schedule.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dayjs from "dayjs"
import duration from "dayjs/plugin/duration"
import { emptySchedule } from "pages/WorkspaceSchedulePage/schedule"
import { emptyTTL } from "pages/WorkspaceSchedulePage/ttl"
import { emptySchedule } from "pages/WorkspaceSettingsPage/WorkspaceSchedulePage/schedule"
import { emptyTTL } from "pages/WorkspaceSettingsPage/WorkspaceSchedulePage/ttl"
import { Workspace } from "../api/typesGenerated"
import * as Mocks from "../testHelpers/entities"
import {
Expand Down
4 changes: 2 additions & 2 deletions site/src/util/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import utc from "dayjs/plugin/utc"
import { Workspace } from "../api/typesGenerated"
import { isWorkspaceOn } from "./workspace"
import { WorkspaceScheduleFormValues } from "components/WorkspaceScheduleForm/WorkspaceScheduleForm"
import { Autostop } from "pages/WorkspaceSchedulePage/ttl"
import { Autostart } from "pages/WorkspaceSchedulePage/schedule"
import { Autostop } from "pages/WorkspaceSettingsPage/WorkspaceSchedulePage/ttl"
import { Autostart } from "pages/WorkspaceSettingsPage/WorkspaceSchedulePage/schedule"

// REMARK: some plugins depend on utc, so it's listed first. Otherwise they're
// sorted alphabetically.
Expand Down