Skip to content

Commit

Permalink
fix gsheets tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Loe committed Jun 21, 2024
1 parent c6de227 commit 73298ee
Showing 1 changed file with 35 additions and 19 deletions.
54 changes: 35 additions & 19 deletions src/contrib/google/sheets/core/sheetsApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,19 @@ import { integrationConfigFactory } from "@/testUtils/factories/integrationFacto
import { locator } from "@/background/locator";
import googleDefinition from "@contrib/integrations/google-oauth2-pkce.yaml";
import { fromJS } from "@/integrations/UserDefinedIntegration";
import { type IntegrationConfig } from "@/integrations/integrationTypes";
import * as backgroundAuthStorage from "@/background/auth/authStorage";
import {
type AuthData,
type IntegrationConfig,
} from "@/integrations/integrationTypes";
import { setPlatform } from "@/platform/platformContext";
import backgroundPlatform from "@/background/backgroundPlatform";
import { readRawConfigurations } from "@/integrations/util/readRawConfigurations";
import {
deleteCachedAuthData,
getCachedAuthData,
setCachedAuthData,
} from "@/background/auth/authStorage";
import launchOAuth2Flow from "@/background/auth/launchOAuth2Flow";

const axiosMock = new MockAdapter(axios);

Expand All @@ -43,18 +51,17 @@ jest.mocked(apiProxyService).mockImplementation(realProxyService);
jest.mock("@/integrations/util/readRawConfigurations");
const readRawConfigurationsMock = jest.mocked(readRawConfigurations);

const { deleteCachedAuthData, getCachedAuthData, setCachedAuthData } =
backgroundAuthStorage;
jest.mock("@/background/auth/authStorage", () => {
const actual = jest.requireActual("@/background/auth/authStorage");

const spyContainer = {
deleteCachedAuthData,
};
return {
__esModule: true,
...actual,
deleteCachedAuthData: jest.fn(actual.deleteCachedAuthData),
};
});

// const deleteCachedAuthDataMock = jest.mocked(deleteCachedAuthData);
const deleteCachedAuthDataSpy = jest.spyOn(
spyContainer,
"deleteCachedAuthData",
);
const deleteCachedAuthDataSpy = jest.mocked(deleteCachedAuthData);

jest.mock("@/integrations/registry", () => {
const actual = jest.requireActual("@/integrations/registry");
Expand All @@ -71,6 +78,10 @@ jest.mock("@/integrations/registry", () => {
};
});

jest.mock("@/background/auth/launchOAuth2Flow");

const launchOAuth2FlowMock = jest.mocked(launchOAuth2Flow);

beforeEach(() => {
// `sheetsApi` uses the ambient platform context to make requests
setPlatform(backgroundPlatform);
Expand All @@ -92,6 +103,7 @@ describe("error handling", () => {

readRawConfigurationsMock.mockResolvedValue([integrationConfig]);

launchOAuth2FlowMock.mockReset();
deleteCachedAuthDataSpy.mockReset();

await locator.refresh();
Expand Down Expand Up @@ -161,18 +173,22 @@ describe("error handling", () => {
integrationConfig.id,
);

await setCachedAuthData(integrationConfig.id, {
const authData: AuthData = {
_oauthBrand: null,
access_token: "NOTAREALTOKEN",
});
};

await setCachedAuthData(integrationConfig.id, authData);

// If no refresh token, requests code will delete auth data and kick off login again
launchOAuth2FlowMock.mockResolvedValue(authData);

await expect(getAllSpreadsheets(config)).rejects.toThrow(message);

await expect(
getCachedAuthData(integrationConfig.id),
).resolves.toStrictEqual({
access_token: "NOTAREALTOKEN",
});
expect(deleteCachedAuthDataSpy).toHaveBeenCalledOnce();
expect(deleteCachedAuthDataSpy).toHaveBeenCalledWith(
integrationConfig.id,
);

expect(
axiosMock.history.get!.filter((x) => x.url!.startsWith(DRIVE_BASE_URL)),
Expand Down

0 comments on commit 73298ee

Please sign in to comment.