Skip to content

Commit

Permalink
feat: update tsdoc comments and file with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ndt080 committed Aug 21, 2024
1 parent a4aa6a2 commit e63e4ae
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 8 deletions.
54 changes: 54 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,40 @@ export type StoreKeys<T> = T extends {
: never

export interface UseStoreOptions<SomeStore, Value> {
/**
* Will re-render components only on specific key changes.
*/
keys?: StoreKeys<SomeStore>[]
/**
* Allows you to extract value from complex/deep objects
*/
selector?: (state: StoreValue<SomeStore>) => Value
}

/**
* Subscribe to store changes and get store’s value.
*
* Can be user with store builder too.
*
* ```js
* import { useStore } from 'nanostores/preact'
*
* import { router } from '../store/router'
*
* export const Layout = () => {
* let page = useStore(router)
* if (page.value.router === 'home') {
* return <HomePage />
* } else {
* return <Error404 />
* }
* }
* ```
*
* @param store Store instance.
* @param options
* @returns Store value.
*/
export function useStore<
SomeStore extends Store,
Value = StoreValue<SomeStore>
Expand All @@ -20,6 +50,30 @@ export function useStore<
options: UseStoreOptions<SomeStore, Value> = {}
): ReadonlySignal<Value>

/**
* Subscribe to store changes and get store’s value.
*
* Can be user with store builder too.
*
* ```js
* import { useLegacyStore } from 'nanostores/preact'
*
* import { router } from '../store/router'
*
* export const Layout = () => {
* let page = useLegacyStore(router)
* if (page.router === 'home') {
* return <HomePage />
* } else {
* return <Error404 />
* }
* }
* ```
*
* @param store Store instance.
* @param options
* @returns Store value.
*/
export function useLegacyStore<
SomeStore extends Store,
Value = StoreValue<SomeStore>
Expand Down
16 changes: 8 additions & 8 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { FunctionalComponent as FC } from 'preact'
import { h } from 'preact'
import { useState } from 'preact/hooks'

import { useStore } from '../index.js'
import { useLegacyStore } from '../index.js'

afterEach(() => {
window.document.head.innerHTML = ''
Expand All @@ -32,13 +32,13 @@ test('renders simple store', async () => {

let Test1: FC = () => {
renders += 1
let value = useStore(letter)
let value = useLegacyStore(letter)
// @ts-expect-error: Preact type issue with data-*
return h('div', { 'data-testid': 'test1' }, value)
}

let Test2: FC = () => {
let value = useStore(letter)
let value = useLegacyStore(letter)
// @ts-expect-error: Preact type issue with data-*
return h('div', { 'data-testid': 'test2' }, value)
}
Expand Down Expand Up @@ -96,13 +96,13 @@ test('does not reload store on component changes', async () => {
})

let TestA: FC = () => {
let simpleValue = useStore(simple)
let simpleValue = useLegacyStore(simple)
// @ts-expect-error: Preact type issue with data-*
return h('div', { 'data-testid': 'test' }, `1 ${simpleValue}`)
}

let TestB: FC = () => {
let simpleValue = useStore(simple)
let simpleValue = useLegacyStore(simple)
// @ts-expect-error: Preact type issue with data-*
return h('div', { 'data-testid': 'test' }, `2 ${simpleValue}`)
}
Expand Down Expand Up @@ -166,7 +166,7 @@ test('has keys option', async () => {
let MapTest: FC = () => {
renderCount++
let [keys, setKeys] = useState<(keyof MapStore)[]>(['a'])
let { a, b } = useStore(mapSore, { keys })
let { a, b } = useLegacyStore(mapSore, { keys })
return h(
// @ts-expect-error: Preact type issue with data-*
'div',
Expand Down Expand Up @@ -231,7 +231,7 @@ test('return correct value for Atom, if store was changed between rendering and
}

let Wrapper: FC = () => {
let value = useStore(store)
let value = useLegacyStore(store)
return h('p', {}, renderWithMutate(value))
}

Expand All @@ -250,7 +250,7 @@ test('return correct value for MapStore, if store was changed between rendering
}

let Wrapper: FC = () => {
let value = useStore(store).value
let value = useLegacyStore(store).value
return h('p', {}, renderWithMutate(value))
}

Expand Down

0 comments on commit e63e4ae

Please sign in to comment.