From 455d9dd34785b147a511854c54eeca430ecc8099 Mon Sep 17 00:00:00 2001 From: Roland Date: Sun, 2 Apr 2023 14:55:02 -0600 Subject: [PATCH] Updating tests with new StatusContext. --- app/src/components/Common.js | 26 ++++++++------- app/src/components/Common.test.js | 55 +++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 11 deletions(-) diff --git a/app/src/components/Common.js b/app/src/components/Common.js index 78a39e56..0262bbc3 100644 --- a/app/src/components/Common.js +++ b/app/src/components/Common.js @@ -2,7 +2,7 @@ import React, {useContext, useEffect, useState} from "react"; import {Card, Container, IconGroup, Input, Modal, Pagination} from 'semantic-ui-react'; import {Link, NavLink, useNavigate} from "react-router-dom"; import Message from "semantic-ui-react/dist/commonjs/collections/Message"; -import {useDirectories, useHotspot, useSettings, useThrottle} from "../hooks/customHooks"; +import {useDirectories, useDownloads, useHotspot, useSettings, useThrottle} from "../hooks/customHooks"; import {Media, StatusContext, ThemeContext} from "../contexts/contexts"; import {Button, CardIcon, darkTheme, Form, Header, Icon, lightTheme, Menu, Popup, Statistic} from "./Theme"; import {FilePreviewContext} from "./FilePreview"; @@ -633,23 +633,27 @@ export function Toggle({label, checked, disabled, onChange, icon}) { } export function DisableDownloadsToggle() { + const [pending, setPending] = React.useState(false); const {status, fetchStatus} = React.useContext(StatusContext); - const {downloads} = status; + + const {downloads} = status ? status : {downloads: null}; const setDownloads = async (enable) => { - if (enable) { - await startDownloads(); - } else { - await killDownloads(); - } - await fetchStatus(); + setPending(true); + if (enable) { + await startDownloads(); + } else { + await killDownloads(); + } + await fetchStatus(); + setPending(false); } - const on = downloads && downloads['disabled'] === false; + const on = downloads && downloads['disabled'] === false && downloads['stopped'] === false; return
diff --git a/app/src/components/Common.test.js b/app/src/components/Common.test.js index e6fb461d..42a81db7 100644 --- a/app/src/components/Common.test.js +++ b/app/src/components/Common.test.js @@ -1,2 +1,57 @@ import React from "react"; +import {getByTestId, render, screen} from '@testing-library/react'; +import {DisableDownloadsToggle} from "./Common"; +import user from '@testing-library/user-event'; +import {StatusContext} from "../contexts/contexts"; +import {startDownloads} from "../components/Common"; +test('DownloadsToggle is off when loading', async () => { + const statusValue = {status: null, fetchStatus: null}; + const {container} = render( + + ); + + let toggle = screen.getByRole('checkbox'); + expect(toggle).not.toBeChecked(); + // Disabled while loading. + expect(toggle).toHaveClass('disabled'); + // Text is "disabled" when downloading is On. + const label = getByTestId(container, 'toggle-label'); + expect(label).toHaveTextContent('Downloading Disabled'); +}); + +test('DownloadsToggle is off if downloading is Off', async () => { + const statusValue = { + status: {downloads: {disabled: true, stopped: true}}, + fetchStatus: null, + } + const {container} = render( + + ); + + const toggle = screen.getByRole('checkbox'); + expect(toggle).not.toBeChecked(); + // Not disabled when loaded. + expect(toggle).not.toHaveClass('disabled'); + // Text is "disabled" when downloading is On. + const label = getByTestId(container, 'toggle-label'); + expect(label).toHaveTextContent('Downloading Disabled'); +}); + +test('DownloadsToggle is on if downloading is On', async () => { + const statusValue = { + status: {downloads: {disabled: false, stopped: false}}, + fetchStatus: null, + } + const {container} = render( + + ); + + const toggle = screen.getByRole('checkbox'); + expect(toggle).toBeChecked(); + // Not disabled when loaded. + expect(toggle).not.toHaveClass('disabled'); + // Text is "enabled" when downloading is On. + const label = getByTestId(container, 'toggle-label'); + expect(label).toHaveTextContent('Downloading Enabled'); +});