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

[Unit Tests]: getBalances and setUsdToSatsExchangeRate #1066

Merged
merged 1 commit into from
Feb 11, 2025
Merged
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
146 changes: 146 additions & 0 deletions src/store/__test__/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3540,3 +3540,149 @@ describe('saveProfile', () => {
});
});
});

describe('MainStore.getBalances', () => {
let mainStore: MainStore;
let fetchStub: sinon.SinonStub;

beforeEach(() => {
mainStore = new MainStore();
fetchStub = sinon.stub(window, 'fetch');
});

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

it('Valid Public Key', async () => {
const mockBalances = [{ asset: 'BTC', amount: 100 }];
fetchStub.resolves({
ok: true,
status: 200,
json: () => Promise.resolve(mockBalances)
} as Response);

waitFor(async () => {
const result = await mainStore.getBalances('validPubKey123');
expect(result).toEqual(mockBalances);
});
});

it('Valid Public Key with No Balances', async () => {
fetchStub.resolves({
ok: true,
status: 200,
json: () => Promise.resolve([])
} as Response);

waitFor(async () => {
const result = await mainStore.getBalances('validPubKey123');
expect(result).toEqual([]);
});
});

it('Empty Public Key', async () => {
const result = await mainStore.getBalances('');
expect(result).toBeUndefined();
});

it('Extremely Long Public Key', async () => {
const longPubKey = 'a'.repeat(10000);
const mockBalances = [{ asset: 'BTC', amount: 100 }];
fetchStub.resolves({
ok: true,
status: 200,
json: () => Promise.resolve(mockBalances)
} as Response);

waitFor(async () => {
const result = await mainStore.getBalances(longPubKey);
expect(result).toEqual(mockBalances);
});
});

it('Invalid Public Key Format', async () => {
const result = await mainStore.getBalances('invalid!@#$%');
expect(result).toBeUndefined();
});

it('Null Public Key', async () => {
const result = await mainStore.getBalances(null);
expect(result).toBeUndefined();
});

it('Network Error', async () => {
fetchStub.rejects(new Error('Network error'));
const result = await mainStore.getBalances('validPubKey123');
expect(result).toBeUndefined();
});

it('Non-JSON Response', async () => {
fetchStub.resolves({
ok: true,
status: 200,
json: () => Promise.reject(new Error('Invalid JSON'))
} as Response);

const result = await mainStore.getBalances('validPubKey123');
expect(result).toBeUndefined();
});

it('High Volume of Balances', async () => {
const largeBalancesArray = Array.from({ length: 1000 }, (_: any, i: number) => ({
asset: `Asset${i}`,
amount: i * 100
}));
fetchStub.resolves({
ok: true,
status: 200,
json: () => Promise.resolve(largeBalancesArray)
} as Response);

waitFor(async () => {
const result = await mainStore.getBalances('validPubKey123');
expect(result).toEqual(largeBalancesArray);
expect(result.length).toBe(1000);
});
});

it('Server Timeout', async () => {
fetchStub.rejects(new Error('TimeoutError'));
const result = await mainStore.getBalances('validPubKey123');
expect(result).toBeUndefined();
});

it('Server Returns Error Code', async () => {
fetchStub.resolves({
ok: false,
status: 500,
json: () => Promise.resolve({ error: 'Internal server error' })
} as Response);

const result = await mainStore.getBalances('validPubKey123');
expect(result).toBeUndefined();
});

it('Malformed URL', async () => {
const invalidPubKey = 'invalid pubkey with spaces';
const result = await mainStore.getBalances(invalidPubKey);
expect(result).toBeUndefined();
});

it('Unexpected Data Structure', async () => {
const unexpectedData = {
notAnArray: true,
someOtherField: 'value'
};
fetchStub.resolves({
ok: true,
status: 200,
json: () => Promise.resolve(unexpectedData)
} as Response);

waitFor(async () => {
const result = await mainStore.getBalances('validPubKey123');
expect(result).toEqual(unexpectedData);
});
});
});
50 changes: 50 additions & 0 deletions src/store/__test__/ui.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,3 +892,53 @@ describe('setToasts', () => {
expect(uiStore.toasts).toEqual(emptyToasts);
});
});

describe('UiStore.setUsdToSatsExchangeRate', () => {
let uiStore: UiStore;

beforeEach(() => {
uiStore = new UiStore();
});

it('Basic Functionality: Positive Integer', () => {
uiStore.setUsdToSatsExchangeRate(100);
expect(uiStore.usdToSatsExchangeRate).toBe(100);
});

it('Basic Functionality: Zero', () => {
uiStore.setUsdToSatsExchangeRate(0);
expect(uiStore.usdToSatsExchangeRate).toBe(0);
});

it('Basic Functionality: Negative Integer', () => {
uiStore.setUsdToSatsExchangeRate(-100);
expect(uiStore.usdToSatsExchangeRate).toBe(-100);
});

it('Edge Case: Very Large Number', () => {
const largeNumber = Number.MAX_SAFE_INTEGER;
uiStore.setUsdToSatsExchangeRate(largeNumber);
expect(uiStore.usdToSatsExchangeRate).toBe(largeNumber);
});

it('Edge Case: Very Small Positive Number', () => {
const smallNumber = 0.00000001;
uiStore.setUsdToSatsExchangeRate(smallNumber);
expect(uiStore.usdToSatsExchangeRate).toBe(smallNumber);
});

it('Special Case: Floating Point Number', () => {
uiStore.setUsdToSatsExchangeRate(3.14159);
expect(uiStore.usdToSatsExchangeRate).toBe(3.14159);
});

it('Special Case: NaN (Not a Number)', () => {
uiStore.setUsdToSatsExchangeRate(NaN);
expect(uiStore.usdToSatsExchangeRate).toBe(NaN);
});

it('Special Case: Negative Floating Point Number', () => {
uiStore.setUsdToSatsExchangeRate(-3.14159);
expect(uiStore.usdToSatsExchangeRate).toBe(-3.14159);
});
});
Loading