Welcome to mirror list, hosted at ThFree Co, Russian Federation.

html_stream_spec.js « streaming « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 115a9ddc803a2c9e6652edb478e5e25bea07a108 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { HtmlStream } from '~/streaming/html_stream';
import { ChunkWriter } from '~/streaming/chunk_writer';

jest.mock('~/streaming/chunk_writer');

describe('HtmlStream', () => {
  let write;
  let close;
  let streamingElement;

  beforeEach(() => {
    write = jest.fn();
    close = jest.fn();
    jest.spyOn(Document.prototype, 'write').mockImplementation(write);
    jest.spyOn(Document.prototype, 'close').mockImplementation(close);
    jest.spyOn(Document.prototype, 'querySelector').mockImplementation(() => {
      streamingElement = document.createElement('div');
      return streamingElement;
    });
  });

  it('attaches to original document', () => {
    // eslint-disable-next-line no-new
    new HtmlStream(document.body);
    expect(document.body.contains(streamingElement)).toBe(true);
  });

  it('can write to a document', () => {
    const htmlStream = new HtmlStream(document.body);
    htmlStream.write('foo');
    htmlStream.close();
    expect(write.mock.calls).toEqual([['<streaming-element>'], ['foo'], ['</streaming-element>']]);
    expect(close).toHaveBeenCalledTimes(1);
  });

  it('returns chunked writer', () => {
    const htmlStream = new HtmlStream(document.body).withChunkWriter();
    expect(htmlStream).toBeInstanceOf(ChunkWriter);
  });

  it('closes on abort', () => {
    const htmlStream = new HtmlStream(document.body);
    htmlStream.abort();
    expect(close).toHaveBeenCalled();
  });
});