-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes: add some additional logic checks and unit tests.
- Loading branch information
1 parent
54dac6e
commit 51f80fb
Showing
6 changed files
with
2,371 additions
and
52 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
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,7 @@ | ||
module.exports = { | ||
// ... | ||
transform: { | ||
'^.+\\.tsx?$': 'ts-jest', | ||
}, | ||
testMatch: ['**/?(*.)+(spec|test).ts'], | ||
}; |
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,90 @@ | ||
import { getFrameAccountAddress } from './getFrameAccountAddress'; | ||
|
||
jest.mock('@farcaster/hub-nodejs', () => { | ||
return { | ||
getSSLHubRpcClient: jest.fn().mockReturnValue({ | ||
validateMessage: jest.fn(), | ||
}), | ||
Message: { | ||
decode: jest.fn(), | ||
}, | ||
}; | ||
}); | ||
|
||
function buildFarcasterResponse(fid: number) { | ||
return { | ||
isOk: () => { | ||
return true; | ||
}, | ||
value: { | ||
valid: true, | ||
message: { | ||
data: { | ||
fid: fid, | ||
}, | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
function mockNeynarResponse(fid: number, addresses: string[]) { | ||
const neynarResponse = { | ||
users: [ | ||
{ | ||
verifications: addresses, | ||
}, | ||
], | ||
}; | ||
|
||
const getSSLHubRpcClientMock = require('@farcaster/hub-nodejs').getSSLHubRpcClient; | ||
const validateMock = getSSLHubRpcClientMock().validateMessage as jest.Mock; | ||
|
||
// Mock the response from the Farcaster hub | ||
validateMock.mockResolvedValue({ | ||
isOk: () => true, | ||
value: { valid: true, message: { fid } }, | ||
}); | ||
|
||
validateMock.mockResolvedValue(buildFarcasterResponse(fid)); | ||
// Mock the response from Neynar | ||
global.fetch = jest.fn(() => | ||
Promise.resolve({ | ||
json: () => Promise.resolve(neynarResponse), | ||
}), | ||
) as jest.Mock; | ||
return { | ||
validateMock, | ||
}; | ||
} | ||
|
||
describe('getFrameAccountAddress', () => { | ||
const fakeFrameData = { | ||
trustedData: {}, | ||
}; | ||
const fakeApiKey = { | ||
NEYNAR_API_KEY: '1234', | ||
}; | ||
|
||
it('should return the first verification for valid input', async () => { | ||
const fid = 1234; | ||
const addresses = ['0xaddr1']; | ||
mockNeynarResponse(fid, addresses); | ||
|
||
const response = await getFrameAccountAddress(fakeFrameData, fakeApiKey); | ||
expect(response).toEqual(addresses[0]); | ||
}); | ||
|
||
it('when the call from farcaster fails we should return undefined', async () => { | ||
const fid = 1234; | ||
const addresses = ['0xaddr1']; | ||
const { validateMock } = mockNeynarResponse(fid, addresses); | ||
validateMock.mockClear(); | ||
validateMock.mockResolvedValue({ | ||
isOk: () => { | ||
return false; | ||
}, | ||
}); | ||
const response = await getFrameAccountAddress(fakeFrameData, fakeApiKey); | ||
expect(response).toEqual(undefined); | ||
}); | ||
}); |
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
Oops, something went wrong.