@cainiaofe/cn-ui-m
Version:
113 lines (112 loc) • 5.86 kB
JavaScript
import { __assign, __awaiter, __generator } from "tslib";
import React from 'react';
import { render, fireEvent, waitFor } from '@testing-library/react';
import { CnDownload } from '../index';
import { download } from '@cainiaofe/cn-utils';
jest.mock('@cainiaofe/cn-utils', function () { return ({
download: jest.fn(),
}); });
jest.mock('@/components/cn-message', function () { return ({
error: jest.fn(),
}); });
describe('CnDownload', function () {
it('传入 `url` 属性时,点击按钮应触发下载并调用 `onSuccess`', function () { return __awaiter(void 0, void 0, void 0, function () {
var onSuccess, url, getByRole;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
onSuccess = jest.fn();
url = 'http://example.com/file.pdf';
getByRole = render(React.createElement(CnDownload, { url: url, onSuccess: onSuccess })).getByRole;
fireEvent.click(getByRole('button'));
return [4 /*yield*/, waitFor(function () { return expect(download).toHaveBeenCalledWith(url, undefined); })];
case 1:
_a.sent();
expect(onSuccess).toHaveBeenCalledWith(url);
return [2 /*return*/];
}
});
}); });
it('没有传入 `url` 但传入 `service` 函数时,点击按钮应调用 `service` 函数并触发下载', function () { return __awaiter(void 0, void 0, void 0, function () {
var service, onSuccess, getByRole;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
service = jest.fn().mockResolvedValue('http://example.com/file.pdf');
onSuccess = jest.fn();
getByRole = render(React.createElement(CnDownload, { service: service, onSuccess: onSuccess })).getByRole;
fireEvent.click(getByRole('button'));
return [4 /*yield*/, waitFor(function () { return expect(service).toHaveBeenCalled(); })];
case 1:
_a.sent();
return [4 /*yield*/, waitFor(function () {
return expect(download).toHaveBeenCalledWith('http://example.com/file.pdf', undefined);
})];
case 2:
_a.sent();
expect(onSuccess).toHaveBeenCalledWith('http://example.com/file.pdf');
return [2 /*return*/];
}
});
}); });
it('没有下载链接时,点击按钮应触发 `onError`', function () { return __awaiter(void 0, void 0, void 0, function () {
var onError, getByRole;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
onError = jest.fn();
getByRole = render(React.createElement(CnDownload, { onError: onError })).getByRole;
fireEvent.click(getByRole('button'));
return [4 /*yield*/, waitFor(function () { return expect(onError).toHaveBeenCalled(); })];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); });
it('传入 `customRender` 函数时,应使用该函数渲染组件并触发 `onClick`', function () { return __awaiter(void 0, void 0, void 0, function () {
var url, customRender, getByTestId, customElement;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
url = 'http://example.com/file.pdf';
customRender = jest.fn().mockImplementation(function (onClick) { return (React.createElement("div", { "data-testid": "custom", onClick: onClick }, "Custom Download")); });
getByTestId = render(React.createElement(CnDownload, { url: url, customRender: customRender })).getByTestId;
customElement = getByTestId('custom');
fireEvent.click(customElement);
return [4 /*yield*/, waitFor(function () { return expect(customRender).toHaveBeenCalled(); })];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); });
it('传入 `children` 属性时,应使用 `children` 渲染组件并触发 `onClick`', function () { return __awaiter(void 0, void 0, void 0, function () {
var children, getByTestId, childElement;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
children = React.createElement("span", { "data-testid": "child" }, "Download");
getByTestId = render(React.createElement(CnDownload, null, children)).getByTestId;
childElement = getByTestId('child');
return [4 /*yield*/, waitFor(function () { return expect(childElement).toBeInTheDocument(); })];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); });
it('正确传递其他属性给 `CnButton` 或 `span`', function () {
var otherProps = {
id: 'download-button',
className: 'download-button',
'data-testid': 'download-button',
disabled: true,
};
var getByTestId = render(React.createElement(CnDownload, __assign({}, otherProps))).getByTestId;
var downloadButton = getByTestId('download-button');
expect(downloadButton).toHaveAttribute('id', 'download-button');
expect(downloadButton).toHaveClass('download-button');
expect(downloadButton).toBeDisabled();
});
});