-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #259 from docknetwork/fix/update-credential-list-o…
…n-network-update fix: update credential list when network is switched
- Loading branch information
Showing
5 changed files
with
248 additions
and
80 deletions.
There are no files selected for viewing
This file contains 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,146 @@ | ||
import { useDocument, useDocuments } from './documentsHooks'; | ||
import { WalletEvents } from '@docknetwork/wallet-sdk-wasm/src/modules/wallet'; | ||
import { getWallet } from './wallet'; | ||
import { act, renderHook } from '@testing-library/react-hooks'; | ||
|
||
const mockDocument = { id: 'mock-document-id' }; | ||
|
||
const mockWallet = { | ||
getDocumentById: jest.fn(() => Promise.resolve(mockDocument)), | ||
getDocumentsByType: jest.fn(() => Promise.resolve([])), | ||
eventManager: { | ||
on: jest.fn(), | ||
removeListener: jest.fn(), | ||
}, | ||
}; | ||
|
||
jest.mock('./wallet', () => ({ | ||
getWallet: jest.fn(() => mockWallet), | ||
})); | ||
|
||
describe('useDocument', () => { | ||
|
||
beforeEach(() => { | ||
getWallet.mockReturnValue(mockWallet); | ||
mockWallet.getDocumentById.mockReset(); | ||
mockWallet.eventManager.on.mockReset(); | ||
mockWallet.eventManager.removeListener.mockReset(); | ||
}); | ||
|
||
it('should fetch the document by documentId', async () => { | ||
const documentId = 'document-id'; | ||
const document = { id: documentId, content: 'content' }; | ||
mockWallet.getDocumentById.mockResolvedValue(document); | ||
|
||
const { result, waitFor } = renderHook(() => | ||
useDocument(documentId), | ||
); | ||
|
||
await waitFor(() => expect(getWallet).toHaveBeenCalled()) | ||
expect(mockWallet.getDocumentById).toHaveBeenCalledWith(documentId); | ||
expect(result.current).toEqual(document); | ||
}); | ||
|
||
it('should fetch the document when documentUpdated event is emitted', async () => { | ||
const documentId = 'document-id'; | ||
const initialDocument = { id: documentId, content: 'content' }; | ||
const updatedDocument = { id: documentId, content: 'updated content' }; | ||
mockWallet.getDocumentById | ||
.mockResolvedValueOnce(initialDocument) | ||
.mockResolvedValueOnce(updatedDocument); | ||
|
||
const { result, waitFor } = renderHook(() => | ||
useDocument(documentId), | ||
); | ||
|
||
act(() => { | ||
mockWallet.eventManager.on.mock.calls[0][1](updatedDocument); | ||
}); | ||
|
||
await waitFor(() => expect(mockWallet.getDocumentById).toHaveBeenCalledTimes(2)); | ||
expect(result.current).toEqual(updatedDocument); | ||
}); | ||
|
||
it('should fetch the document when documentAdded event is emitted', async () => { | ||
const documentId = 'document-id'; | ||
const initialDocument = null; | ||
const newDocument = { id: documentId, content: 'added content' }; | ||
mockWallet.getDocumentById | ||
.mockResolvedValueOnce(initialDocument) | ||
.mockResolvedValueOnce(newDocument); | ||
|
||
const { result, waitFor } = renderHook(() => | ||
useDocument(documentId), | ||
); | ||
|
||
act(() => { | ||
mockWallet.eventManager.on.mock.calls[0][1](newDocument); | ||
}); | ||
|
||
await waitFor(() => expect(mockWallet.getDocumentById).toHaveBeenCalledTimes(2)); | ||
expect(result.current).toEqual(newDocument); | ||
}); | ||
|
||
it('should fetch the document when documentRemoved event is emitted', async () => { | ||
const documentId = 'document-id'; | ||
const initialDocument = { id: documentId, content: 'content' }; | ||
const newDocument = { id: documentId, content: 'updated content' }; | ||
mockWallet.getDocumentById | ||
.mockResolvedValueOnce(initialDocument) | ||
.mockResolvedValueOnce(newDocument); | ||
|
||
const { result, waitFor } = renderHook(() => | ||
useDocument(documentId), | ||
); | ||
|
||
act(() => { | ||
mockWallet.eventManager.on.mock.calls[0][1](newDocument); | ||
}); | ||
|
||
await waitFor(() => expect(mockWallet.getDocumentById).toHaveBeenCalledTimes(2)); | ||
expect(result.current).toEqual(newDocument); | ||
}); | ||
|
||
}); | ||
|
||
describe('useDocuments', () => { | ||
beforeEach(() => { | ||
getWallet.mockReturnValue(mockWallet); | ||
mockWallet.getDocumentById.mockReset(); | ||
mockWallet.eventManager.on.mockReset(); | ||
mockWallet.eventManager.removeListener.mockReset(); | ||
}); | ||
|
||
it('should fetch the document correctly', async () => { | ||
const type = 'type1'; | ||
const documents = [ | ||
{ id: 'doc1', type }, | ||
{ id: 'doc2', type }, | ||
]; | ||
mockWallet.getDocumentsByType.mockResolvedValue(documents); | ||
|
||
const { result, waitFor } = renderHook(() => useDocuments({ type })); | ||
|
||
await waitFor(() => expect(mockWallet.getDocumentsByType).toHaveBeenCalledWith(type)); | ||
expect(result.current.documents).toEqual(documents); | ||
expect(result.current.loading).toEqual(false); | ||
}); | ||
it('should refetch documents on networkUpdated event', async () => { | ||
const { waitFor } = renderHook(() => | ||
useDocuments({ type: 'mockType' }), | ||
); | ||
await mockWallet.eventManager.on.mock.calls[2][1](); | ||
|
||
expect(mockWallet.getDocumentsByType).toHaveBeenCalledWith('mockType'); | ||
}); | ||
|
||
it('should force refetch documents on networkUpdated event if type is not set', async () => { | ||
const { waitFor } = renderHook(() => | ||
useDocuments(), | ||
); | ||
mockWallet.getDocumentsByType.mockReset(); | ||
await mockWallet.eventManager.on.mock.calls[3][1](); | ||
|
||
expect(mockWallet.getDocumentsByType).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
This file contains 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,77 @@ | ||
import {WalletEvents} from '@docknetwork/wallet-sdk-wasm/src/modules/wallet'; | ||
import {useCallback, useEffect, useState} from 'react'; | ||
import {getWallet} from './wallet'; | ||
|
||
const useEventListener = (eventManager, eventNames, listener) => { | ||
useEffect(() => { | ||
eventNames.forEach(eventName => eventManager.on(eventName, listener)); | ||
return () => | ||
eventNames.forEach(eventName => | ||
eventManager.removeListener(eventName, listener), | ||
); | ||
}, [eventManager, eventNames, listener]); | ||
}; | ||
|
||
const events = [ | ||
WalletEvents.documentAdded, | ||
WalletEvents.documentRemoved, | ||
WalletEvents.documentUpdated, | ||
]; | ||
|
||
export function useDocument(id) { | ||
const [document, setDocument] = useState(null); | ||
|
||
const refetchDocument = useCallback( | ||
async updatedDoc => { | ||
if (updatedDoc.id !== id) return; | ||
const doc = await getWallet().getDocumentById(id); | ||
setDocument(doc); | ||
}, | ||
[id], | ||
); | ||
|
||
useEffect(() => { | ||
getWallet().getDocumentById(id).then(setDocument); | ||
}, [id]); | ||
|
||
useEventListener(getWallet().eventManager, events, refetchDocument); | ||
|
||
return document; | ||
} | ||
|
||
export function useDocuments({type = null} = {}) { | ||
const [documents, setDocuments] = useState([]); | ||
const [loading, setLoading] = useState(true); | ||
|
||
const fetchDocuments = useCallback( | ||
async (updatedDoc, forceFetch = false) => { | ||
console.log('fetching documents', updatedDoc, forceFetch); | ||
if ( | ||
forceFetch || | ||
updatedDoc?.type === type || | ||
updatedDoc?.type?.includes(type) | ||
) { | ||
const docs = await getWallet().getDocumentsByType(type); | ||
setDocuments(docs); | ||
setLoading(false); | ||
} | ||
}, | ||
[type], | ||
); | ||
|
||
useEffect(() => { | ||
fetchDocuments(null, true); | ||
}, [fetchDocuments, setLoading]); | ||
|
||
useEventListener(getWallet().eventManager, events, fetchDocuments); | ||
useEventListener( | ||
getWallet().eventManager, | ||
[WalletEvents.networkUpdated], | ||
async () => fetchDocuments(null, true), | ||
); | ||
|
||
return { | ||
documents, | ||
loading, | ||
}; | ||
} |
This file contains 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 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 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