Skip to content

Commit

Permalink
test: update tests for refactored stamp data
Browse files Browse the repository at this point in the history
  • Loading branch information
lucianHymer committed Feb 6, 2025
1 parent 1280ed3 commit 777abe4
Show file tree
Hide file tree
Showing 7 changed files with 351 additions and 137 deletions.
6 changes: 3 additions & 3 deletions src/components/Body/PlatformVerification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export const PlatformVerification = ({

console.log("DEBUG THIS ON CLICK VERIFY CREDENTIALS platform");
let signature, credential;
if (platform.requireSignature) {
if (platform.requiresSignature) {
// get the challenge and sign it
if (!queryProps.address) {
console.error("No address found");
Expand Down Expand Up @@ -158,10 +158,10 @@ export const PlatformVerification = ({
signature = await generateSignatureCallback(challengeToSign);
}

if (platform.requiresPopup && platform.popUpUrl) {
if (platform.requiresPopup && platform.popupUrl) {
// open the popup
const oAuthPopUpUrl = `${
platform.popUpUrl
platform.popupUrl
}?address=${encodeURIComponent(
queryProps.address || ""
)}&scorerId=${encodeURIComponent(
Expand Down
37 changes: 8 additions & 29 deletions src/hooks/useStampPages.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useEffect, useState, ReactNode } from "react";
import axios from "axios";
import { DEFAULT_IAM_URL } from "./usePassportScore";
import { SanitizedHTMLComponent } from "../components/SanitizedHTMLComponent";
import { fetchStampPages } from "../utils/stampDataApi";

export type Credential = {
id: string;
Expand All @@ -12,9 +11,9 @@ export type Platform = {
name: string;
description: ReactNode;
documentationLink: string;
requireSignature?: boolean;
requiresSignature?: boolean;
requiresPopup?: boolean;
popUpUrl?: string;
popupUrl?: string;
credentials: Credential[];
displayWeight: string;
};
Expand All @@ -32,13 +31,9 @@ type RawStampPageData = Omit<StampPage, "platforms"> & {
platforms: RawPlatformData[];
};

type StampsMetadataResponse = RawStampPageData[];
export type StampsMetadataResponse = RawStampPageData[];

export const usePaginatedStampPages = ({
apiKey,
scorerId,
overrideIamUrl,
}: {
export const usePaginatedStampPages = (props: {
apiKey: string;
scorerId: string;
overrideIamUrl?: string;
Expand All @@ -49,24 +44,10 @@ export const usePaginatedStampPages = ({
const [idx, setIdx] = useState(0);

useEffect(() => {
const fetchStampPages = async () => {
(async () => {
try {
// TODO: fix this to use propr url encoding
const response = await axios.get<StampsMetadataResponse>(
`${
overrideIamUrl || DEFAULT_IAM_URL
}/embed/stamps/metadata?scorerId=${scorerId}`,
{
headers: {
"X-API-KEY": apiKey,
"Content-Type": "application/json",
},
}
);

const data = response.data;
const data = await fetchStampPages(props);

// Convert description from HTML string to JSX
const formattedData: StampPage[] = data.map(
(page: RawStampPageData) => ({
...page,
Expand All @@ -87,9 +68,7 @@ export const usePaginatedStampPages = ({
} finally {
setLoading(false);
}
};

fetchStampPages();
})();
}, []);

// Pagination controls
Expand Down
28 changes: 28 additions & 0 deletions src/utils/stampDataApi.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import axios from "axios";
import { StampsMetadataResponse } from "../hooks/useStampPages";
import { DEFAULT_IAM_URL } from "../hooks/usePassportScore";

export const fetchStampPages = async ({
apiKey,
scorerId,
overrideIamUrl,
}: {
apiKey: string;
scorerId: string;
overrideIamUrl?: string;
}) => {
// TODO: fix this to use propr url encoding
const response = await axios.get<StampsMetadataResponse>(
`${
overrideIamUrl || DEFAULT_IAM_URL
}/embed/stamps/metadata?scorerId=${scorerId}`,
{
headers: {
"X-API-KEY": apiKey,
"Content-Type": "application/json",
},
}
);

return response.data;
};
78 changes: 42 additions & 36 deletions test/components/PlatformVerification.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { PlatformVerification } from "../../src/components/Body/PlatformVerifica
import * as usePassportScore from "../../src/hooks/usePassportScore";
import * as usePlatformStatus from "../../src/hooks/usePlatformStatus";
import * as QueryContext from "../../src/contexts/QueryContext";
import { setupTestQueryClient } from "../testUtils";
import {
mockExpectedConsoleErrorLog,
setupTestQueryClient,
} from "../testUtils";
import { Platform } from "../../src/hooks/useStampPages";

// Mock the hooks
jest.mock("../../src/hooks/usePassportScore");
Expand All @@ -19,12 +23,13 @@ describe("PlatformVerification", () => {
setupTestQueryClient();

// Common test props
const mockPlatform = {
const mockPlatform: Platform = {
name: "LinkedIn",
description: <div>Verify your LinkedIn account</div>,
credentials: [{ id: "linkedin", weight: "1" }],
requireSignature: true,
oAuthPopup: true,
requiresSignature: true,
requiresPopup: true,
popupUrl: "https://test.com/oauth",
documentationLink: "https://docs.example.com",
displayWeight: "1",
};
Expand Down Expand Up @@ -168,8 +173,8 @@ describe("PlatformVerification", () => {
<PlatformVerification
platform={{
...mockPlatform,
oAuthPopup: false,
requireSignature: false,
requiresSignature: false,
requiresPopup: false,
}}
onClose={mockOnClose}
generateSignatureCallback={mockGenerateSignature}
Expand All @@ -187,39 +192,40 @@ describe("PlatformVerification", () => {
});
});

it("handles missing address error", async () => {
const consoleSpy = jest.spyOn(console, "error").mockImplementation();

// Mock missing address in context
(QueryContext.useQueryContext as jest.Mock).mockReturnValue({
address: null,
});

const mockVerifyCredentials = jest.fn();
(usePassportScore.useWidgetVerifyCredentials as jest.Mock).mockReturnValue({
verifyCredentials: mockVerifyCredentials,
});
describe("Errors", () => {
mockExpectedConsoleErrorLog();

it("handles missing address error", async () => {
// Mock missing address in context
(QueryContext.useQueryContext as jest.Mock).mockReturnValue({
address: null,
});

const mockVerifyCredentials = jest.fn();
(
usePassportScore.useWidgetVerifyCredentials as jest.Mock
).mockReturnValue({
verifyCredentials: mockVerifyCredentials,
});

render(
<PlatformVerification
platform={mockPlatform}
onClose={mockOnClose}
generateSignatureCallback={mockGenerateSignature}
/>
);

render(
<PlatformVerification
platform={mockPlatform}
onClose={mockOnClose}
generateSignatureCallback={mockGenerateSignature}
/>
);
// Click verify button
fireEvent.click(screen.getByRole("button", { name: /verify/i }));

// Click verify button
fireEvent.click(screen.getByRole("button", { name: /verify/i }));
await waitFor(() => {
expect(
screen.getByText(/Unable to claim this Stamp/i)
).toBeInTheDocument();
});

await waitFor(() => {
expect(
screen.getByText(/Unable to claim this Stamp/i)
).toBeInTheDocument();
expect(mockVerifyCredentials).not.toHaveBeenCalled();
});

expect(mockVerifyCredentials).not.toHaveBeenCalled();

expect(consoleSpy).toHaveBeenCalled();
consoleSpy.mockRestore();
});
});
Loading

0 comments on commit 777abe4

Please sign in to comment.