-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from mcrowder65/add-local-storage
added useLocalStorage
- Loading branch information
Showing
6 changed files
with
186 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import React from "react"; | ||
import PropTypes from "prop-types"; | ||
import { render, fireEvent } from "react-testing-library"; | ||
|
||
import useLocalStorageSetState from "../useLocalStorageSetState"; | ||
|
||
function MyComponent() { | ||
const [name, setName] = useLocalStorageSetState("", "name"); | ||
const [flag, setFlag] = useLocalStorageSetState(false, "flag"); | ||
const [array, setArray] = useLocalStorageSetState([], "array"); | ||
return ( | ||
<div> | ||
{name} | ||
<input | ||
data-testid="name" | ||
value={name} | ||
onChange={(e) => setName(e.target.value)} | ||
/> | ||
|
||
<button data-testid="button" onClick={() => setFlag((state) => !state)}> | ||
{String(flag)} | ||
</button> | ||
{array.join(",")} | ||
<button | ||
data-testid="array-button" | ||
onClick={() => setArray((state) => [...state, state.length + 1])} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
test("that after inserting text into the document then remounting, the text will remain", () => { | ||
const { queryByText, queryByTestId, unmount, rerender } = render( | ||
<MyComponent />, | ||
); | ||
|
||
const newValue = "Lebron James"; | ||
fireEvent.change(queryByTestId("name"), { target: { value: newValue } }); | ||
|
||
expect(queryByText(newValue)).toBeInTheDocument(); | ||
|
||
unmount(); | ||
|
||
rerender(<MyComponent />); | ||
|
||
expect(queryByText(newValue)).toBeInTheDocument(); | ||
}); | ||
|
||
test("that on component reloads, booleans persist", () => { | ||
const { getByText, getByTestId, unmount, rerender } = render(<MyComponent />); | ||
|
||
fireEvent.click(getByTestId("button")); | ||
|
||
expect(getByText(/true/i)).toBeInTheDocument(); | ||
|
||
unmount(); | ||
|
||
rerender(<MyComponent />); | ||
|
||
expect(getByText(/true/i)).toBeInTheDocument(); | ||
}); | ||
|
||
test("that on component reloads, arrays persist", () => { | ||
const { getByText, getByTestId, unmount, rerender } = render(<MyComponent />); | ||
|
||
fireEvent.click(getByTestId("array-button")); | ||
|
||
expect(getByText(/1/i)).toBeInTheDocument(); | ||
|
||
fireEvent.click(getByTestId("array-button")); | ||
|
||
expect(getByText(/1,2/i)).toBeInTheDocument(); | ||
|
||
unmount(); | ||
|
||
rerender(<MyComponent />); | ||
|
||
expect(getByText(/1,2/i)).toBeInTheDocument(); | ||
}); | ||
|
||
test("that it throws when no name is provided", () => { | ||
expect(() => useLocalStorageSetState("asdf")).toThrow(); | ||
}); | ||
|
||
test("that when passing a function, it utilizes it to set the value", () => { | ||
function MyComp(props) { | ||
const [index, setIndex] = useLocalStorageSetState((prev) => { | ||
if (prev >= props.arr.length || prev === undefined) { | ||
return 0; | ||
} else { | ||
return prev; | ||
} | ||
}, "index-local-storage"); | ||
return ( | ||
<div> | ||
<button data-testid="set-index-0" onClick={() => setIndex(0)} /> | ||
<div data-testid="index">{props.arr[index]}</div> | ||
<button data-testid="set-index-1" onClick={() => setIndex(1)} /> | ||
</div> | ||
); | ||
} | ||
|
||
MyComp.propTypes = { | ||
arr: PropTypes.array.isRequired, | ||
}; | ||
|
||
const { getByTestId, getByText, rerender, unmount } = render( | ||
<MyComp arr={["first", "second"]} />, | ||
); | ||
|
||
expect(getByText(/first/i)).toBeInTheDocument(); | ||
|
||
fireEvent.click(getByTestId("set-index-1")); | ||
|
||
expect(getByText(/second/i)).toBeInTheDocument(); | ||
|
||
unmount(); | ||
rerender(<MyComp arr={["first"]} />); | ||
|
||
expect(getByText(/first/i)).toBeInTheDocument(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from "react"; | ||
import store from "store"; | ||
function useLocalStorageSetState(initialValue, name) { | ||
if (!name) { | ||
throw new Error( | ||
"Name must be provided to persist properly to localStorage", | ||
); | ||
} | ||
let actualInitialValue = | ||
store.get(name) !== undefined ? store.get(name) : initialValue; | ||
if (typeof initialValue === "function") { | ||
actualInitialValue = initialValue(actualInitialValue); | ||
} | ||
const [value, setValue] = React.useState(actualInitialValue); | ||
|
||
const theirSetValue = (theirNewValue) => { | ||
let valueToSet; | ||
if (typeof theirNewValue === "function") { | ||
valueToSet = theirNewValue(value); | ||
setValue(valueToSet); | ||
} else { | ||
setValue(theirNewValue); | ||
valueToSet = theirNewValue; | ||
} | ||
store.set(name, valueToSet); | ||
}; | ||
return [value, theirSetValue]; | ||
} | ||
|
||
export default useLocalStorageSetState; |