mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
chore: Move FetchUtil to util folder.
This commit is contained in:
38
test/unit/util/FetchUtil.test.ts
Normal file
38
test/unit/util/FetchUtil.test.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import fetch from '@rdfjs/fetch';
|
||||
import type { DatasetResponse } from '@rdfjs/fetch-lite';
|
||||
import type { Dataset } from 'rdf-js';
|
||||
import { fetchDataset } from '../../../src/util/FetchUtil';
|
||||
|
||||
jest.mock('@rdfjs/fetch');
|
||||
|
||||
describe('FetchUtil', (): void => {
|
||||
describe('#fetchDataset', (): void => {
|
||||
const fetchMock: jest.Mock = fetch as any;
|
||||
const url = 'http://test.com/foo';
|
||||
let datasetResponse: DatasetResponse<Dataset>;
|
||||
const dataset: Dataset = {} as any;
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
datasetResponse = {
|
||||
dataset: jest.fn().mockReturnValue(dataset),
|
||||
} as any;
|
||||
|
||||
fetchMock.mockResolvedValue(datasetResponse);
|
||||
});
|
||||
|
||||
it('errors if there was an issue fetching.', async(): Promise<void> => {
|
||||
fetchMock.mockRejectedValueOnce(new Error('Invalid webId!'));
|
||||
await expect(fetchDataset(url)).rejects.toThrow(`Cannot fetch ${url}`);
|
||||
expect(fetchMock).toHaveBeenCalledWith(url);
|
||||
});
|
||||
|
||||
it('errors if there was an issue parsing the returned RDF.', async(): Promise<void> => {
|
||||
(datasetResponse.dataset as jest.Mock).mockRejectedValueOnce(new Error('Invalid RDF!'));
|
||||
await expect(fetchDataset(url)).rejects.toThrow(`Cannot fetch ${url}`);
|
||||
});
|
||||
|
||||
it('returns the resulting Dataset.', async(): Promise<void> => {
|
||||
await expect(fetchDataset(url)).resolves.toBe(dataset);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user