Skip to content

Commit

Permalink
change ctor to get options
Browse files Browse the repository at this point in the history
  • Loading branch information
shelly_goldblit committed Oct 4, 2023
1 parent f516420 commit b7cab38
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 37 deletions.
12 changes: 12 additions & 0 deletions documents/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ In the [examples](https://github.com/ShellyDCMS/cypress-test-utils-examples/tree
Each app contains a driver that uses helpers, component tests, integration tests and e2e tests.
As you can see, all test levels use the *same* driver, meaning that if the feature's implementation changes, you'll need to change the driver alone, not the tests.

## Framework Spesific Information
### Using Shadow DOM
When using <slot> elements with shadow dom, some things may not be where you expect them, fo example the text of this button is not directly inside the slot containing it.
![image](https://github.com/ShellyDCMS/cypress-test-utils/assets/60476837/c14b0877-4c9a-4f37-ba18-0220b9192b0f)
CypressHelper will look for the assignedNode to retrieve the text, given that the selector of the slot has a '-slot' suffix.
You may change this behaviour by overriding the default values when creating CypressHelper.

### Angular
When mounting an angular component, autoSpyOutputs is set to true, meaning all event emitters are automatically spied on and may be accessed during a test using `helper.get.spy("<EventEmitterName>")`

###
## Usage
This library provides an API to interact with UI elements - `CypressHelper` that combines the common features.
To add it to your repo use
Expand All @@ -65,3 +76,4 @@ or
## Developing
1. Set up the repo - `yarn`
2. Build the project - `npm run build`
3. Runnin tests - `npm run cy:run`
2 changes: 1 addition & 1 deletion documents/classes/CypressAngularComponentHelper.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[@shellygo/cypress-test-utils - v1.0.34](../README.md) / [Modules](../modules.md) / CypressAngularComponentHelper
[@shellygo/cypress-test-utils - v1.0.36](../README.md) / [Modules](../modules.md) / CypressAngularComponentHelper

# Class: CypressAngularComponentHelper<T\>

Expand Down
32 changes: 12 additions & 20 deletions documents/classes/CypressHelper.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[@shellygo/cypress-test-utils - v1.0.34](../README.md) / [Modules](../modules.md) / CypressHelper
[@shellygo/cypress-test-utils - v1.0.36](../README.md) / [Modules](../modules.md) / CypressHelper

# Class: CypressHelper

Expand All @@ -14,10 +14,9 @@ CypressHelper exposes the following public properties:

### Properties

- [defaultDataAttribute](CypressHelper.md#defaultdataattribute)
- [defaultShadowSlotSuffix](CypressHelper.md#defaultshadowslotsuffix)
- [get](CypressHelper.md#get)
- [given](CypressHelper.md#given)
- [options](CypressHelper.md#options)
- [when](CypressHelper.md#when)

### Methods
Expand All @@ -28,29 +27,16 @@ CypressHelper exposes the following public properties:

### constructor

**new CypressHelper**(`defaultDataAttribute?`, `defaultShadowSlotSuffix?`)
**new CypressHelper**(`options?`)

#### Parameters

| Name | Type | Default value |
| :------ | :------ | :------ |
| `defaultDataAttribute?` | `string` | `"data-cy"` |
| `defaultShadowSlotSuffix?` | `string` | `"slot"` |
| Name | Type |
| :------ | :------ |
| `options?` | `Properties`<`CypressHelperOptions`\> |

## Properties

### defaultDataAttribute

`Private` `Readonly` **defaultDataAttribute**: `string` = `"data-cy"`

___

### defaultShadowSlotSuffix

`Private` `Readonly` **defaultShadowSlotSuffix**: `string` = `"slot"`

___

### get

**get**: `Object`
Expand Down Expand Up @@ -362,6 +348,12 @@ Replace a function, record its usage and control its behavior.
___
### options
`Readonly` **options**: `Properties`<`CypressHelperOptions`\>
___
### when
• **when**: `Object`
Expand Down
2 changes: 1 addition & 1 deletion documents/classes/CypressLitComponentHelper.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[@shellygo/cypress-test-utils - v1.0.34](../README.md) / [Modules](../modules.md) / CypressLitComponentHelper
[@shellygo/cypress-test-utils - v1.0.36](../README.md) / [Modules](../modules.md) / CypressLitComponentHelper

# Class: CypressLitComponentHelper

Expand Down
2 changes: 1 addition & 1 deletion documents/classes/CypressReactComponentHelper.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[@shellygo/cypress-test-utils - v1.0.34](../README.md) / [Modules](../modules.md) / CypressReactComponentHelper
[@shellygo/cypress-test-utils - v1.0.36](../README.md) / [Modules](../modules.md) / CypressReactComponentHelper

# Class: CypressReactComponentHelper

Expand Down
4 changes: 2 additions & 2 deletions documents/modules.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[@shellygo/cypress-test-utils - v1.0.34](README.md) / Modules
[@shellygo/cypress-test-utils - v1.0.36](README.md) / Modules

# @shellygo/cypress-test-utils - v1.0.34
# @shellygo/cypress-test-utils - v1.0.36

## Table of contents

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@shellygo/cypress-test-utils",
"description": "Test Automation Utilities",
"version": "1.0.35",
"version": "1.0.36",
"author": "Shelly Goldblit",
"private": false,
"license": "MIT",
Expand Down
8 changes: 6 additions & 2 deletions src/cypress-helper-spec.cy.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { CypressHelper } from "./cypress-helper";

describe("cypress helper tests", () => {
let { beforeAndAfter, given, when, get } = new CypressHelper("data-hook");
let { beforeAndAfter, given, when, get } = new CypressHelper({
defaultDataAttribute: "data-hook"
});
beforeAndAfter();
beforeEach(() => {
({ given, when, get } = new CypressHelper("data-hook"));
({ given, when, get } = new CypressHelper({
defaultDataAttribute: "data-hook"
}));

when.visit(
"https://htmlpreview.github.io/?https://raw.githubusercontent.com/ShellyDCMS/cypress-test-utils/main/index.html"
Expand Down
29 changes: 20 additions & 9 deletions src/cypress-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ import "cypress-wait-if-happens";
import "cypress-wait-until";

import { StringMatcher } from "cypress/types/net-stubbing";

type Properties<T> = Pick<
T,
{ [K in keyof T]: T[K] extends Function ? never : K }[keyof T]
>;

class CypressHelperOptions {
defaultDataAttribute?: string = "data-cy";
defaultShadowSlotSuffix?: string = "slot";
handleSlotShadowDOM?: boolean = true;
}
/**
* @class CypressHelper was designed to help you develop cypress tests faster.
* @classdes CypressHelper exposes the following public properties:
Expand All @@ -19,13 +30,12 @@ import { StringMatcher } from "cypress/types/net-stubbing";
export class CypressHelper {
/**
*
* @param [defaultDataAttribute = "data-cy"]
* @param [defaultShadowSlotSuffix = "slot]
* @param [options = {
* defaultDataAttribute : "data-cy" ,
* defaultShadowSlotSuffix : "slot",
* handleSlotShadowDOM : true}]
*/
constructor(
private readonly defaultDataAttribute: string = "data-cy",
private readonly defaultShadowSlotSuffix: string = "slot"
) {}
constructor(public readonly options: Properties<CypressHelperOptions>) {}

beforeAndAfter = () => {
before(() => {
Expand Down Expand Up @@ -358,7 +368,7 @@ export class CypressHelper {
*/
bySelector: (
selector: string,
attribute: string = this.defaultDataAttribute
attribute: string = this.options.defaultDataAttribute!
) => cy.get(`[${attribute}="${selector}"]`),
/**
* Get A DOM element at a specific index from elements.
Expand All @@ -374,7 +384,7 @@ export class CypressHelper {
nthBySelector: (
selector: string,
index: number,
attribute: string = this.defaultDataAttribute
attribute: string = this.options.defaultDataAttribute!
) => this.get.bySelector(selector, attribute).eq(index),
/**
* Returns specific environment variable or undefined
Expand Down Expand Up @@ -474,7 +484,8 @@ export class CypressHelper {
* @param [index = 0]
*/
elementByTestId: (selector: string, index: number = 0) =>
selector.endsWith(`-${this.defaultShadowSlotSuffix}`)
this.options.handleSlotShadowDOM &&
selector.endsWith(`-${this.options.defaultShadowSlotSuffix}`)
? this.get.nthBySelector(selector, index).then(slot =>
cy.wrap(
Cypress.$(slot as JQuery<HTMLSlotElement>)
Expand Down

0 comments on commit b7cab38

Please sign in to comment.