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

DOP-5053: Fix client-side fetch refresh - legacy page #1283

Merged
merged 4 commits into from
Oct 18, 2024
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
2 changes: 1 addition & 1 deletion src/components/DeprecatedVersionSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ const DeprecatedVersionSelector = () => {
resp.filter((project) => project.hasEolVersions),
'project'
);
if (reposBranchesMap.size > 0) setReposMap(reposBranchesMap);
if (!isEmpty(reposBranchesMap)) setReposMap(reposBranchesMap);
})
.catch((error) => {
console.error(`ERROR: could not access ${reposDatabase} for dropdown data.`);
Expand Down
44 changes: 29 additions & 15 deletions tests/unit/DeprecatedVersionSelector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { render, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import DeprecatedVersionSelector from '../../src/components/DeprecatedVersionSelector';
import * as realm from '../../src/utils/realm';
import { useAllDocsets } from '../../src/hooks/useAllDocsets';

const mockedReposBranches = [
{
Expand Down Expand Up @@ -60,7 +61,7 @@ jest.mock('../../src/hooks/use-site-metadata', () => ({
}));

jest.mock('../../src/hooks/useAllDocsets', () => ({
useAllDocsets: () => mockedReposBranches,
useAllDocsets: jest.fn(),
}));

describe('DeprecatedVersionSelector when rendered', () => {
Expand All @@ -70,10 +71,12 @@ describe('DeprecatedVersionSelector when rendered', () => {
mockFetchDocuments = jest.spyOn(realm, 'fetchDocsets').mockImplementation(async (dbName) => {
return mockedReposBranches;
});
useAllDocsets.mockImplementation(() => mockedReposBranches);
});

afterAll(() => {
mockFetchDocuments.mockClear();
useAllDocsets.mockClear();
});

it('shows two dropdowns', async () => {
Expand Down Expand Up @@ -118,8 +121,8 @@ describe('DeprecatedVersionSelector when rendered', () => {
const productDropdown = wrapper.container.querySelectorAll('button')[0];
userEvent.click(productDropdown);

expect(wrapper.findByText('MongoDB Manual')).toBeTruthy();
expect(wrapper.findByText('MongoDB Atlas Open Service Broker on Kubernetes')).toBeTruthy();
expect(wrapper.getByText('MongoDB Manual')).toBeTruthy();
expect(wrapper.getByText('MongoDB Atlas Open Service Broker on Kubernetes')).toBeTruthy();
});

it('version dropdown text is correct', () => {
Expand Down Expand Up @@ -157,6 +160,7 @@ describe('DeprecatedVersionSelector when rendered', () => {
],
])('generates the correct docs URL or download link', async (product, versionSelection, expectedUrl) => {
const wrapper = render(<DeprecatedVersionSelector />);
await waitFor(() => expect(mockFetchDocuments).toBeCalled());
const productDropdown = wrapper.container.querySelectorAll('button')[0];
userEvent.click(productDropdown);
const productOption = await wrapper.findByText(product);
Expand All @@ -173,7 +177,7 @@ describe('DeprecatedVersionSelector when rendered', () => {
});
});

describe('when the selected product has multiple deprecated versions, versions are sorted correctly', () => {
it('when the selected product has multiple deprecated versions, versions are sorted correctly', () => {
const wrapper = render(<DeprecatedVersionSelector />);
const productDropdown = wrapper.container.querySelectorAll('button')[0];
userEvent.click(productDropdown);
Expand Down Expand Up @@ -204,19 +208,29 @@ describe('DeprecatedVersionSelector when rendered', () => {
}
});

describe('if fetching the dropdown data from atlas fails', () => {
it('still populates the dropdown using build-time data', () => {
mockFetchDocuments = jest.spyOn(realm, 'fetchDocsets').mockImplementation(async (dbName) => {
return Promise.reject();
});
it('populates the dropdown using build-time data if atlas fails', () => {
mockFetchDocuments.mockImplementation(async (dbName) => {
return Promise.reject();
});

wrapper = render(<DeprecatedVersionSelector />);
wrapper = render(<DeprecatedVersionSelector />);

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

expect(wrapper.findByText('MongoDB Manual')).toBeTruthy();
expect(wrapper.findByText('MongoDB Atlas Open Service Broker on Kubernetes')).toBeTruthy();
});
expect(wrapper.getByText('MongoDB Manual')).toBeTruthy();
expect(wrapper.getByText('MongoDB Atlas Open Service Broker on Kubernetes')).toBeTruthy();
});

it('client-side fetchDocsets overwrites build-time data', async () => {
useAllDocsets.mockImplementation(() => []);
wrapper = render(<DeprecatedVersionSelector />);
await waitFor(() => expect(mockFetchDocuments).toBeCalled());

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

expect(wrapper.getByText('MongoDB Manual')).toBeTruthy();
expect(wrapper.getByText('MongoDB Atlas Open Service Broker on Kubernetes')).toBeTruthy();
});
});
Loading