Skip to content

Commit

Permalink
Includes networks store unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
karynemayer committed May 15, 2024
1 parent 9f860c8 commit e39d645
Showing 1 changed file with 165 additions and 0 deletions.
165 changes: 165 additions & 0 deletions test/jest/__tests__/stores/networks.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import { createPinia, setActivePinia } from 'pinia';
import { useNetworksStore } from 'src/stores/networks';

describe('Networks Store', () => {
beforeEach(() => {
setActivePinia(createPinia());
});

afterEach(() => {
const networksStore = useNetworksStore();
networksStore.currentNetworkName = '';
networksStore.preferredNetworkName = '';
});

describe('setupNetworks', () => {
it('sets supported networks', () => {
const networksStore = useNetworksStore();

expect(networksStore.networks.length).toBe(0);
expect(networksStore.currentNetworkName).toBe('');
expect(networksStore.preferredNetworkName).toBe('');

networksStore.setupNetworks();

expect(networksStore.networks.length).toBe(2);
expect(networksStore.currentNetworkName).toBe('');
expect(networksStore.preferredNetworkName).toBe('');
});

it('sets supported networks and applies preferred network', () => {
const networksStore = useNetworksStore();
networksStore.preferredNetworkName = 'telos';

const updateCurrentNetwork = jest.spyOn(networksStore, 'updateCurrentNetwork');

expect(networksStore.networks.length).toBe(0);
expect(networksStore.currentNetworkName).toBe('');
expect(networksStore.preferredNetworkName).toBe('telos');

networksStore.setupNetworks();

expect(networksStore.networks.length).toBe(2);
expect(updateCurrentNetwork).toBeCalledWith('telos');
expect(networksStore.currentNetworkName).toBe('telos');
expect(networksStore.preferredNetworkName).toBe('telos');
});

it('sets supported networks and applies current network', () => {
const networksStore = useNetworksStore();
networksStore.currentNetworkName = 'telos';

const updateCurrentNetwork = jest.spyOn(networksStore, 'updateCurrentNetwork');

expect(networksStore.networks.length).toBe(0);
expect(networksStore.currentNetworkName).toBe('telos');
expect(networksStore.preferredNetworkName).toBe('');

networksStore.setupNetworks();

expect(networksStore.networks.length).toBe(2);
expect(updateCurrentNetwork).toBeCalledWith('telos');
expect(networksStore.currentNetworkName).toBe('telos');
expect(networksStore.preferredNetworkName).toBe('');
});
});

describe('updateCurrentNetwork', () => {
it('updates when network is supported', () => {
const networksStore = useNetworksStore();

expect(networksStore.networks.length).toBe(0);
expect(networksStore.currentNetworkName).toBe('');
expect(networksStore.preferredNetworkName).toBe('');

networksStore.setupNetworks();
networksStore.updateCurrentNetwork('telos');

expect(networksStore.networks.length).toBe(2);
expect(networksStore.currentNetworkName).toBe('telos');
expect(networksStore.preferredNetworkName).toBe('');
});

it('does not update when network is not supported', () => {
const networksStore = useNetworksStore();

expect(networksStore.networks.length).toBe(0);
expect(networksStore.currentNetworkName).toBe('');
expect(networksStore.preferredNetworkName).toBe('');

networksStore.setupNetworks();
networksStore.updateCurrentNetwork('unsupported');

expect(networksStore.networks.length).toBe(2);
expect(networksStore.currentNetworkName).toBe('');
expect(networksStore.preferredNetworkName).toBe('');
});
});

describe('updatePreferredNetwork', () => {
it('updates when network is supported', () => {
const networksStore = useNetworksStore();

expect(networksStore.networks.length).toBe(0);
expect(networksStore.currentNetworkName).toBe('');
expect(networksStore.preferredNetworkName).toBe('');

networksStore.setupNetworks();
networksStore.updatePreferredNetwork('telos');

expect(networksStore.networks.length).toBe(2);
expect(networksStore.currentNetworkName).toBe('');
expect(networksStore.preferredNetworkName).toBe('telos');
});

it('does not update when network is not supported', () => {
const networksStore = useNetworksStore();

expect(networksStore.networks.length).toBe(0);
expect(networksStore.currentNetworkName).toBe('');
expect(networksStore.preferredNetworkName).toBe('');

networksStore.setupNetworks();
networksStore.updatePreferredNetwork('unsupported');

expect(networksStore.networks.length).toBe(2);
expect(networksStore.currentNetworkName).toBe('');
expect(networksStore.preferredNetworkName).toBe('');
});
});

describe('isNetworkSupported', () => {
it('returns true for supported network', () => {
const networksStore = useNetworksStore();

expect(networksStore.networks.length).toBe(0);
expect(networksStore.currentNetworkName).toBe('');
expect(networksStore.preferredNetworkName).toBe('');

networksStore.setupNetworks();
const result = networksStore.isNetworkSupported('telos');

expect(networksStore.networks.length).toBe(2);
expect(networksStore.currentNetworkName).toBe('');
expect(networksStore.preferredNetworkName).toBe('');
expect(result).toBe(true);
});

it('returns false for unsupported networks', () => {
const networksStore = useNetworksStore();

expect(networksStore.networks.length).toBe(0);
expect(networksStore.currentNetworkName).toBe('');
expect(networksStore.preferredNetworkName).toBe('');

networksStore.setupNetworks();
const result = networksStore.isNetworkSupported('unsupported');

expect(networksStore.networks.length).toBe(2);
expect(networksStore.currentNetworkName).toBe('');
expect(networksStore.preferredNetworkName).toBe('');
expect(result).toBe(false);
});
});
});

0 comments on commit e39d645

Please sign in to comment.