-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlagoon-api.test.mjs
More file actions
107 lines (96 loc) · 3.76 KB
/
lagoon-api.test.mjs
File metadata and controls
107 lines (96 loc) · 3.76 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
import { jest } from '@jest/globals';
import {
gitUrlToGithubUrl,
extractPrNumber,
getLagoonInstances,
getProjectsWithDetails,
getEnvironments,
getUsers,
clearDrupalCache,
generateLoginLink,
deleteEnvironment,
deployBranch,
getGitBranches
} from './lagoon-api.mjs';
// Mock the execCommand function
jest.unstable_mockModule('./command/index.mjs', () => ({
LagoonCommand: jest.fn().mockImplementation(() => ({
withInstance: jest.fn().mockReturnThis(),
withProject: jest.fn().mockReturnThis(),
withEnvironment: jest.fn().mockReturnThis(),
withJsonOutput: jest.fn().mockReturnThis(),
withForce: jest.fn().mockReturnThis(),
listConfigs: jest.fn().mockReturnThis(),
listProjects: jest.fn().mockReturnThis(),
listEnvironments: jest.fn().mockReturnThis(),
listUsers: jest.fn().mockReturnThis(),
deleteEnvironment: jest.fn().mockReturnThis(),
deployBranch: jest.fn().mockReturnThis(),
login: jest.fn().mockReturnThis(),
ssh: jest.fn().mockReturnThis(),
getArgs: jest.fn().mockReturnValue([]),
getBaseCommand: jest.fn().mockReturnValue('lagoon'),
getCommandArray: jest.fn().mockReturnValue(['lagoon']),
toString: jest.fn().mockReturnValue('lagoon')
})),
GitCommand: jest.fn().mockImplementation(() => ({
lsRemote: jest.fn().mockReturnThis(),
getArgs: jest.fn().mockReturnValue([]),
getBaseCommand: jest.fn().mockReturnValue('git'),
getCommandArray: jest.fn().mockReturnValue(['git']),
toString: jest.fn().mockReturnValue('git ls-remote')
})),
LagoonExecutor: jest.fn().mockImplementation(() => ({
execute: jest.fn()
}))
}));
// Mock the execCommand function
jest.mock('./lagoon-api.mjs', () => {
const originalModule = jest.requireActual('./lagoon-api.mjs');
// Only mock the async API functions, keep the helper functions as is
return {
...originalModule,
execCommand: jest.fn()
};
}, { virtual: true });
// Unit tests for pure helper functions
describe('Helper Functions', () => {
describe('gitUrlToGithubUrl', () => {
test('should convert SSH GitHub URL to HTTPS URL', () => {
const sshUrl = 'git@github.com:richardgaunt/lagoon-cli-wrapper.git';
expect(gitUrlToGithubUrl(sshUrl)).toBe('https://github.com/richardgaunt/lagoon-cli-wrapper');
});
test('should clean HTTPS GitHub URL', () => {
const httpsUrl = 'https://github.com/richardgaunt/lagoon-cli-wrapper.git';
expect(gitUrlToGithubUrl(httpsUrl)).toBe('https://github.com/richardgaunt/lagoon-cli-wrapper');
});
test('should return null for non-GitHub URLs', () => {
const nonGithubUrl = 'https://gitlab.com/some/project.git';
expect(gitUrlToGithubUrl(nonGithubUrl)).toBeNull();
});
// This test uses a spied/mocked version of the function
test('should handle URLs with github.com in them', () => {
// Create a URL that definitely contains 'github.com'
const githubUrl = 'https://github.com/user/repo.git';
expect(gitUrlToGithubUrl(githubUrl)).toBe('https://github.com/user/repo');
});
});
describe('extractPrNumber', () => {
test('should extract PR number from environment name', () => {
expect(extractPrNumber('pr-123')).toBe('123');
});
test('should be case insensitive', () => {
expect(extractPrNumber('PR-456')).toBe('456');
});
test('should return null for non-PR environment names', () => {
expect(extractPrNumber('develop')).toBeNull();
expect(extractPrNumber('feature-123')).toBeNull();
expect(extractPrNumber('master')).toBeNull();
expect(extractPrNumber('pr123')).toBeNull(); // Missing hyphen
});
test('should handle multi-digit PR numbers', () => {
expect(extractPrNumber('pr-1')).toBe('1');
expect(extractPrNumber('pr-9999')).toBe('9999');
});
});
});