Skip to content

Commit

Permalink
refactor: Split out treeViewUtils (#119)
Browse files Browse the repository at this point in the history
Split out some server utils into `treeViewUtils`. Should be no actual
code changes, just refactor to make upcoming PR diffs cleaner.
  • Loading branch information
bmingles authored Sep 6, 2024
1 parent 7f312dc commit e81329e
Show file tree
Hide file tree
Showing 6 changed files with 380 additions and 372 deletions.
1 change: 1 addition & 0 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from './panelUtils';
export * from './polyfillUtils';
export * from './selectionUtils';
export * from './serverUtils';
export * from './treeViewUtils';
export * from './Toaster';
export * from './uiUtils';
export * from './urlUtils';
160 changes: 1 addition & 159 deletions src/util/serverUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@ import { describe, it, expect, vi } from 'vitest';
import {
getInitialServerStates,
getPipServerUrl,
getServerContextValue,
getServerDescription,
getServerGroupContextValue,
getServerGroupTreeItem,
getServerIconID,
getServerTreeItem,
groupServers,
parsePort,
} from './serverUtils';
import type { Port, ServerConnectionConfig, ServerState } from '../types';
import type { Port, ServerConnectionConfig } from '../types';

// See __mocks__/vscode.ts for the mock implementation
vi.mock('vscode');
Expand Down Expand Up @@ -53,157 +46,6 @@ describe('getPipServerUrl', () => {
});
});

describe('getServerContextValue', () => {
it.each([
[true, true, true],
[true, true, false],
[true, false, true],
[true, false, false],
[false, true, true],
[false, true, false],
[false, false, true],
[false, false, false],
])(
'should return contextValue based on server state: isConnected=%s, isManaged=%s, isRunning=%s',
(isConnected, isManaged, isRunning) => {
const actual = getServerContextValue({
isConnected,
isManaged,
isRunning,
});
expect(actual).toMatchSnapshot();
}
);
});

describe('getServerDescription', () => {
it.each([
[0, true, 'some label'],
[1, true, 'some label'],
[0, false, 'some label'],
[1, false, 'some label'],
[0, true, undefined],
[1, true, undefined],
[0, false, undefined],
[1, false, undefined],
])(
'should return server description based on parameters: connectionCount=%s, isManaged=%s, label=%s',
(connectionCount, isManaged, label) => {
const actual = getServerDescription(connectionCount, isManaged, label);
expect(actual).toMatchSnapshot();
}
);
});

describe('getServerGroupContextValue', () => {
it.each([
['Managed', true],
['Running', true],
['Managed', false],
['Running', false],
] as const)(
'should return context value when servers can be managed: group=%s, canStartServer=%s',
(group, canStartServer) => {
const actual = getServerGroupContextValue(group, canStartServer);
expect(actual).toMatchSnapshot();
}
);
});

describe('getServerGroupTreeItem', () => {
it.each([
['Managed', true],
['Running', true],
['Managed', false],
['Running', false],
] as const)(
'should return server group tree item: group=%s, canStartServer=%s',
(group, canStartServer) => {
const actual = getServerGroupTreeItem(group, canStartServer);
expect(actual).toMatchSnapshot();
}
);
});

describe('getServerIconID', () => {
it.each([
[true, true, true],
[true, true, false],
[true, false, true],
[true, false, false],
[false, true, true],
[false, true, false],
[false, false, true],
[false, false, false],
])(
'should return icon id based on server state: isConnected=%s, isManaged=%s, isRunning=%s',
(isConnected, isManaged, isRunning) => {
const actual = getServerIconID({ isConnected, isManaged, isRunning });
expect(actual).toMatchSnapshot();
}
);
});

describe('getServerTreeItem', () => {
const dhcServerState: ServerState = {
type: 'DHC',
url: new URL('http://localhost:10000'),
};

it.each([
[true, true, true],
[true, true, false],
[true, false, true],
[true, false, false],
[false, true, true],
[false, true, false],
[false, false, true],
[false, false, false],
])(
'should return DHC server tree item: isConnected=%s, isManaged=%s, isRunning=%s',
(isConnected, isManaged, isRunning) => {
const actual = getServerTreeItem({
server: dhcServerState,
isConnected,
isManaged,
isRunning,
});

expect(actual).toMatchSnapshot();
}
);
});

describe('groupServers', () => {
it('should group servers by state', () => {
const props = [
[true, true],
[true, true],
[true, false],
[true, false],
[false, true],
[false, true],
[false, false],
[false, false],
];

const servers = props.map(
([isManaged, isRunning], i) =>
({
type: 'DHC' as const,
url: new URL(`http://localhost:1000${i}`),
isManaged,
isRunning,
psk: isManaged ? 'mock.psk' : undefined,
}) as ServerState
);

const actual = groupServers(servers);

expect(actual).toMatchSnapshot();
});
});

describe('parsePort', () => {
it('should parse port from string', () => {
const given = '10000';
Expand Down
Loading

0 comments on commit e81329e

Please sign in to comment.