Skip to content

Commit

Permalink
fix: define @podman-desktop/api mock implementation in __mocks__ (#149)
Browse files Browse the repository at this point in the history
Related to #56.

Signed-off-by: Denis Golovin <[email protected]>
  • Loading branch information
dgolovin authored Jun 3, 2024
1 parent 5db331c commit b76eb17
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 116 deletions.
54 changes: 53 additions & 1 deletion __mocks__/@podman-desktop/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,57 @@
* Mock the extension API for vitest.
* This file is referenced from vitest.config.js file.
*/
const plugin = {};
const plugin = {
EventEmitter: vi.fn().mockImplementation(() => {
return {
fire: vi.fn(),
};
}),
registry: {
suggestRegistry: vi.fn(),
unregisterRegistry: vi.fn(),
},
authentication: {
registerAuthenticationProvider: vi.fn(),
getSession: vi.fn(),
onDidChangeSessions: vi.fn(),
},
commands: {
registerCommand: vi.fn(),
executeCommand: vi.fn(),
},
window: {
createStatusBarItem: () => ({
show: vi.fn(),
iconClass: '',
}),
withProgress: (options, callback) => {
return callback({
report: () => {},
});
},
showInformationMessage: vi.fn(),
},
env: {
createTelemetryLogger: vi.fn().mockImplementation(() => ({
logUsage: vi.fn(),
logError: vi.fn(),
})),
},
StatusBarAlignLeft: 'LEFT',
ProgressLocation: {
TASK_WIDGET: 2,
},
process: {
exec: vi.fn(),
},
configuration: {
getConfiguration: () => {
return {
get: vi.fn(),
};
},
},
};

module.exports = plugin;
21 changes: 1 addition & 20 deletions src/authentication-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,7 @@ import { beforeEach, expect, test, vi } from 'vitest';
import { convertToSession, RedHatAuthenticationService } from './authentication-service';
import { getAuthConfig } from './configuration';

vi.mock('@podman-desktop/api', async () => {
return {
EventEmitter: vi.fn().mockImplementation(() => {
return {
fire: vi.fn(),
};
}),
registry: {
suggestRegistry: vi.fn(),
},
authentication: {
registerAuthenticationProvider: vi.fn(),
onDidChangeSessions: vi.fn(),
getSession: vi.fn(),
},
commands: {
registerCommand: vi.fn(),
},
};
});
vi.mock('@podman-desktop/api');

beforeEach(() => {
vi.restoreAllMocks();
Expand Down
69 changes: 2 additions & 67 deletions src/extension.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/explicit-function-return-type */

import type {
AuthenticationGetSessionOptions,
AuthenticationSession,
ExtensionContext,
ProgressLocation,
TelemetryLogger,
} from '@podman-desktop/api';
import type { AuthenticationGetSessionOptions, AuthenticationSession, ExtensionContext } from '@podman-desktop/api';
import { authentication, commands } from '@podman-desktop/api';
import { OrganizationService } from '@redhat-developer/rhsm-client';
import { afterEach, beforeEach, expect, suite, test, vi } from 'vitest';
Expand All @@ -35,66 +29,7 @@ import * as subscription from './subscription';
import { ExtensionTelemetryLogger } from './telemetry';
import * as util from './util';

vi.mock('@podman-desktop/api', async () => {
return {
EventEmitter: vi.fn().mockImplementation(() => {
return {
fire: vi.fn(),
};
}),
registry: {
suggestRegistry: vi.fn(),
unregisterRegistry: vi.fn(),
},
authentication: {
registerAuthenticationProvider: vi.fn(),
getSession: vi.fn(),
onDidChangeSessions: vi.fn(),
},
commands: {
registerCommand: vi.fn(),
executeCommand: vi.fn(),
},
window: {
createStatusBarItem: () => ({
show: vi.fn(),
iconClass: '',
}),
withProgress: (
options: { location: ProgressLocation; title: string },
callback: (progress: { report: (m: string) => void }) => void,
) => {
return callback({
report: () => {},
});
},
showInformationMessage: vi.fn(),
},
env: {
createTelemetryLogger: vi.fn().mockImplementation(
() =>
({
logUsage: vi.fn(),
logError: vi.fn(),
}) as unknown as TelemetryLogger,
),
},
StatusBarAlignLeft: 'LEFT',
ProgressLocation: {
TASK_WIDGET: 2,
},
process: {
exec: vi.fn(),
},
configuration: {
getConfiguration: () => {
return {
get: vi.fn(),
};
},
},
};
});
vi.mock('@podman-desktop/api');

function createExtContext(): ExtensionContext {
return {
Expand Down
28 changes: 1 addition & 27 deletions src/podman-cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/explicit-function-return-type */

import type { TelemetryLogger } from '@podman-desktop/api';
import { process as podmanProcess } from '@podman-desktop/api';
import { beforeEach, expect, test, vi } from 'vitest';

Expand All @@ -35,32 +34,7 @@ import {
} from './podman-cli';
import { ExtensionTelemetryLogger } from './telemetry';

vi.mock('@podman-desktop/api', async () => {
return {
env: {
createTelemetryLogger: vi.fn().mockImplementation(
() =>
({
logUsage: vi.fn(),
logError: vi.fn(),
}) as unknown as TelemetryLogger,
),
},
ProgressLocation: {
TASK_WIDGET: 2,
},
process: {
exec: vi.fn(),
},
configuration: {
getConfiguration: () => {
return {
get: vi.fn(),
};
},
},
};
});
vi.mock('@podman-desktop/api');

const runResult = { command: 'command line', stdout: 'stdout output', stderr: 'stderr output' };
const runError = {
Expand Down
2 changes: 1 addition & 1 deletion vitest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const config = {
},
resolve: {
alias: {
'@podman-desktop/api': path.resolve(PACKAGE_ROOT, '__mocks__/@podman-desktop/api.js'),
'@podman-desktop/api': path.resolve(PACKAGE_ROOT, '__mocks__/@podman-desktop/api.ts'),
},
},
};
Expand Down

0 comments on commit b76eb17

Please sign in to comment.