Skip to content

Commit

Permalink
DOP-4152 fixed testing, cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
anabellabuckvar committed Jan 12, 2024
1 parent f117d43 commit c661105
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 31 deletions.
14 changes: 5 additions & 9 deletions src/components/DeprecatedVersionSelector.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { useCallback, useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import queryString from 'query-string';
import { keyBy, isEmpty } from 'lodash';
import Button from '@leafygreen-ui/button';
Expand Down Expand Up @@ -50,7 +49,7 @@ const hasValidHostName = (repoDocument) => {
return true;
};

const DeprecatedVersionSelector = ({ metadata: { deprecated_versions: deprecatedVersions } }) => {
const DeprecatedVersionSelector = () => {
const { reposDatabase } = useSiteMetadata();
const reposBranchesBuildData = useAllDocsets().filter((project) => !!project.hasEolVersions);
const reposBranchesBuildDataMap = keyBy(reposBranchesBuildData, 'project');
Expand Down Expand Up @@ -82,16 +81,15 @@ const DeprecatedVersionSelector = ({ metadata: { deprecated_versions: deprecated
}
}, [reposDatabase]);

//this can be removed? i dont think its used anywhere
useEffect(() => {
if (isBrowser) {
// Extract the value of 'site' query string from the page url to pre-select product
const { site } = queryString.parse(window.location.search);
if (site && Object.keys(deprecatedVersions).includes(site)) {
if (site && Object.keys(reposMap).includes(site)) {
setProduct(site);
}
}
}, [deprecatedVersions]);
}, [reposMap]);

const generateUrl = () => {
// Our current LG button version has a bug where a disabled button with an href allows the disabled
Expand Down Expand Up @@ -136,7 +134,7 @@ const DeprecatedVersionSelector = ({ metadata: { deprecated_versions: deprecated
else return null;
})
.filter((versionChoice) => !!versionChoice)
.sort()
.sort(alphabetize)
: [];

return (
Expand Down Expand Up @@ -171,8 +169,6 @@ const DeprecatedVersionSelector = ({ metadata: { deprecated_versions: deprecated
);
};

DeprecatedVersionSelector.propTypes = {
metadata: PropTypes.object.isRequired,
};
DeprecatedVersionSelector.propTypes = {};

export default DeprecatedVersionSelector;
33 changes: 11 additions & 22 deletions tests/unit/DeprecatedVersionSelector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,6 @@ import userEvent from '@testing-library/user-event';
import DeprecatedVersionSelector from '../../src/components/DeprecatedVersionSelector';
import * as realm from '../../src/utils/realm';

const deprecatedVersions = {
docs: ['v2.2', 'v2.4', 'v2.6', 'v3.0', 'v3.2', 'v3.4'],
mms: ['v1.1', 'v1.2', 'v1.3'],
mongocli: ['v0.5.0'],
'atlas-open-service-broker': ['master'],
};

const metadata = {
deprecated_versions: deprecatedVersions,
};

const mockedReposBranches = [
{
project: 'docs',
Expand Down Expand Up @@ -83,7 +72,7 @@ describe('DeprecatedVersionSelector when rendered', () => {
});

it('shows two dropdowns', async () => {
wrapper = render(<DeprecatedVersionSelector metadata={metadata} />);
wrapper = render(<DeprecatedVersionSelector />);

const productDropdown = await wrapper.findAllByText('Select a Product');
const versionDropdown = await wrapper.findAllByText('Select a Version');
Expand All @@ -93,33 +82,33 @@ describe('DeprecatedVersionSelector when rendered', () => {
});

it('shows a disabled submit button', async () => {
wrapper = render(<DeprecatedVersionSelector metadata={metadata} />);
wrapper = render(<DeprecatedVersionSelector />);

const button = await wrapper.findByTitle('View or Download Documentation');
expect(button).toBeTruthy();
expect(button).toHaveAttribute('aria-disabled', 'true');
});

it('shows a disabled version selector', async () => {
wrapper = render(<DeprecatedVersionSelector metadata={metadata} />);
wrapper = render(<DeprecatedVersionSelector />);

await wrapper.findAllByRole('button');
expect(wrapper.container.querySelectorAll('button')[1]).toHaveAttribute('aria-disabled', 'true');
});

it('does not show either dropdown menu', async () => {
await waitFor(() => {
wrapper = render(<DeprecatedVersionSelector metadata={metadata} />);
wrapper = render(<DeprecatedVersionSelector />);
});

expect(wrapper.queryAllByText('mms')).toHaveLength(0);
expect(wrapper.queryAllByText(deprecatedVersions.mms[0])).toHaveLength(0);
expect(wrapper.queryAllByText('MongoDB Connector for BI')).toHaveLength(0);
expect(wrapper.queryAllByText(mockedReposBranches[0].branches[0].gitBranchName)).toHaveLength(0);
});

// Test product dropdown
describe('when the product button is clicked', () => {
it('shows the dropdown menu with elements per metadata node', () => {
wrapper = render(<DeprecatedVersionSelector metadata={metadata} />);
wrapper = render(<DeprecatedVersionSelector />);

const productDropdown = wrapper.container.querySelectorAll('button')[0];
userEvent.click(productDropdown);
Expand All @@ -130,7 +119,7 @@ describe('DeprecatedVersionSelector when rendered', () => {
});

it('version dropdown text is correct', () => {
wrapper = render(<DeprecatedVersionSelector metadata={metadata} />);
wrapper = render(<DeprecatedVersionSelector />);

const productDropdown = wrapper.container.querySelectorAll('button')[0];
userEvent.click(productDropdown);
Expand All @@ -140,7 +129,7 @@ describe('DeprecatedVersionSelector when rendered', () => {

describe('when the product button is clicked again', () => {
it('hides the dropdown menu', () => {
wrapper = render(<DeprecatedVersionSelector metadata={metadata} />);
wrapper = render(<DeprecatedVersionSelector />);

const productDropdown = wrapper.container.querySelectorAll('button')[0];
userEvent.click(productDropdown);
Expand All @@ -159,7 +148,7 @@ describe('DeprecatedVersionSelector when rendered', () => {
'https://mongodb.com/docs/atlas-open-service-broker/',
],
])('generates the correct docs URL', async (product, versionSelection, expectedUrl) => {
const wrapper = render(<DeprecatedVersionSelector metadata={metadata} />);
const wrapper = render(<DeprecatedVersionSelector />);
const productDropdown = wrapper.container.querySelectorAll('button')[0];
userEvent.click(productDropdown);
const productOption = await wrapper.findByText(product);
Expand All @@ -182,7 +171,7 @@ describe('DeprecatedVersionSelector when rendered', () => {
return Promise.reject();
});

wrapper = render(<DeprecatedVersionSelector metadata={metadata} />);
wrapper = render(<DeprecatedVersionSelector />);

const productDropdown = wrapper.container.querySelectorAll('button')[0];
userEvent.click(productDropdown);
Expand Down

0 comments on commit c661105

Please sign in to comment.