react-beautiful-dnd-test-utils
Version:
Test utils for react-beautiful-dnd built with react-testing-library.
210 lines (198 loc) • 10.1 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
require('@testing-library/jest-dom');
var react = require('@testing-library/react');
var userEvent = require('@testing-library/user-event');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var userEvent__default = /*#__PURE__*/_interopDefaultLegacy(userEvent);
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
function __generator(thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
}
/*
* window.getComputedStyle mock
*/
var noSpacing = {
top: 0,
right: 0,
bottom: 0,
left: 0
};
var getComputedStyle = function (_a) {
var _b = _a === void 0 ? {} : _a, _c = _b.margin, margin = _c === void 0 ? noSpacing : _c, _d = _b.padding, padding = _d === void 0 ? noSpacing : _d, _e = _b.border, border = _e === void 0 ? noSpacing : _e, _f = _b.display, display = _f === void 0 ? 'block' : _f;
return ({
paddingTop: padding.top + "px",
paddingRight: padding.right + "px",
paddingBottom: padding.bottom + "px",
paddingLeft: padding.left + "px",
marginTop: margin.top + "px",
marginRight: margin.right + "px",
marginBottom: margin.bottom + "px",
marginLeft: margin.left + "px",
borderTopWidth: border.top + "px",
borderRightWidth: border.right + "px",
borderBottomWidth: border.bottom + "px",
borderLeftWidth: border.left + "px",
display: display
// there are over 400 more properties, but these are the ones we need
});
};
var mockGetComputedStyle = function () {
return jest
.spyOn(window, 'getComputedStyle')
.mockImplementation(function () { return getComputedStyle(); });
};
/*
* element.getBoundingClientRect mock
*/
var mockGetBoundingClientRect = function (element) {
return jest.spyOn(element, 'getBoundingClientRect').mockImplementation(function () { return ({
bottom: 0,
height: 0,
left: 0,
right: 0,
top: 0,
width: 0,
x: 0,
y: 0,
toJSON: function () { return undefined; }
}); });
};
var executeAsyncFnsSerially = function (fns) {
return fns.reduce(function (promise, fn) {
return promise.then(function (result) { return fn().then(Array.prototype.concat.bind(result)); });
}, Promise.resolve([]));
};
/*
* react-beautiful-dnd utils
*/
var DND_DROPPABLE_DATA_ATTR = '[data-rbd-droppable-id]';
var DND_DRAGGABLE_DATA_ATTR = '[data-rbd-draggable-id]';
var mockDndSpacing = function (container) {
var droppables = container.querySelectorAll(DND_DROPPABLE_DATA_ATTR);
droppables.forEach(function (droppable) {
mockGetBoundingClientRect(droppable);
var draggables = droppable.querySelectorAll(DND_DRAGGABLE_DATA_ATTR);
draggables.forEach(function (draggable) { return mockGetBoundingClientRect(draggable); });
});
};
var DND_DIRECTION_UP = 'UP';
var DND_DIRECTION_DOWN = 'DOWN';
var makeDnd = function (_a) {
var text = _a.text, getDragElement = _a.getDragElement, direction = _a.direction, positions = _a.positions;
return __awaiter(void 0, void 0, void 0, function () {
var spaceKey, arrowUpKey, arrowDownKey, getKeyForDirection, handleMovementInDirection, draggable, movements, i;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (!text && !getDragElement) {
throw new Error('text or getDragElement must be defined');
}
spaceKey = '{space}';
arrowUpKey = '{arrowup}';
arrowDownKey = '{arrowdown}';
getKeyForDirection = function () {
switch (direction) {
case DND_DIRECTION_UP:
return arrowUpKey;
case DND_DIRECTION_DOWN:
return arrowDownKey;
default:
throw new Error('Unhandled direction');
}
};
handleMovementInDirection = function () { return __awaiter(void 0, void 0, void 0, function () {
var _a, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
// enable keyboard dragging
userEvent__default["default"].keyboard(spaceKey);
_a = expect;
return [4 /*yield*/, react.screen.findByText(/You have lifted an item/i)];
case 1:
_a.apply(void 0, [_d.sent()]).toBeInTheDocument();
// move draggable based on direction
userEvent__default["default"].keyboard(getKeyForDirection());
_b = expect;
return [4 /*yield*/, react.screen.findByText(/You have moved the item/i)];
case 2:
_b.apply(void 0, [_d.sent()]).toBeInTheDocument();
// disable keyboard dragging
userEvent__default["default"].keyboard(spaceKey);
_c = expect;
return [4 /*yield*/, react.screen.findByText(/You have dropped the item/i)];
case 3:
_c.apply(void 0, [_d.sent()]).toBeInTheDocument();
return [2 /*return*/];
}
});
}); };
draggable = getDragElement
? getDragElement()
: react.screen.getByText(text).closest(DND_DRAGGABLE_DATA_ATTR);
draggable.focus();
expect(draggable).toHaveFocus();
movements = [];
for (i = 0; i < positions; i += 1) {
movements.push(handleMovementInDirection);
}
return [4 /*yield*/, executeAsyncFnsSerially(movements)];
case 1:
_b.sent();
return [2 /*return*/];
}
});
});
};
exports.DND_DIRECTION_DOWN = DND_DIRECTION_DOWN;
exports.DND_DIRECTION_UP = DND_DIRECTION_UP;
exports.DND_DRAGGABLE_DATA_ATTR = DND_DRAGGABLE_DATA_ATTR;
exports.DND_DROPPABLE_DATA_ATTR = DND_DROPPABLE_DATA_ATTR;
exports.makeDnd = makeDnd;
exports.mockDndSpacing = mockDndSpacing;
exports.mockGetComputedStyle = mockGetComputedStyle;