Skip to content

Commit

Permalink
feat: [M3-6968] ad unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
abailly-akamai committed Aug 22, 2023
1 parent d5f5403 commit f9a8f54
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { styled } from '@mui/material/styles';

import { Box } from 'src/components/Box';
import { FormLabel } from 'src/components/FormLabel';
import { Paper } from 'src/components/Paper';

export const StyledPaper = styled(Paper, { label: 'StyledPaper' })(
Expand All @@ -25,10 +24,15 @@ export const StyledDiv = styled('div', { label: 'StyledDiv' })(({ theme }) => ({
marginBottom: theme.spacing(2),
}));

export const StyledFormLabel = styled(FormLabel, {
label: 'StyledFormLabel',
export const StyledSpan = styled('span', {
label: 'StyledSpan',
})(({ theme }) => ({
color: theme.color.label,
display: 'block',
fontFamily: theme.font.bold,
fontSize: theme.typography.body1.fontSize,
lineHeight: '1.43rem',
marginBottom: theme.spacing(1),
marginTop: theme.spacing(2),
}));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { fireEvent, waitFor } from '@testing-library/react';
import React from 'react';

import { renderWithTheme, wrapWithTheme } from 'src/utilities/testHelpers';

import { ConfigureForm } from './ConfigureForm';

const regionData = [
{ country: 'ca', id: 'ca-central', label: 'Canada Central' },
{ country: 'us', id: 'us-west', label: 'US West' },
{ country: 'us', id: 'us-east', label: 'US East' },
{ country: 'br', id: 'br-gru', label: 'São Paulo' },
];

// Mock the useFlags hook
jest.mock('src/hooks/useFlags', () => ({
useFlags: () => ({
dcSpecificPricing: true, // Mock the flag value
}),
}));

// Mock the useRegionsQuery hook
jest.mock('src/queries/regions', () => ({
useRegionsQuery: () => ({
data: regionData,
}),
}));

// Mock the useTypeQuery hook
jest.mock('src/queries/types', () => ({
useTypeQuery: () => ({
data: {
addons: { backups: { price: { hourly: 0.004, monthly: 2.5 } } },
class: 'standard',
disk: 51200,
gpus: 0,
id: 'g6-standard-1',
label: 'Linode 2GB',
memory: 2048,
network_out: 2000,
price: { hourly: 0.018, monthly: 12.0 },
successor: null,
transfer: 2000,
vcpus: 1,
},
}),
}));

describe('ConfigureForm component with price comparison', () => {
const handleSelectRegion = jest.fn();
const currentPriceLabel = 'Current Price';
const newPriceLabel = 'New Price';

const props = {
currentRegion: 'ca-central',
handleSelectRegion,
linodeType: 'standard',
selectedRegion: '',
};

const { getByLabelText, getByText, queryByText } = renderWithTheme(
<ConfigureForm {...props} />
);

const selectNewRegion = (regionLabel: string, regionId: string) => {
const { getByLabelText, getByText, rerender } = renderWithTheme(
<ConfigureForm {...props} />
);
const regionSelect = getByLabelText('New Region');

fireEvent.focus(regionSelect);
fireEvent.keyDown(regionSelect, {
code: 40,
key: 'ArrowDown',
});
fireEvent.click(getByText(`${regionLabel} (${regionId})`));

expect(handleSelectRegion).toHaveBeenCalled();
rerender(
wrapWithTheme(<ConfigureForm {...props} selectedRegion={regionId} />)
);
};

it('should render the initial ConfigureForm fields', () => {
// Test whether the initial component renders the expected content
expect(getByText('Configure Migration')).toBeInTheDocument();
expect(getByText('Current Region')).toBeInTheDocument();

// Verify that the RegionSelect component is rendered
const regionSelect = getByLabelText('New Region');
expect(regionSelect).toBeInTheDocument();

// Verify that the MigrationPricing component content is not rendered on page load
expect(queryByText(currentPriceLabel)).not.toBeInTheDocument();
expect(queryByText(newPriceLabel)).not.toBeInTheDocument();
});

it("shouldn't render the MigrationPricing component when the current region is selected", async () => {
selectNewRegion('US East', 'us-east');
await waitFor(() => {
expect(queryByText(currentPriceLabel)).not.toBeInTheDocument();
expect(queryByText(newPriceLabel)).not.toBeInTheDocument();
});
});

it("shouldn't render the MigrationPricing component when a region without price increase is selected", async () => {
selectNewRegion('US West', 'us-west');
await waitFor(() => {
expect(queryByText(currentPriceLabel)).not.toBeInTheDocument();
expect(queryByText(newPriceLabel)).not.toBeInTheDocument();
});
});

it('should render the MigrationPricing component when a region with price increase is selected', async () => {
selectNewRegion('São Paulo', 'br-gru');
await waitFor(() => {
expect(queryByText(currentPriceLabel)).toBeInTheDocument();
expect(queryByText(newPriceLabel)).toBeInTheDocument();
});
});

it("shouldn't render the MigrationPricingComponent if the flag is disabled", () => {
jest.isolateModules(async () => {
jest.mock('src/hooks/useFlags', () => ({
useFlags: () => ({
dcSpecificPricing: false,
}),
}));

selectNewRegion('São Paulo', 'br-gru');
await waitFor(() => {
expect(queryByText('Current Price')).not.toBeInTheDocument();
expect(queryByText('New Price')).not.toBeInTheDocument();
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import {

import {
StyledDiv,
StyledFormLabel,
StyledMigrationBox,
StyledMigrationContainer,
StyledPaper,
StyledSpan,
} from './ConfigureForm.styles';
import { MigrationPricing } from './MigrationPricing';

Expand Down Expand Up @@ -93,14 +93,10 @@ export const ConfigureForm = React.memo((props: Props) => {
<Typography variant="h3">Configure Migration</Typography>
<StyledMigrationContainer>
<StyledMigrationBox>
<StyledFormLabel htmlFor={`current-region-${currentRegion}`}>
Current Region
</StyledFormLabel>
<StyledSpan>Current Region</StyledSpan>
<StyledDiv>
<Flag country={country as Lowercase<Country>} />
<Typography
id={`current-region-${currentRegion}`}
>{`${getRegionCountryGroup(currentActualRegion)}: ${
<Typography>{`${getRegionCountryGroup(currentActualRegion)}: ${
currentActualRegion?.label ?? currentRegion
}`}</Typography>
</StyledDiv>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Box } from 'src/components/Box';
import { DisplayPrice } from 'src/components/DisplayPrice';
import { Typography } from 'src/components/Typography';

import { StyledFormLabel } from './ConfigureForm.styles';
import { StyledSpan } from './ConfigureForm.styles';

export interface MigrationPricingProps {
backups: null | number | undefined;
Expand All @@ -24,9 +24,7 @@ export const MigrationPricing = (props: MigrationPricingProps) => {

return monthly && hourly && backups ? (
<StyledMigrationPricingContainer panelType={panelType}>
<StyledFormLabel htmlFor={`${panelType}-price`}>
{currentPanel ? 'Current' : 'New'} Price
</StyledFormLabel>
<StyledSpan>{currentPanel ? 'Current' : 'New'} Price</StyledSpan>
<Box
alignItems="baseline"
display="inline-flex"
Expand Down Expand Up @@ -61,7 +59,7 @@ const StyledMigrationPricingContainer = styled(Box, {
marginBottom: theme.spacing(2),
...(panelType === 'current' && {
[theme.breakpoints.up('md')]: {
marginTop: 26,
marginTop: 24,
},
}),
})
Expand Down

0 comments on commit f9a8f54

Please sign in to comment.