-
Notifications
You must be signed in to change notification settings - Fork 26
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
feat(frontend): add sol to idb.api #3995
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cea4508
feat(frontend): add sol type to idb.ts
loki344 cf5c907
feat(frontend): add sol to idb.api, add tests for all networks in idb…
loki344 8bfc6c2
feat(frontend): fix linting
loki344 b042a3e
feat(frontend): add usage in auth.services.ts
loki344 bde0d46
🤖 Apply formatting changes
github-actions[bot] 254f67d
Merge branch 'main' into feat(frontend)/sol-addresses
loki344 54d0cac
feat(frontend): MR feedback, reuse mock principal
loki344 e96675e
Merge remote-tracking branch 'origin/feat(frontend)/sol-addresses' in…
loki344 4ff15e2
🤖 Apply formatting changes
github-actions[bot] 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 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
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,196 @@ | ||
import { | ||
deleteIdbBtcAddressMainnet, | ||
deleteIdbEthAddress, | ||
deleteIdbSolAddressMainnet, | ||
getIdbBtcAddressMainnet, | ||
getIdbEthAddress, | ||
getIdbSolAddressMainnet, | ||
setIdbBtcAddressMainnet, | ||
setIdbEthAddress, | ||
setIdbSolAddressMainnet, | ||
updateIdbBtcAddressMainnetLastUsage, | ||
updateIdbEthAddressLastUsage, | ||
updateIdbSolAddressMainnetLastUsage | ||
} from '$lib/api/idb.api'; | ||
import { Principal } from '@dfinity/principal'; | ||
import * as idbKeyval from 'idb-keyval'; | ||
import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
||
vi.mock('idb-keyval', () => ({ | ||
createStore: vi.fn(() => ({ | ||
/* mock store implementation */ | ||
})), | ||
set: vi.fn(), | ||
get: vi.fn(), | ||
del: vi.fn(), | ||
update: vi.fn() | ||
})); | ||
|
||
vi.mock('$app/environment', () => ({ | ||
browser: true | ||
})); | ||
|
||
describe('idb.api', () => { | ||
const mockPrincipal = Principal.fromText('2vxsx-fae'); | ||
const mockAddress = { | ||
address: '0x123', | ||
lastUsedTimestamp: Date.now(), | ||
createdAtTimestamp: Date.now() | ||
}; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe('BTC operations', () => { | ||
it('should set BTC address', async () => { | ||
await setIdbBtcAddressMainnet({ | ||
principal: mockPrincipal, | ||
address: mockAddress | ||
}); | ||
|
||
expect(idbKeyval.set).toHaveBeenCalledWith( | ||
mockPrincipal.toText(), | ||
mockAddress, | ||
expect.any(Object) | ||
); | ||
}); | ||
|
||
it('should get BTC address', async () => { | ||
vi.mocked(idbKeyval.get).mockResolvedValue(mockAddress); | ||
|
||
const result = await getIdbBtcAddressMainnet(mockPrincipal); | ||
|
||
expect(result).toEqual(mockAddress); | ||
expect(idbKeyval.get).toHaveBeenCalledWith(mockPrincipal.toText(), expect.any(Object)); | ||
}); | ||
|
||
it('should delete BTC address', async () => { | ||
await deleteIdbBtcAddressMainnet(mockPrincipal); | ||
|
||
expect(idbKeyval.del).toHaveBeenCalledWith(mockPrincipal.toText(), expect.any(Object)); | ||
}); | ||
|
||
it('should update BTC address last usage', async () => { | ||
// eslint-disable-next-line local-rules/prefer-object-params | ||
vi.mocked(idbKeyval.update).mockImplementation((_, updater) => { | ||
const updated = updater(mockAddress) as typeof mockAddress; | ||
expect(updated.lastUsedTimestamp).toBeGreaterThan(mockAddress.lastUsedTimestamp); | ||
return Promise.resolve(); | ||
}); | ||
|
||
await updateIdbBtcAddressMainnetLastUsage(mockPrincipal); | ||
|
||
expect(idbKeyval.update).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('ETH operations', () => { | ||
it('should set ETH address', async () => { | ||
await setIdbEthAddress({ | ||
principal: mockPrincipal, | ||
address: mockAddress | ||
}); | ||
|
||
expect(idbKeyval.set).toHaveBeenCalledWith( | ||
mockPrincipal.toText(), | ||
mockAddress, | ||
expect.any(Object) | ||
); | ||
}); | ||
|
||
it('should get ETH address', async () => { | ||
vi.mocked(idbKeyval.get).mockResolvedValue(mockAddress); | ||
|
||
const result = await getIdbEthAddress(mockPrincipal); | ||
|
||
expect(result).toEqual(mockAddress); | ||
expect(idbKeyval.get).toHaveBeenCalledWith(mockPrincipal.toText(), expect.any(Object)); | ||
}); | ||
|
||
it('should delete ETH address', async () => { | ||
await deleteIdbEthAddress(mockPrincipal); | ||
|
||
expect(idbKeyval.del).toHaveBeenCalledWith(mockPrincipal.toText(), expect.any(Object)); | ||
}); | ||
|
||
it('should update ETH address last usage', async () => { | ||
// eslint-disable-next-line local-rules/prefer-object-params | ||
vi.mocked(idbKeyval.update).mockImplementation((_, updater) => { | ||
const updated = updater(mockAddress) as typeof mockAddress; | ||
expect(updated.lastUsedTimestamp).toBeGreaterThan(mockAddress.lastUsedTimestamp); | ||
return Promise.resolve(); | ||
}); | ||
|
||
await updateIdbEthAddressLastUsage(mockPrincipal); | ||
|
||
expect(idbKeyval.update).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('SOL operations', () => { | ||
it('should set SOL address', async () => { | ||
await setIdbSolAddressMainnet({ | ||
principal: mockPrincipal, | ||
address: mockAddress | ||
}); | ||
|
||
expect(idbKeyval.set).toHaveBeenCalledWith( | ||
mockPrincipal.toText(), | ||
mockAddress, | ||
expect.any(Object) | ||
); | ||
}); | ||
|
||
it('should get SOL address', async () => { | ||
vi.mocked(idbKeyval.get).mockResolvedValue(mockAddress); | ||
|
||
const result = await getIdbSolAddressMainnet(mockPrincipal); | ||
|
||
expect(result).toEqual(mockAddress); | ||
expect(idbKeyval.get).toHaveBeenCalledWith(mockPrincipal.toText(), expect.any(Object)); | ||
}); | ||
|
||
it('should delete SOL address', async () => { | ||
await deleteIdbSolAddressMainnet(mockPrincipal); | ||
|
||
expect(idbKeyval.del).toHaveBeenCalledWith(mockPrincipal.toText(), expect.any(Object)); | ||
}); | ||
|
||
it('should update SOL address last usage', async () => { | ||
// eslint-disable-next-line local-rules/prefer-object-params | ||
vi.mocked(idbKeyval.update).mockImplementation((_, updater) => { | ||
const updated = updater(mockAddress) as typeof mockAddress; | ||
expect(updated.lastUsedTimestamp).toBeGreaterThan(mockAddress.lastUsedTimestamp); | ||
return Promise.resolve(); | ||
}); | ||
|
||
await updateIdbSolAddressMainnetLastUsage(mockPrincipal); | ||
|
||
expect(idbKeyval.update).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('Edge cases', () => { | ||
it('should handle undefined address when updating last usage', async () => { | ||
// eslint-disable-next-line local-rules/prefer-object-params | ||
vi.mocked(idbKeyval.update).mockImplementation((_, updater) => { | ||
const result = updater(undefined); | ||
expect(result).toBeUndefined(); | ||
return Promise.resolve(); | ||
}); | ||
|
||
await updateIdbBtcAddressMainnetLastUsage(mockPrincipal); | ||
|
||
expect(idbKeyval.update).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should return undefined when getting non-existent address', async () => { | ||
vi.mocked(idbKeyval.get).mockResolvedValue(undefined); | ||
|
||
const result = await getIdbBtcAddressMainnet(mockPrincipal); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
}); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: there is already a reusable
mockPrincipal
defined insrc/frontend/src/tests/mocks/identity.mock.ts