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
124
125
126
127
|
import $ from 'jquery';
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import { mockTracking, unmockTracking } from 'helpers/tracking_helper';
import waitForPromises from 'helpers/wait_for_promises';
import blobBundle from '~/blob_edit/blob_bundle';
import SourceEditor from '~/blob_edit/edit_blob';
import { createAlert } from '~/alert';
jest.mock('~/blob_edit/edit_blob');
jest.mock('~/alert');
describe('BlobBundle', () => {
beforeAll(() => {
// HACK: Workaround readonly property in Jest
Object.defineProperty(window, 'onbeforeunload', {
writable: true,
});
});
it('does not load SourceEditor by default', () => {
blobBundle();
expect(SourceEditor).not.toHaveBeenCalled();
});
it('loads SourceEditor for the edit screen', async () => {
setHTMLFixture(`<div class="js-edit-blob-form"></div>`);
blobBundle();
await waitForPromises();
expect(SourceEditor).toHaveBeenCalled();
resetHTMLFixture();
});
describe('No Suggest Popover', () => {
beforeEach(() => {
setHTMLFixture(`
<div class="js-edit-blob-form" data-blob-filename="blah">
<button class="js-commit-button"></button>
<button id='cancel-changes'></button>
</div>`);
blobBundle();
});
afterEach(() => {
resetHTMLFixture();
});
it('sets the window beforeunload listener to a function returning a string', () => {
expect(window.onbeforeunload()).toBe('');
});
it('removes beforeunload listener if commit button is clicked', () => {
$('.js-commit-button').click();
expect(window.onbeforeunload).toBeNull();
});
it('removes beforeunload listener when cancel link is clicked', () => {
$('#cancel-changes').click();
expect(window.onbeforeunload).toBeNull();
});
});
describe('Suggest Popover', () => {
let trackingSpy;
beforeEach(() => {
setHTMLFixture(`
<div class="js-edit-blob-form" data-blob-filename="blah" id="target">
<div class="js-suggest-gitlab-ci-yml"
data-target="#target"
data-track-label="suggest_gitlab_ci_yml"
data-dismiss-key="1"
data-human-access="owner"
data-merge-request-path="path/to/mr">
<button id='commit-changes' class="js-commit-button"></button>
<button id='cancel-changes'></button>
</div>
</div>`);
trackingSpy = mockTracking('_category_', $('#commit-changes').element, jest.spyOn);
document.body.dataset.page = 'projects:blob:new';
blobBundle();
});
afterEach(() => {
unmockTracking();
resetHTMLFixture();
});
it('sends a tracking event when the commit button is clicked', () => {
$('#commit-changes').click();
expect(trackingSpy).toHaveBeenCalledTimes(1);
expect(trackingSpy).toHaveBeenCalledWith(undefined, undefined, {
label: 'suggest_gitlab_ci_yml',
property: 'owner',
});
});
});
describe('Error handling', () => {
let message;
beforeEach(() => {
setHTMLFixture(`<div class="js-edit-blob-form" data-blob-filename="blah"></div>`);
message = 'Foo';
SourceEditor.mockImplementation(() => {
throw new Error(message);
});
});
afterEach(() => {
resetHTMLFixture();
SourceEditor.mockClear();
});
it('correctly outputs error message when it occurs', async () => {
blobBundle();
await waitForPromises();
expect(createAlert).toHaveBeenCalledWith({ message });
});
});
});
|