describe('cancel:Cancel', () => { test('should returns correct result when message is specified', () => { const cancel = newCancel('Operation has been canceled.') expect(cancel.message).toBe('Operation has been canceled.') })
test('should returns true if value is a Cancel', () => { expect(isCancel(newCancel())).toBeTruthy() })
test('should returns false if value is not a Cancel', () => { expect(isCancel({ foo: 'bar' })).toBeFalsy() }) })
describe('CancelToken', () => { describe('reason', () => { test('should returns a Cancel if cancellation has been requested', () => { letcancel: Canceler let token = newCancelToken(c => { cancel = c }) cancel!('Operation has been canceled.') expect(token.reason).toEqual(expect.any(Cancel)) expect(token.reason!.message).toBe('Operation has been canceled.') })
test('should has no side effect if call cancellation for multi times', () => { letcancel: Canceler let token = newCancelToken(c => { cancel = c }) cancel!('Operation has been canceled.') cancel!('Operation has been canceled.') expect(token.reason).toEqual(expect.any(Cancel)) expect(token.reason!.message).toBe('Operation has been canceled.') })
test('should returns undefined if cancellation has not been requested', () => { const token = newCancelToken(() => { // do nothing }) expect(token.reason).toBeUndefined() }) })
describe('promise', () => { test('should returns a Promise that resolves when cancellation is requested', done => { letcancel: Canceler const token = newCancelToken(c => { cancel = c }) token.promise.then(value => { expect(value).toEqual(expect.any(Cancel)) expect(value.message).toBe('Operation has been canceled.') done() }) cancel!('Operation has been canceled.') }) })
describe('throwIfRequested', () => { test('should throws if cancellation has been requested', () => { letcancel: Canceler const token = newCancelToken(c => { cancel = c }) cancel!('Operation has been canceled.') try { token.throwIfRequested() fail('Expected throwIfRequested to throw.') } catch (thrown) { if (!(thrown instanceofCancel)) { fail('Expected throwIfRequested to throw a Cancel, but test threw ' + thrown + '.') } expect(thrown.message).toBe('Operation has been canceled.') } })
test('should does not throw if cancellation has not been requested', () => { const token = newCancelToken(() => { // do nothing }) token.throwIfRequested() }) })
describe('source', () => { test('should returns an object containing token and cancel function', () => { const source = CancelToken.source() expect(source.token).toEqual(expect.any(CancelToken)) expect(source.cancel).toEqual(expect.any(Function)) expect(source.token.reason).toBeUndefined() source.cancel('Operation has been canceled.') expect(source.token.reason).toEqual(expect.any(Cancel)) expect(source.token.reason!.message).toBe('Operation has been canceled.') }) }) })
注意,这里我们使用了 fail 函数表示一个测试的失败,这个并未在 Jest 文档中体现,但它是一个可以用的 API。
describe('when called before sending request', () => { test('should rejects Promise with a Cancel object', () => { const source = CancelToken.source() source.cancel('Operation has been canceled.')
return axios .get('/foo', { cancelToken: source.token }) .catch(reason => { expect(reason).toEqual(expect.any(Cancel)) expect(reason.message).toBe('Operation has been canceled.') }) }) })
describe('when called after request has been sent', () => { test('should rejects Promise with a Cancel object', done => { const source = CancelToken.source() axios .get('/foo/bar', { cancelToken: source.token }) .catch(reason => { expect(reason).toEqual(expect.any(Cancel)) expect(reason.message).toBe('Operation has been canceled.') done() })
getAjaxRequest().then(request => { source.cancel('Operation has been canceled.') setTimeout(() => { request.respondWith({ status: 200, responseText: 'OK' }) }, 100) }) })