From f616aa22c1d7e23a2c3418900521e29f043be31c Mon Sep 17 00:00:00 2001 From: mjhenkes Date: Wed, 20 Oct 2021 12:38:51 -0500 Subject: [PATCH 1/4] fix: validate selectorPriority configuration --- .../cypress/selector_playground_spec.js | 43 ++++++++++++++++++- packages/driver/src/cypress/error_messages.ts | 6 ++- .../driver/src/cypress/selector_playground.ts | 21 ++++++--- 3 files changed, 61 insertions(+), 9 deletions(-) diff --git a/packages/driver/cypress/integration/cypress/selector_playground_spec.js b/packages/driver/cypress/integration/cypress/selector_playground_spec.js index e45fabc4ddc9..89c3dd764beb 100644 --- a/packages/driver/cypress/integration/cypress/selector_playground_spec.js +++ b/packages/driver/cypress/integration/cypress/selector_playground_spec.js @@ -23,11 +23,50 @@ describe('src/cypress/selector_playground', () => { }) it('sets selector:playground:priority if selectorPriority specified', () => { + const selectorPriority = [ + 'data-1', + 'data-2', + 'id', + 'class', + 'tag', + 'attributes', + 'nth-child', + ] + SelectorPlayground.defaults({ - selectorPriority: ['foo'], + selectorPriority, }) - expect(SelectorPlayground.getSelectorPriority()).to.eql(['foo']) + expect(SelectorPlayground.getSelectorPriority()).to.eql(selectorPriority) + }) + + it('throws if selector:playground:priority if selectorPriority contains an unsupported priority', () => { + const fn = () => { + SelectorPlayground.defaults({ + selectorPriority: [ + 'id', + 'name', + ], + }) + } + + expect(fn).to.throw() + .with.property('message') + .and.include('`Cypress.SelectorPlayground.defaults()` called with invalid `selectorPriority` property. It must be one of: `data-*`, `id`, `class`, `tag`, `attributes`, `nth-child`. You passed: `name`') + }) + + it('throws if selector:playground:priority if selectorPriority contains an unsupported priority that contains a valid priority', () => { + const fn = () => { + SelectorPlayground.defaults({ + selectorPriority: [ + 'idIsNotValid', + ], + }) + } + + expect(fn).to.throw() + .with.property('message') + .and.include('`Cypress.SelectorPlayground.defaults()` called with invalid `selectorPriority` property. It must be one of: `data-*`, `id`, `class`, `tag`, `attributes`, `nth-child`. You passed: `idIsNotValid`') }) it('sets selector:playground:on:element if onElement specified', () => { diff --git a/packages/driver/src/cypress/error_messages.ts b/packages/driver/src/cypress/error_messages.ts index 3438c3b79581..bdbc6350ce88 100644 --- a/packages/driver/src/cypress/error_messages.ts +++ b/packages/driver/src/cypress/error_messages.ts @@ -1428,10 +1428,14 @@ export default { message: '`Cypress.SelectorPlayground.defaults()` must be called with an object. You passed: `{{arg}}`', docsUrl: 'https://on.cypress.io/selector-playground-api', }, - defaults_invalid_priority: { + defaults_invalid_priority_type: { message: '`Cypress.SelectorPlayground.defaults()` called with invalid `selectorPriority` property. It must be an array. You passed: `{{arg}}`', docsUrl: 'https://on.cypress.io/selector-playground-api', }, + defaults_invalid_priority: { + message: '`Cypress.SelectorPlayground.defaults()` called with invalid `selectorPriority` property. It must be one of: `data-*`, `id`, `class`, `tag`, `attributes`, `nth-child`. You passed: `{{arg}}`. Consider using the `onElement` property if a specific selector is desired.', + docsUrl: 'https://on.cypress.io/selector-playground-api', + }, defaults_invalid_on_element: { message: '`Cypress.SelectorPlayground.defaults()` called with invalid `onElement` property. It must be a function. You passed: `{{arg}}`', docsUrl: 'https://on.cypress.io/selector-playground-api', diff --git a/packages/driver/src/cypress/selector_playground.ts b/packages/driver/src/cypress/selector_playground.ts index b3aadf9234b3..35fe910918ad 100644 --- a/packages/driver/src/cypress/selector_playground.ts +++ b/packages/driver/src/cypress/selector_playground.ts @@ -58,16 +58,25 @@ export default { }) } - const { selectorPriority: priority, onElement } = props + const { selectorPriority, onElement } = props - if (priority) { - if (!_.isArray(priority)) { - $errUtils.throwErrByPath('selector_playground.defaults_invalid_priority', { - args: { arg: $utils.stringify(priority) }, + if (selectorPriority) { + if (!_.isArray(selectorPriority)) { + $errUtils.throwErrByPath('selector_playground.defaults_invalid_priority_type', { + args: { arg: $utils.stringify(selectorPriority) }, }) } + // Validate that the priority is on of: "data-*", "id", "class", "tag", "attributes", "nth-child" + + selectorPriority.forEach((priority) => { + if (!priority.match(/^(data\-.*|id|class|tag|attributes|nth\-child)$/)) { + $errUtils.throwErrByPath('selector_playground.defaults_invalid_priority', { + args: { arg: priority }, + }) + } + }) - defaults.selectorPriority = priority + defaults.selectorPriority = selectorPriority } if (onElement) { From e884ae5f1c64f12b884acb58ce82771c54961c54 Mon Sep 17 00:00:00 2001 From: Matt Henkes Date: Wed, 20 Oct 2021 11:19:43 -0700 Subject: [PATCH 2/4] Update packages/driver/src/cypress/selector_playground.ts Co-authored-by: Emily Rohrbough --- packages/driver/src/cypress/selector_playground.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/driver/src/cypress/selector_playground.ts b/packages/driver/src/cypress/selector_playground.ts index 35fe910918ad..27df9ca12e1e 100644 --- a/packages/driver/src/cypress/selector_playground.ts +++ b/packages/driver/src/cypress/selector_playground.ts @@ -66,7 +66,7 @@ export default { args: { arg: $utils.stringify(selectorPriority) }, }) } - // Validate that the priority is on of: "data-*", "id", "class", "tag", "attributes", "nth-child" + // Validate that the priority is one of: "data-*", "id", "class", "tag", "attributes", "nth-child" selectorPriority.forEach((priority) => { if (!priority.match(/^(data\-.*|id|class|tag|attributes|nth\-child)$/)) { From 2be65ce427cbdc732638fe7bbd4f7df6b5e51633 Mon Sep 17 00:00:00 2001 From: mjhenkes Date: Wed, 20 Oct 2021 13:33:46 -0500 Subject: [PATCH 3/4] update test name --- .../cypress/integration/cypress/selector_playground_spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/driver/cypress/integration/cypress/selector_playground_spec.js b/packages/driver/cypress/integration/cypress/selector_playground_spec.js index 89c3dd764beb..7c8555b91a77 100644 --- a/packages/driver/cypress/integration/cypress/selector_playground_spec.js +++ b/packages/driver/cypress/integration/cypress/selector_playground_spec.js @@ -55,7 +55,7 @@ describe('src/cypress/selector_playground', () => { .and.include('`Cypress.SelectorPlayground.defaults()` called with invalid `selectorPriority` property. It must be one of: `data-*`, `id`, `class`, `tag`, `attributes`, `nth-child`. You passed: `name`') }) - it('throws if selector:playground:priority if selectorPriority contains an unsupported priority that contains a valid priority', () => { + it('throws if selector:playground:priority has an unsupported priority that contains a substring of a valid priority', () => { const fn = () => { SelectorPlayground.defaults({ selectorPriority: [ From 1ae3e29732c37ae4e646c0d26f3ed628b94f3e0e Mon Sep 17 00:00:00 2001 From: Matt Henkes Date: Thu, 21 Oct 2021 06:14:20 -0700 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: Chris Breiding --- .../cypress/integration/cypress/selector_playground_spec.js | 4 ++-- packages/driver/src/cypress/selector_playground.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/driver/cypress/integration/cypress/selector_playground_spec.js b/packages/driver/cypress/integration/cypress/selector_playground_spec.js index 7c8555b91a77..dffc390dcb89 100644 --- a/packages/driver/cypress/integration/cypress/selector_playground_spec.js +++ b/packages/driver/cypress/integration/cypress/selector_playground_spec.js @@ -40,7 +40,7 @@ describe('src/cypress/selector_playground', () => { expect(SelectorPlayground.getSelectorPriority()).to.eql(selectorPriority) }) - it('throws if selector:playground:priority if selectorPriority contains an unsupported priority', () => { + it('throws if selectorPriority contains an unsupported priority', () => { const fn = () => { SelectorPlayground.defaults({ selectorPriority: [ @@ -55,7 +55,7 @@ describe('src/cypress/selector_playground', () => { .and.include('`Cypress.SelectorPlayground.defaults()` called with invalid `selectorPriority` property. It must be one of: `data-*`, `id`, `class`, `tag`, `attributes`, `nth-child`. You passed: `name`') }) - it('throws if selector:playground:priority has an unsupported priority that contains a substring of a valid priority', () => { + it('throws if selectorPriority has an unsupported priority that contains a substring of a valid priority', () => { const fn = () => { SelectorPlayground.defaults({ selectorPriority: [ diff --git a/packages/driver/src/cypress/selector_playground.ts b/packages/driver/src/cypress/selector_playground.ts index 27df9ca12e1e..60cf9e1446e7 100644 --- a/packages/driver/src/cypress/selector_playground.ts +++ b/packages/driver/src/cypress/selector_playground.ts @@ -69,7 +69,7 @@ export default { // Validate that the priority is one of: "data-*", "id", "class", "tag", "attributes", "nth-child" selectorPriority.forEach((priority) => { - if (!priority.match(/^(data\-.*|id|class|tag|attributes|nth\-child)$/)) { + if (!/^(data\-.*|id|class|tag|attributes|nth\-child)$/.test(priority)) { $errUtils.throwErrByPath('selector_playground.defaults_invalid_priority', { args: { arg: priority }, })