Skip to content

Commit

Permalink
feat: implement isLoading state (#56)
Browse files Browse the repository at this point in the history
Co-authored-by: L <6723574+louisgv@users.noreply.github.com>
  • Loading branch information
nvitaterna and louisgv committed Jun 18, 2024
1 parent 45dbeb2 commit e22a8c6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export function useStorage<T = any>(rawKey: RawKey, onInit?: Setter<T>) {

const key = isObjectKey ? rawKey.key : rawKey

const [isLoading, setIsLoading] = useState(true);

// Render state
const [renderValue, setRenderValue] = useState(onInit)
const [isLoading, setIsLoading] = useState(true)
Expand Down Expand Up @@ -137,7 +139,8 @@ export function useStorage<T = any>(rawKey: RawKey, onInit?: Setter<T>) {
{
setRenderValue,
setStoreValue,
remove
remove,
isLoading
}
] as const
}
30 changes: 30 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,36 @@ describe("react hook", () => {

unmount()
})

test('isLoading is true until value is fetched', async () => {
const { getTriggers } = createStorageMock()

const key = 'key'
const value = 'hello'

const { result, unmount } = renderHook(() => useStorage(key))

expect(result.current[2].isLoading).toBe(true)

unmount()
})

test('isLoading is false after value is fetched', async () => {
const { getTriggers } = createStorageMock()

const key = 'key'
const value = 'hello'

const { result, unmount } = renderHook(() => useStorage(key))

await act(async () => {
await result.current[1](value)
})

expect(result.current[2].isLoading).toBe(false)

unmount()
})
})

describe("watch/unwatch", () => {
Expand Down

0 comments on commit e22a8c6

Please sign in to comment.