-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.test.ts
118 lines (103 loc) · 4.83 KB
/
index.test.ts
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
import { run } from './index';
import axios from "axios";
// Mock axios and reset after each test
jest.mock("axios");
const mockedAxiosPost = axios.post as jest.MockedFunction<typeof axios.post>;
const mockedAxiosPatch = axios.patch as jest.MockedFunction<typeof axios.patch>;
afterEach(() => {
jest.clearAllMocks();
});
describe('Grafana Annotation Action Tests', () => {
// ************************ Input Validation Tests ****************************************
// ****************************************************************************************
test('Input Validation: missing all required params', () => {
run();
expect(axios.post).not.toHaveBeenCalled();
expect(axios.patch).not.toHaveBeenCalled();
});
test('Input Validation: supplied grafanaHost', () => {
process.env.INPUT_GRAFANAHOST = 'something';
run();
expect(axios.post).not.toHaveBeenCalled();
expect(axios.patch).not.toHaveBeenCalled();
});
test('Input Validation: supplied grafanaToken', () => {
process.env.INPUT_GRAFANAHOST = 'something';
process.env.INPUT_GRAFANATOKEN = 'something';
run();
expect(axios.post).not.toHaveBeenCalled();
expect(axios.patch).not.toHaveBeenCalled();
});
test('Input Validation: supplied grafanaText, satisfied minimum required', () => {
process.env.INPUT_GRAFANAHOST = 'something';
process.env.INPUT_GRAFANATOKEN = 'something';
process.env.INPUT_GRAFANATEXT = 'something';
mockedAxiosPost.mockImplementationOnce(() => Promise.resolve({ status: 200, data: { id: 1} }));
run();
expect(axios.post).toHaveBeenCalledTimes(1);
expect(axios.post).toHaveBeenCalledWith(expect.anything(), {
tags: [],
text: 'something',
}, expect.anything());
expect(axios.patch).not.toHaveBeenCalled();
});
// ************************ Create Annotation Tests ***************************************
// ****************************************************************************************
test('Create Annotation: global annotation with tags', () => {
process.env.INPUT_GRAFANATAGS = 'tag:something\ntag2:something'
mockedAxiosPost.mockImplementation(() => Promise.resolve({ status: 200, data: { id: 1} }));
run();
expect(axios.post).toHaveBeenCalledTimes(1);
expect(axios.post).toHaveBeenCalledWith(expect.anything(), {
tags: [
'tag:something',
'tag2:something',
],
text: 'something',
}, expect.anything());
expect(axios.patch).not.toHaveBeenCalled();
});
test('Create Annotation: new annotation for dashboard but no panel specified', () => {
process.env.INPUT_GRAFANADASHBOARDID = '12345';
mockedAxiosPost.mockImplementation(() => Promise.resolve({ status: 200, data: { id: 1} }));
run();
expect(axios.post).not.toHaveBeenCalled();
expect(axios.patch).not.toHaveBeenCalled();
});
test('Create Annotation: new annotation for panel but no dashboard specified', () => {
process.env.INPUT_GRAFANAPANELID = '12345';
process.env.INPUT_GRAFANADASHBOARDID = undefined;
mockedAxiosPost.mockImplementation(() => Promise.resolve({ status: 200, data: { id: 1} }));
run();
expect(axios.post).not.toHaveBeenCalled();
expect(axios.patch).not.toHaveBeenCalled();
});
test('Create Annotation: new annotation for panel and dashboard', () => {
process.env.INPUT_GRAFANAPANELID = '12345';
process.env.INPUT_GRAFANADASHBOARDID = '12345';
mockedAxiosPost.mockImplementation(() => Promise.resolve({ status: 200, data: { id: 1} }));
run();
expect(axios.post).toHaveBeenCalledTimes(1);
expect(axios.post).toHaveBeenCalledWith(expect.anything(), {
tags: [
'tag:something',
'tag2:something',
],
dashboardId: 12345,
panelId: 12345,
text: 'something',
}, expect.anything());
expect(axios.patch).not.toHaveBeenCalled();
});
// ************************ Annotation Update Tests ***************************************
// ****************************************************************************************
test('Update Annotation: update', () => {
process.env.INPUT_GRAFANAANNOTATIONID = "12345";
delete process.env.INPUT_GRAFANATEXT; // Make sure text is not present
mockedAxiosPatch.mockImplementation(() => Promise.resolve({ status: 200}));
run();
expect(axios.post).not.toHaveBeenCalled();
expect(axios.patch).toHaveBeenCalledTimes(1);
expect(axios.patch).toHaveBeenCalledWith(expect.anything(), { timeEnd: expect.any(Number) }, expect.anything());
});
});