-
Notifications
You must be signed in to change notification settings - Fork 61
feat(react): add usePowerSyncStatus hook #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
14e66dd
feat(react): add usePowerSyncStatus hook
6239e5e
chore: merge main:
c00530f
chore: update demos
0f0adf5
docs: update with new hook and add example
ba94361
chore: update rootdir
aaca353
chore: add changeset
ecc5dcb
chore: add changeset
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| "@journeyapps/powersync-react": minor | ||
| --- | ||
|
|
||
| Add `usePowerSyncStatus` hook |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,31 @@ | ||
| import { useContext, useEffect, useState } from 'react'; | ||
| import { PowerSyncContext } from './PowerSyncContext'; | ||
|
|
||
| /** | ||
| * Custom hook that provides access to the current status of PowerSync. | ||
| * @returns The PowerSync Database status. | ||
| * @example | ||
| * const Component = () => { | ||
| * const status = usePowerSyncStatus(); | ||
| * | ||
| * return <div> | ||
| * status.connected ? 'wifi' : 'wifi-off' | ||
| * </div> | ||
| * }; | ||
| */ | ||
| export const usePowerSyncStatus = () => { | ||
| const powerSync = useContext(PowerSyncContext); | ||
| const [syncStatus, setSyncStatus] = useState(powerSync.currentStatus); | ||
|
|
||
| useEffect(() => { | ||
| const listener = powerSync.registerListener({ | ||
| statusChanged: (status) => { | ||
| setSyncStatus(status); | ||
| } | ||
| }); | ||
|
|
||
| return () => listener?.(); | ||
| }, [powerSync]); | ||
|
|
||
| return syncStatus; | ||
| }; |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export * from './hooks/PowerSyncContext'; | ||
| export * from './hooks/usePowerSyncQuery'; | ||
| export * from './hooks/usePowerSyncWatchedQuery'; | ||
| export { usePowerSyncQuery } from './hooks/usePowerSyncQuery'; | ||
| export { usePowerSyncWatchedQuery } from './hooks/usePowerSyncWatchedQuery'; | ||
| export { usePowerSyncStatus } from './hooks/usePowerSyncStatus'; |
This file contains hidden or 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,24 @@ | ||
| import React from 'react'; | ||
| import { render } from '@testing-library/react'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import * as SUT from '../src/hooks/PowerSyncContext'; | ||
|
|
||
| describe('PowerSyncContext', () => { | ||
| describe('usePowerSync', () => { | ||
| it('should retrieve the PowerSync DB from the context and display the test text', async () => { | ||
| const TestComponent = () => { | ||
| const powerSyncDb = SUT.usePowerSync() as any; | ||
| return <div>{powerSyncDb.testText}</div>; | ||
| }; | ||
|
|
||
| const { findByText } = render( | ||
| <SUT.PowerSyncContext.Provider value={{ testText: 'Test Text' } as any}> | ||
stevensJourney marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <TestComponent /> | ||
| </SUT.PowerSyncContext.Provider> | ||
| ); | ||
| const hello = await findByText('Test Text'); | ||
|
|
||
| expect(hello).toBeDefined; | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or 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,22 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "baseUrl": "./", | ||
| "esModuleInterop": true, | ||
| "jsx": "react", | ||
| "rootDir": "../", | ||
| "composite": true, | ||
| "outDir": "./lib", | ||
| "lib": ["esnext", "DOM"], | ||
| "module": "esnext", | ||
| "sourceMap": true, | ||
| "moduleResolution": "node", | ||
| "noFallthroughCasesInSwitch": true, | ||
| "noImplicitReturns": true, | ||
| "noImplicitUseStrict": false, | ||
| "noStrictGenericChecks": false, | ||
| "resolveJsonModule": true, | ||
| "skipLibCheck": true, | ||
| "target": "esnext" | ||
| }, | ||
| "include": ["../src/**/*"] | ||
| } |
59 changes: 59 additions & 0 deletions
59
packages/powersync-react/tests/usePowerSyncStatus.test.tsx
This file contains hidden or 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,59 @@ | ||
| import React from 'react'; | ||
| import { act, renderHook } from '@testing-library/react-hooks'; | ||
| import { vi, describe, expect, it, afterEach } from 'vitest'; | ||
| import { usePowerSyncStatus } from '../src/hooks/usePowerSyncStatus'; | ||
| import { PowerSyncContext } from '../src/hooks/PowerSyncContext'; | ||
|
|
||
| const mockPowerSync = { | ||
| currentStatus: { status: 'initial' }, | ||
| registerListener: vi.fn(() => ({ | ||
| statusChanged: vi.fn(() => 'updated') | ||
| })) | ||
| }; | ||
|
|
||
| vi.mock('./PowerSyncContext', () => ({ | ||
| useContext: vi.fn(() => mockPowerSync) | ||
| })); | ||
|
|
||
| describe('usePowerSyncStatus', () => { | ||
| afterEach(() => { | ||
| vi.resetAllMocks(); | ||
| }); | ||
|
|
||
| it('should initialize with the current status', () => { | ||
| const wrapper = ({ children }) => ( | ||
| <PowerSyncContext.Provider value={mockPowerSync as any}>{children}</PowerSyncContext.Provider> | ||
| ); | ||
|
|
||
| const { result } = renderHook(() => usePowerSyncStatus(), { wrapper }); | ||
| expect(result.current).toEqual(mockPowerSync.currentStatus); | ||
| }); | ||
|
|
||
| // TODO: Get this test to work | ||
| it.skip('should update the status when the listener is called', () => { | ||
| const wrapper = ({ children }) => ( | ||
| <PowerSyncContext.Provider value={mockPowerSync as any}>{children}</PowerSyncContext.Provider> | ||
| ); | ||
|
|
||
| const { result } = renderHook(() => usePowerSyncStatus(), { wrapper }); | ||
|
|
||
| act(() => { | ||
| mockPowerSync.registerListener.mockResolvedValue({ statusChanged: vi.fn(() => 'updated') }); | ||
| }); | ||
|
|
||
| expect(result.current).toEqual({ status: 'updated' }); | ||
| }); | ||
|
|
||
| it('should run the listener on unmount', () => { | ||
| const wrapper = ({ children }) => ( | ||
| <PowerSyncContext.Provider value={mockPowerSync as any}>{children}</PowerSyncContext.Provider> | ||
| ); | ||
|
|
||
| const { unmount } = renderHook(() => usePowerSyncStatus(), { wrapper }); | ||
| const listenerUnsubscribe = mockPowerSync.registerListener; | ||
|
|
||
| unmount(); | ||
|
|
||
| expect(listenerUnsubscribe).toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -22,5 +22,6 @@ | |
| { | ||
| "path": "../powersync-sdk-common" | ||
| } | ||
| ] | ||
| ], | ||
| "include": ["src/**/*"] | ||
| } | ||
This file contains hidden or 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,9 @@ | ||
| import { defineConfig, UserConfigExport } from 'vitest/config'; | ||
|
|
||
| const config: UserConfigExport = { | ||
| test: { | ||
| environment: 'jsdom' | ||
| } | ||
| }; | ||
|
|
||
| export default defineConfig(config); |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.