Skip to content

Commit

Permalink
added tests for new functionallity
Browse files Browse the repository at this point in the history
  • Loading branch information
vadim-ghostman committed Jan 23, 2025
1 parent 2e75d34 commit 0c8723d
Showing 1 changed file with 137 additions and 2 deletions.
139 changes: 137 additions & 2 deletions frontend/test/services/wallet.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { connect } from 'starknetkit';
import { checkForCRMToken, connectWallet, getTokenBalances, getBalances, logout } from '../../src/services/wallet';
import { InjectedConnector } from 'starknetkit/injected';
import { checkForCRMToken, connectWallet, getTokenBalances, getBalances, logout, getWallet, getConnectors } from '../../src/services/wallet';
import { ETH_ADDRESS, STRK_ADDRESS, USDC_ADDRESS } from '../../src/utils/constants';

jest.mock('starknetkit', () => ({
Expand All @@ -10,7 +11,7 @@ jest.mock('starknetkit', () => ({
jest.mock(
'starknetkit/injected',
() => ({
InjectedConnector: jest.fn(),
InjectedConnector: jest.fn().mockImplementation((options) => options),
}),
{ virtual: true }
);
Expand Down Expand Up @@ -71,6 +72,140 @@ describe('Wallet Services', () => {
});
});

describe('getConnectors', () => {
it('should return connectors array with injected connectors', () => {
const mockGetItem = jest.fn();
Object.defineProperty(window, 'localStorage', {
value: {
getItem: mockGetItem.mockReturnValue(null),
},
writable: true,
});

const connectors = getConnectors();

expect(connectors).toEqual([
new InjectedConnector({ options: { id: 'argentX' } }),
new InjectedConnector({ options: { id: 'braavos' } }),
]);
});

['argentX', 'braavos'].forEach((connector) => {
it(`should return connectors array with injected connectors from local storage (${connector})`, () => {
const mockGetItem = jest.fn();
Object.defineProperty(window, 'localStorage', {
value: {
getItem: mockGetItem.mockReturnValue(connector),
},
writable: true,
});

const connectors = getConnectors();

expect(connectors).toEqual([
new InjectedConnector({ options: { id: connector } }),
]);
})
});
});

describe('getWallet', () => {
const testCases = [
{ connectorId: 'argentX', expectedAddress: '0x123' },
{ connectorId: 'braavos', expectedAddress: '0x456' },
];

testCases.forEach(({ connectorId, expectedAddress }) => {
it(`should return wallet object if wallet is connected with ${connectorId}`, async () => {
const mockStarknet = {
wallet: {
isConnected: true,
enable: jest.fn(),
selectedAddress: expectedAddress,
},
};

connect.mockResolvedValue(mockStarknet);

const mockGetItem = jest.fn().mockReturnValue(connectorId);
const mockSetItem = jest.fn();

Object.defineProperty(window, 'localStorage', {
value: {
getItem: mockGetItem,
},
writable: true,
});

const wallet = await getWallet();

expect(mockGetItem).toHaveBeenCalledWith('starknetLastConnectedWallet');

expect(connect).toHaveBeenCalledWith(
expect.objectContaining({
connectors: expect.arrayContaining([
expect.objectContaining({
options: expect.objectContaining({
id: connectorId,
}),
}),
]),
modalMode: 'neverAsk',
})
);
expect(wallet.isConnected).toBe(true);
expect(wallet.enable).toHaveBeenCalled();
expect(wallet.selectedAddress).toBe(expectedAddress);
});
})

it('should return wallet object if wallet is not choosen before', async () => {
const mockStarknet = {
wallet: {
isConnected: true,
enable: jest.fn(),
selectedAddress: '0x123',
},
};

connect.mockResolvedValue(mockStarknet);

const mockGetItem = jest.fn().mockReturnValue(null);

Object.defineProperty(window, 'localStorage', {
value: {
getItem: mockGetItem,
},
writable: true,
});

const wallet = await getWallet();

expect(mockGetItem).toHaveBeenCalledWith('starknetLastConnectedWallet');

expect(connect).toHaveBeenCalledWith(
expect.objectContaining({
connectors: expect.arrayContaining([
expect.objectContaining({
options: expect.objectContaining({
id: 'argentX'
}),
}),
expect.objectContaining({
options: expect.objectContaining({
id: 'braavos'
}),
}),
]),
modalMode: 'neverAsk'
})
);
expect(wallet.isConnected).toBe(true);
expect(wallet.enable).toHaveBeenCalled();
expect(wallet.selectedAddress).toBe('0x123');
});
});

describe('connectWallet', () => {
it('should successfully connect wallet and return address', async () => {
const mockStarknet = {
Expand Down

0 comments on commit 0c8723d

Please sign in to comment.