forked from tilt-dev/tilt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BrowerStorage.test.tsx
60 lines (53 loc) · 1.36 KB
/
BrowerStorage.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { render, screen } from "@testing-library/react"
import React from "react"
import {
makeKey,
tiltfileKeyContext,
usePersistentState,
} from "./BrowserStorage"
describe("localStorageContext", () => {
afterEach(() => {
localStorage.clear()
})
it("stores data to local storage", () => {
function ValueSetter() {
const [value, setValue] = usePersistentState<string>(
"test-key",
"initial"
)
if (value !== "test-write-value") {
setValue("test-write-value")
}
return null
}
render(
<tiltfileKeyContext.Provider value={"test"}>
<ValueSetter />
</tiltfileKeyContext.Provider>
)
expect(localStorage.getItem(makeKey("test", "test-key"))).toEqual(
JSON.stringify("test-write-value")
)
})
it("reads data from local storage", () => {
localStorage.setItem(
makeKey("test", "test-key"),
JSON.stringify("test-read-value")
)
function ValueGetter() {
const [value, _setValue] = usePersistentState<string>(
"test-key",
"initial"
)
return <p aria-label="saved value">{value}</p>
}
render(
<tiltfileKeyContext.Provider value="test">
<ValueGetter />
</tiltfileKeyContext.Provider>
)
expect(screen.getByLabelText("saved value")).toHaveTextContent(
"test-read-value"
)
})
})