cypress-localstorage-commands
Advanced tools
Comparing version 1.6.1 to 1.7.0
@@ -7,17 +7,20 @@ /// <reference types="cypress" /> | ||
* Command to save current localStorage values into an internal "snapshot" | ||
* @param {string} snapshotName - Name of the snapshot | ||
* @example cy.saveLocalStorage() | ||
*/ | ||
saveLocalStorage(): Chainable<undefined> | ||
saveLocalStorage(snapshotName?: string): Chainable<undefined> | ||
/** | ||
* Command to restore localStorage to previously "snapshot" saved values | ||
* @param {string} snapshotName - Name of the snapshot | ||
* @example cy.restoreLocalStorage() | ||
*/ | ||
restoreLocalStorage(): Chainable<undefined> | ||
restoreLocalStorage(snapshotName?: string): Chainable<undefined> | ||
/** | ||
* Command to clear localStorage "snapshot" values | ||
* @param {string} snapshotName - Name of the snapshot | ||
* @example cy.clearLocalStorageSnapshot() | ||
*/ | ||
clearLocalStorageSnapshot(): Chainable<undefined> | ||
clearLocalStorageSnapshot(snapshotName?: string): Chainable<undefined> | ||
@@ -24,0 +27,0 @@ /** |
{ | ||
"name": "cypress-localstorage-commands", | ||
"version": "1.6.1", | ||
"version": "1.7.0", | ||
"description": "Extends Cypress' cy commands with localStorage methods. Allows preserving localStorage between tests", | ||
@@ -30,6 +30,7 @@ "keywords": [ | ||
"lint:all": "npm run lint:javascript && npm run lint:typescript", | ||
"lint": "npm run test:e2e:typescript:install && npm run lint:all", | ||
"lint": "npm run test:e2e:typescript:install && npm run test:e2e:react:install && npm run lint:all", | ||
"lint:local": "npm run lint:all", | ||
"lint-staged": "lint-staged", | ||
"test": "jest", | ||
"test:e2e:react:install": "cd test-e2e/react-app && npm i", | ||
"test:e2e:typescript:install": "cd test-e2e/typescript && npm i", | ||
@@ -44,2 +45,3 @@ "test:e2e:react": "cd test-e2e/react-app && npm run test:ci", | ||
"test:ci": "npm run test:unit && npm run test:mutation && npm run test:e2e", | ||
"tsc": "tsc", | ||
"prepare": "is-ci || husky install" | ||
@@ -51,15 +53,21 @@ }, | ||
"devDependencies": { | ||
"@stryker-mutator/core": "5.4.1", | ||
"@stryker-mutator/jest-runner": "5.4.1", | ||
"babel-eslint": "10.1.0", | ||
"eslint": "7.32.0", | ||
"eslint-config-prettier": "8.3.0", | ||
"eslint-plugin-prettier": "3.4.1", | ||
"eslint-plugin-react": "7.26.1", | ||
"@babel/eslint-parser": "7.17.0", | ||
"@babel/preset-react": "7.16.7", | ||
"@stryker-mutator/core": "5.6.1", | ||
"@stryker-mutator/jest-runner": "5.6.1", | ||
"cypress": "9.5.0", | ||
"eslint": "8.9.0", | ||
"eslint-config-prettier": "8.4.0", | ||
"eslint-plugin-jest": "26.1.1", | ||
"eslint-plugin-mocha": "10.0.3", | ||
"eslint-plugin-no-only-tests": "2.6.0", | ||
"eslint-plugin-prettier": "4.0.0", | ||
"eslint-plugin-react": "7.28.0", | ||
"husky": "7.0.4", | ||
"is-ci": "3.0.1", | ||
"jest": "27.3.1", | ||
"lint-staged": "11.2.6", | ||
"prettier": "2.4.1", | ||
"sinon": "11.1.2" | ||
"jest": "27.5.1", | ||
"lint-staged": "12.3.4", | ||
"prettier": "2.5.1", | ||
"sinon": "13.0.1", | ||
"typescript": "4.5.5" | ||
}, | ||
@@ -66,0 +74,0 @@ "lint-staged": { |
[![Build status][build-image]][build-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Quality Gate][quality-gate-image]][quality-gate-url] [![Mutation testing status][mutation-image]][mutation-url] | ||
[![NPM dependencies][npm-dependencies-image]][npm-dependencies-url] [![Renovate](https://img.shields.io/badge/renovate-enabled-brightgreen.svg)](https://renovatebot.com) [![Last commit][last-commit-image]][last-commit-url] [![Last release][release-image]][release-url] | ||
[![Renovate](https://img.shields.io/badge/renovate-enabled-brightgreen.svg)](https://renovatebot.com) [![Last commit][last-commit-image]][last-commit-url] [![Last release][release-image]][release-url] | ||
@@ -42,14 +42,20 @@ [![NPM downloads][npm-downloads-image]][npm-downloads-url] [![License][license-image]][license-url] [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fjavierbrea%2Fcypress-localstorage-commands.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fjavierbrea%2Fcypress-localstorage-commands?ref=badge_shield) | ||
##### `cy.saveLocalStorage()` | ||
##### `cy.saveLocalStorage([snapshotName])` | ||
Saves current localStorage values into an internal "snapshot". | ||
##### `cy.restoreLocalStorage()` | ||
* `snapshotName` _(String)_: Optionally, a `snapshotName` can be provided, and then data from localStorage will be saved into a snapshot with that name. So, multiple snapshots can be stored. | ||
Restores localStorage to previously "snapshot" saved values. | ||
##### `cy.restoreLocalStorage([snapshotName])` | ||
##### `cy.clearLocalStorageSnapshot()` | ||
Restores localStorage to previously "snapshot" saved values. __ | ||
* `snapshotName` _(String)_: Optional. If provided, the localStorage will be restored using data from that specific snapshot. | ||
##### `cy.clearLocalStorageSnapshot([snapshotName])` | ||
Clears localStorage "snapshot" values, so previously saved values are cleaned. | ||
* `snapshotName` _(String)_: Optional. If provided, only data from that specific snapshot will be cleared. | ||
##### `cy.getLocalStorage(item)` | ||
@@ -157,2 +163,40 @@ | ||
#### Named snapshots | ||
Next example shows how named snapshots can be used to storage different states of `localStorage` and restore one or another depending of the test: | ||
```js | ||
describe("Accept cookies button", () => { | ||
const COOKIES_BUTTON = "#accept-cookies"; | ||
before(() => { | ||
cy.clearLocalStorageSnapshot(); | ||
}); | ||
it("should be visible", () => { | ||
cy.visit("/"); | ||
cy.get(COOKIES_BUTTON).should("be.visible"); | ||
cy.saveLocalStorage("cookies-not-accepted"); | ||
}); | ||
it("should not exist after clicked", () => { | ||
cy.get(COOKIES_BUTTON).click(); | ||
cy.get(COOKIES_BUTTON).should("not.exist"); | ||
cy.saveLocalStorage("cookies-accepted"); | ||
}); | ||
it("should be visible when cookies are not accepted", () => { | ||
cy.restoreLocalStorage("cookies-not-accepted"); | ||
cy.visit("/"); | ||
cy.get(COOKIES_BUTTON).should("be.visible"); | ||
}); | ||
it("should not exist when cookies are accepted", () => { | ||
cy.restoreLocalStorage("cookies-accepted"); | ||
cy.visit("/"); | ||
cy.get(COOKIES_BUTTON).should("not.exist"); | ||
}); | ||
}); | ||
``` | ||
### Disabling localStorage | ||
@@ -268,4 +312,2 @@ | ||
[npm-downloads-url]: https://www.npmjs.com/package/cypress-localstorage-commands | ||
[npm-dependencies-image]: https://img.shields.io/david/javierbrea/cypress-localstorage-commands.svg | ||
[npm-dependencies-url]: https://david-dm.org/javierbrea/cypress-localstorage-commands | ||
[quality-gate-image]: https://sonarcloud.io/api/project_badges/measure?project=javierbrea_cypress-localstorage-commands&metric=alert_status | ||
@@ -272,0 +314,0 @@ [quality-gate-url]: https://sonarcloud.io/dashboard?id=javierbrea_cypress-localstorage-commands |
@@ -27,2 +27,3 @@ const LOCAL_STORAGE_METHODS = ["setItem", "getItem", "removeItem", "clear"]; | ||
constructor(localStorage, cy) { | ||
this._namedSnapshots = {}; | ||
this._cy = cy; | ||
@@ -36,11 +37,23 @@ this._localStorage = localStorage; | ||
clearLocalStorageSnapshot() { | ||
this._snapshot = {}; | ||
_saveLocalStorageKey(key, snapshotName) { | ||
if (snapshotName) { | ||
this._namedSnapshots[snapshotName][key] = this._localStorage.getItem(key); | ||
} else { | ||
this._snapshot[key] = this._localStorage.getItem(key); | ||
} | ||
} | ||
saveLocalStorage() { | ||
clearLocalStorageSnapshot(snapshotName) { | ||
if (snapshotName) { | ||
this._namedSnapshots[snapshotName] = {}; | ||
} else { | ||
this._snapshot = {}; | ||
} | ||
} | ||
saveLocalStorage(snapshotName) { | ||
if (!this._localStorage.getItem.wrappedMethod) { | ||
this.clearLocalStorageSnapshot(); | ||
this.clearLocalStorageSnapshot(snapshotName); | ||
Object.keys(this._localStorage).forEach((key) => { | ||
this._snapshot[key] = this._localStorage.getItem(key); | ||
this._saveLocalStorageKey(key, snapshotName); | ||
}); | ||
@@ -50,6 +63,9 @@ } | ||
restoreLocalStorage() { | ||
restoreLocalStorage(snapshotName) { | ||
this._localStorage.clear(); | ||
Object.keys(this._snapshot).forEach((key) => { | ||
this._localStorage.setItem(key, this._snapshot[key]); | ||
const snapshotToRestore = !!snapshotName | ||
? this._namedSnapshots[snapshotName] || {} | ||
: this._snapshot; | ||
Object.keys(snapshotToRestore).forEach((key) => { | ||
this._localStorage.setItem(key, snapshotToRestore[key]); | ||
}); | ||
@@ -56,0 +72,0 @@ } |
20853
155
316
19