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 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
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
8 changes: 4 additions & 4 deletions src/i18n/en-us/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,15 @@ export default {
column_description_asset_type: 'asset type (collectible or token)',
column_description_updated: 'date of last allowance update',
you_own: 'You own {asset}',
revoke_all_checkbox_aria_label: 'Toggle revoke selection for all allowances shown (revoke selected allowances by clicking Revoke Selected button)',
revoke_checkbox_aria_label: 'Toggle revoke selection for {token} allowance for spender {spender} (revoke all selected allowances by clicking Revoke Selected button)',
revoke_all_checkbox_aria_label: 'Toggle revoke selection for all allowances shown (revoke selected allowances by clicking \'Revoke Selected\' button)',
revoke_checkbox_aria_label: 'Toggle revoke selection for {token} allowance for spender {spender} (revoke all selected allowances by clicking \'Revoke Selected\' button)',
edit_allowance_aria_label: 'Edit {token} allowance for spender {spender}',
edit_modal_description: 'Define new token allowance for spender',
entire_collection: 'Entire Collection',
token_amount_input_label: 'Token Amount',
erc_721_single_allowance_blurb: 'Note: there may only be up to one approved spender for this type of collectible (ERC-721). Making a change here will revoke the previous approval.',
revoking_allowances_title: 'Revoking {total} allowances ({remaining} remaining)',
revoking_allowances_description: 'Please wait while we revoke the selected allowances. You will need to approve each transaction in your wallet.',
revoking_allowances_title: 'Revoking {total} allowance(s) ({remaining} remaining)',
revoking_allowances_description: 'Please wait while we revoke the selected allowance(s). You will need to approve each transaction in your wallet.',
revoking_allowances_cancel_note: 'Note: clicking \'Cancel\' will not cancel transactions which have already been approved. Any pending transaction(s) must be cancelled in your wallet.',
},
notification:{
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);
});
});
});

5 changes: 3 additions & 2 deletions test/jest/__tests__/antelope/stores/profile.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setActivePinia, createPinia } from 'pinia';
import { setActivePinia, createPinia, Store } from 'pinia';

// Mockups
jest.mock('src/antelope/stores/feedback', () => ({
Expand All @@ -9,7 +9,8 @@ jest.mock('src/antelope/stores/feedback', () => ({
import { useProfileStore } from 'src/antelope/stores/profile';

describe('Profile Store', () => {
let store: any;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let store: Store;

beforeEach(() => {
jest.clearAllMocks();
Expand Down
Loading
Loading