Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
@trivago/preact-hooks-testing-library
Advanced tools
preact port of @testing-library/react-hooks
preact port of the the @testing-library/react-hooks library.
@testing-library/react-hooks
?Currently, due to the use of react-test-renderer
, the react hooks testing library most likely will never be compatible with preact.
At the time of writing, a library did not exist to test preact hooks.
Install with your favorite package manager
yarn add -D @trivago/preact-hooks-testing-library
OR
npm install --save-dev @trivago/preact-hooks-testing-library
useCounter.ts
import { useState, useCallback } from 'react'; // aliased to preact
const useCounter = () => {
const [count, setCount] = useState(0);
const increment = useCallback(() => setCount(c => c + 1));
return {
count,
increment
}
}
export default useCounter;
useCounter.test.ts
import { renderHook, act } from 'preact-hooks-testing-library';
import useCounter from './useCounter';
test('should increment counter', () => {
const { result } = renderHook(() => useCounter());
act(() => {
result.current.increment();
});
expect(result.current.count).toBe(1);
});
Sometimes, hooks may need access to values or functionality outside of itself that are provided by a context provider or some other HOC.
import { createContext } from 'preact'
import { useState, useCallback, useContext } from 'preact/hooks'
const CounterStepContext = createContext(1)
export const CounterStepProvider = ({ step, children }) => (
<CounterStepContext.Provider value={step}>{children}</CounterStepContext.Provider>
)
export function useCounter(initialValue = 0) {
const [count, setCount] = useState(initialValue)
const step = useContext(CounterStepContext)
const increment = useCallback(() => setCount((x) => x + step), [step])
const reset = useCallback(() => setCount(initialValue), [initialValue])
return { count, increment, reset }
}
In our test, we simply use CoounterStepProvider as the wrapper when rendering the hook:
import { renderHook, act } from 'preact-hooks-testing-library'
import { CounterStepProvider, useCounter } from './counter'
test('should use custom step when incrementing', () => {
const wrapper = ({ children }) => <CounterStepProvider step={2}>{children}</CounterStepProvider>
const { result } = renderHook(() => useCounter(), { wrapper })
act(() => {
result.current.increment()
})
expect(result.current.count).toBe(2)
})
@ts-nocheck
flag from testsFAQs
preact port of @testing-library/react-hooks
The npm package @trivago/preact-hooks-testing-library receives a total of 0 weekly downloads. As such, @trivago/preact-hooks-testing-library popularity was classified as not popular.
We found that @trivago/preact-hooks-testing-library demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.