我有两个简单的构成部分。 我是用 j子为他们撰写考试。 在我点击货币体系时,我想测试一下,有关阵列应当添加到选定国家。 您可以看到,我通过<条码>,作为货币的推进剂。 因此,我也想 mo弄它。
货币轨道。
import { useEffect, useState } from "react";
import { currencies } from "../constants";
import { Currency } from "../types";
import CurrencyItem from "./CurrencyItem";
const CurrencySelector = () => {
const initialSelected = () => {
const storedCurrencies = sessionStorage.getItem("currencies");
return storedCurrencies ? JSON.parse(storedCurrencies) : [];
};
const [selected, setSelected] = useState<Currency[]>(initialSelected);
const handleCurrencyToggle = (currency: Currency) => {
const isSelected = selected.some(
(selectedCurrency) => selectedCurrency.name === currency.name
);
if (isSelected) {
setSelected((prevSelected) =>
prevSelected.filter(
(selectedCurrency) => selectedCurrency.name !== currency.name
)
);
} else {
setSelected((prevSelected) => [...prevSelected, currency]);
}
};
useEffect(() => {
sessionStorage.setItem("currencies", JSON.stringify(selected));
}, [selected]);
return (
<div className="currency-selector">
<div className="selected-currencies currency-items">
{selected.length > 0 ? (
selected.map((currency: Currency, index: number) => (
<div
data-testid="selected-currency"
key={index}
className="selected-currency currency"
>
<span>{currency.name}</span>
<button
className="unselect-currency"
onClick={() => handleCurrencyToggle(currency)}
>
x
</button>
</div>
))
) : (
<div data-testid="no-selected-currencies" className="no-currencies">
No selected currencies
</div>
)}
</div>
<div className="currency-items">
{currencies.map((currency: Currency, index: number) => (
<CurrencyItem
data-testid={`currency-item-${index}`}
key={index}
currency={currency}
onClick={() => handleCurrencyToggle(currency)}
isSelected={selected.some(
(selectedCurrency) => selectedCurrency.name === currency.name
)}
/>
))}
</div>
</div>
);
};
export default CurrencySelector;
货币估算值。
import { Currency } from "../types";
const CurrencyItem = ({
currency,
onClick,
isSelected,
}: {
currency: Currency;
onClick: () => void;
isSelected: boolean;
}) => {
const handleCheckboxChange = () => {
onClick();
};
return (
<div data-testid="currency-item" onClick={onClick} className="currency">
<label>
<input onChange={handleCheckboxChange} checked={isSelected} type="checkbox" />
<span className="checkbox-container"></span>
</label>
<span>{currency.name}</span>
</div>
);
};
export default CurrencyItem;
货币阵列:
import { Currency } from "../types";
export const currencies: Currency[] = [
{
name: "EUR",
},
{
name: "PLN",
},
{
name: "GEL",
},
{
name: "DKK",
},
{
name: "CZK",
},
{
name: "GBP",
},
{
name: "SEK",
},
{
name: "USD",
},
{
name: "RUB",
},
];
我的考验:
it("adds currency to select list", () => {
const handleCurrencyToggle = jest.fn()
const { getByTestId: getByTestId2 } = render(
<CurrencySelector />
);
const { getByTestId } = render(
<CurrencyItem
key={1}
currency={currencies[0]}
onClick={() => () => {
handleCurrencyToggle();
}}
isSelected={true}
/>
);
const currencyItem = getByTestId("currency-item");
fireEvent.click(currencyItem);
const selectItem = getByTestId2("selected-currency").textContent
expect(selectItem).toBe("EUR");
});
});
我知道这并不正确,但我要指导我如何篡改真正处理Toggle职能。