Skip to content

Commit 5adce75

Browse files
authored
refactor(main/proxy-system.spec.ts): use vitest v4 compatible syntax (podman-desktop#14917)
refactor(main/proxy-system.spec.ts): use vitest v4 compatible synthax Signed-off-by: axel7083 <42176370+axel7083@users.noreply.github.com>
1 parent 85210ce commit 5adce75

File tree

1 file changed

+42
-46
lines changed

1 file changed

+42
-46
lines changed

packages/main/src/plugin/proxy-system.spec.ts

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,17 @@
1717
***********************************************************************/
1818

1919
import type { RunResult } from '@podman-desktop/api';
20-
import { describe, expect, test, vi } from 'vitest';
20+
import { beforeEach, describe, expect, test, vi } from 'vitest';
21+
import type { RegistryItem } from 'winreg';
22+
import WinReg from 'winreg';
2123

2224
import type { Proxy } from '/@/plugin/proxy.js';
2325
import { getProxySettingsFromSystem } from '/@/plugin/proxy-system.js';
2426
import { Exec } from '/@/plugin/util/exec.js';
2527

2628
import * as util from '../util.js';
2729

28-
const mocks = vi.hoisted(() => {
29-
return {
30-
WinRegMock: vi.fn(),
31-
};
32-
});
33-
34-
vi.mock('winreg', () => {
35-
return {
36-
default: mocks.WinRegMock,
37-
};
38-
});
30+
vi.mock(import('winreg'));
3931

4032
enum Platform {
4133
WINDOWS,
@@ -50,36 +42,40 @@ function setupPlatform(platform: Platform): void {
5042
vi.spyOn(util, 'isUnixLike').mockReturnValue(platform === Platform.LINUX);
5143
}
5244

45+
beforeEach(() => {
46+
vi.resetAllMocks();
47+
});
48+
5349
describe('Windows platform tests', () => {
54-
setupPlatform(Platform.WINDOWS);
5550
test('No state returned in case of execution error', async () => {
56-
mocks.WinRegMock.mockReturnValue({
57-
values: vi.fn().mockImplementation(cb => {
51+
setupPlatform(Platform.WINDOWS);
52+
vi.mocked(WinReg.prototype.values).mockImplementation(
53+
(cb: (err: Error | undefined, result: RegistryItem[] | undefined) => void) => {
5854
cb(new Error('execution error'), undefined);
59-
}),
60-
});
55+
},
56+
);
6157
const settings = await getProxySettingsFromSystem({} as Proxy);
6258
expect(settings).toBeUndefined();
6359
});
6460

6561
test('No state returned in case of proxy disabled', async () => {
6662
setupPlatform(Platform.WINDOWS);
67-
mocks.WinRegMock.mockReturnValue({
68-
values: vi.fn().mockImplementation(cb => {
69-
cb(undefined, [{ name: 'ProxyEnable', value: '0x0' }]);
70-
}),
71-
});
63+
vi.mocked(WinReg.prototype.values).mockImplementation(
64+
(cb: (err: Error | undefined, result: RegistryItem[] | undefined) => void) => {
65+
cb(undefined, [{ name: 'ProxyEnable', value: '0x0' } as RegistryItem]);
66+
},
67+
);
7268
const settings = await getProxySettingsFromSystem({} as Proxy);
7369
expect(settings).toBeUndefined();
7470
});
7571

7672
test('Empty state returned in case of proxy enabled only', async () => {
7773
setupPlatform(Platform.WINDOWS);
78-
mocks.WinRegMock.mockReturnValue({
79-
values: vi.fn().mockImplementation(cb => {
80-
cb(undefined, [{ name: 'ProxyEnable', value: '0x1' }]);
81-
}),
82-
});
74+
vi.mocked(WinReg.prototype.values).mockImplementation(
75+
(cb: (err: Error | undefined, result: RegistryItem[] | undefined) => void) => {
76+
cb(undefined, [{ name: 'ProxyEnable', value: '0x1' } as RegistryItem]);
77+
},
78+
);
8379
const settings = await getProxySettingsFromSystem({} as Proxy);
8480
expect(settings).toBeDefined();
8581
expect(settings?.httpProxy).toBeUndefined();
@@ -89,14 +85,14 @@ describe('Windows platform tests', () => {
8985

9086
test('State returned in case of proxy enabled and proxy server', async () => {
9187
setupPlatform(Platform.WINDOWS);
92-
mocks.WinRegMock.mockReturnValue({
93-
values: vi.fn().mockImplementation(cb => {
88+
vi.mocked(WinReg.prototype.values).mockImplementation(
89+
(cb: (err: Error | undefined, result: RegistryItem[] | undefined) => void) => {
9490
cb(undefined, [
95-
{ name: 'ProxyEnable', value: '0x1' },
96-
{ name: 'ProxyServer', value: 'http=127.0.0.1:8888;https=127.0.0.1:8889' },
91+
{ name: 'ProxyEnable', value: '0x1' } as RegistryItem,
92+
{ name: 'ProxyServer', value: 'http=127.0.0.1:8888;https=127.0.0.1:8889' } as RegistryItem,
9793
]);
98-
}),
99-
});
94+
},
95+
);
10096
const settings = await getProxySettingsFromSystem({} as Proxy);
10197
expect(settings).toBeDefined();
10298
expect(settings?.httpProxy).toBe('http://127.0.0.1:8888');
@@ -106,15 +102,15 @@ describe('Windows platform tests', () => {
106102

107103
test('State returned in case of proxy enabled and proxy server with exceptions', async () => {
108104
setupPlatform(Platform.WINDOWS);
109-
mocks.WinRegMock.mockReturnValue({
110-
values: vi.fn().mockImplementation(cb => {
105+
vi.mocked(WinReg.prototype.values).mockImplementation(
106+
(cb: (err: Error | undefined, result: RegistryItem[] | undefined) => void) => {
111107
cb(undefined, [
112-
{ name: 'ProxyEnable', value: '0x1' },
113-
{ name: 'ProxyServer', value: 'http=127.0.0.1:8888;https=127.0.0.1:8889' },
114-
{ name: 'ProxyOverride', value: '*.internal' },
108+
{ name: 'ProxyEnable', value: '0x1' } as RegistryItem,
109+
{ name: 'ProxyServer', value: 'http=127.0.0.1:8888;https=127.0.0.1:8889' } as RegistryItem,
110+
{ name: 'ProxyOverride', value: '*.internal' } as RegistryItem,
115111
]);
116-
}),
117-
});
112+
},
113+
);
118114
const settings = await getProxySettingsFromSystem({} as Proxy);
119115
expect(settings).toBeDefined();
120116
expect(settings?.httpProxy).toBe('http://127.0.0.1:8888');
@@ -124,14 +120,14 @@ describe('Windows platform tests', () => {
124120

125121
test('State returned in case of proxy enabled and proxy server is <ip:port>', async () => {
126122
setupPlatform(Platform.WINDOWS);
127-
mocks.WinRegMock.mockReturnValue({
128-
values: vi.fn().mockImplementation(cb => {
123+
vi.mocked(WinReg.prototype.values).mockImplementation(
124+
(cb: (err: Error | undefined, result: RegistryItem[] | undefined) => void) => {
129125
cb(undefined, [
130-
{ name: 'ProxyEnable', value: '0x1' },
131-
{ name: 'ProxyServer', value: '127.0.0.1:8888' },
126+
{ name: 'ProxyEnable', value: '0x1' } as RegistryItem,
127+
{ name: 'ProxyServer', value: '127.0.0.1:8888' } as RegistryItem,
132128
]);
133-
}),
134-
});
129+
},
130+
);
135131
const settings = await getProxySettingsFromSystem({} as Proxy);
136132
expect(settings).toBeDefined();
137133
expect(settings?.httpProxy).toBe('http://127.0.0.1:8888');

0 commit comments

Comments
 (0)