From 5ac8b0103f0f2f3ea87894e41f523688aef8b0bf Mon Sep 17 00:00:00 2001 From: Nicholas Boll Date: Fri, 20 Dec 2019 09:08:07 -0700 Subject: [PATCH] fix: Off by one error in next/previous tab (#15) * fix: Off by one error in next/previous tab * Fixes #13 * bump cypress Co-authored-by: Ben Kucera <14625260+Bkucera@users.noreply.github.com> --- .vscode/settings.json | 3 +++ circle.yml | 2 ++ cypress/integration/spec.js | 10 ++++++++++ package.json | 2 +- src/index.js | 16 ++++------------ yarn.lock | 20 ++++++++++++++++---- 6 files changed, 36 insertions(+), 17 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 70ad95f..5702ffc 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -23,4 +23,7 @@ "autoFix": true }, ], + "editor.codeActionsOnSave": { + "source.fixAll.eslint": true + }, } diff --git a/circle.yml b/circle.yml index a816e4d..54356d3 100644 --- a/circle.yml +++ b/circle.yml @@ -19,6 +19,8 @@ jobs: command: yarn lint - run: command: yarn test-e2e + - run: + command: yarn add -D cypress@3.4.1 && yarn test-e2e - run: command: yarn run semantic-release workflows: diff --git a/cypress/integration/spec.js b/cypress/integration/spec.js index c5cc9c4..d31ab98 100644 --- a/cypress/integration/spec.js +++ b/cypress/integration/spec.js @@ -93,6 +93,16 @@ describe('form test', () => { cy.get('body').should(beFocused) }) + it('moves focus back to the first element when the last element is focused', () => { + cy.get('a:last').tab() + cy.get('a:first').should(beFocused) + }) + + it('moves focus back to the first element when the last element is focused', () => { + cy.get('a:first').tab({ shift: true }) + cy.get('a:last').should(beFocused) + }) + describe('events', () => { beforeEach(() => { diff --git a/package.json b/package.json index d5d3bd8..5d69a3c 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "@cypress/eslint-plugin-dev": "^4.0.0", "@typescript-eslint/eslint-plugin": "^2.3.0", "@typescript-eslint/parser": "^2.3.0", - "cypress": "^3.4.1", + "cypress": "^3.8.0", "eslint": "^6.4.0", "eslint-plugin-cypress": "^2.2.1", "eslint-plugin-json-format": "^2.0.1", diff --git a/src/index.js b/src/index.js index d5330bb..6e55a0f 100644 --- a/src/index.js +++ b/src/index.js @@ -75,22 +75,14 @@ const performTab = (el, options) => { const nextItemFromIndex = (i, seq, reverse) => { if (reverse) { - if (i === 0 || i === -1) { - i = seq.length - } - } else { - if (i === seq.length) { - i = 0 - } + const nextIndex = i <= 0 ? seq.length - 1 : i - 1 + return seq[nextIndex] } - // } - if (reverse) { - return seq[i - 1] - } + const nextIndex = i === seq.length - 1 ? 0 : i + 1 - return seq[i + 1] + return seq[nextIndex] } const tabKeyEventPartial = { diff --git a/yarn.lock b/yarn.lock index de83349..c00bbbd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -246,6 +246,11 @@ resolved "https://registry.yarnpkg.com/@types/retry/-/retry-0.12.0.tgz#2b35eccfcee7d38cd72ad99232fbd58bffb3c84d" integrity sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA== +"@types/sizzle@2.3.2": + version "2.3.2" + resolved "https://registry.yarnpkg.com/@types/sizzle/-/sizzle-2.3.2.tgz#a811b8c18e2babab7d542b3365887ae2e4d9de47" + integrity sha512-7EJYyKTL7tFR8+gDbB6Wwz/arpGa0Mywk1TJbNzKzHtzbwVmY4HR9WqS5VV7dsBUKQmPNr192jHr/VpBluj/hg== + "@typescript-eslint/eslint-plugin@^2.3.0": version "2.3.0" resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-2.3.0.tgz#6ead12c6b15a9b930430931e396e01a1fe181fcc" @@ -1118,13 +1123,14 @@ cyclist@^1.0.1: resolved "https://registry.yarnpkg.com/cyclist/-/cyclist-1.0.1.tgz#596e9698fd0c80e12038c2b82d6eb1b35b6224d9" integrity sha1-WW6WmP0MgOEgOMK4LW6xs1tiJNk= -cypress@^3.4.1: - version "3.4.1" - resolved "https://registry.yarnpkg.com/cypress/-/cypress-3.4.1.tgz#ca2e4e9864679da686c6a6189603efd409664c30" - integrity sha512-1HBS7t9XXzkt6QHbwfirWYty8vzxNMawGj1yI+Fu6C3/VZJ8UtUngMW6layqwYZzLTZV8tiDpdCNBypn78V4Dg== +cypress@^3.8.0: + version "3.8.0" + resolved "https://registry.yarnpkg.com/cypress/-/cypress-3.8.0.tgz#7d4cd08f81f9048ee36760cc9ee3b9014f9e84ab" + integrity sha512-gtEbqCgKETRc3pQFMsELRgIBNgiQg7vbOWTrCi7WE7bgOwNCaW9PEX8Jb3UN8z/maIp9WwzoFfeySfelYY7nRA== dependencies: "@cypress/listr-verbose-renderer" "0.4.1" "@cypress/xvfb" "1.2.4" + "@types/sizzle" "2.3.2" arch "2.1.1" bluebird "3.5.0" cachedir "1.3.0" @@ -1151,6 +1157,7 @@ cypress@^3.4.1: request-progress "3.0.0" supports-color "5.5.0" tmp "0.1.0" + untildify "3.0.3" url "0.11.0" yauzl "2.10.0" @@ -5528,6 +5535,11 @@ unpipe@~1.0.0: resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec" integrity sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw= +untildify@3.0.3: + version "3.0.3" + resolved "https://registry.yarnpkg.com/untildify/-/untildify-3.0.3.tgz#1e7b42b140bcfd922b22e70ca1265bfe3634c7c9" + integrity sha512-iSk/J8efr8uPT/Z4eSUywnqyrQU7DSdMfdqK4iWEaUVVmcP5JcnpRqmVMwcwcnmI1ATFNgC5V90u09tBynNFKA== + unzip-response@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/unzip-response/-/unzip-response-2.0.1.tgz#d2f0f737d16b0615e72a6935ed04214572d56f97"