@yoroi/api
Version:
The API package of Yoroi SDK
72 lines (70 loc) • 3.03 kB
JavaScript
;
var _bestBlock = require("./best-block");
var _bestBlock2 = require("./best-block.mocks");
describe('getBestBlock', () => {
const baseUrl = 'https://localhost';
const mockFetch = jest.fn();
const customFetcher = jest.fn().mockResolvedValue(_bestBlock2.bestBlockMockResponse);
it('returns parsed data when response is valid', async () => {
mockFetch.mockResolvedValue(_bestBlock2.bestBlockMockResponse);
const tipStatus = (0, _bestBlock.getBestBlock)(baseUrl, mockFetch);
const result = await tipStatus();
expect(result).toEqual(_bestBlock2.bestBlockMockResponse);
});
it('throws an error if response is invalid', async () => {
mockFetch.mockResolvedValue(null);
const tipStatus = (0, _bestBlock.getBestBlock)(baseUrl, mockFetch);
await expect(tipStatus()).rejects.toThrow('Invalid best block response');
});
it('rejects when response data fails validation', async () => {
const invalidResponse = {
unexpectedField: 'invalid data'
};
mockFetch.mockResolvedValue(invalidResponse);
const tipStatus = (0, _bestBlock.getBestBlock)(baseUrl, mockFetch);
await expect(tipStatus()).rejects.toThrow('Invalid best block response');
});
it('uses a custom fetcher function', async () => {
const tipStatus = (0, _bestBlock.getBestBlock)(baseUrl, customFetcher);
const result = await tipStatus();
expect(customFetcher).toHaveBeenCalled();
expect(result).toEqual(_bestBlock2.bestBlockMockResponse);
// coverage
const tipStatus2 = (0, _bestBlock.getBestBlock)(baseUrl);
expect(tipStatus2).toBeDefined();
});
it('uses fetcher and returns data on successful fetch', async () => {
const fetcherMock = jest.fn().mockResolvedValue(_bestBlock2.bestBlockMockResponse);
const tipStatus = (0, _bestBlock.getBestBlock)(baseUrl, fetcherMock);
const result = await tipStatus();
expect(fetcherMock).toHaveBeenCalled();
expect(result).toEqual(_bestBlock2.bestBlockMockResponse);
});
it('throws an error on network issues', async () => {
const networkError = new Error('Network Error');
mockFetch.mockRejectedValue(networkError);
const tipStatus = (0, _bestBlock.getBestBlock)(baseUrl, mockFetch);
await expect(tipStatus()).rejects.toThrow(networkError.message);
});
});
describe('isBestBlock', () => {
it('returns true for a valid best block response', () => {
expect((0, _bestBlock.isBestBlock)(_bestBlock2.bestBlockMockResponse)).toBe(true);
});
it('returns false for an invalid best block response', () => {
const invalidResponse = {
..._bestBlock2.bestBlockMockResponse,
epoch: 'invalid'
};
expect((0, _bestBlock.isBestBlock)(invalidResponse)).toBe(false);
});
it('returns false for an incomplete best block response', () => {
const incompleteResponse = {
bestBlock: {
epoch: 1
}
}; // Missing fields
expect((0, _bestBlock.isBestBlock)(incompleteResponse)).toBe(false);
});
});
//# sourceMappingURL=best-block.test.js.map