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` 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(); });
/** * Insert the placeholder for pipe assets at the end of fragment stream */ insertEnd() { if (this.scriptRefs.length > 0) { const range = [this.index - this.scriptRefs.length, this.index - 1]; this.index--; const fragmentId = this.attributes.id || range[0]; const attributes = Object.assign({}, this.pipeAttributes, { id: fragmentId, range }); this.scriptRefs.reverse().forEach(uri => { this.stream.write( `<script data-pipe>${this.pipeInstanceName}.end(${this .index--}, "${uri}", ${JSON.stringify( attributes )})</script>` ); }); } else { this.stream.write( `<script data-pipe>${this.pipeInstanceName}.end(${this.index - 1})</script>` ); } }
const { async: isAsync, id } = this.attributes; this.styleRefs.forEach(uri => { this.stream.write( isAsync ? `<script>${this this.stream.write( `<script data-pipe>${this.pipeInstanceName}.start(${this .index})</script>` }); this.scriptRefs.forEach(uri => { this.stream.write( `<script data-pipe>${this.pipeInstanceName}.start(${this .index}, "${uri}", ${JSON.stringify(attributes)})</script>`
this.stream.end(); span.finish(); });
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` 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(); });