Skip to content

Commit

Permalink
Add test for LeatherService
Browse files Browse the repository at this point in the history
  • Loading branch information
ronaldsg20 committed Feb 27, 2025
1 parent 6c0bfda commit c81abb8
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions tests/unit/common/services/LeatherService.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import sinon from 'sinon';
import LeatherService from '@/common/services/LeatherService';
import * as constants from '@/common/store/constants';
import { LeatherProvider } from '@leather.io/rpc';
import { UserSession } from '@stacks/connect';
import { BtcAccount } from '@/common/types';

describe('LeatherService', () => {
let leatherService: LeatherService;
let userSessionStub: sinon.SinonStubbedInstance<UserSession>;

beforeEach(() => {
userSessionStub = sinon.createStubInstance(UserSession);
leatherService = new LeatherService();
leatherService.userSession = userSessionStub as unknown as UserSession;
});

afterEach(() => {
sinon.restore();
});

it('should return the correct wallet name', () => {
expect(leatherService.name()).toEqual(constants.WALLET_NAMES.LEATHER);
});

it('should return available accounts', () => {
const accounts = leatherService.availableAccounts();
expect(accounts).toEqual([BtcAccount.BITCOIN_NATIVE_SEGWIT_ADDRESS]);
});

it('should check if user is connected', async () => {
userSessionStub.isUserSignedIn.returns(true);
const isConnected = await leatherService.isConnected();
expect(isConnected).toBeTruthy();
});

it('should get account addresses', async () => {
const addressesResponse = {
result: {
addresses: [
{
address: 'test-address',
derivationPath: 'm/84\'/0\'/0\'/0/0',
publicKey: 'test-public-key',
},
],
},
};
const stubProvider = {
request: sinon.stub().resolves(addressesResponse),
};
// eslint-disable-next-line dot-notation
leatherService['btcProvider'] = stubProvider as unknown as typeof LeatherProvider;

const addresses = await leatherService.getAccountAddresses();
expect(addresses).toEqual([
{
address: 'test-address',
derivationPath: 'm/84\'/0\'/0\'/0/0',
publicKey: 'test-public-key',
},
]);
expect(stubProvider.request.calledOnceWith('getAddresses')).toBeTruthy();
});
});

0 comments on commit c81abb8

Please sign in to comment.