Skip to content

refactor: improve test isolation for Axios API logic #13125

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 9 commits into from
May 12, 2024
Prev Previous commit
refactor: update import setup for tests
  • Loading branch information
Parkreiner committed May 12, 2024
commit c52827c1130a3752967767b0c05a7dd9c23ce9c7
4 changes: 2 additions & 2 deletions site/src/modules/resources/AgentLogs/AgentLogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
useState,
} from "react";
import { FixedSizeList as List } from "react-window";
import * as API from "api/api";
import { watchWorkspaceAgentLogs } from "api/api";
import type { WorkspaceAgentLogSource } from "api/typesGenerated";
import {
AGENT_LOG_LINE_HEIGHT,
Expand Down Expand Up @@ -193,7 +193,7 @@ export const useAgentLogs = (
return;
}

const socket = API.watchWorkspaceAgentLogs(agentId, {
const socket = watchWorkspaceAgentLogs(agentId, {
// Get all logs
after: 0,
onMessage: (logs) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import WS from "jest-websocket-mock";
import { HttpResponse, http } from "msw";
import { QueryClient } from "react-query";
import { RouterProvider, createMemoryRouter } from "react-router-dom";
import * as api from "api/api";
import * as apiModule from "api/api";
import { templateVersionVariablesKey } from "api/queries/templates";
import type { TemplateVersion } from "api/typesGenerated";
import { AppProviders } from "App";
Expand All @@ -26,7 +26,7 @@ import type { MonacoEditorProps } from "./MonacoEditor";
import { Language } from "./PublishTemplateVersionDialog";
import TemplateVersionEditorPage from "./TemplateVersionEditorPage";

const { API } = api;
const { API } = apiModule;

// For some reason this component in Jest is throwing a MUI style warning so,
// since we don't need it for this test, we can mock it out
Expand Down Expand Up @@ -86,7 +86,7 @@ const buildTemplateVersion = async (
.spyOn(API, "getTemplateVersionByName")
.mockResolvedValue(templateVersion);
jest
.spyOn(api, "watchBuildLogsByTemplateVersionId")
.spyOn(apiModule, "watchBuildLogsByTemplateVersionId")
.mockImplementation((_, options) => {
options.onMessage(MockWorkspaceBuildLogs[0]);
options.onDone?.();
Expand Down
8 changes: 4 additions & 4 deletions site/src/pages/WorkspacePage/WorkspacePage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { screen, waitFor, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { HttpResponse, http } from "msw";
import * as api from "api/api";
import * as apiModule from "api/api";
import type { TemplateVersionParameter, Workspace } from "api/typesGenerated";
import EventSourceMock from "eventsourcemock";
import {
Expand All @@ -22,7 +22,7 @@ import { renderWithAuth } from "testHelpers/renderHelpers";
import { server } from "testHelpers/server";
import { WorkspacePage } from "./WorkspacePage";

const { API } = api;
const { API, MissingBuildParameters } = apiModule;

// Renders the workspace page and waits for it be loaded
const renderWorkspacePage = async (workspace: Workspace) => {
Expand All @@ -33,7 +33,7 @@ const renderWorkspacePage = async (workspace: Workspace) => {
.spyOn(API, "getDeploymentConfig")
.mockResolvedValueOnce(MockDeploymentConfig);
jest
.spyOn(api, "watchWorkspaceAgentLogs")
.spyOn(apiModule, "watchWorkspaceAgentLogs")
.mockImplementation((_, options) => {
options.onDone?.();
return new WebSocket("");
Expand Down Expand Up @@ -256,7 +256,7 @@ describe("WorkspacePage", () => {
const updateWorkspaceSpy = jest
.spyOn(API, "updateWorkspace")
.mockRejectedValueOnce(
new api.MissingBuildParameters(
new MissingBuildParameters(
[MockTemplateVersionParameter1, MockTemplateVersionParameter2],
MockOutdatedWorkspace.template_active_version_id,
),
Expand Down
Loading