Updating tests with new StatusContext.

This commit is contained in:
Roland 2023-04-02 14:55:02 -06:00
parent c26e47a55d
commit 455d9dd347
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
2 changed files with 70 additions and 11 deletions

View file

@ -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 <Form>
<Toggle
label={on ? 'Downloading Enabled' : 'Downloading Disabled'}
disabled={on === null}
label={on === true ? 'Downloading Enabled' : 'Downloading Disabled'}
disabled={pending || downloads === null}
checked={on === true}
onChange={setDownloads}
/>

View file

@ -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(<StatusContext.Provider value={statusValue}>
<DisableDownloadsToggle/>
</StatusContext.Provider>);
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(<StatusContext.Provider value={statusValue}>
<DisableDownloadsToggle/>
</StatusContext.Provider>);
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(<StatusContext.Provider value={statusValue}>
<DisableDownloadsToggle/>
</StatusContext.Provider>);
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');
});