Skip to content

Commit d93e915

Browse files
committed
added tests for coverage
1 parent b28701a commit d93e915

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
import * as vscode from 'vscode';
2+
3+
import {
4+
DevsphereConfigurationManager,
5+
executeVSCodeCommand,
6+
getDefaultLifecycleFns,
7+
LifecycleFns,
8+
updateConfig,
9+
View,
10+
} from './devsphere-config/devsphereConfigurationManager';
11+
12+
// Mock vscode module
13+
jest.mock('vscode', () => ({
14+
commands: {
15+
executeCommand: jest.fn(),
16+
},
17+
workspace: {
18+
getConfiguration: jest.fn(),
19+
},
20+
}));
21+
22+
describe('executeVSCodeCommand', () => {
23+
beforeEach(() => {
24+
jest.clearAllMocks();
25+
});
26+
27+
it('should execute command successfully', async () => {
28+
const mockExecuteCommand = vscode.commands.executeCommand as jest.Mock;
29+
mockExecuteCommand.mockResolvedValue(undefined);
30+
31+
await executeVSCodeCommand('test.command');
32+
33+
expect(mockExecuteCommand).toHaveBeenCalledWith('test.command');
34+
});
35+
36+
it('should handle command execution error and log it', async () => {
37+
const mockExecuteCommand = vscode.commands.executeCommand as jest.Mock;
38+
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
39+
const error = new Error('Command failed');
40+
mockExecuteCommand.mockRejectedValue(error);
41+
42+
await executeVSCodeCommand('test.command');
43+
44+
expect(consoleErrorSpy).toHaveBeenCalledWith('Failed to execute VS Code command: test.command', error);
45+
46+
consoleErrorSpy.mockRestore();
47+
});
48+
});
49+
50+
describe('updateConfig', () => {
51+
beforeEach(() => {
52+
jest.clearAllMocks();
53+
});
54+
55+
it('should update config when value is different', async () => {
56+
const mockGetConfiguration = vscode.workspace.getConfiguration as jest.Mock;
57+
const mockGet = jest.fn().mockReturnValue('oldValue');
58+
const mockUpdate = jest.fn().mockResolvedValue(undefined);
59+
60+
mockGetConfiguration.mockReturnValue({
61+
get: mockGet,
62+
update: mockUpdate,
63+
});
64+
65+
await updateConfig('testSection', 'testKey', 'newValue');
66+
67+
expect(mockGetConfiguration).toHaveBeenCalledWith('testSection');
68+
expect(mockGet).toHaveBeenCalledWith('testKey');
69+
expect(mockUpdate).toHaveBeenCalledWith('testKey', 'newValue', true);
70+
});
71+
72+
it('should not update config when value is the same', async () => {
73+
const mockGetConfiguration = vscode.workspace.getConfiguration as jest.Mock;
74+
const mockGet = jest.fn().mockReturnValue('sameValue');
75+
const mockUpdate = jest.fn();
76+
77+
mockGetConfiguration.mockReturnValue({
78+
get: mockGet,
79+
update: mockUpdate,
80+
});
81+
82+
await updateConfig('testSection', 'testKey', 'sameValue');
83+
84+
expect(mockGetConfiguration).toHaveBeenCalledWith('testSection');
85+
expect(mockGet).toHaveBeenCalledWith('testKey');
86+
expect(mockUpdate).not.toHaveBeenCalled();
87+
});
88+
});
89+
90+
describe('getDefaultLifecycleFns', () => {
91+
it('should return default lifecycle functions', () => {
92+
const lifecycle = getDefaultLifecycleFns();
93+
94+
expect(lifecycle).toHaveProperty('setup');
95+
expect(lifecycle).toHaveProperty('shutdown');
96+
expect(typeof lifecycle.setup).toBe('function');
97+
expect(typeof lifecycle.shutdown).toBe('function');
98+
});
99+
100+
it('should have setup function that resolves', async () => {
101+
const lifecycle = getDefaultLifecycleFns();
102+
await expect(lifecycle.setup()).resolves.toBeUndefined();
103+
});
104+
105+
it('should have shutdown function that resolves', async () => {
106+
const lifecycle = getDefaultLifecycleFns();
107+
await expect(lifecycle.shutdown()).resolves.toBeUndefined();
108+
});
109+
});
110+
111+
describe('DevsphereConfigurationManager', () => {
112+
let manager: DevsphereConfigurationManager;
113+
let mockLifecycleFns: Record<View, LifecycleFns>;
114+
115+
beforeEach(() => {
116+
jest.clearAllMocks();
117+
118+
mockLifecycleFns = {
119+
[View.Noop]: {
120+
setup: jest.fn().mockResolvedValue(undefined),
121+
shutdown: jest.fn().mockResolvedValue(undefined),
122+
},
123+
[View.Review]: {
124+
setup: jest.fn().mockResolvedValue(undefined),
125+
shutdown: jest.fn().mockResolvedValue(undefined),
126+
},
127+
};
128+
129+
manager = new DevsphereConfigurationManager(mockLifecycleFns);
130+
});
131+
132+
describe('constructor', () => {
133+
it('should create instance successfully', () => {
134+
expect(manager).toBeInstanceOf(DevsphereConfigurationManager);
135+
});
136+
});
137+
138+
describe('dispose', () => {
139+
it('should dispose and reset lifecycle functions', () => {
140+
manager.dispose();
141+
142+
// After dispose, the manager should work but with default lifecycle functions
143+
expect(manager).toBeInstanceOf(DevsphereConfigurationManager);
144+
});
145+
});
146+
147+
describe('setupView', () => {
148+
it('should setup new view and shutdown old one', async () => {
149+
await manager.setupView(View.Review);
150+
151+
expect(mockLifecycleFns[View.Noop].shutdown).toHaveBeenCalled();
152+
expect(mockLifecycleFns[View.Review].setup).toHaveBeenCalled();
153+
});
154+
155+
it('should not change view if same view is requested', async () => {
156+
await manager.setupView(View.Noop);
157+
158+
expect(mockLifecycleFns[View.Noop].shutdown).not.toHaveBeenCalled();
159+
expect(mockLifecycleFns[View.Noop].setup).not.toHaveBeenCalled();
160+
});
161+
162+
it('should handle shutdown error gracefully', async () => {
163+
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
164+
const shutdownError = new Error('Shutdown failed');
165+
(mockLifecycleFns[View.Noop].shutdown as jest.Mock).mockRejectedValue(shutdownError);
166+
167+
await manager.setupView(View.Review);
168+
169+
expect(consoleErrorSpy).toHaveBeenCalledWith('Failed to shutdown view: noop', shutdownError);
170+
expect(mockLifecycleFns[View.Review].setup).toHaveBeenCalled();
171+
172+
consoleErrorSpy.mockRestore();
173+
});
174+
175+
it('should handle setup error gracefully', async () => {
176+
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
177+
const setupError = new Error('Setup failed');
178+
(mockLifecycleFns[View.Review].setup as jest.Mock).mockRejectedValue(setupError);
179+
180+
await manager.setupView(View.Review);
181+
182+
expect(mockLifecycleFns[View.Noop].shutdown).toHaveBeenCalled();
183+
expect(consoleErrorSpy).toHaveBeenCalledWith('Failed to setup view: review', setupError);
184+
185+
consoleErrorSpy.mockRestore();
186+
});
187+
});
188+
});

0 commit comments

Comments
 (0)