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
111
112
113
114
115
116
117
118
119
120
121
122
123
|
import { nextTick } from 'vue';
import { GlIntersectionObserver } from '@gitlab/ui';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import Chunk from '~/vue_shared/components/source_viewer/components/chunk_deprecated.vue';
import ChunkLine from '~/vue_shared/components/source_viewer/components/chunk_line.vue';
import LineHighlighter from '~/blob/line_highlighter';
const lineHighlighter = new LineHighlighter();
jest.mock('~/blob/line_highlighter', () =>
jest.fn().mockReturnValue({
highlightHash: jest.fn(),
}),
);
const DEFAULT_PROPS = {
chunkIndex: 2,
isHighlighted: false,
content: '// Line 1 content \n // Line 2 content',
startingFrom: 140,
totalLines: 50,
language: 'javascript',
blamePath: 'blame/file.js',
};
const hash = '#L142';
describe('Chunk component', () => {
let wrapper;
let idleCallbackSpy;
const createComponent = (props = {}) => {
wrapper = shallowMountExtended(Chunk, {
mocks: { $route: { hash } },
propsData: { ...DEFAULT_PROPS, ...props },
});
};
const findIntersectionObserver = () => wrapper.findComponent(GlIntersectionObserver);
const findChunkLines = () => wrapper.findAllComponents(ChunkLine);
const findLineNumbers = () => wrapper.findAllByTestId('line-number');
const findContent = () => wrapper.findByTestId('content');
beforeEach(() => {
idleCallbackSpy = jest.spyOn(window, 'requestIdleCallback').mockImplementation((fn) => fn());
createComponent();
});
afterEach(() => wrapper.destroy());
describe('Intersection observer', () => {
it('renders an Intersection observer component', () => {
expect(findIntersectionObserver().exists()).toBe(true);
});
it('emits an appear event when intersection-observer appears', () => {
findIntersectionObserver().vm.$emit('appear');
expect(wrapper.emitted('appear')).toEqual([[DEFAULT_PROPS.chunkIndex]]);
});
it('does not emit an appear event is isHighlighted is true', () => {
createComponent({ isHighlighted: true });
findIntersectionObserver().vm.$emit('appear');
expect(wrapper.emitted('appear')).toEqual(undefined);
});
});
describe('rendering', () => {
it('does not register window.requestIdleCallback if isFirstChunk prop is true, renders lines immediately', () => {
jest.clearAllMocks();
createComponent({ isFirstChunk: true });
expect(window.requestIdleCallback).not.toHaveBeenCalled();
expect(findContent().exists()).toBe(true);
});
it('does not render a Chunk Line component if isHighlighted is false', () => {
expect(findChunkLines().length).toBe(0);
});
it('does not render simplified line numbers and content if browser is not in idle state', () => {
idleCallbackSpy.mockRestore();
createComponent();
expect(findLineNumbers()).toHaveLength(0);
expect(findContent().exists()).toBe(false);
});
it('renders simplified line numbers and content if isHighlighted is false', () => {
expect(findLineNumbers().length).toBe(DEFAULT_PROPS.totalLines);
expect(findLineNumbers().at(0).attributes('id')).toBe(`L${DEFAULT_PROPS.startingFrom + 1}`);
expect(findContent().text()).toBe(DEFAULT_PROPS.content);
});
it('renders Chunk Line components if isHighlighted is true', () => {
const splitContent = DEFAULT_PROPS.content.split('\n');
createComponent({ isHighlighted: true });
expect(findChunkLines().length).toBe(splitContent.length);
expect(findChunkLines().at(0).props()).toMatchObject({
number: DEFAULT_PROPS.startingFrom + 1,
content: splitContent[0],
language: DEFAULT_PROPS.language,
blamePath: DEFAULT_PROPS.blamePath,
});
});
it('does not scroll to route hash if last chunk is not loaded', () => {
expect(LineHighlighter).not.toHaveBeenCalled();
});
it('scrolls to route hash if last chunk is loaded', async () => {
createComponent({ totalChunks: DEFAULT_PROPS.chunkIndex + 1 });
await nextTick();
expect(LineHighlighter).toHaveBeenCalledWith({ scrollBehavior: 'auto' });
expect(lineHighlighter.highlightHash).toHaveBeenCalledWith(hash);
});
});
});
|