Skip to content

Commit c696ebb

Browse files
committed
feat(automine): add unit tests for automine
1 parent 7295794 commit c696ebb

File tree

2 files changed

+124
-1
lines changed

2 files changed

+124
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import React from 'react';
2+
import { fireEvent } from '@testing-library/dom';
3+
import { getNetwork, renderWithProviders } from 'utils/tests';
4+
import AutoMineButton from './AutoMineButton';
5+
import { AutoMineMode } from 'types';
6+
import { AutoMinerModel } from 'store/models/network';
7+
import { Status } from 'shared/types';
8+
import { waitFor } from '@testing-library/react';
9+
10+
describe('AutoMineButton', () => {
11+
let unmount: () => void;
12+
13+
const renderComponent = (autoMineMode: AutoMineMode = AutoMineMode.AutoOff) => {
14+
const network = getNetwork(1, 'test network', Status.Started);
15+
network.autoMineMode = autoMineMode;
16+
17+
const autoMiner = {
18+
startTime: 0,
19+
timer: undefined,
20+
mining: false,
21+
} as AutoMinerModel;
22+
23+
if (autoMineMode != AutoMineMode.AutoOff) {
24+
// set start time into the past to test the percentage of the timer
25+
autoMiner.startTime = Date.now() - 15000;
26+
autoMiner.mining = true;
27+
}
28+
29+
const initialState = {
30+
network: {
31+
networks: [network],
32+
designer: {
33+
activeId: network.id,
34+
},
35+
autoMiners: {
36+
'1': autoMiner,
37+
},
38+
},
39+
};
40+
const cmp = <AutoMineButton network={network}></AutoMineButton>;
41+
const result = renderWithProviders(cmp, { initialState });
42+
unmount = result.unmount;
43+
return result;
44+
};
45+
46+
afterEach(() => unmount());
47+
48+
it('should display the button text', async () => {
49+
const { getByText } = renderComponent();
50+
expect(getByText('Auto Mine: Off')).toBeInTheDocument();
51+
});
52+
53+
it('should display dropdown options', async () => {
54+
const { getByText, findByText } = renderComponent();
55+
fireEvent.mouseOver(getByText('Auto Mine: Off'));
56+
expect(await findByText('30s')).toBeInTheDocument();
57+
expect(await findByText('1m')).toBeInTheDocument();
58+
expect(await findByText('5m')).toBeInTheDocument();
59+
expect(await findByText('10m')).toBeInTheDocument();
60+
});
61+
62+
it('should display correct automine mode based on network', async () => {
63+
const { getByText } = renderComponent(AutoMineMode.Auto30s);
64+
expect(getByText('Auto Mine: 30s')).toBeInTheDocument();
65+
});
66+
67+
it('should calculate remaining percentage correctly', async () => {
68+
jest.useFakeTimers({ now: Date.now() });
69+
70+
const { findByText } = renderComponent(AutoMineMode.Auto30s);
71+
72+
// advance by one interval
73+
jest.advanceTimersByTime(1500);
74+
75+
const progressBar = (await findByText('Auto Mine: 30s'))
76+
.nextElementSibling as HTMLElement;
77+
78+
const progressBarWidthPercentage = parseFloat(progressBar.style.width.slice(0, -1));
79+
expect(progressBarWidthPercentage).toBeGreaterThan(49.0);
80+
expect(progressBarWidthPercentage).toBeLessThan(51.0);
81+
82+
jest.useRealTimers();
83+
});
84+
85+
it('should change remaining percentage on mode change', async () => {
86+
const { getByText, findByText } = renderComponent();
87+
88+
let progressBar = getByText('Auto Mine: Off').nextElementSibling as HTMLElement;
89+
expect(progressBar.style.width).toBe('0%');
90+
91+
fireEvent.mouseOver(getByText('Auto Mine: Off'));
92+
fireEvent.click(await findByText('30s'));
93+
94+
// the button text doesn't change since the autoMine store action is not really run
95+
progressBar = getByText('Auto Mine: Off').nextElementSibling as HTMLElement;
96+
expect(progressBar.style.width).toBe('100%');
97+
});
98+
});

src/store/models/network.spec.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { waitFor } from '@testing-library/react';
33
import detectPort from 'detect-port';
44
import { createStore } from 'easy-peasy';
55
import { NodeImplementation, Status } from 'shared/types';
6-
import { CustomImage, Network } from 'types';
6+
import { AutoMineMode, CustomImage, Network } from 'types';
77
import { initChartFromNetwork } from 'utils/chart';
88
import { defaultRepoState } from 'utils/constants';
99
import * as files from 'utils/files';
@@ -893,5 +893,30 @@ describe('Network model', () => {
893893
const { exportNetwork } = store.getActions().network;
894894
await expect(exportNetwork({ id: 10 })).rejects.toThrow();
895895
});
896+
897+
it('should automine blocks when automine enabled', async () => {
898+
jest.useFakeTimers();
899+
900+
const { addNetwork } = store.getActions().network;
901+
await addNetwork(addNetworkArgs);
902+
const { networks } = store.getState().network;
903+
904+
await store
905+
.getActions()
906+
.network.autoMine({ id: networks[0].id, mode: AutoMineMode.Auto30s });
907+
908+
jest.advanceTimersByTime(65000);
909+
expect(bitcoindServiceMock.mine).toHaveBeenCalledTimes(2);
910+
911+
await store
912+
.getActions()
913+
.network.autoMine({ id: networks[0].id, mode: AutoMineMode.AutoOff });
914+
915+
jest.advanceTimersByTime(65000);
916+
// the call count is not incremented
917+
expect(bitcoindServiceMock.mine).toHaveBeenCalledTimes(2);
918+
919+
jest.useRealTimers();
920+
});
896921
});
897922
});

0 commit comments

Comments
 (0)