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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
import waitForPromises from 'helpers/wait_for_promises';
import { renderBlamePageStreams } from '~/blame/streaming';
import { setHTMLFixture } from 'helpers/fixtures';
import { renderHtmlStreams } from '~/streaming/render_html_streams';
import { rateLimitStreamRequests } from '~/streaming/rate_limit_stream_requests';
import { handleStreamedAnchorLink } from '~/streaming/handle_streamed_anchor_link';
import { toPolyfillReadable } from '~/streaming/polyfills';
import { createAlert } from '~/alert';
jest.mock('~/streaming/render_html_streams');
jest.mock('~/streaming/rate_limit_stream_requests');
jest.mock('~/streaming/handle_streamed_anchor_link');
jest.mock('~/streaming/polyfills');
jest.mock('~/sentry');
jest.mock('~/alert');
global.fetch = jest.fn();
describe('renderBlamePageStreams', () => {
let stopAnchor;
const PAGES_URL = 'https://example.com/';
const findStreamContainer = () => document.querySelector('#blame-stream-container');
const findStreamLoadingIndicator = () => document.querySelector('#blame-stream-loading');
const setupHtml = (totalExtraPages = 0) => {
setHTMLFixture(`
<div id="blob-content-holder"
data-total-extra-pages="${totalExtraPages}"
data-pages-url="${PAGES_URL}"
></div>
<div id="blame-stream-container"></div>
<div id="blame-stream-loading"></div>
`);
};
handleStreamedAnchorLink.mockImplementation(() => stopAnchor);
rateLimitStreamRequests.mockImplementation(({ factory, total }) => {
return Array.from({ length: total }, (_, i) => {
return Promise.resolve(factory(i));
});
});
toPolyfillReadable.mockImplementation((obj) => obj);
beforeEach(() => {
stopAnchor = jest.fn();
fetch.mockClear();
});
it('does nothing for an empty page', async () => {
await renderBlamePageStreams();
expect(handleStreamedAnchorLink).not.toHaveBeenCalled();
expect(renderHtmlStreams).not.toHaveBeenCalled();
});
it('renders a single stream', async () => {
let res;
const stream = new Promise((resolve) => {
res = resolve;
});
renderHtmlStreams.mockImplementationOnce(() => stream);
setupHtml();
renderBlamePageStreams(stream);
expect(handleStreamedAnchorLink).toHaveBeenCalledTimes(1);
expect(stopAnchor).toHaveBeenCalledTimes(0);
expect(renderHtmlStreams).toHaveBeenCalledWith([stream], findStreamContainer());
expect(findStreamLoadingIndicator()).not.toBe(null);
res();
await waitForPromises();
expect(stopAnchor).toHaveBeenCalledTimes(1);
expect(findStreamLoadingIndicator()).toBe(null);
});
it('renders rest of the streams', async () => {
const stream = Promise.resolve();
const stream2 = Promise.resolve({ body: null });
fetch.mockImplementationOnce(() => stream2);
setupHtml(1);
await renderBlamePageStreams(stream);
expect(fetch.mock.calls[0][0].toString()).toBe(`${PAGES_URL}?page=3`);
expect(renderHtmlStreams).toHaveBeenCalledWith([stream, stream2], findStreamContainer());
});
it('shows an error message when failed', async () => {
const stream = Promise.resolve();
const error = new Error();
renderHtmlStreams.mockImplementationOnce(() => Promise.reject(error));
setupHtml();
try {
await renderBlamePageStreams(stream);
} catch (err) {
expect(err).toBe(error);
}
expect(createAlert).toHaveBeenCalledWith({
message: 'Blame could not be loaded as a single page.',
primaryButton: {
text: 'View blame as separate pages',
clickHandler: expect.any(Function),
},
});
});
});
|