@gravity-ui/data-source
Version:
A wrapper around data fetching
106 lines (103 loc) • 3.98 kB
JavaScript
;
var _withCancellation = require("../withCancellation");
describe('withCancellation', function () {
describe('isCancellable', function () {
it('should return true for objects with cancel method', function () {
var cancellable = {
cancel: jest.fn()
};
expect((0, _withCancellation.isCancellable)(cancellable)).toBe(true);
});
it('should return false for null', function () {
expect((0, _withCancellation.isCancellable)(null)).toBe(false);
});
it('should return false for objects without cancel method', function () {
expect((0, _withCancellation.isCancellable)({})).toBe(false);
});
it('should return false for objects with invalid cancel method', function () {
expect((0, _withCancellation.isCancellable)({
cancel: 'not a function'
})).toBe(false);
});
});
describe('isAbortable', function () {
it('should return true for objects with valid signal', function () {
var abortController = new AbortController();
var abortable = {
signal: abortController.signal
};
expect((0, _withCancellation.isAbortable)(abortable)).toBe(true);
});
it('should return false for null', function () {
expect((0, _withCancellation.isAbortable)(null)).toBe(false);
});
it('should return false for objects without signal', function () {
expect((0, _withCancellation.isAbortable)({})).toBe(false);
});
it('should return false for objects with invalid signal', function () {
expect((0, _withCancellation.isAbortable)({
signal: {}
})).toBe(false);
});
});
describe('withCancellation', function () {
it('should add abort listener when fetch returns cancellable and context is abortable', function () {
var mockCancel = jest.fn();
var cancellable = {
cancel: mockCancel
};
var abortController = new AbortController();
var fetchContext = {
signal: abortController.signal
};
var mockFetch = jest.fn().mockReturnValue(cancellable);
var dataSource = {
name: 'test',
fetch: mockFetch
};
var wrappedFetch = (0, _withCancellation.withCancellation)(dataSource.fetch);
var result = wrappedFetch({}, fetchContext, {});
expect(mockFetch).toHaveBeenCalledWith({}, fetchContext, {});
expect(result).toBe(cancellable);
expect(mockCancel).not.toHaveBeenCalled();
// Simulate abort
abortController.abort();
expect(mockCancel).toHaveBeenCalled();
});
it('should not add abort listener when fetch returns non-cancellable', function () {
var abortController = new AbortController();
var fetchContext = {
signal: abortController.signal
};
var mockFetch = jest.fn().mockReturnValue('not cancellable');
var dataSource = {
name: 'test',
fetch: mockFetch
};
var wrappedFetch = (0, _withCancellation.withCancellation)(dataSource.fetch);
var result = wrappedFetch({}, fetchContext, {});
expect(mockFetch).toHaveBeenCalledWith({}, fetchContext, {});
expect(result).toBe('not cancellable');
// No error should occur when aborting
abortController.abort();
});
it('should not add abort listener when context is not abortable', function () {
var mockCancel = jest.fn();
var cancellable = {
cancel: mockCancel
};
var fetchContext = {};
var mockFetch = jest.fn().mockReturnValue(cancellable);
var dataSource = {
name: 'test',
fetch: mockFetch
};
var wrappedFetch = (0, _withCancellation.withCancellation)(dataSource.fetch);
var result = wrappedFetch({}, fetchContext, {});
expect(mockFetch).toHaveBeenCalledWith({}, fetchContext, {});
expect(result).toBe(cancellable);
expect(mockCancel).not.toHaveBeenCalled();
});
});
});
// #sourceMappingURL=withCancellation.test.js.map