Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit 5c0856a

Browse files
committed
add localStorage hooks example
1 parent d8dd37c commit 5c0856a

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ Spec | Description
156156
[testing-lib-example](cypress/component/advanced/testing-lib-example) | A spec adopted from [@testing-library/react](https://testing-library.com/docs/react-testing-library/example-intro) that uses [@testing-library/cypress](https://testing-library.com/docs/cypress-testing-library/intro)
157157
[timers](cypress/component/advanced/timers) | Testing components that set timers, adopted from [ReactJS Testing recipes](https://reactjs.org/docs/testing-recipes.html#timers)
158158
[tutorial](cypress/component/advanced/tutorial) | A few tests adopted from [ReactJS Tutorial](https://reactjs.org/tutorial/tutorial.html), including Tic-Tac-Toe game
159+
[use-local-storage](cypress/component/advanced/use-local-storage) | Use hooks to load and save items into `localStorage`
159160
[portal](cypress/component/advanced/portal) | Component test for `ReactDOM.createPortal` feature
160161
[react-bootstrap](cypress/component/advanced/react-bootstrap) | Confirms [react-bootstrap](https://react-bootstrap.github.io/) components are working
161162
[select React component](cypress/component/advanced/react-book-example/src/components/ProductsList.spec.js) | Uses [cypress-react-selector](https://github.com/abhinaba-ghosh/cypress-react-selector) to find DOM elements using React component name and state values
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React, { useState, useEffect } from 'react'
2+
3+
export default function App() {
4+
const [cart, setCart] = useState(
5+
JSON.parse(localStorage.getItem('cart')) || ['kiwi 🥝'],
6+
)
7+
8+
const addJuice = () => {
9+
const updatedCart = cart.concat('juice 🧃')
10+
setCart(updatedCart)
11+
localStorage.setItem('cart', JSON.stringify(updatedCart))
12+
}
13+
14+
return (
15+
<div className="cart">
16+
<ul>
17+
{cart.map((item, k) => (
18+
<li className="item" key={k}>
19+
{item}
20+
</li>
21+
))}
22+
</ul>
23+
<button onClick={addJuice}>Add juice</button>
24+
</div>
25+
)
26+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/// <reference types="cypress" />
2+
import React from 'react'
3+
import { mount } from 'cypress-react-unit-test'
4+
import App from './App.jsx'
5+
6+
describe('App', () => {
7+
beforeEach(() => {
8+
// we need to clear the state before each test ourselves
9+
localStorage.clear('cart')
10+
})
11+
12+
it('uses cart from localStorage', () => {
13+
const items = ['apples 🍎', 'oranges 🍊', 'grapes 🍇']
14+
localStorage.setItem('cart', JSON.stringify(items))
15+
mount(<App />)
16+
cy.get('.item').should('have.length', 3)
17+
cy.contains('.item', 'oranges 🍊').should('be.visible')
18+
})
19+
20+
it('sets default cart otherwise', () => {
21+
mount(<App />)
22+
cy.contains('.item', 'kiwi 🥝').should('be.visible')
23+
cy.contains('Add juice').click()
24+
cy.contains('.item', 'juice 🧃')
25+
// and the new item should be added to localStorage
26+
// make an assertion retry so even if the localStorage
27+
// is updated after a delay, the assertion waits for it
28+
// https://on.cypress.io/retry-ability
29+
cy.wrap(localStorage)
30+
.invoke('getItem', 'cart')
31+
.should('equal', JSON.stringify(['kiwi 🥝', 'juice 🧃']))
32+
})
33+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# localStorage with useEffect
2+
3+
App idea from https://twitter.com/housecor/status/1268900696604258304
4+
5+
Shows how to set `localStorage` before mounting the component, and how to clear the local storage before each test.
6+
7+
![Test](images/cart.png)
8+
9+
See [App.jsx](App.jsx) and [App.spec.jsx](App.spec.jsx)
366 KB
Loading

0 commit comments

Comments
 (0)