Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest Tests for lnbitsService.ts, lnbitsServiceLocal.ts, setupTests.tsx #48

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/services/lnbitsService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// lnbitsService.test.ts
import {
getAccessToken,
createInvoice,
getWallets,
getUserWallets,
payInvoice
} from './lnbitsService'; // Adjust import as necessary

import {
expect,
describe,
test,
beforeEach,
jest
} from '@jest/globals';

// Mock the necessary dependencies (like fetch)
jest.mock('./lnbitsService');

describe('LNbitsService tests', () => {
const mockAccessToken = 'testAccessToken';
const mockAdminKey = 'adminKey';
const mockInKey = 'inKey';

beforeEach(() => {
jest.clearAllMocks();
});

test('getAccessToken should return cached token', async () => {
(getAccessToken as jest.Mock).mockResolvedValue(mockAccessToken);

const token = await getAccessToken('user', 'pass');
expect(token).toBe(mockAccessToken);
});

test('createInvoice should return payment request', async () => {
const mockPaymentRequest = 'lnbc1...';
(createInvoice as jest.Mock).mockResolvedValue(mockPaymentRequest);

const result = await createInvoice(mockInKey, 'walletId', 1000, 'test', {});
expect(result).toBe(mockPaymentRequest);
expect(createInvoice).toHaveBeenCalledWith(mockInKey, 'walletId', 1000, 'test', {});
});

test('getWallets should return filtered wallet data', async () => {
const mockWallets = [{ id: '1', name: 'testWallet' }];
(getWallets as jest.Mock).mockResolvedValue(mockWallets);

const wallets = await getWallets(mockAdminKey);
expect(wallets).toEqual(mockWallets);
expect(getWallets).toHaveBeenCalledWith(mockAdminKey, undefined, undefined);
});

test('getUserWallets should return user wallets', async () => {
const mockUserWallets = [{ id: 'wallet1', name: 'userWallet' }];
(getUserWallets as jest.Mock).mockResolvedValue(mockUserWallets);

const result = await getUserWallets(mockAdminKey, 'userId');
expect(result).toEqual(mockUserWallets);
expect(getUserWallets).toHaveBeenCalledWith(mockAdminKey, 'userId');
});

test('payInvoice should resolve payment', async () => {
const mockPaymentResult = { payment_hash: '123abc' };
(payInvoice as jest.Mock).mockResolvedValue(mockPaymentResult);

const result = await payInvoice(mockAdminKey, 'paymentRequest', {});
expect(result).toEqual(mockPaymentResult);
expect(payInvoice).toHaveBeenCalledWith(mockAdminKey, 'paymentRequest', {});
});
});

84 changes: 84 additions & 0 deletions tabs/src/services/lnbitsServiceLocal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// lnbitsServiceLocal.test.ts
import {
getAccessToken,
getWallets,
getWalletBalance,
getUserWallets,
createInvoice,
payInvoice
} from './lnbitsServiceLocal'; // Adjust the path if necessary
import {
expect,
describe,
test,
beforeEach,
jest
} from '@jest/globals';

const mockFetch = global.fetch as jest.MockedFunction<typeof fetch>;

describe('lnbitsServiceLocal Tests', () => {
const mockAccessToken: string = 'mockedAccessToken'; // Explicit type
const mockInKey: string = 'mockedInKey';
const mockAdminKey: string = 'mockedAdminKey';
const mockPaymentRequest: string = 'lnbc1...'; // Explicit type
const mockPaymentResult: { payment_hash: string } = { payment_hash: '123abc' }; // Explicit type
const mockWallets: { id: string; name: string }[] = [{ id: 'wallet1', name: 'testWallet' }]; // Explicit type
const mockUserWallets: { id: string; name: string }[] = [{ id: 'wallet1', name: 'userWallet' }]; // Explicit type

beforeEach(() => {
jest.clearAllMocks();
localStorage.setItem('accessToken', mockAccessToken);
});

test('getAccessToken should return the access token from localStorage', async () => {
const token = await getAccessToken('user', 'password');
expect(token).toBe(mockAccessToken);
expect(fetch).not.toHaveBeenCalled(); // Should not make an API call if token is cached
});

test('getWallets should return a filtered list of wallets', async () => {
mockFetch.mockResolvedValueOnce({
ok: true,
json: async () => mockWallets,
} as Response);

const result = await getWallets();
expect(result).toEqual(mockWallets);
expect(fetch).toHaveBeenCalledWith(expect.any(String), expect.any(Object));
});

test('getUserWallets should return user wallets', async () => {
mockFetch.mockResolvedValueOnce({
ok: true,
json: async () => mockUserWallets,
} as Response);

const result = await getUserWallets(mockAdminKey, 'userId');
expect(result).toEqual(mockUserWallets);
expect(fetch).toHaveBeenCalled();
});

test('createInvoice should create an invoice and return the payment request', async () => {
mockFetch.mockResolvedValueOnce({
ok: true,
json: async () => ({ payment_request: mockPaymentRequest }),
} as Response);

const paymentRequest = await createInvoice(mockInKey, 'walletId', 1000, 'test memo');
expect(paymentRequest).toBe(mockPaymentRequest);
expect(fetch).toHaveBeenCalled();
});

test('payInvoice should resolve the payment successfully', async () => {
mockFetch.mockResolvedValueOnce({
ok: true,
json: async () => mockPaymentResult,
} as Response);

const result = await payInvoice(mockAdminKey, 'paymentRequest');
expect(result).toEqual(mockPaymentResult);
expect(fetch).toHaveBeenCalled();
});
});

15 changes: 15 additions & 0 deletions tabs/src/setupTests.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// setupTests.test.tsx
import '@testing-library/jest-dom';
import {
expect,
describe,
test,
} from '@jest/globals';

describe('Jest DOM Setup', () => {
test('jest-dom is properly configured', () => {
const div = document.createElement('div');
div.textContent = 'React Testing';
expect(div).toHaveTextContent(/react/i);
});
});