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] Add tests for recently visited assets #20321

Merged
merged 4 commits into from
Mar 7, 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
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const AssetsCatalogRoot = () => {
// eslint-disable-next-line import/no-default-export
export default AssetsCatalogRoot;

const ASSETS_CATALOG_ROOT_QUERY = gql`
export const ASSETS_CATALOG_ROOT_QUERY = gql`
query AssetsCatalogRootQuery($assetKey: AssetKeyInput!) {
assetOrError(assetKey: $assetKey) {
... on Asset {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import {MockedProvider} from '@apollo/client/testing';
import {render, screen, waitFor} from '@testing-library/react';
import Router, {MemoryRouter} from 'react-router-dom';

import {AnalyticsContext} from '../../app/analytics';
import {AssetLiveDataProvider} from '../../asset-data/AssetLiveDataProvider';
import {buildAsset, buildAssetKey} from '../../graphql/types';
import {buildQueryMock} from '../../testing/mocking';
import AssetsCatalogRoot, {ASSETS_CATALOG_ROOT_QUERY} from '../AssetsCatalogRoot';
import {fetchRecentlyVisitedAssetsFromLocalStorage} from '../RecentlyVisitedAssetsStorage';
import {
AssetsCatalogRootQuery,
AssetsCatalogRootQueryVariables,
} from '../types/AssetsCatalogRoot.types';

// Mock the `useParams` hook to override the asset key in the URL.
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: jest.fn(),
}));

describe('RecentlyVisitedAssetsStorage', () => {
beforeEach(() => {
window.localStorage.clear();
});

it('Store visited asset in local storage', async () => {
// Set the asset key in the URL
jest.spyOn(Router, 'useParams').mockReturnValue({0: 'sda_asset'});

const assetKey = {path: ['sda_asset']};
const TestAssetView = (
<MockedProvider
mocks={[
buildQueryMock<AssetsCatalogRootQuery, AssetsCatalogRootQueryVariables>({
query: ASSETS_CATALOG_ROOT_QUERY,
variables: {
assetKey,
},
data: {
assetOrError: buildAsset({
key: buildAssetKey(assetKey),
}),
},
}),
]}
>
<AnalyticsContext.Provider value={{page: () => {}} as any}>
<AssetLiveDataProvider>
<MemoryRouter initialEntries={['/assets/sda_asset']}>
<AssetsCatalogRoot />
</MemoryRouter>
</AssetLiveDataProvider>
</AnalyticsContext.Provider>
</MockedProvider>
);

// First render displays the page in a loading state
const {rerender} = render(TestAssetView);
// Assert that the asset is not stored if the page is still loading
expect(fetchRecentlyVisitedAssetsFromLocalStorage()).toEqual([]);
// Wait for the page to load
await waitFor(() => {
expect(screen.queryByText('Loading assets…')).toBeNull();
});
// Second render displays the page in a loaded state
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect(fetchRecentlyVisitedAssetsFromLocalStorage()).toEqual([]); to confirm it doesn't save it until after the loading finishes

// Effect called to store asset in local storage
rerender(TestAssetView);

expect(fetchRecentlyVisitedAssetsFromLocalStorage()).toEqual([assetKey]);
});

it('Do not store nonexistent asset in local storage', async () => {
// Set the asset key in the URL
jest.spyOn(Router, 'useParams').mockReturnValue({0: 'nonexistent_asset'});

const assetKey = {path: ['nonexistent_asset']};
const TestAssetView = (
<MockedProvider
mocks={[
buildQueryMock<AssetsCatalogRootQuery, AssetsCatalogRootQueryVariables>({
query: ASSETS_CATALOG_ROOT_QUERY,
variables: {
assetKey,
},
data: {
assetOrError: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buildAssetNotFoundError for next time

__typename: 'AssetNotFoundError',
},
},
}),
]}
>
<AnalyticsContext.Provider value={{page: () => {}} as any}>
<AssetLiveDataProvider>
<MemoryRouter initialEntries={['/assets/nonexistent_asset']}>
<AssetsCatalogRoot />
</MemoryRouter>
</AssetLiveDataProvider>
</AnalyticsContext.Provider>
</MockedProvider>
);

// First render displays the page in a loading state
const {rerender} = render(TestAssetView);
// Assert that the asset is not stored if the page is still loading
expect(fetchRecentlyVisitedAssetsFromLocalStorage()).toEqual([]);
// Wait for the page to load
await waitFor(() => {
expect(screen.queryByText('Loading assets…')).toBeNull();
});
// Second render displays the page in a loaded state
// Effect called to store asset in local storage
rerender(TestAssetView);

expect(fetchRecentlyVisitedAssetsFromLocalStorage()).toEqual([]);
});
});
Loading