Skip to content

Commit

Permalink
Merge pull request #7 from hammzj/dev/add-more-methods
Browse files Browse the repository at this point in the history
add:simple assertion methods for component object
  • Loading branch information
hammzj authored Jan 16, 2024
2 parents afb8bef + 522cd57 commit a2f7c6b
Show file tree
Hide file tree
Showing 6 changed files with 619 additions and 518 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hammzj/cypress-page-object",
"version": "1.0.1",
"version": "1.1.0",
"description": "A set of template classes and guides to help with developing component and page objects in Cypress.",
"author": "Zachary Hamm <zjhamm304+github@gmail.com>",
"license": "MIT",
Expand Down
30 changes: 27 additions & 3 deletions src/component.object.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isEqual } from "lodash";
import { gt, isEqual } from "lodash";
import ElementCollection from "./element.collection";

/**
Expand All @@ -11,9 +11,33 @@ export default class ComponentObject extends ElementCollection {
super(baseContainerFn);
}

//TODO: should there be specific methods for ComponentObjects?
/**
* Expects that there are no instances of the component found.
* When calling this method, it is best to ensure that the baseContainerFn only uses a single Cypress command;
* using multiple chained commands may cause this to fail if an earlier selector in the base container function cannot be found.
*/
__assertNoneExist() {
this.getAllContainers().should("not.exist");
}

__assertExists(expectation = true) {
this.container.should(isEqual(expectation, false) ? "not.exist" : "exist");
if (isEqual(expectation, false)) {
if (gt(this._scopedIndex, 0)) {
/*
The scoped index is set above 0 (i.e., it is not the first-found instance).
Make sure at least a base container exists first,
then check that the specified instance does not exist
*/
cy.log(
"Checking that at least one instance exists, then will check scoped index at " + this._scopedIndex
);
this._baseContainerFn().should("exist"); //At least one instance exists
this.container.should("not.exist"); //The specified instance does not exist
} else {
this._baseContainerFn().should("not.exist");
}
} else {
this.container.should("exist");
}
}
}
3 changes: 2 additions & 1 deletion src/element.collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ export default class ElementCollection {

/**
* Returns the first base container of the component/element.
* When `_scopedIndex` is set, then it will only select the `i` indexed container, when expecting multiple to be located.
* When `_scopedIndex` is set, then it will only return the "i"-indexed container,
* when expecting multiple elements to be located.
* @return baseContainerElement {Chainable<JQuery<E>>}
*/
get container() {
Expand Down
1 change: 1 addition & 0 deletions src/page.object.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export default class PageObject extends ElementCollection {

__assertIsOnPage(...pathInputs) {
const pageUrl = this.url(...pathInputs);
cy.log(cy.url());
cy.url().should("eq", pageUrl);
}
}
Loading

0 comments on commit a2f7c6b

Please sign in to comment.