|
| 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 | +}); |
0 commit comments