|
| 1 | +import React from "react"; |
| 2 | +import { render, screen, fireEvent, waitFor, waitForElementToBeRemoved } from "@testing-library/react"; |
| 3 | +import { BrightidCard } from "../../../components/ProviderCards"; |
| 4 | + |
| 5 | +import { UserContext, UserContextState } from "../../../context/userContext"; |
| 6 | +import { mockAddress, mockWallet } from "../../../__test-fixtures__/onboardHookValues"; |
| 7 | +import { STAMP_PROVIDERS } from "../../../config/providers"; |
| 8 | +import { brightidStampFixture } from "../../../__test-fixtures__/databaseStorageFixtures"; |
| 9 | +import { SUCCESFUL_BRIGHTID_RESULT } from "../../../__test-fixtures__/verifiableCredentialResults"; |
| 10 | +import { fetchVerifiableCredential } from "@dpopp/identity/dist/commonjs"; |
| 11 | + |
| 12 | +jest.mock("@dpopp/identity/dist/commonjs", () => ({ |
| 13 | + fetchVerifiableCredential: jest.fn(), |
| 14 | +})); |
| 15 | +jest.mock("../../../utils/onboard.ts"); |
| 16 | + |
| 17 | +const mockHandleConnection = jest.fn(); |
| 18 | +const mockCreatePassport = jest.fn(); |
| 19 | +const handleAddStamp = jest.fn(); |
| 20 | +const mockUserContext: UserContextState = { |
| 21 | + userDid: "mockUserDid", |
| 22 | + loggedIn: true, |
| 23 | + passport: undefined, |
| 24 | + isLoadingPassport: false, |
| 25 | + allProvidersState: { |
| 26 | + Brightid: { |
| 27 | + providerSpec: STAMP_PROVIDERS.Brightid, |
| 28 | + stamp: undefined, |
| 29 | + }, |
| 30 | + }, |
| 31 | + handleAddStamp: handleAddStamp, |
| 32 | + handleCreatePassport: mockCreatePassport, |
| 33 | + handleConnection: mockHandleConnection, |
| 34 | + address: mockAddress, |
| 35 | + wallet: mockWallet, |
| 36 | + signer: undefined, |
| 37 | + walletLabel: mockWallet.label, |
| 38 | +}; |
| 39 | + |
| 40 | +function setupFetchStub(valid: any) { |
| 41 | + return function fetchStub(_url: any) { |
| 42 | + return new Promise((resolve) => { |
| 43 | + resolve({ |
| 44 | + json: () => |
| 45 | + Promise.resolve({ |
| 46 | + valid, |
| 47 | + }), |
| 48 | + }); |
| 49 | + }); |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +describe("when user has not verfied with BrightId Provider", () => { |
| 54 | + it("should display a verify button", () => { |
| 55 | + render( |
| 56 | + <UserContext.Provider value={mockUserContext}> |
| 57 | + <BrightidCard /> |
| 58 | + </UserContext.Provider> |
| 59 | + ); |
| 60 | + |
| 61 | + const initialVerifyButton = screen.queryByTestId("button-verify-brightid"); |
| 62 | + |
| 63 | + expect(initialVerifyButton).toBeInTheDocument(); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +describe("when user has verified with BrightId Provider", () => { |
| 68 | + it("should display that a verified credential was returned", () => { |
| 69 | + render( |
| 70 | + <UserContext.Provider |
| 71 | + value={{ |
| 72 | + ...mockUserContext, |
| 73 | + allProvidersState: { |
| 74 | + Brightid: { |
| 75 | + providerSpec: STAMP_PROVIDERS.Brightid, |
| 76 | + stamp: brightidStampFixture, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }} |
| 80 | + > |
| 81 | + <BrightidCard /> |
| 82 | + </UserContext.Provider> |
| 83 | + ); |
| 84 | + |
| 85 | + const brightidVerified = screen.queryByText(/Verified/); |
| 86 | + |
| 87 | + expect(brightidVerified).toBeInTheDocument(); |
| 88 | + }); |
| 89 | +}); |
| 90 | + |
| 91 | +describe("when the verify button is clicked", () => { |
| 92 | + let originalFetch: any; |
| 93 | + beforeEach(() => { |
| 94 | + originalFetch = global.fetch; |
| 95 | + global.fetch = jest.fn().mockImplementation(setupFetchStub(true)); |
| 96 | + }); |
| 97 | + |
| 98 | + afterEach(() => { |
| 99 | + global.fetch = originalFetch; |
| 100 | + jest.clearAllMocks(); |
| 101 | + }); |
| 102 | + |
| 103 | + describe("and when a successful BrightId result is returned", () => { |
| 104 | + beforeEach(() => { |
| 105 | + (fetchVerifiableCredential as jest.Mock).mockResolvedValue(SUCCESFUL_BRIGHTID_RESULT); |
| 106 | + }); |
| 107 | + |
| 108 | + it("the modal displays the verify button", async () => { |
| 109 | + render( |
| 110 | + <UserContext.Provider value={mockUserContext}> |
| 111 | + <BrightidCard /> |
| 112 | + </UserContext.Provider> |
| 113 | + ); |
| 114 | + |
| 115 | + const initialVerifyButton = screen.queryByTestId("button-verify-brightid"); |
| 116 | + |
| 117 | + fireEvent.click(initialVerifyButton!); |
| 118 | + |
| 119 | + const verifyModal = await screen.findByRole("dialog"); |
| 120 | + const verifyModalButton = screen.getByTestId("modal-verify"); |
| 121 | + |
| 122 | + expect(verifyModal).toBeInTheDocument(); |
| 123 | + |
| 124 | + await waitFor(() => { |
| 125 | + expect(verifyModalButton).toBeInTheDocument(); |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + it("clicking verify adds the stamp", async () => { |
| 130 | + render( |
| 131 | + <UserContext.Provider value={mockUserContext}> |
| 132 | + <BrightidCard /> |
| 133 | + </UserContext.Provider> |
| 134 | + ); |
| 135 | + |
| 136 | + const initialVerifyButton = screen.queryByTestId("button-verify-brightid"); |
| 137 | + |
| 138 | + // Click verify button on brightid card |
| 139 | + fireEvent.click(initialVerifyButton!); |
| 140 | + |
| 141 | + // Wait to see the verify button on the modal |
| 142 | + await waitFor(() => { |
| 143 | + const verifyModalButton = screen.getByTestId("modal-verify"); |
| 144 | + expect(verifyModalButton).toBeInTheDocument(); |
| 145 | + }); |
| 146 | + |
| 147 | + const finalVerifyButton = screen.queryByRole("button", { |
| 148 | + name: /Verify/, |
| 149 | + }); |
| 150 | + |
| 151 | + // Click the verify button on modal |
| 152 | + fireEvent.click(finalVerifyButton!); |
| 153 | + |
| 154 | + expect(handleAddStamp).toBeCalled(); |
| 155 | + }); |
| 156 | + |
| 157 | + it("clicking cancel closes the modal and a stamp should not be added", async () => { |
| 158 | + (fetchVerifiableCredential as jest.Mock).mockResolvedValue(SUCCESFUL_BRIGHTID_RESULT); |
| 159 | + render( |
| 160 | + <UserContext.Provider value={mockUserContext}> |
| 161 | + <BrightidCard /> |
| 162 | + </UserContext.Provider> |
| 163 | + ); |
| 164 | + |
| 165 | + const initialVerifyButton = screen.queryByTestId("button-verify-brightid"); |
| 166 | + |
| 167 | + fireEvent.click(initialVerifyButton!); |
| 168 | + |
| 169 | + // Wait to see the cancel button on the modal |
| 170 | + let modalCancelButton: HTMLElement | null = null; |
| 171 | + await waitFor(() => { |
| 172 | + modalCancelButton = screen.queryByRole("button", { |
| 173 | + name: /Cancel/, |
| 174 | + }); |
| 175 | + expect(modalCancelButton).toBeInTheDocument(); |
| 176 | + }); |
| 177 | + |
| 178 | + fireEvent.click(modalCancelButton!); |
| 179 | + |
| 180 | + expect(handleAddStamp).not.toBeCalled(); |
| 181 | + |
| 182 | + await waitForElementToBeRemoved(modalCancelButton); |
| 183 | + expect(modalCancelButton).not.toBeInTheDocument(); |
| 184 | + }); |
| 185 | + }); |
| 186 | + |
| 187 | + describe("and when a failed Bright Id result is returned", () => { |
| 188 | + it("modal displays steps to get sponsored", async () => { |
| 189 | + global.fetch = jest.fn().mockImplementation(setupFetchStub(false)); |
| 190 | + (fetchVerifiableCredential as jest.Mock).mockRejectedValue("ERROR"); |
| 191 | + render( |
| 192 | + <UserContext.Provider value={mockUserContext}> |
| 193 | + <BrightidCard /> |
| 194 | + </UserContext.Provider> |
| 195 | + ); |
| 196 | + |
| 197 | + const initialVerifyButton = screen.queryByTestId("button-verify-brightid"); |
| 198 | + |
| 199 | + fireEvent.click(initialVerifyButton!); |
| 200 | + |
| 201 | + const verifyModal = await screen.findByRole("dialog"); |
| 202 | + const triggerSponsorButton = screen.queryByTestId("button-sponsor-brightid"); |
| 203 | + const verifyModalText = screen.getByText( |
| 204 | + "A verifiable credential was not generated for your address. Follow the steps below to qualify:" |
| 205 | + ); |
| 206 | + |
| 207 | + expect(verifyModal).toBeInTheDocument(); |
| 208 | + await waitFor(() => { |
| 209 | + expect(verifyModalText).toBeInTheDocument(); |
| 210 | + }); |
| 211 | + expect(triggerSponsorButton).toBeInTheDocument(); |
| 212 | + }); |
| 213 | + }); |
| 214 | +}); |
0 commit comments