it('triggers `end(contentSize)` when the content is succesfully retreived', done => { nock('https://fragment') .get('/') .reply(200, '12345'); const fragment = new Fragment(getOptions(TAG)); fragment.on('end', contentSize => { assert.equal(contentSize, 5); done(); }); fragment.fetch(REQUEST); fragment.stream.resume(); });
it('should not trigger `response` and `end` for fallback fragment', done => { const onResponse = sinon.spy(); const onEnd = sinon.spy(); const onFallback = sinon.spy(); nock('https://fragment') .get('/') .reply(500); const fragment = new Fragment(getOptions(TAG_FALLBACK)); fragment.on('response', onResponse); fragment.on('end', onEnd); fragment.on('fallback', onFallback); fragment.fetch(REQUEST); fragment.stream.on('end', () => { assert.equal(onResponse.callCount, 0); assert.equal(onEnd.callCount, 0); assert.equal(onFallback.callCount, 1); done(); }); fragment.stream.resume(); });
it('should not trigger error and response event when fallback is triggered', done => { const onFallback = sinon.spy(); const onError = sinon.spy(); nock('https://fragment') .get('/') .reply(500, 'Server Error'); nock('https://fallback-fragment') .get('/') .reply(200); const fragment = new Fragment(getOptions(TAG_FALLBACK)); fragment.on('fallback', onFallback); fragment.on('error', onError); fragment.stream.on('end', () => { assert.equal(onFallback.callCount, 1); assert.equal(onError.callCount, 0); done(); }); fragment.fetch(REQUEST); fragment.stream.resume(); });
it('should not trigger `response` and `end` if there was an `error`', done => { const onResponse = sinon.spy(); const onEnd = sinon.spy(); const onError = sinon.spy(); nock('https://fragment') .get('/') .reply(500); const fragment = new Fragment(getOptions(TAG)); fragment.on('response', onResponse); fragment.on('end', onEnd); fragment.on('error', onError); fragment.fetch(REQUEST); fragment.stream.on('end', () => { assert.equal(onResponse.callCount, 0); assert.equal(onEnd.callCount, 0); assert.equal(onError.callCount, 1); done(); }); fragment.stream.resume(); });