Skip to content

Commit

Permalink
Web: make SelectResource.tsx component leaner (#51698)
Browse files Browse the repository at this point in the history
* Web: Move resource specs into resource directory

* Web: move code out into separate files
  • Loading branch information
kimlisa authored Feb 1, 2025
1 parent 4d4a406 commit 2004893
Show file tree
Hide file tree
Showing 13 changed files with 660 additions and 537 deletions.
4 changes: 1 addition & 3 deletions web/packages/teleport/src/Discover/Discover.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ import cfg from 'teleport/config';
import { Discover, DiscoverComponent } from 'teleport/Discover/Discover';
import { ResourceViewConfig } from 'teleport/Discover/flow';
import {
APPLICATIONS,
DATABASES,
DATABASES_UNGUIDED,
DATABASES_UNGUIDED_DOC,
} from 'teleport/Discover/SelectResource/databases';
import {
APPLICATIONS,
KUBERNETES,
SERVERS,
} from 'teleport/Discover/SelectResource/resources';
Expand Down
2 changes: 1 addition & 1 deletion web/packages/teleport/src/Discover/Fixtures/databases.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
IntegrationStatusCode,
} from 'teleport/services/integrations';

import { DATABASES } from '../SelectResource/databases';
import { DATABASES } from '../SelectResource/resources';
import { ResourceKind } from '../Shared';
import { TeleportProvider } from './fixtures';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,10 @@ import * as userUserContext from 'teleport/User/UserContext';

import { ResourceKind } from '../Shared';
import { resourceKindToPreferredResource } from '../Shared/ResourceKind';
import {
filterResources,
SelectResource,
sortResources,
} from './SelectResource';
import { SelectResource } from './SelectResource';
import { ResourceSpec } from './types';
import { filterBySupportedPlatformsAndAuthTypes } from './utils/filters';
import { sortResourcesByPreferences } from './utils/sort';

const setUp = () => {
jest
Expand Down Expand Up @@ -85,7 +83,7 @@ const onboardDiscoverNoResources: OnboardDiscover = {
hasVisited: false,
};

test('sortResources without preferred resources, sorts resources alphabetically with guided resources first', () => {
test('sortResourcesByPreferences without preferred resources, sorts resources alphabetically with guided resources first', () => {
setUp();
const mockIn: ResourceSpec[] = [
// unguided
Expand All @@ -99,7 +97,7 @@ test('sortResources without preferred resources, sorts resources alphabetically
makeResourceSpec({ name: 'costco' }),
];

const actual = sortResources(
const actual = sortResourcesByPreferences(
mockIn,
makeDefaultUserPreferences(),
onboardDiscoverWithResources
Expand Down Expand Up @@ -358,7 +356,7 @@ describe('preferred resources', () => {
test.each(testCases)('$name', testCase => {
const preferences = makeDefaultUserPreferences();
preferences.onboard.preferredResources = testCase.preferred;
const actual = sortResources(
const actual = sortResourcesByPreferences(
kindBasedList,
preferences,
onboardDiscoverWithResources
Expand Down Expand Up @@ -563,7 +561,7 @@ describe('marketing params', () => {
test.each(testCases)('$name', testCase => {
const preferences = makeDefaultUserPreferences();
preferences.onboard = testCase.preferred;
const actual = sortResources(
const actual = sortResourcesByPreferences(
kindBasedList,
preferences,
onboardDiscoverWithResources
Expand Down Expand Up @@ -707,7 +705,7 @@ describe('os sorted resources', () => {
test.each(testCases)('$name', testCase => {
OS.mockReturnValue(testCase.userAgent);

const actual = sortResources(
const actual = sortResourcesByPreferences(
osBasedList,
makeDefaultUserPreferences(),
onboardDiscoverWithResources
Expand All @@ -726,7 +724,7 @@ describe('os sorted resources', () => {
];
OS.mockReturnValue(UserAgent.macOS);

const actual = sortResources(
const actual = sortResourcesByPreferences(
mockIn,
makeDefaultUserPreferences(),
onboardDiscoverWithResources
Expand Down Expand Up @@ -773,7 +771,7 @@ describe('os sorted resources', () => {
},
};

const actual = sortResources(
const actual = sortResourcesByPreferences(
oneOfEachList,
preferences,
onboardDiscoverWithResources
Expand Down Expand Up @@ -853,7 +851,7 @@ describe('sorting Connect My Computer', () => {
it('puts the Connect My Computer resource as the first resource if the user has no preferences', () => {
OS.mockReturnValue(UserAgent.macOS);

const actual = sortResources(
const actual = sortResourcesByPreferences(
oneOfEachList,
makeDefaultUserPreferences(),
onboardDiscoverNoResources
Expand Down Expand Up @@ -892,7 +890,7 @@ describe('sorting Connect My Computer', () => {
},
};

const actual = sortResources(
const actual = sortResourcesByPreferences(
oneOfEachList,
preferences,
onboardDiscoverNoResources
Expand Down Expand Up @@ -935,7 +933,7 @@ describe('sorting Connect My Computer', () => {
platform: Platform.Linux,
});

const actual = sortResources(
const actual = sortResourcesByPreferences(
[
unguidedA,
guidedServerForMatchingPlatformB,
Expand Down Expand Up @@ -988,7 +986,7 @@ describe('sorting Connect My Computer', () => {
},
};

const actual = sortResources(
const actual = sortResourcesByPreferences(
[
unguidedA,
guidedServerForMatchingPlatformB,
Expand All @@ -1014,7 +1012,7 @@ describe('sorting Connect My Computer', () => {
it('puts the Connect My Computer resource as the last guided resource if the user has resources', () => {
OS.mockReturnValue(UserAgent.macOS);

const actual = sortResources(
const actual = sortResourcesByPreferences(
oneOfEachList,
makeDefaultUserPreferences(),
onboardDiscoverWithResources
Expand Down Expand Up @@ -1053,7 +1051,7 @@ describe('sorting Connect My Computer', () => {
},
};

const actual = sortResources(
const actual = sortResourcesByPreferences(
oneOfEachList,
preferences,
onboardDiscoverWithResources
Expand Down Expand Up @@ -1099,7 +1097,7 @@ describe('sorting Connect My Computer', () => {
},
};

const actual = sortResources(
const actual = sortResourcesByPreferences(
[...oneOfEachList, databaseForAnotherPlatform],
preferences,
onboardDiscoverNoResources
Expand Down Expand Up @@ -1195,12 +1193,11 @@ describe('filterResources', () => {
supportedPlatforms: [Platform.macOS],
});

const result = filterResources(Platform.macOS, 'local', [
winAndLinux,
win,
macosAndLinux,
macos,
]);
const result = filterBySupportedPlatformsAndAuthTypes(
Platform.macOS,
'local',
[winAndLinux, win, macosAndLinux, macos]
);

expect(result).toContain(macosAndLinux);
expect(result).toContain(macos);
Expand All @@ -1209,24 +1206,28 @@ describe('filterResources', () => {
});

it('does not filter out resources with supportedPlatforms and supportedAuthTypes that are missing or empty', () => {
const result = filterResources(Platform.macOS, 'local', [
makeResourceSpec({
name: 'Empty supportedPlatforms',
supportedPlatforms: [],
}),
makeResourceSpec({
name: 'Missing supportedPlatforms',
supportedPlatforms: undefined,
}),
makeResourceSpec({
name: 'Empty supportedAuthTypes',
supportedAuthTypes: [],
}),
makeResourceSpec({
name: 'Missing supportedAuthTypes',
supportedAuthTypes: undefined,
}),
]);
const result = filterBySupportedPlatformsAndAuthTypes(
Platform.macOS,
'local',
[
makeResourceSpec({
name: 'Empty supportedPlatforms',
supportedPlatforms: [],
}),
makeResourceSpec({
name: 'Missing supportedPlatforms',
supportedPlatforms: undefined,
}),
makeResourceSpec({
name: 'Empty supportedAuthTypes',
supportedAuthTypes: [],
}),
makeResourceSpec({
name: 'Missing supportedAuthTypes',
supportedAuthTypes: undefined,
}),
]
);

expect(result).toHaveLength(4);
});
Expand All @@ -1249,12 +1250,11 @@ describe('filterResources', () => {
supportedAuthTypes: ['local'],
});

const result = filterResources(Platform.macOS, 'local', [
ssoAndPasswordless,
sso,
localAndPasswordless,
local,
]);
const result = filterBySupportedPlatformsAndAuthTypes(
Platform.macOS,
'local',
[ssoAndPasswordless, sso, localAndPasswordless, local]
);

expect(result).toContain(localAndPasswordless);
expect(result).toContain(local);
Expand Down
Loading

0 comments on commit 2004893

Please sign in to comment.