|
| 1 | +import { fromFetch } from 'rxjs/fetch'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { root } from '../../../src/internal/util/root'; |
| 4 | + |
| 5 | +const OK_RESPONSE = { |
| 6 | + ok: true, |
| 7 | +} as Response; |
| 8 | + |
| 9 | +function mockFetchImpl(input: string | Request, init?: RequestInit): Promise<Response> { |
| 10 | + (mockFetchImpl as MockFetch).calls.push({ input, init }); |
| 11 | + return new Promise<any>((resolve, reject) => { |
| 12 | + if (init.signal) { |
| 13 | + init.signal.addEventListener('abort', () => { |
| 14 | + reject(new MockDOMException()); |
| 15 | + }); |
| 16 | + } |
| 17 | + return Promise.resolve(null).then(() => { |
| 18 | + resolve((mockFetchImpl as any).respondWith); |
| 19 | + }); |
| 20 | + }); |
| 21 | +} |
| 22 | +(mockFetchImpl as MockFetch).reset = function (this: any) { |
| 23 | + this.calls = [] as any[]; |
| 24 | + this.respondWith = OK_RESPONSE; |
| 25 | +}; |
| 26 | +(mockFetchImpl as MockFetch).reset(); |
| 27 | + |
| 28 | +const mockFetch: MockFetch = mockFetchImpl as MockFetch; |
| 29 | + |
| 30 | +class MockDOMException {} |
| 31 | + |
| 32 | +class MockAbortController { |
| 33 | + readonly signal = new MockAbortSignal(); |
| 34 | + |
| 35 | + abort() { |
| 36 | + this.signal._signal(); |
| 37 | + } |
| 38 | + |
| 39 | + constructor() { |
| 40 | + MockAbortController.created++; |
| 41 | + } |
| 42 | + |
| 43 | + static created = 0; |
| 44 | + |
| 45 | + static reset() { |
| 46 | + MockAbortController.created = 0; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +class MockAbortSignal { |
| 51 | + private _listeners: Function[] = []; |
| 52 | + |
| 53 | + aborted = false; |
| 54 | + |
| 55 | + addEventListener(name: 'abort', handler: Function) { |
| 56 | + this._listeners.push(handler); |
| 57 | + } |
| 58 | + |
| 59 | + removeEventListener(name: 'abort', handler: Function) { |
| 60 | + const index = this._listeners.indexOf(handler); |
| 61 | + if (index >= 0) { |
| 62 | + this._listeners.splice(index, 1); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + _signal() { |
| 67 | + this.aborted = true; |
| 68 | + while (this._listeners.length > 0) { |
| 69 | + this._listeners.shift()(); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +interface MockFetch { |
| 75 | + (input: string | Request, init?: RequestInit): Promise<Response>; |
| 76 | + calls: { input: string | Request, init: RequestInit | undefined }[]; |
| 77 | + reset(): void; |
| 78 | + respondWith: Response; |
| 79 | +} |
| 80 | + |
| 81 | +describe('fromFetch', () => { |
| 82 | + let _fetch: typeof fetch; |
| 83 | + let _AbortController: AbortController; |
| 84 | + |
| 85 | + beforeEach(() => { |
| 86 | + mockFetch.reset(); |
| 87 | + if (root.fetch) { |
| 88 | + _fetch = root.fetch; |
| 89 | + } |
| 90 | + root.fetch = mockFetch; |
| 91 | + |
| 92 | + MockAbortController.reset(); |
| 93 | + if (root.AbortController) { |
| 94 | + _AbortController = root.AbortController; |
| 95 | + } |
| 96 | + root.AbortController = MockAbortController; |
| 97 | + }); |
| 98 | + |
| 99 | + afterEach(() => { |
| 100 | + root.fetch = _fetch; |
| 101 | + root.AbortController = _AbortController; |
| 102 | + }); |
| 103 | + |
| 104 | + it('should exist', () => { |
| 105 | + expect(fromFetch).to.be.a('function'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should fetch', done => { |
| 109 | + const fetch$ = fromFetch('/foo'); |
| 110 | + expect(mockFetch.calls.length).to.equal(0); |
| 111 | + expect(MockAbortController.created).to.equal(0); |
| 112 | + |
| 113 | + fetch$.subscribe({ |
| 114 | + next: response => { |
| 115 | + expect(response).to.equal(OK_RESPONSE); |
| 116 | + }, |
| 117 | + error: done, |
| 118 | + complete: done, |
| 119 | + }); |
| 120 | + |
| 121 | + expect(MockAbortController.created).to.equal(1); |
| 122 | + expect(mockFetch.calls.length).to.equal(1); |
| 123 | + expect(mockFetch.calls[0].input).to.equal('/foo'); |
| 124 | + expect(mockFetch.calls[0].init.signal).not.to.be.undefined; |
| 125 | + expect(mockFetch.calls[0].init.signal.aborted).to.be.false; |
| 126 | + }); |
| 127 | + |
| 128 | + it('should handle Response that is not `ok`', done => { |
| 129 | + mockFetch.respondWith = { |
| 130 | + ok: false, |
| 131 | + status: 400, |
| 132 | + body: 'Bad stuff here' |
| 133 | + } as any as Response; |
| 134 | + |
| 135 | + const fetch$ = fromFetch('/foo'); |
| 136 | + expect(mockFetch.calls.length).to.equal(0); |
| 137 | + expect(MockAbortController.created).to.equal(0); |
| 138 | + |
| 139 | + fetch$.subscribe({ |
| 140 | + next: response => { |
| 141 | + expect(response).to.equal(mockFetch.respondWith); |
| 142 | + }, |
| 143 | + complete: done, |
| 144 | + error: done |
| 145 | + }); |
| 146 | + |
| 147 | + expect(MockAbortController.created).to.equal(1); |
| 148 | + expect(mockFetch.calls.length).to.equal(1); |
| 149 | + expect(mockFetch.calls[0].input).to.equal('/foo'); |
| 150 | + expect(mockFetch.calls[0].init.signal).not.to.be.undefined; |
| 151 | + expect(mockFetch.calls[0].init.signal.aborted).to.be.false; |
| 152 | + }); |
| 153 | + |
| 154 | + it('should abort when unsubscribed', () => { |
| 155 | + const fetch$ = fromFetch('/foo'); |
| 156 | + expect(mockFetch.calls.length).to.equal(0); |
| 157 | + expect(MockAbortController.created).to.equal(0); |
| 158 | + const subscription = fetch$.subscribe(); |
| 159 | + |
| 160 | + expect(MockAbortController.created).to.equal(1); |
| 161 | + expect(mockFetch.calls.length).to.equal(1); |
| 162 | + expect(mockFetch.calls[0].input).to.equal('/foo'); |
| 163 | + expect(mockFetch.calls[0].init.signal).not.to.be.undefined; |
| 164 | + expect(mockFetch.calls[0].init.signal.aborted).to.be.false; |
| 165 | + |
| 166 | + subscription.unsubscribe(); |
| 167 | + expect(mockFetch.calls[0].init.signal.aborted).to.be.true; |
| 168 | + }); |
| 169 | + |
| 170 | + it('should allow passing of init object', done => { |
| 171 | + const myInit = {}; |
| 172 | + const fetch$ = fromFetch('/foo', myInit); |
| 173 | + fetch$.subscribe({ |
| 174 | + error: done, |
| 175 | + complete: done, |
| 176 | + }); |
| 177 | + expect(mockFetch.calls[0].init).to.equal(myInit); |
| 178 | + expect(mockFetch.calls[0].init.signal).not.to.be.undefined; |
| 179 | + }); |
| 180 | + |
| 181 | + it('should treat passed signals as a cancellation token which triggers an error', done => { |
| 182 | + const controller = new MockAbortController(); |
| 183 | + const signal = controller.signal as any; |
| 184 | + const fetch$ = fromFetch('/foo', { signal }); |
| 185 | + const subscription = fetch$.subscribe({ |
| 186 | + error: err => { |
| 187 | + expect(err).to.be.instanceof(MockDOMException); |
| 188 | + done(); |
| 189 | + } |
| 190 | + }); |
| 191 | + controller.abort(); |
| 192 | + expect(mockFetch.calls[0].init.signal.aborted).to.be.true; |
| 193 | + // The subscription will not be closed until the error fires when the promise resolves. |
| 194 | + expect(subscription.closed).to.be.false; |
| 195 | + }); |
| 196 | +}); |
0 commit comments