Skip to content

Commit

Permalink
Merge pull request #18 from ShellyDCMS/remove-all-promises
Browse files Browse the repository at this point in the history
remove all promises and use only chainables
  • Loading branch information
ShellyDCMS committed Aug 14, 2023
2 parents f2de59e + 584d20a commit ea3f82b
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 90 deletions.
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.29](../README.md) / [Modules](../modules.md) / CypressAngularComponentHelper
[@shellygo/cypress-test-utils - v1.0.30](../README.md) / [Modules](../modules.md) / CypressAngularComponentHelper

# Class: CypressAngularComponentHelper<T\>

Expand Down
24 changes: 13 additions & 11 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.29](../README.md) / [Modules](../modules.md) / CypressHelper
[@shellygo/cypress-test-utils - v1.0.30](../README.md) / [Modules](../modules.md) / CypressHelper

# Class: CypressHelper

Expand Down Expand Up @@ -58,16 +58,16 @@ The get property will hold methods which will give our tests access to the “ou
| `elementByTestId` | (`selector`: `string`, `index?`: `number`) => `Chainable`<`JQuery`<`HTMLElement`\>\> |
| `elementByText` | (`content`: `string` \| `RegExp`, `index?`: `number`) => `Chainable`<`JQuery`<`HTMLElement`\>\> |
| `elementsAttribute` | (`selector`: `string`, `attribute`: `string`, `index?`: `number`) => `Chainable`<`undefined` \| `string`\> |
| `elementsComputedStyle` | (`selector`: `string`, `index?`: `number`, `pseudoElement?`: `string`) => `PromiseLike`<`CSSStyleDeclaration`\> |
| `elementsComputedStyle` | (`selector`: `string`, `index?`: `number`, `pseudoElement?`: `string`) => `Chainable`<`CSSStyleDeclaration`\> |
| `elementsText` | (`selector`: `string`, `index?`: `number`) => `Chainable`<`string`\> |
| `env` | (`key`: `string`) => `any` |
| `inputValue` | (`selector`: `string`, `index?`: `number`) => `Chainable`<`string` \| `number` \| `string`[]\> |
| `nthBySelector` | (`selector`: `string`, `index?`: `number`, `attribute?`: `string`) => `Chainable`<`JQuery`<`HTMLElement`\>\> |
| `numberOfElements` | (`selector`: `string`) => `Chainable`<`number`\> |
| `requestBody` | (`alias`: `string`) => `PromiseLike`<`Object`\> |
| `requestHeader` | (`alias`: `string`) => `PromiseLike`<`Object`\> |
| `requestQueryParam` | (`alias`: `string`, `queryParam`: `string`) => `PromiseLike`<``null`` \| `string`\> |
| `requestUrl` | (`alias`: `string`) => `PromiseLike`<`string`\> |
| `requestBody` | (`alias`: `string`) => `Chainable`<`any`\> |
| `requestHeader` | (`alias`: `string`) => `Chainable`<{ `[key: string]`: `string` \| `string`[]; }\> |
| `requestQueryParams` | (`alias`: `string`) => `Chainable`<{ `[k: string]`: `string`; }\> |
| `requestUrl` | (`alias`: `string`) => `Chainable`<`string`\> |
| `spy` | (`name`: `string`) => `Chainable`<`JQuery`<`HTMLElement`\>\> |
| `spyFromFunction` | (`func`: `Function`) => `Chainable`<`JQuery`<`HTMLElement`\>\> |
| `stub` | (`name`: `string`) => `Chainable`<`JQuery`<`HTMLElement`\>\> |
Expand Down Expand Up @@ -130,7 +130,7 @@ expect(helper.get.elementsAttribute('avatar-picture', 'style').should("include",
-----
**elementsComputedStyle**: (`selector`: `string`, `index?`: `number`, `pseudoElement?`: `string`) => `PromiseLike`<`CSSStyleDeclaration`\>
**elementsComputedStyle**: (`selector`: `string`, `index?`: `number`, `pseudoElement?`: `string`) => `Chainable`<`CSSStyleDeclaration`\>
Returns element's computed style, including pseudo elements
Expand Down Expand Up @@ -204,25 +204,27 @@ Get number of elements with a specific selector
-----
**requestBody**: (`alias`: `string`) => `PromiseLike`<`Object`\>
**requestBody**: (`alias`: `string`) => `Chainable`<`any`\>
Get intercepted request's body
If a JSON Content-Type was used and the body was valid JSON, this will be an object.
If the body was binary content, this will be a buffer.
-----
**requestHeader**: (`alias`: `string`) => `PromiseLike`<`Object`\>
**requestHeader**: (`alias`: `string`) => `Chainable`<{ `[key: string]`: `string` \| `string`[]; }\>
Get intercepted request's header
-----
**requestQueryParam**: (`alias`: `string`, `queryParam`: `string`) => `PromiseLike`<``null`` \| `string`\>
**requestQueryParams**: (`alias`: `string`) => `Chainable`<{ `[k: string]`: `string`; }\>
Get intercepted request's query param
-----
**requestUrl**: (`alias`: `string`) => `PromiseLike`<`string`\>
**requestUrl**: (`alias`: `string`) => `Chainable`<`string`\>
Get intercepted request's url
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.29](../README.md) / [Modules](../modules.md) / CypressLitComponentHelper
[@shellygo/cypress-test-utils - v1.0.30](../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.29](../README.md) / [Modules](../modules.md) / CypressReactComponentHelper
[@shellygo/cypress-test-utils - v1.0.30](../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.29](README.md) / Modules
[@shellygo/cypress-test-utils - v1.0.30](README.md) / Modules

# @shellygo/cypress-test-utils - v1.0.29
# @shellygo/cypress-test-utils - v1.0.30

## 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.30",
"version": "1.0.31",
"author": "Shelly Goldblit",
"private": false,
"license": "MIT",
Expand Down
71 changes: 44 additions & 27 deletions src/cypress-helper-spec.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,24 @@ describe("cypress helper tests", () => {
});
});

it("should wait for multiple responses", async () => {
given.interceptAndMockResponse({
url: "**/shellygo/whatever**",
response: { shelly: "go" },
alias: "shellygo"
describe("given multiple requests", () => {
beforeEach(() => {
given.interceptAndMockResponse({
url: "**/shellygo/whatever**",
response: { shelly: "go" },
alias: "shellygo"
});
fetch("https:/shellygo/whatever");
fetch("https:/shellygo/whatever");
});

it("should wait for multiple responses", () => {
fetch("https:/shellygo/whatever?shelly=go");
when.waitForResponses("shellygo", 2);
expect(
get.requestQueryParams("shellygo").should("include", { shelly: "go" })
);
});
fetch("https:/shellygo/whatever");
fetch("https:/shellygo/whatever");
fetch("https:/shellygo/whatever?shelly=go");
when.waitForResponses("shellygo", 2);
expect(await get.requestQueryParam("shellygo", "shelly")).to.eq("go");
});

describe("when waiting for last call", () => {
Expand All @@ -46,23 +53,27 @@ describe("cypress helper tests", () => {
when.waitForLastCall("shellygo");
});

it("should wait for last call", async () => {
it("should wait for last call", () => {
fetch("https:/shellygo/whatever?shelly=go");
expect(await get.requestQueryParam("shellygo", "shelly")).to.eq("go");
expect(
get.requestQueryParams("shellygo").should("include", { shelly: "go" })
);
});
});

it("should intercept request and test query params", async () => {
it("should intercept request and test query params", () => {
fetch("https:/shellygo/whatever?shelly=go");
given.interceptAndMockResponse({
url: "**/shellygo/whatever**",
response: { shelly: "go" },
alias: "shellygo"
});
expect(await get.requestQueryParam("shellygo", "shelly")).to.eq("go");
expect(
get.requestQueryParams("shellygo").should("include", { shelly: "go" })
);
});

it("should intercept request and test body", async () => {
it("should intercept request and test body", () => {
given.interceptAndMockResponse({
url: "**/shellygo/whatever",
method: "POST",
Expand All @@ -82,23 +93,25 @@ describe("cypress helper tests", () => {
referrerPolicy: "no-referrer",
body: JSON.stringify({ shelly: "go" })
});
expect(await get.requestBody("shellygo")).to.include({
shelly: "go"
});
expect(
get.requestBody("shellygo").should("include", {
shelly: "go"
})
);
});

it("should intercept request and test url", async () => {
it("should intercept request and test url", () => {
given.interceptAndMockResponse({
url: "**/shellygo/whatever/**",
response: { shelly: "go" },
alias: "shellygo"
});

fetch("https:/shellygo/whatever/18?param=value");
expect(await get.requestUrl("shellygo")).to.contain("whatever/18");
expect(get.requestUrl("shellygo").should("contain", "whatever/18"));
});

it("should intercept request and test header", async () => {
it("should intercept request and test header", () => {
given.interceptAndMockResponse({
url: "**/shellygo/whatever",
method: "POST",
Expand All @@ -119,9 +132,11 @@ describe("cypress helper tests", () => {
referrerPolicy: "no-referrer",
body: JSON.stringify({ shelly: "go" })
});
expect(await get.requestHeader("shellygo")).to.include({
shelly: "go"
});
expect(
get.requestHeader("shellygo").should("include", {
shelly: "go"
})
);
});

it("should get current location", () => {
Expand Down Expand Up @@ -182,9 +197,11 @@ describe("cypress helper tests", () => {
expect(get.elementByTestId("checkbox", 2).should("not.be.checked"));
});

it("should get computed style", async () => {
expect((await get.elementsComputedStyle("button")).backgroundColor).to.eq(
"rgb(255, 0, 0)"
it("should get computed style", () => {
expect(
get
.elementsComputedStyle("button")
.should("include", { backgroundColor: "rgb(255, 0, 0)" })
);
});

Expand Down
79 changes: 33 additions & 46 deletions src/cypress-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,7 @@ export class CypressHelper {
env: (key: string) => Cypress.env(key),
/**
* Get the current URL of the page that is currently active.
* @returns {Cypress.Chainable<string>
Get the current URL of the page that is }
* @returns {Cypress.Chainable<string>}
*/
currentLocation: (): Cypress.Chainable<string> => cy.url(),

Expand All @@ -393,19 +392,18 @@ Get the current URL of the page that is }
* @param selector : string
* @param [index = 0]
* @param [pseudoElement]
* @returns {PromiseLike<CSSStyleDeclaration>}
* @returns {Cypress.Chainable<CSSStyleDeclaration>}
*/
elementsComputedStyle: (
selector: string,
index: number = 0,
pseudoElement?: string
): PromiseLike<CSSStyleDeclaration> =>
new Cypress.Promise<CSSStyleDeclaration>(resolve =>
this.get.elementByTestId(selector, index).should($element => {
const element = $element.get(0);
resolve(window.getComputedStyle(element, pseudoElement));
})
),
): Cypress.Chainable<CSSStyleDeclaration> =>
this.get
.elementByTestId(selector, index)
.then($element =>
window.getComputedStyle($element.get(0), pseudoElement)
),

/**
* @example
Expand Down Expand Up @@ -491,57 +489,46 @@ Get the current URL of the page that is }
this.get.elementByTestId(selector, index).invoke("attr", attribute),
/**
* Get intercepted request's body
* If a JSON Content-Type was used and the body was valid JSON, this will be an object.
* If the body was binary content, this will be a buffer.
* @param alias
* @returns {PromiseLike<Object>}
* @returns {Cypress.Chainable<any>}
*/
requestBody: (alias: string): PromiseLike<Object> =>
new Cypress.Promise((resolve, reject) =>
this.when
.waitForResponse(alias)
.should(xhr => resolve(xhr.request.body))
),
requestBody: (alias: string): Cypress.Chainable<any> =>
this.when.waitForResponse(alias).then(xhr => xhr.request.body),
/**
* Get intercepted request's url
* @param alias
* @returns {PromiseLike<string>}
* @returns {Cypress.Chainable<string>}
*/
requestUrl: (alias: string): PromiseLike<string> =>
new Cypress.Promise((resolve, reject) =>
this.when.waitForResponse(alias).should(xhr => resolve(xhr.request.url))
),
requestUrl: (alias: string): Cypress.Chainable<string> =>
this.when.waitForResponse(alias).then(xhr => xhr.request.url),
/**
* Get intercepted request's header
* @param alias
* @returns {PromiseLike<Object>}
* @returns { Cypress.Chainable<{ [key: string]: string | string[]}>}
*/
requestHeader: (alias: string): PromiseLike<Object> =>
new Cypress.Promise((resolve, reject) =>
this.when
.waitForResponse(alias)
.should(xhr => resolve(xhr.request.headers))
),
requestHeader: (
alias: string
): Cypress.Chainable<{ [key: string]: string | string[] }> =>
this.when.waitForResponse(alias).then(xhr => xhr.request.headers),

/**
* Get intercepted request's query param
* @param alias
* @param queryParam
* @returns {PromiseLike<string | null>}
*/
requestQueryParam: (
alias: string,
queryParam: string
): PromiseLike<string | null> =>
new Cypress.Promise((resolve, reject) =>
this.when
.waitForResponse(alias)
.should(xhr =>
resolve(
new URLSearchParams(new URL(xhr.request.url).search).get(
queryParam
)
)
* @returns { Cypress.Chainable<{[k: string]: string;}>}
*/
requestQueryParams: (
alias: string
): Cypress.Chainable<{ [k: string]: string }> =>
this.when
.waitForResponse(alias)
.then(xhr =>
Object.fromEntries(
new URLSearchParams(new URL(xhr.request.url).search)
)
),

),
/**
* Get spy by alias
* @param name
Expand Down

0 comments on commit ea3f82b

Please sign in to comment.