cypress-cy-select
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -1,55 +0,56 @@ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global = global || self, global.commands = factory()); | ||
}(this, (function () { 'use strict'; | ||
/** | ||
* format any matched attribute to its proper css selector notation | ||
* e.g. "-cy:my-selector-name" will be replaced by [data-cy="my-selector-name"] | ||
* | ||
* config example: | ||
* const config = { | ||
* name: “” | ||
* prefix: “” | ||
* separator: “:” | ||
* } | ||
* | ||
* @param {string} selectors the css selectors | ||
* @param {object} config the options | ||
* @param {string} config.name the attribute name - default is 'cy' | ||
* @param {string} config.prefix the attribute prefix - default is 'data-' | ||
* @param {string} config.separator the separator between the attribute and its value - default is '|' | ||
* @return {string} the formatted selectors | ||
*/ | ||
const formatSelectors = (selectors, { | ||
name = 'cy', | ||
prefix = 'data-', | ||
separator = '|' | ||
} = {}) => { | ||
const shortNotation = name + separator; | ||
const attr = prefix + name; | ||
if (!selectors.includes(shortNotation)) return selectors; | ||
selectors.split(' ').forEach(selector => { | ||
if (selector === '>') return; | ||
if (selector.startsWith(name)) { | ||
const value = selector.replace(shortNotation, ''); | ||
selectors = selectors.replace(`${shortNotation}${value}`, `[${attr}=\"${value}\"]`); | ||
} | ||
}); | ||
/** | ||
* format any matched attribute to its proper css selector notation | ||
* e.g. "-cy:my-selector-name" will be replaced by [data-cy="my-selector-name"] | ||
* | ||
* config example: | ||
* const config = { | ||
* name: “” | ||
* prefix: “” | ||
* separator: “:” | ||
* } | ||
* | ||
* @param {string} selectors the css selectors | ||
* @param {object} config the options | ||
* @param {string} config.name the attribute name - default is 'cy' | ||
* @param {string} config.prefix the attribute prefix - default is 'data-' | ||
* @param {string} config.separator the separator between the attribute and its value - default is '|' | ||
* @return {string} the formatted selectors | ||
*/ | ||
const formatSelectors = (selectors, { | ||
name = 'cy', | ||
prefix = 'data-', | ||
separator = '|' | ||
} = {}) => { | ||
const shortNotation = name + separator; | ||
const attr = prefix + name; | ||
if (!selectors.includes(shortNotation)) { | ||
return selectors; | ||
}; | ||
} | ||
selectors.split(' ').forEach(selector => { | ||
if (selector === '>') return; | ||
if (selector.startsWith(name)) { | ||
const value = selector.replace(shortNotation, ''); | ||
selectors = selectors.replace(`${shortNotation}${value}`, `[${attr}=\"${value}\"]`); | ||
} | ||
}); | ||
return selectors; | ||
}; | ||
function commands (...args) { | ||
const get = (originalFn, selectors, options) => originalFn(formatSelectors(selectors, ...args), options); | ||
const find = (originalFn, subject, selectors, options) => originalFn(subject, formatSelectors(selectors, ...args), options); | ||
Cypress.Commands.overwrite('get', get); | ||
Cypress.Commands.overwrite('find', find); | ||
function commands (...args) { | ||
function get(originalFn, selectors, options) { | ||
const innerFn = originalFn.apply(this, [formatSelectors(selectors, ...args), options]); | ||
return subject => { | ||
return innerFn(subject); | ||
}; | ||
} | ||
function find(originalFn, selectors, options) { | ||
const innerFn = originalFn.apply(this, [formatSelectors(selectors, ...args), options]); | ||
return subject => { | ||
return innerFn(subject); | ||
}; | ||
} | ||
Cypress.Commands.overwriteQuery('get', get); | ||
Cypress.Commands.overwriteQuery('find', find); | ||
} | ||
return commands; | ||
}))); | ||
export { commands as default }; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).commands=t()}(this,(function(){"use strict";const e=(e,{name:t="cy",prefix:n="data-",separator:o="|"}={})=>{const s=t+o,r=n+t;return e.includes(s)?(e.split(" ").forEach(n=>{if(">"!==n&&n.startsWith(t)){const t=n.replace(s,"");e=e.replace(`${s}${t}`,`[${r}="${t}"]`)}}),e):e};return function(...t){Cypress.Commands.overwrite("get",(n,o,s)=>n(e(o,...t),s)),Cypress.Commands.overwrite("find",(n,o,s,r)=>n(o,e(s,...t),r))}})); | ||
const t=(t,{name:e="cy",prefix:r="data-",separator:n="|"}={})=>{const s=e+n,a=r+e;return t.includes(s)?(t.split(" ").forEach((r=>{if(">"!==r&&r.startsWith(e)){const e=r.replace(s,"");t=t.replace(`${s}${e}`,`[${a}="${e}"]`)}})),t):t};function e(...e){Cypress.Commands.overwriteQuery("get",(function(r,n,s){const a=r.apply(this,[t(n,...e),s]);return t=>a(t)})),Cypress.Commands.overwriteQuery("find",(function(r,n,s){const a=r.apply(this,[t(n,...e),s]);return t=>a(t)}))}export{e as default}; |
{ | ||
"name": "cypress-cy-select", | ||
"description": "data-cy shorthand notation for cypress get and find functions", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"private": false, | ||
"keywords": [ | ||
@@ -27,5 +28,6 @@ "cypress", | ||
"scripts": { | ||
"build": "npx rollup -c", | ||
"build": "npx rollup -c --format es", | ||
"test": "jest --verbose ./test/jest", | ||
"test-cypress": "npx cypress run --project test", | ||
"test-cypress-log": "ELECTRON_ENABLE_LOGGING=1 npx cypress run --project test", | ||
"test-cypress-open": "npx cypress open --project test", | ||
@@ -35,11 +37,11 @@ "prettier": "npx prettier --find-config-path --write ./src/*.js ./test/*.js ./test/cypress/**/*.js" | ||
"devDependencies": { | ||
"@babel/core": "^7.6.4", | ||
"@babel/preset-env": "^7.6.3", | ||
"babel-jest": "^24.9.0", | ||
"cypress": "^3.5.0", | ||
"jest": "^24.9.0", | ||
"rollup": "^1.26.0", | ||
"rollup-plugin-babel": "^4.3.3", | ||
"rollup-plugin-terser": "^5.1.2" | ||
"@babel/core": "^7.21.0", | ||
"@babel/preset-env": "^7.20.2", | ||
"@rollup/plugin-babel": "^6.0.3", | ||
"@rollup/plugin-terser": "^0.4.0", | ||
"babel-jest": "^29.4.3", | ||
"cypress": "^12.7.0", | ||
"jest": "^29.4.3", | ||
"rollup": "^3.17.3" | ||
} | ||
} |
8374
10
139