Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ui] Refactor RunActionsMenu.test #17243

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ import {RunTableRunFragment} from './types/RunTable.types';
import {useJobAvailabilityErrorForRun} from './useJobAvailabilityErrorForRun';
import {useJobReexecution} from './useJobReExecution';

export const RunActionsMenu: React.FC<{
interface Props {
run: RunTableRunFragment;
additionalActionsForRun?: (run: RunTableRunFragment) => React.ReactNode[];
onAddTag?: (token: RunFilterToken) => void;
}> = React.memo(({run, onAddTag, additionalActionsForRun}) => {
}

export const RunActionsMenu = React.memo(({run, onAddTag, additionalActionsForRun}: Props) => {
const {refetch} = React.useContext(RunsQueryRefetchContext);
const [visibleDialog, setVisibleDialog] = React.useState<
'none' | 'terminate' | 'delete' | 'config' | 'tags'
Expand Down Expand Up @@ -467,7 +469,7 @@ const OPEN_LAUNCHPAD_UNKNOWN =
'Launchpad is unavailable because the pipeline is not present in the current repository.';

// Avoid fetching envYaml and parentPipelineSnapshotId on load in Runs page, they're slow.
const PIPELINE_ENVIRONMENT_QUERY = gql`
export const PIPELINE_ENVIRONMENT_QUERY = gql`
query PipelineEnvironmentQuery($runId: ID!) {
pipelineRunOrError(runId: $runId) {
... on Run {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import {MockedResponse} from '@apollo/client/testing';

import {
RunStatus,
buildPipeline,
buildRepository,
buildRepositoryLocation,
buildRepositoryOrigin,
buildRun,
buildWorkspace,
buildWorkspaceLocationEntry,
} from '../../graphql/types';
import {ROOT_WORKSPACE_QUERY} from '../../workspace/WorkspaceContext';
import {RootWorkspaceQuery} from '../../workspace/types/WorkspaceContext.types';
import {PIPELINE_ENVIRONMENT_QUERY} from '../RunActionsMenu';
import {
PipelineEnvironmentQuery,
PipelineEnvironmentQueryVariables,
} from '../types/RunActionsMenu.types';

const LOCATION_NAME = 'my-origin';
const REPO_NAME = 'my-repo';
const JOB_NAME = 'job-bar';
const RUN_ID = 'run-foo-bar';
const SNAPSHOT_ID = 'snapshotID';

type RunConfigInput = {
hasReExecutePermission: boolean;
};

export const buildRunActionsMenuFragment = ({hasReExecutePermission}: RunConfigInput) => {
return buildRun({
id: RUN_ID,
status: RunStatus.SUCCESS,
stepKeysToExecute: null,
canTerminate: true,
hasDeletePermission: true,
hasReExecutePermission,
hasTerminatePermission: true,
mode: 'default',
rootRunId: 'abcdef12',
parentRunId: null,
pipelineSnapshotId: SNAPSHOT_ID,
pipelineName: JOB_NAME,
repositoryOrigin: buildRepositoryOrigin({
id: 'repo',
repositoryName: REPO_NAME,
repositoryLocationName: LOCATION_NAME,
}),
solidSelection: null,
assetSelection: null,
assetCheckSelection: null,
tags: [],
startTime: 123,
endTime: 456,
updateTime: 789,
});
};

export const buildMockRootWorkspaceQuery = (): MockedResponse<RootWorkspaceQuery> => {
return {
request: {
query: ROOT_WORKSPACE_QUERY,
},
result: {
data: {
__typename: 'Query',
workspaceOrError: buildWorkspace({
id: 'workspace',
locationEntries: [
buildWorkspaceLocationEntry({
id: LOCATION_NAME,
locationOrLoadError: buildRepositoryLocation({
id: LOCATION_NAME,
repositories: [
buildRepository({
id: REPO_NAME,
name: REPO_NAME,
pipelines: [
buildPipeline({
id: JOB_NAME,
name: JOB_NAME,
pipelineSnapshotId: SNAPSHOT_ID,
}),
],
}),
],
}),
}),
],
}),
},
},
};
};

export const buildPipelineEnvironmentQuery = (
runConfig: RunConfigInput,
): MockedResponse<PipelineEnvironmentQuery, PipelineEnvironmentQueryVariables> => {
return {
request: {
query: PIPELINE_ENVIRONMENT_QUERY,
variables: {
runId: RUN_ID,
},
},
result: {
data: {
__typename: 'Query',
pipelineRunOrError: buildRunActionsMenuFragment(runConfig),
},
},
};
};
Original file line number Diff line number Diff line change
@@ -1,56 +1,34 @@
import {MockedProvider} from '@apollo/client/testing';
import {render, screen} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import * as React from 'react';
import {MemoryRouter} from 'react-router-dom';

import {RunStatus} from '../../graphql/types';
import {TestProvider} from '../../testing/TestProvider';
import {WorkspaceProvider} from '../../workspace/WorkspaceContext';
import {RunActionsMenu} from '../RunActionsMenu';
import {RunTableRunFragment} from '../types/RunTable.types';
import {
buildMockRootWorkspaceQuery,
buildPipelineEnvironmentQuery,
buildRunActionsMenuFragment,
} from '../__fixtures__/RunActionsMenu.fixtures';

describe('RunActionsMenu', () => {
const Test: React.FC<{permissionOverrides?: any; run: RunTableRunFragment}> = ({
permissionOverrides,
run,
}) => {
return (
<TestProvider permissionOverrides={permissionOverrides}>
<RunActionsMenu run={run} />
</TestProvider>
);
};

const runFragment: RunTableRunFragment = {
__typename: 'Run',
id: 'run-foo-bar',
status: RunStatus.SUCCESS,
stepKeysToExecute: null,
canTerminate: true,
hasDeletePermission: true,
hasReExecutePermission: true,
hasTerminatePermission: true,
mode: 'default',
rootRunId: 'abcdef12',
parentRunId: null,
pipelineSnapshotId: 'snapshotID',
pipelineName: 'job-bar',
repositoryOrigin: {
__typename: 'RepositoryOrigin',
id: 'repo',
repositoryName: 'my-repo',
repositoryLocationName: 'my-origin',
},
solidSelection: null,
assetSelection: null,
assetCheckSelection: null,
tags: [],
startTime: 123,
endTime: 456,
updateTime: 789,
};

describe('Permissions', () => {
it('renders menu when open', async () => {
render(<Test run={runFragment} />);
render(
<MockedProvider
mocks={[
buildMockRootWorkspaceQuery(),
buildPipelineEnvironmentQuery({hasReExecutePermission: true}),
]}
>
<MemoryRouter>
<WorkspaceProvider>
<RunActionsMenu run={buildRunActionsMenuFragment({hasReExecutePermission: true})} />
</WorkspaceProvider>
</MemoryRouter>
</MockedProvider>,
);

const button = await screen.findByRole('button');
expect(button).toBeVisible();
Expand All @@ -66,12 +44,18 @@ describe('RunActionsMenu', () => {

it('disables re-execution if no permission', async () => {
render(
<Test
run={runFragment}
permissionOverrides={{
launch_pipeline_reexecution: {enabled: false, disabledReason: 'lol nope'},
}}
/>,
<MockedProvider
mocks={[
buildMockRootWorkspaceQuery(),
buildPipelineEnvironmentQuery({hasReExecutePermission: false}),
]}
>
<MemoryRouter>
<WorkspaceProvider>
<RunActionsMenu run={buildRunActionsMenuFragment({hasReExecutePermission: false})} />
</WorkspaceProvider>
</MemoryRouter>
</MockedProvider>,
);

const button = await screen.findByRole('button');
Expand Down