Skip to content

Commit

Permalink
unit tests for ecosystem hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
maycon-mello committed Oct 29, 2024
1 parent 38e0ed2 commit 25e45bc
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
74 changes: 74 additions & 0 deletions packages/react-native/lib/ecosystem-tools/ecosystemHooks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { renderHook } from '@testing-library/react-hooks';
import { useEcosystems } from './ecosystemHooks';
import { getEcosystems } from '@docknetwork/wallet-sdk-core/src/ecosystem-tools';
import { getWallet } from '../wallet';
import { captureException } from '@docknetwork/wallet-sdk-core/src/helpers';

jest.mock('@docknetwork/wallet-sdk-core/src/ecosystem-tools', () => ({
getEcosystems: jest.fn(),
}));

jest.mock('../wallet', () => ({
getWallet: jest.fn(),
}));

jest.mock('@docknetwork/wallet-sdk-core/src/helpers', () => ({
captureException: jest.fn(),
}));

const mockEcosystems = {
eco1: { govFramework: 'hex-url-1' },
eco2: { govFramework: 'hex-url-2' },
};

const mockWallet = {
getNetworkId: jest.fn(() => 'test-network'),
};

getWallet.mockReturnValue(mockWallet);

describe('useEcosystems', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should fetch and format ecosystem data', async () => {
getEcosystems.mockResolvedValue(mockEcosystems);

const { result, waitForNextUpdate } = renderHook(() =>
useEcosystems({ issuer: 'issuerId', verifier: 'verifierId', schemaId: 'schemaId' })
);

expect(result.current.isLoading).toBe(true);

await waitForNextUpdate();

expect(getEcosystems).toHaveBeenCalledWith({
issuerDID: 'issuerId',
verifierDID: 'verifierId',
schemaId: 'schemaId',
networkId: 'test-network',
});

expect(result.current.isLoading).toBe(false);
expect(result.current.isError).toBe(false);
expect(result.current.ecosystems).toEqual(expect.any(Array));
});

it('should handle error while fetching ecosystems', async () => {
const error = new Error('Error fetching ecosystems');
getEcosystems.mockRejectedValue(error);

const { result, waitForNextUpdate } = renderHook(() =>
useEcosystems({ issuer: 'issuerId', verifier: 'verifierId', schemaId: 'schemaId' })
);

expect(result.current.isLoading).toBe(true);

await waitForNextUpdate();

expect(result.current.isLoading).toBe(false);
expect(result.current.isError).toBe(true);
expect(captureException).toHaveBeenCalledWith(error);
});
});
4 changes: 2 additions & 2 deletions packages/react-native/lib/ecosystem-tools/ecosystemHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ const getMetadata = async govFramework => {

const formatEcosystemData = async ecosystems => {
const formattedEcosystems = [];
const ecosystemsList = Object.entries(ecosystems);
const ecosystemsList: any = Object.entries(ecosystems);

for (let i = 0; i < ecosystemsList.length; i++) {
let ecosystemData = {};
let ecosystemData: any = {};
ecosystemData.trustRegistryId = ecosystemsList[i][0];
ecosystemData = {...ecosystemData, ...ecosystemsList[i][1]};

Expand Down

0 comments on commit 25e45bc

Please sign in to comment.