cypress-dragndrop-kit 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.js +36 -1
- package/dist/types.d.ts +30 -0
- package/package.json +3 -3
- package/dist/commands.d.ts +0 -1
- package/dist/commands.js +0 -38
package/README.md
CHANGED
@@ -8,3 +8,41 @@ A lightweight, Cypress-native plugin that simplifies drag‑and‑drop and movem
|
|
8
8
|
```bash
|
9
9
|
npm install --save-dev cypress-dragndrop-kit
|
10
10
|
```
|
11
|
+
|
12
|
+
## 🛠 Usage
|
13
|
+
|
14
|
+
In order to start using the package, all you need to do is import it within you `./cypress/support/e2e.ts` file:
|
15
|
+
|
16
|
+
```typescript
|
17
|
+
// Location: cypress/support/e2e.ts
|
18
|
+
import 'cypress-dragndrop-kit';
|
19
|
+
```
|
20
|
+
|
21
|
+
### ✨ Available commands
|
22
|
+
|
23
|
+
- `dragTo(targetElementLocator)`: Custom child command for dragging and dropping a chained element to a specified element location
|
24
|
+
- `dragAndDrop(sourceElementLocator, targetElementLocator)`: Custom command for dragging and dropping from one element location to another
|
25
|
+
|
26
|
+
### ⚙️ Examples
|
27
|
+
|
28
|
+
```typescript
|
29
|
+
// Usage of "dragTo(target)"
|
30
|
+
it('should be able to drag and drop an item by using "dragTo()"', () => {
|
31
|
+
cy.get('[data-id="3"]').dragTo('[data-id="47"]');
|
32
|
+
|
33
|
+
cy.get('[data-id="47"]').should('have.attr', 'data-index', 46);
|
34
|
+
cy.get('[data-id="3"]').should('have.attr', 'data-index', 47);
|
35
|
+
})
|
36
|
+
|
37
|
+
// Usage of "dragAndDrop(source, target)"
|
38
|
+
it('should be able to drag and drop an item by using "dragAndDrop()"', () => {
|
39
|
+
cy.dragAndDrop('[data-id="47"]', '[data-id="3"]');
|
40
|
+
|
41
|
+
cy.get('[data-id="47"]').should('have.attr', 'data-index', 3);
|
42
|
+
cy.get('[data-id="3"]').should('have.attr', 'data-index', 4);
|
43
|
+
})
|
44
|
+
```
|
45
|
+
|
46
|
+
## 🤝 Contributing
|
47
|
+
|
48
|
+
Contributions are welcome! If you have ideas, bugs, or feature requests—feel free to open an issue or submit a pull request.
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
@@ -1,3 +1,38 @@
|
|
1
1
|
"use strict";
|
2
|
+
/// <reference types="cypress" />
|
3
|
+
/// <reference path="./types.d.ts" />
|
2
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
require("./
|
5
|
+
const utils_1 = require("./utils");
|
6
|
+
Cypress.Commands.add("dragTo", { prevSubject: 'element' }, (sourceElement, targetElement, options) => {
|
7
|
+
cy.get(targetElement).first().then(([target]) => {
|
8
|
+
cy.wrap(sourceElement).first().then(([source]) => {
|
9
|
+
var _a;
|
10
|
+
let sourceCoordinates = source.getBoundingClientRect();
|
11
|
+
let targetCoordinates = target.getBoundingClientRect();
|
12
|
+
const isScrollingDown = targetCoordinates.top - sourceCoordinates.top > 0;
|
13
|
+
const isScrollingRight = targetCoordinates.right - sourceCoordinates.right > 0;
|
14
|
+
if (isScrollingDown || isScrollingRight) {
|
15
|
+
cy.get(targetElement).first().scrollIntoView();
|
16
|
+
}
|
17
|
+
else {
|
18
|
+
const startX = sourceCoordinates.left + sourceCoordinates.width / 2;
|
19
|
+
const startY = sourceCoordinates.top + sourceCoordinates.height / 2;
|
20
|
+
sourceCoordinates = Object.assign(Object.assign({}, sourceCoordinates), { x: startX, y: startY });
|
21
|
+
const endX = targetCoordinates.left + targetCoordinates.width / 2;
|
22
|
+
const endY = targetCoordinates.top + targetCoordinates.height / 2;
|
23
|
+
targetCoordinates = Object.assign(Object.assign({}, targetCoordinates), { x: endX, y: endY });
|
24
|
+
}
|
25
|
+
cy.wrap(source)
|
26
|
+
.trigger("mousedown", (0, utils_1.params)(sourceCoordinates))
|
27
|
+
.wait((_a = options === null || options === void 0 ? void 0 : options.pressDelay) !== null && _a !== void 0 ? _a : 0)
|
28
|
+
.trigger("mousemove", (0, utils_1.params)(sourceCoordinates, 10));
|
29
|
+
cy.get("body")
|
30
|
+
.trigger("mousemove", (0, utils_1.params)(targetCoordinates))
|
31
|
+
.wait(1000)
|
32
|
+
.trigger("mouseup", { force: true });
|
33
|
+
});
|
34
|
+
});
|
35
|
+
});
|
36
|
+
Cypress.Commands.add("dragAndDrop", (sourceElement, targetElement, options) => {
|
37
|
+
cy.get(sourceElement).dragTo(targetElement, options);
|
38
|
+
});
|
package/dist/types.d.ts
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
/// <reference types="cypress" />
|
2
|
+
|
3
|
+
import { DraggableOption } from "./utils";
|
4
|
+
|
5
|
+
declare global {
|
6
|
+
namespace Cypress {
|
7
|
+
interface Chainable {
|
8
|
+
/**
|
9
|
+
*
|
10
|
+
* @description Custom child command for dragging and dropping a chained element to a specified element location
|
11
|
+
* @param targetElement locator for the target element
|
12
|
+
* @example cy.get('[data-id="3"]').dragTo('[data-id="47"]');
|
13
|
+
*
|
14
|
+
*/
|
15
|
+
dragTo(targetElement: string, options?: DraggableOption): void;
|
16
|
+
|
17
|
+
/**
|
18
|
+
*
|
19
|
+
* @description Custom command for dragging and dropping from one element location to another
|
20
|
+
* @param sourceElement locator for the source element
|
21
|
+
* @param targetElement locator for the target element
|
22
|
+
* @example cy.dragAndDrop('[data-id="3"]', '[data-id="47"]');
|
23
|
+
*
|
24
|
+
*/
|
25
|
+
dragAndDrop(sourceElement: string, targetElement: string, options?: DraggableOption): void;
|
26
|
+
}
|
27
|
+
}
|
28
|
+
}
|
29
|
+
|
30
|
+
export {}
|
package/package.json
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
{
|
2
2
|
"name": "cypress-dragndrop-kit",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.4",
|
4
4
|
"description": "Custom command meant for interacting with draggable elements in the UI",
|
5
5
|
"main": "dist/index.js",
|
6
|
-
"types": "dist/
|
6
|
+
"types": "dist/types.d.ts",
|
7
7
|
"files": ["dist"],
|
8
8
|
"scripts": {
|
9
|
-
"build": "rm -rf dist/* && tsc",
|
9
|
+
"build": "rm -rf dist/* && tsc && cp src/types.d.ts dist/",
|
10
10
|
"publish": "npx semantic-release",
|
11
11
|
"test": "npx cypress run",
|
12
12
|
"test:open": "npx cypress open"
|
package/dist/commands.d.ts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export {};
|
package/dist/commands.js
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
/// <reference types="cypress" />
|
3
|
-
/// <reference path="./types.d.ts" />
|
4
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
5
|
-
const utils_1 = require("./utils");
|
6
|
-
Cypress.Commands.add("dragTo", { prevSubject: 'element' }, (sourceElement, targetElement, options) => {
|
7
|
-
cy.get(targetElement).first().then(([target]) => {
|
8
|
-
cy.wrap(sourceElement).first().then(([source]) => {
|
9
|
-
var _a;
|
10
|
-
let sourceCoordinates = source.getBoundingClientRect();
|
11
|
-
let targetCoordinates = target.getBoundingClientRect();
|
12
|
-
const isScrollingDown = targetCoordinates.top - sourceCoordinates.top > 0;
|
13
|
-
const isScrollingRight = targetCoordinates.right - sourceCoordinates.right > 0;
|
14
|
-
if (isScrollingDown || isScrollingRight) {
|
15
|
-
cy.get(targetElement).first().scrollIntoView();
|
16
|
-
}
|
17
|
-
else {
|
18
|
-
const startX = sourceCoordinates.left + sourceCoordinates.width / 2;
|
19
|
-
const startY = sourceCoordinates.top + sourceCoordinates.height / 2;
|
20
|
-
sourceCoordinates = Object.assign(Object.assign({}, sourceCoordinates), { x: startX, y: startY });
|
21
|
-
const endX = targetCoordinates.left + targetCoordinates.width / 2;
|
22
|
-
const endY = targetCoordinates.top + targetCoordinates.height / 2;
|
23
|
-
targetCoordinates = Object.assign(Object.assign({}, targetCoordinates), { x: endX, y: endY });
|
24
|
-
}
|
25
|
-
cy.wrap(source)
|
26
|
-
.trigger("mousedown", (0, utils_1.params)(sourceCoordinates))
|
27
|
-
.wait((_a = options === null || options === void 0 ? void 0 : options.pressDelay) !== null && _a !== void 0 ? _a : 0)
|
28
|
-
.trigger("mousemove", (0, utils_1.params)(sourceCoordinates, 10));
|
29
|
-
cy.get("body")
|
30
|
-
.trigger("mousemove", (0, utils_1.params)(targetCoordinates))
|
31
|
-
.wait(1000)
|
32
|
-
.trigger("mouseup", { force: true });
|
33
|
-
});
|
34
|
-
});
|
35
|
-
});
|
36
|
-
Cypress.Commands.add("dragAndDrop", (sourceElement, targetElement, options) => {
|
37
|
-
cy.get(sourceElement).dragTo(targetElement, options);
|
38
|
-
});
|