This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17b3456
commit a097c61
Showing
1 changed file
with
80 additions
and
0 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,80 @@ | ||
import IpfsServerClient from '../e2e/utils/utils.ts/ipfsServerClient'; | ||
import version from '../src/_version'; | ||
|
||
// Mock Endpoint | ||
const _mock = { | ||
get: jest.fn(), | ||
delete: jest.fn(), | ||
post: jest.fn(), | ||
put: jest.fn(), | ||
}; | ||
jest.mock('axios', () => { | ||
return { | ||
create: (): unknown => _mock, | ||
}; | ||
}); | ||
|
||
const BASEURL = 'http://base.url.com'; | ||
const projectId = 'ProjectId'; | ||
const apiKeySecret = 'ApiKeySecret'; | ||
describe('IpfsServerClient', () => { | ||
it('should throw when args are missing (baseURL)', async () => { | ||
expect(() => new IpfsServerClient({ baseURL: '', projectId, apiKeySecret })).toThrow( | ||
`missing argument: No baseURL supplied (location=\"[IPFS.constructor]\", code=MISSING_ARGUMENT, version=${version})`, | ||
); | ||
}); | ||
|
||
it('should throw when args are missing (projectId)', async () => { | ||
expect(() => new IpfsServerClient({ baseURL: BASEURL, projectId: '', apiKeySecret })).toThrow( | ||
`missing argument: No project id supplied. (location=\"[IPFS.constructor]\", code=MISSING_ARGUMENT, version=${version})`, | ||
); | ||
}); | ||
|
||
it('should fail to add file', async () => { | ||
_mock.post.mockImplementationOnce(() => { | ||
throw Error('test'); | ||
}); | ||
|
||
const instance = new IpfsServerClient({ baseURL: BASEURL, projectId, apiKeySecret }); | ||
const res = async () => await instance.add('test'); | ||
expect(res).rejects.toThrow( | ||
`An error occured with infura ipfs api (location=\"[IPFS.uploadFile]\", error=\"Error: test\", argument=\"file\", value=\"test\", code=INVALID_ARGUMENT, version=${version})`, | ||
); | ||
}); | ||
|
||
it('should succeed to add file', async () => { | ||
_mock.post.mockImplementationOnce(() => ({ | ||
data: { | ||
Hash: 'Hash', | ||
}, | ||
})); | ||
|
||
const instance = new IpfsServerClient({ baseURL: BASEURL, projectId, apiKeySecret }); | ||
const res = await instance.add('test'); | ||
expect(_mock.post).toHaveBeenCalled(); | ||
expect(res).toBe('Hash'); | ||
}); | ||
|
||
it('should fail to add all files', async () => { | ||
_mock.post.mockImplementationOnce(() => { | ||
throw Error('test'); | ||
}); | ||
|
||
const instance = new IpfsServerClient({ baseURL: BASEURL, projectId, apiKeySecret }); | ||
const res = async () => await instance.addAll([{ path: 'test', content: 'test' }]); | ||
expect(res).rejects.toThrow( | ||
`An error occured with infura ipfs api (location=\"[IPFS.uploadFile]\", error=\"Error: test\", argument=\"file\", value=[{\"path\":\"test\",\"content\":\"test\"}], code=INVALID_ARGUMENT, version=${version})`, | ||
); | ||
}); | ||
|
||
it('should succeed to add all files', async () => { | ||
_mock.post.mockImplementationOnce(() => ({ | ||
data: `{"Hash": "Hash"}`, | ||
})); | ||
|
||
const instance = new IpfsServerClient({ baseURL: BASEURL, projectId, apiKeySecret }); | ||
const res = await instance.addAll([{ path: 'test', content: 'test' }]); | ||
expect(_mock.post).toHaveBeenCalled(); | ||
expect(res).toBe('Hash'); | ||
}); | ||
}); |