Skip to content

Commit

Permalink
Merge pull request #13 from javierbrea/v1.1.1
Browse files Browse the repository at this point in the history
V1.1.1
  • Loading branch information
javierbrea committed Dec 28, 2019
2 parents 65b7453 + 2cfd86a commit 985cc46
Show file tree
Hide file tree
Showing 42 changed files with 5,566 additions and 3,359 deletions.
4 changes: 0 additions & 4 deletions .gitignore
Expand Up @@ -8,10 +8,6 @@

# tests
/coverage
/test-acceptance/app/build
/test-acceptance/app/node_modules
/test-acceptance/app/cypress/screenshots
/test-acceptance/app/cypress/fixtures

# misc
.DS_Store
Expand Down
9 changes: 2 additions & 7 deletions .travis.yml
Expand Up @@ -8,7 +8,7 @@ cache:
- ~/.npm
- ~/.cache
- node_modules
- test-acceptance/app/node_modules
- test-e2e/react-app/node_modules
- ~/.sonar/cache

addons:
Expand All @@ -25,12 +25,7 @@ addons:
script:
- npm run lint
- npm run test
- cd test-acceptance/app
- npm ci
- npm run cypress:install
- npm run cypress:verify
- npm run test:ci
- cd ../..
- npm run test-e2e:ci
- npm run coveralls
- 'if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then sonar-scanner; fi'

Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Removed
### BREAKING CHANGES

## [1.1.1] - 2019-12-28
### Changed
- Upgrade dependencies.
- Rename acceptance tests into e2e tests.
- Improve documentation examples.

### Added
- Add npm command for running e2e tests.
- Add e2e tests to check that examples works.

## [1.1.0] - 2019-10-27
### Added
- Add getLocalStorage command
Expand Down
76 changes: 67 additions & 9 deletions README.md
Expand Up @@ -76,19 +76,77 @@ cy.removeLocalStorage("item");

### Preserving local storage between tests

Use `saveLocalStorage` to save a snapshot of current `localStorage` at the end of one test, and use the `restoreLocalStorage` command to restore it at the beginning of another one:
Use `saveLocalStorage` to save a snapshot of current `localStorage` at the end of one test, and use the `restoreLocalStorage` command to restore it at the beginning of another one. _The usage of `beforeEach` and `afterEach` is recommended for this purpose._

### Examples

#### Cookies button example

Next example shows how this package can be used to test a "cookies button" _(which theoretically set a flag into `localStorage` and can be clicked only once)_

```js
it("should hide privacy policy message on click accept cookies button", () => {
cy.get("#accept-cookies").click();
cy.get("#privacy-policy").should("not.be.visible");
cy.saveLocalStorage();
describe("Accept cookies button", () => {
const COOKIES_BUTTON = "#accept-cookies";

before(() => {
cy.clearLocalStorageSnapshot();
});

beforeEach(() => {
cy.restoreLocalStorage();
cy.visit("/");
});

afterEach(() => {
cy.saveLocalStorage();
});

it("should be visible", () => {
cy.get(COOKIES_BUTTON).should("be.visible");
});

it("should not be visible after clicked", () => {
cy.get(COOKIES_BUTTON).click();
cy.get(COOKIES_BUTTON).should("not.be.visible");
});

it("should not be visible after reloading", () => {
cy.get(COOKIES_BUTTON).should("not.be.visible");
});
});
```

it("should not show privacy policy message after reloading page", () => {
cy.restoreLocalStorage();
cy.reload();
cy.get("#privacy-policy").should("not.be.visible");
> Note the usage of `beforeEach` and `afterEach` for preserving `localStorage` between all tests. Also `clearLocalStorageSnapshot` is used in the `before` statement to avoid possible conflicts with other test files preserving localStorage.
#### localStorage assertions

Based on the previous example, assertions could be added to check values of `localStorage`:

```js
describe("localStorage cookies-accepted item", () => {
beforeEach(() => {
cy.restoreLocalStorage();
cy.visit("/");
});

afterEach(() => {
cy.saveLocalStorage();
});

it("should be null first time page is visited", () => {
cy.getLocalStorage("cookies-accepted").should("equal", null);
});

it("should be true after clicking cookies button", () => {
cy.get("#accept-cookies").click();
cy.getLocalStorage("cookies-accepted").should("equal", "true");
});

it("should be true after reloading", () => {
cy.getLocalStorage("cookies-accepted").then(cookiesAccepted => {
expect(cookiesAccepted).to.equal("true");
});
});
});
```

Expand Down

0 comments on commit 985cc46

Please sign in to comment.