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

#660 | Unit tests fix #722

Merged
merged 6 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions jest.init.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { QBtn } from 'quasar';

config.global.mocks = {
$t: str => str,
$warningNotification: jest.fn(),
};


config.global.components = {
'q-btn': QBtn,
};
Expand Down
1 change: 0 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script lang="ts">
import { getAntelope, useChainStore } from 'src/antelope';
import { ComplexMessage } from 'src/antelope/config';
import EVMChainSettings from 'src/antelope/chains/EVMChainSettings';
import { TELOS_CHAIN_IDS } from 'src/antelope/chains/chain-constants';
import packageInfo from '../package.json';
Expand Down
1 change: 0 additions & 1 deletion src/antelope/stores/balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
EvmABI,
addressString,
AntelopeError,

} from 'src/antelope/types';
import { createTraceFunction, isTracingAll } from 'src/antelope/stores/feedback';
import NativeChainSettings from 'src/antelope/chains/NativeChainSettings';
Expand Down
18 changes: 16 additions & 2 deletions src/pages/home/EVMLoginButtons.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
<script lang="ts">
import { CURRENT_CONTEXT, getAntelope, useAccountStore, useChainStore, useEVMStore, useFeedbackStore, usePlatformStore } from 'src/antelope';
import { ComponentInternalInstance, PropType, computed, defineComponent, getCurrentInstance, ref, watch } from 'vue';
import {
CURRENT_CONTEXT,
getAntelope,
useAccountStore,
useChainStore,
useFeedbackStore,
usePlatformStore,
} from 'src/antelope';
import {
ComponentInternalInstance,
PropType,
computed,
defineComponent,
getCurrentInstance,
ref,
} from 'vue';
import { QSpinnerFacebook } from 'quasar';
import { OreIdAuth } from 'src/antelope/wallets';
import { Menu } from 'src/pages/home/MenuType';
Expand Down
234 changes: 125 additions & 109 deletions test/jest/__tests__/antelope/stores/balances.spec.ts
Original file line number Diff line number Diff line change
@@ -1,114 +1,130 @@
// https://github.com/telosnetwork/telos-wallet/issues/660
/* eslint-disable @typescript-eslint/no-explicit-any */
import { setActivePinia, createPinia } from 'pinia';

import {
useFeedbackStore,
FeedbackActions,
MockData,
useChainStore,
getAntelope,
useAccountStore,
useContractStore,
CURRENT_CONTEXT,
} from 'test/jest/utils/antelope/index';

import { useBalancesStore } from 'src/antelope/stores/balances';
import { ethers } from 'ethers';
import { TokenClass } from 'src/antelope/types/TokenClass';

jest.mock('src/antelope/chains/EVMChainSettings', () => ({}));
jest.mock('src/antelope/chains/NativeChainSettings', () => ({}));
jest.mock('src/api/price', () => ({}));
jest.mock('src/antelope/types', () => {
const { TokenClass, TokenMarketData } = jest.requireActual('src/antelope/types/TokenClass');
const { TokenBalance } = jest.requireActual('src/antelope/types');
return {
TokenBalance,
TokenClass,
TokenMarketData,
};
});
jest.mock('src/antelope', () => ({
getAntelope,
useAccountStore,
useContractStore,
useFeedbackStore,
useChainStore,
CURRENT_CONTEXT,
}));


describe('Antelope Balance Store', () => {
let store: any;

beforeEach(() => {
jest.clearAllMocks();
setActivePinia(createPinia());
store = useBalancesStore();
});

describe('temporary', () => {
it('should prevent jest from thowing a "Your test suite must contain at least one test" error', () => {
expect(true).toBe(true);
describe('Initial state', () => {
test('__balances should be {}', () => {
expect(JSON.stringify(store.__balances)).toBe('{}');
});
test('__wagmiSystemTokenTransferConfig should be {}', () => {
expect(JSON.stringify(store.__wagmiSystemTokenTransferConfig)).toBe('{"current":null,"logged":null}');
});
test('__wagmiTokenTransferConfig should be {}', () => {
expect(JSON.stringify(store.__wagmiTokenTransferConfig)).toBe('{"current":null,"logged":null}');
});
});
});

describe('Initializing the store', () => {
test('should initialize the store', () => {
expect(useFeedbackStore).not.toHaveBeenCalled();
expect(FeedbackActions.setDebug).not.toHaveBeenCalled();

store.init();

// /* eslint-disable @typescript-eslint/no-explicit-any */
// import { setActivePinia, createPinia } from 'pinia';

// import {
// useFeedbackStore,
// FeedbackActions,
// MockData,
// } from 'test/jest/utils/antelope/index';

// import { useBalancesStore } from 'src/antelope/stores/balances';
// import { TokenClass } from 'src/antelope/types';
// import { ethers } from 'ethers';

// describe('Antelope Balance Store', () => {
// let store: any;

// beforeEach(() => {
// jest.clearAllMocks();
// setActivePinia(createPinia());
// store = useBalancesStore();
// });

// describe('Initial state', () => {
// test('__balances should be {}', () => {
// expect(JSON.stringify(store.__balances)).toBe('{}');
// });
// test('__wagmiSystemTokenTransferConfig should be {}', () => {
// expect(JSON.stringify(store.__wagmiSystemTokenTransferConfig)).toBe('{"current":null,"logged":null}');
// });
// test('__wagmiTokenTransferConfig should be {}', () => {
// expect(JSON.stringify(store.__wagmiTokenTransferConfig)).toBe('{"current":null,"logged":null}');
// });
// });

// describe('Initializing the store', () => {
// test('should initialize the store', () => {
// expect(useFeedbackStore).not.toHaveBeenCalled();
// expect(FeedbackActions.setDebug).not.toHaveBeenCalled();

// store.init();

// expect(useFeedbackStore).toHaveBeenCalled();
// expect(FeedbackActions.setDebug).toHaveBeenCalled();
// });
// });

// describe('updateBalancesForAccount should update __balances', () => {
// test('with the minimum system tokens when has zero balances for all tokens', async () => {
// const label = 'label';

// const authenticator = {
// getSystemTokenBalance: () => ethers.constants.Zero,
// getERC20TokenBalance: () => ({
// then: jest.fn().mockImplementation((cb: any) => {
// cb(ethers.constants.Zero);
// return {
// catch: jest.fn(),
// };
// }),
// }),
// };

// const account = {
// ...MockData.Account,
// authenticator,
// };

// await store.updateBalancesForAccount(label, account);

// const expected = [
// MockData.Token.SYSTEM_TOKEN.symbol,
// MockData.Token.WRAPPED_TOKEN.symbol,
// MockData.Token.STAKED_TOKEN.symbol,
// ];

// expect((store.__balances[label] as TokenClass[]).map((x: TokenClass) => x.symbol)).toStrictEqual(expected);
// });

// test('with system token first despite not having the higher balance', async () => {
// const label = 'label';
// const account = MockData.Account;

// // we need to spy on store.sortBalances and see if it is called with the right label argument
// const sortBalancesSpy = jest.spyOn(store, 'sortBalances');

// await store.updateBalancesForAccount(label, account);

// // now we check if the spy was called with the right label argument
// expect(sortBalancesSpy).toHaveBeenCalledWith('label');

// const expected: string[] = [
// MockData.Token.SYSTEM_TOKEN.symbol,
// MockData.Token.WRAPPED_TOKEN.symbol,
// MockData.Token.B_TOKEN.symbol,
// MockData.Token.A_TOKEN.symbol,
// MockData.Token.STAKED_TOKEN.symbol,
// ];

// expect((store.__balances[label] as TokenClass[]).map((x: TokenClass) => x.symbol)).toStrictEqual(expected);
// });
// });


// });
expect(useFeedbackStore).toHaveBeenCalled();
expect(FeedbackActions.setDebug).toHaveBeenCalled();
});
});

describe('updateBalancesForAccount should update __balances', () => {
test('with the minimum system tokens when has zero balances for all tokens', async () => {
const label = 'label';

const authenticator = {
getSystemTokenBalance: () => ethers.constants.Zero,
getERC20TokenBalance: () => ({
then: jest.fn().mockImplementation((cb: any) => {
cb(ethers.constants.Zero);
return {
catch: jest.fn(),
};
}),
}),
};

const account = {
...MockData.Account,
authenticator,
};

await store.updateBalancesForAccount(label, account);

const expected = [
MockData.Token.SYSTEM_TOKEN.symbol,
MockData.Token.WRAPPED_TOKEN.symbol,
MockData.Token.STAKED_TOKEN.symbol,
];

expect((store.__balances[label] as TokenClass[]).map((x: TokenClass) => x.symbol)).toStrictEqual(expected);
});

test('with system token first despite not having the higher balance', async () => {
const label = 'label';
const account = MockData.Account;

// we need to spy on store.sortBalances and see if it is called with the right label argument
const sortBalancesSpy = jest.spyOn(store, 'sortBalances');

await store.updateBalancesForAccount(label, account);

// now we check if the spy was called with the right label argument
expect(sortBalancesSpy).toHaveBeenCalledWith('label');

const expected: string[] = [
MockData.Token.SYSTEM_TOKEN.symbol,
MockData.Token.WRAPPED_TOKEN.symbol,
MockData.Token.B_TOKEN.symbol,
MockData.Token.A_TOKEN.symbol,
MockData.Token.STAKED_TOKEN.symbol,
];

expect((store.__balances[label] as TokenClass[]).map((x: TokenClass) => x.symbol)).toStrictEqual(expected);
});
});
});

Loading
Loading