UNPKG

@gravity-ui/data-source

Version:
104 lines (102 loc) 3.73 kB
import { isAbortable, isCancellable, withCancellation } from '../withCancellation'; describe('withCancellation', function () { describe('isCancellable', function () { it('should return true for objects with cancel method', function () { var cancellable = { cancel: jest.fn() }; expect(isCancellable(cancellable)).toBe(true); }); it('should return false for null', function () { expect(isCancellable(null)).toBe(false); }); it('should return false for objects without cancel method', function () { expect(isCancellable({})).toBe(false); }); it('should return false for objects with invalid cancel method', function () { expect(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(isAbortable(abortable)).toBe(true); }); it('should return false for null', function () { expect(isAbortable(null)).toBe(false); }); it('should return false for objects without signal', function () { expect(isAbortable({})).toBe(false); }); it('should return false for objects with invalid signal', function () { expect(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 = 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 = 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 = withCancellation(dataSource.fetch); var result = wrappedFetch({}, fetchContext, {}); expect(mockFetch).toHaveBeenCalledWith({}, fetchContext, {}); expect(result).toBe(cancellable); expect(mockCancel).not.toHaveBeenCalled(); }); }); }); // #sourceMappingURL=withCancellation.test.js.map