Skip to content

Commit 7299012

Browse files
committed
Improve documentation examples and add e2e tests to check that examples works
1 parent e78a91a commit 7299012

File tree

4 files changed

+131
-11
lines changed

4 files changed

+131
-11
lines changed

README.md

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,77 @@ cy.removeLocalStorage("item");
7676

7777
### Preserving local storage between tests
7878

79-
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:
79+
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._
80+
81+
### Examples
82+
83+
#### Cookies button example
84+
85+
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)_
8086

8187
```js
82-
it("should hide privacy policy message on click accept cookies button", () => {
83-
cy.get("#accept-cookies").click();
84-
cy.get("#privacy-policy").should("not.be.visible");
85-
cy.saveLocalStorage();
88+
describe("Accept cookies button", () => {
89+
const COOKIES_BUTTON = "#accept-cookies";
90+
91+
before(() => {
92+
cy.clearLocalStorageSnapshot();
93+
});
94+
95+
beforeEach(() => {
96+
cy.restoreLocalStorage();
97+
cy.visit("/");
98+
});
99+
100+
afterEach(() => {
101+
cy.saveLocalStorage();
102+
});
103+
104+
it("should be visible", () => {
105+
cy.get(COOKIES_BUTTON).should("be.visible");
106+
});
107+
108+
it("should not be visible after clicked", () => {
109+
cy.get(COOKIES_BUTTON).click();
110+
cy.get(COOKIES_BUTTON).should("not.be.visible");
111+
});
112+
113+
it("should not be visible after reloading", () => {
114+
cy.get(COOKIES_BUTTON).should("not.be.visible");
115+
});
86116
});
117+
```
87118

88-
it("should not show privacy policy message after reloading page", () => {
89-
cy.restoreLocalStorage();
90-
cy.reload();
91-
cy.get("#privacy-policy").should("not.be.visible");
119+
> 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.
120+
121+
#### localStorage Assertions
122+
123+
Based on the previous example, assertions could be added to check values of `localStorage`:
124+
125+
```js
126+
describe("localStorage cookies-accepted item", () => {
127+
beforeEach(() => {
128+
cy.restoreLocalStorage();
129+
cy.visit("/");
130+
});
131+
132+
afterEach(() => {
133+
cy.saveLocalStorage();
134+
});
135+
136+
it("should be null first time page is visited", () => {
137+
cy.getLocalStorage("cookies-accepted").should("equal", null);
138+
});
139+
140+
it("should be true after clicking cookies button", () => {
141+
cy.get("#accept-cookies").click();
142+
cy.getLocalStorage("cookies-accepted").should("equal", "true");
143+
});
144+
145+
it("should be true after reloading", () => {
146+
cy.getLocalStorage("cookies-accepted").then(cookiesAccepted => {
147+
expect(cookiesAccepted).to.equal("true");
148+
});
149+
});
92150
});
93151
```
94152

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
describe("localStorage cookies-accepted item", () => {
2+
beforeEach(() => {
3+
cy.restoreLocalStorage();
4+
cy.visit("/");
5+
});
6+
7+
afterEach(() => {
8+
cy.saveLocalStorage();
9+
});
10+
11+
it("should be null first time page is visited", () => {
12+
cy.getLocalStorage("cookies-accepted").should("equal", null);
13+
});
14+
15+
it("should be true after clicking cookies button", () => {
16+
cy.get("#accept-cookies").click();
17+
cy.getLocalStorage("cookies-accepted").should("equal", "true");
18+
});
19+
20+
it("should be true after reloading", () => {
21+
cy.getLocalStorage("cookies-accepted").then(cookiesAccepted => {
22+
expect(cookiesAccepted).to.equal("true");
23+
});
24+
});
25+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
describe("Accept cookies button", () => {
2+
const COOKIES_BUTTON = "#accept-cookies";
3+
4+
before(() => {
5+
cy.clearLocalStorageSnapshot();
6+
});
7+
8+
beforeEach(() => {
9+
cy.restoreLocalStorage();
10+
cy.visit("/");
11+
});
12+
13+
afterEach(() => {
14+
cy.saveLocalStorage();
15+
});
16+
17+
it("should be visible", () => {
18+
cy.get(COOKIES_BUTTON).should("be.visible");
19+
});
20+
21+
it("should not be visible after clicked", () => {
22+
cy.get(COOKIES_BUTTON).click();
23+
cy.get(COOKIES_BUTTON).should("not.be.visible");
24+
});
25+
26+
it("should not be visible after reloading", () => {
27+
cy.get(COOKIES_BUTTON).should("not.be.visible");
28+
});
29+
});
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { userPreferences } from "./origins";
22

3-
export const acceptCookies = () => userPreferences.cookiesAccepted().update(true);
3+
export const acceptCookies = () => {
4+
// save value directly in another localStorage key for an easier assertions example
5+
localStorage.setItem("cookies-accepted", true);
6+
return userPreferences.cookiesAccepted().update(true);
7+
};
48

5-
export const rejectCookies = () => userPreferences.cookiesAccepted().update(false);
9+
export const rejectCookies = () => {
10+
// save value directly in another localStorage key for an easier assertions example
11+
localStorage.setItem("cookies-accepted", false);
12+
return userPreferences.cookiesAccepted().update(false);
13+
};

0 commit comments

Comments
 (0)