Skip to content

Commit 46c09c2

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

File tree

2 files changed

+123
-1
lines changed

2 files changed

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

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)