Skip to content

Commit

Permalink
"shadowEq" command refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
abramenal committed Jul 16, 2019
1 parent 8c19d5d commit 6609119
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 9 deletions.
6 changes: 2 additions & 4 deletions example/cypress/integration/todolist.spec.js
Expand Up @@ -19,9 +19,7 @@ describe('Todo List', () => {
/* Or, same thing via */
cy.shadowGet('todo-list')
.shadowFind('todo-list-item')

// TODO: test negative values
.shadowEq(-2)
.shadowEq(0)
.shadowContains('Read some books');
});

Expand All @@ -34,7 +32,7 @@ describe('Todo List', () => {
/* Or, same thing via */
cy.shadowGet('todo-list')
.shadowFind('todo-list-item')
.shadowEq()
.shadowEq(-1)
.shadowContains('Buy some serials');
});

Expand Down
4 changes: 2 additions & 2 deletions src/commands/shadowEq/command.js
Expand Up @@ -3,9 +3,9 @@ import { validateElement, validateSubject } from '../../validators';

export default function shadowEq(subject, index) {
validateSubject(subject);
validateIndex(index);
validateIndex(index, subject.length);

const element = subject[index < 0 ? subject.length - index : index];
const element = subject[index < 0 ? subject.length + index : index];
validateElement(element);

Cypress.log({
Expand Down
4 changes: 2 additions & 2 deletions src/commands/shadowEq/validators.js
@@ -1,7 +1,7 @@
import { ERR_TYPES, InternalError } from '../../error';

export const validateIndex = index => {
if (typeof index !== 'number') {
export const validateIndex = (index, collectionLength) => {
if (typeof index !== 'number' || Math.abs(index) >= collectionLength) {
throw new InternalError(ERR_TYPES.INVALID_INDEX);
}
};
2 changes: 1 addition & 1 deletion src/error.js
Expand Up @@ -25,7 +25,7 @@ export const ERR_TYPES = {
},
INVALID_INDEX: {
message: 'Index is not valid',
tip: 'A number indicates the index to find the element at within an array of elements',
tip: 'It should be a positive or negative number within a range of the items collection',
},
INVALID_TEXT: {
message: 'Text is not valid',
Expand Down

0 comments on commit 6609119

Please sign in to comment.