UNPKG

zarm

Version:

基于 React 的移动端UI库

100 lines (96 loc) 3.63 kB
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _regeneratorRuntime from "@babel/runtime/regenerator"; import { mocked } from 'ts-jest/utils'; import createThumbnail from '../../utils/createThumbnail'; import changeImageSize from '../../utils/changeImageSize'; import { mockCreateObjectURL, mockResetCreateObjectURL } from '../../../../tests/utils'; jest.mock('../../utils/changeImageSize'); var mChangeImageSize = mocked(changeImageSize); describe('createThumbnail', function () { var imageOnload; var mCreateObjectURL = jest.fn(); afterAll(function () { jest.resetAllMocks(); mockResetCreateObjectURL(); }); beforeAll(function () { Object.defineProperty(Image.prototype, 'onload', { get: function get() { return this._onload; }, set: function set(fn) { imageOnload = fn; this._onload = fn; } }); mockCreateObjectURL(mCreateObjectURL); }); afterEach(function () { jest.restoreAllMocks(); jest.clearAllMocks(); }); it('should create thumbnail for image', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() { var createElementSpy, mFile, createThumbnailPromise, actual; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: mChangeImageSize.mockReturnValueOnce('data:,'); createElementSpy = jest.spyOn(document, 'createElement'); mFile = new File(['foo'], 'foo.png', { type: 'image/png' }); createThumbnailPromise = createThumbnail({ file: mFile, quality: 0, fileType: 'image/png', maxWidth: 100 }); imageOnload(); _context.next = 7; return createThumbnailPromise; case 7: actual = _context.sent; expect(actual).toEqual('data:,'); expect(createElementSpy).toBeCalledWith('img'); expect(mChangeImageSize).toBeCalledWith(expect.any(Image), 0, 'image/png'); expect(window.URL.createObjectURL).toBeCalledWith(mFile); case 12: case "end": return _context.stop(); } } }, _callee); }))); it('should return image url get from src if quality, maxWidth, maxHeight are falsy value', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() { var createElementSpy, mFile, createThumbnailPromise, actual; return _regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: mCreateObjectURL.mockReturnValueOnce('./test.png'); createElementSpy = jest.spyOn(document, 'createElement'); mFile = new File(['foo'], 'foo.png', { type: 'image/png' }); createThumbnailPromise = createThumbnail({ file: mFile, quality: 0, fileType: 'image/png' }); imageOnload(); _context2.next = 7; return createThumbnailPromise; case 7: actual = _context2.sent; expect(createElementSpy).toBeCalledWith('img'); expect(mChangeImageSize).not.toBeCalled(); expect(actual).toEqual("".concat(window.location.href, "test.png")); case 11: case "end": return _context2.stop(); } } }, _callee2); }))); });