-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.test.ts
More file actions
114 lines (93 loc) · 3.57 KB
/
index.test.ts
File metadata and controls
114 lines (93 loc) · 3.57 KB
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
/*
* Copyright (c) Trainline Limited, 2020. All rights reserved.
* See LICENSE.md in the project root for license information.
*/
import axios from 'axios';
import TeamCityDataSource, { TeamCityDataSourceConfiguration } from '.';
import { DataSourceBranchType } from '../BaseDataSource';
import baseCompilationStats from '../../../test/fixtures/base-compilation-stats.json';
import { Stats } from '../../types';
jest.mock('axios');
const options: TeamCityDataSourceConfiguration = {
serverUrl: 'https://teamcity.company.com',
buildType: 'Test_Build',
username: 'teamcity-build-user',
password: 'teamcity-build-password',
};
describe('TeamCityDataSource', () => {
let dataSource: TeamCityDataSource;
beforeEach(() => {
dataSource = new TeamCityDataSource(options);
(<jest.Mock>axios.get).mockResolvedValue({ data: { chunks: [] } });
});
afterEach(() => {
(<jest.Mock>axios.get).mockClear();
});
describe('getCompilationStats()', () => {
const baseStats = (baseCompilationStats as unknown) as Stats;
describe('happy path', () => {
beforeEach(() => {
(axios.get as jest.Mock).mockResolvedValue({ data: baseStats });
});
it('calls TeamCity with expected URL', async () => {
const { serverUrl, buildType } = options;
const sha = 'some-sha';
await dataSource.getCompilationStats(DataSourceBranchType.base, sha);
expect(axios.get).toHaveBeenCalledWith(
`${serverUrl}/app/rest/builds/buildType:${buildType},branch:default:any,revision:${sha}/artifacts/content/compilation-stats.json`,
expect.anything()
);
});
it('calls TeamCity with correct auth', async () => {
const { username, password } = options;
await dataSource.getCompilationStats(DataSourceBranchType.head, 'some-sha');
expect(axios.get).toHaveBeenCalledWith(expect.anything(), {
auth: { username, password },
});
});
it('calls TeamCity with specified file name', async () => {
const localOptions: TeamCityDataSourceConfiguration = {
...options,
fileName: 'other-stats.json',
};
const localDataSource = new TeamCityDataSource(localOptions);
const { serverUrl, buildType, fileName } = localOptions;
const sha = 'some-sha';
await localDataSource.getCompilationStats(DataSourceBranchType.base, sha);
expect(axios.get).toHaveBeenCalledWith(
`${serverUrl}/app/rest/builds/buildType:${buildType},branch:default:any,revision:${sha}/artifacts/content/${fileName}`,
expect.anything()
);
});
it('returns back the stats', () => {
return expect(
dataSource.getCompilationStats(DataSourceBranchType.head, 'some-sha')
).resolves.toMatchObject<Stats>(baseStats);
});
});
describe('validation', () => {
it('throws error when assets are not defined', () => {
(axios.get as jest.Mock).mockResolvedValue({
data: {
...baseStats.children[0],
assets: null,
},
});
return expect(
dataSource.getCompilationStats(DataSourceBranchType.head, 'some-sha')
).rejects.toThrow();
});
it('throws error when modules are not defined', () => {
(axios.get as jest.Mock).mockResolvedValue({
data: {
...baseStats.children[0],
modules: null,
},
});
return expect(
dataSource.getCompilationStats(DataSourceBranchType.head, 'some-sha')
).rejects.toThrow();
});
});
});
});