Skip to content

Commit

Permalink
chore: add a test file for projectSelection
Browse files Browse the repository at this point in the history
  • Loading branch information
lilbitner committed May 6, 2024
1 parent 53ee986 commit 39cb9c5
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { screen, waitFor } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';

import { AppInstallationParameters } from '@customTypes/configPage';
import { ProjectSelectionSection } from './ProjectSelectionSection';
import { copies } from '@constants/copies';
import { renderConfigPageComponent } from '@test/helpers/renderConfigPageComponent';
import userEvent from '@testing-library/user-event';
import * as fetchData from '@hooks/useFetchData/useFetchData';

const projects = [
{ id: 'project-1', name: 'Project 1', targets: { production: { id: 'project-1' } }, env: [] },
];

describe('ProjectSelectionSection', () => {
it('renders dropdown when paths are present and no errors are present', () => {
const { unmount } = renderConfigPageComponent(<ProjectSelectionSection projects={projects} />);

const select = screen.getByTestId('optionsSelect');
expect(select).toBeTruthy();
unmount();
});

it('renders no selected value in dropdown when projectNotFound error', () => {
const mockDispatchParameters = vi.fn();
const parameters = {
dispatchParameters: mockDispatchParameters,
} as unknown as AppInstallationParameters;
const { unmount } = renderConfigPageComponent(
<ProjectSelectionSection projects={projects} />,
parameters
);

const emptyInput = screen.getByText(copies.configPage.projectSelectionSection.placeholder);
expect(emptyInput).toBeTruthy();
unmount();
});

it('handles project selection', async () => {
const mockValidation = vi.fn().mockImplementationOnce(() => Promise.resolve());
vi.spyOn(fetchData, 'useFetchData').mockReturnValue({
validateProjectEnv: mockValidation,
validateToken: vi.fn(),
fetchProjects: vi.fn(),
fetchApiPaths: vi.fn(),
});
const user = userEvent.setup();
const mockHandleAppConfigurationChange = vi.fn();
const mockDispatchParameters = vi.fn();
const parameters = {
dispatchParameters: mockDispatchParameters,
handleAppConfigurationChange: mockHandleAppConfigurationChange,
} as unknown as AppInstallationParameters;
const projects = [
{ id: 'project-1', name: 'Project 1', targets: { production: { id: 'project-1' } }, env: [] },
];
const { unmount } = renderConfigPageComponent(
<ProjectSelectionSection projects={projects} />,
parameters
);

const selectDropdown = screen.getByTestId('optionsSelect');

user.selectOptions(selectDropdown, projects[0].name);

await waitFor(() => expect(mockHandleAppConfigurationChange).toBeCalled());
await waitFor(() => expect(mockDispatchParameters).toBeCalled());
await waitFor(() => expect(mockValidation).toBeCalled());
unmount();
});
});
1 change: 1 addition & 0 deletions apps/vercel/frontend/test/mocks/mockSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const mockSdk: any = {
},
ids: {
app: 'test-app',
space: '1234',
},
cma: {
contentType: {
Expand Down

0 comments on commit 39cb9c5

Please sign in to comment.