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

[CORE-272] Fix Missing Prompt for Billing Workspace in Requester Pays Workspaces Analysis Tab #5238

Merged
merged 5 commits into from
Jan 30, 2025
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
17 changes: 17 additions & 0 deletions src/analysis/Analyses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,4 +424,21 @@ describe('Analyses', () => {
const modalTitle = document.getElementById('analysis-modal-title');
expect(modalTitle).toBeInTheDocument();
});

it('calls refreshAnalyses on mount', async () => {
// Arrange
const refreshAnalysesMock = jest.fn();
asMockedFn(useAnalysisFiles).mockReturnValue({
...defaultUseAnalysisStore,
refreshFileStore: refreshAnalysesMock,
});

// Act
await act(async () => {
render(h(BaseAnalyses, defaultAnalysesProps));
});

// Assert
expect(refreshAnalysesMock).toHaveBeenCalled();
});
});
1 change: 1 addition & 0 deletions src/analysis/Analyses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ export const BaseAnalyses = (
setActiveFileTransfers(!_.isEmpty(fileTransfers));
});
if (workspace?.workspaceInitialized) {
refreshAnalyses(); // Load Analyses by default
load();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
78 changes: 78 additions & 0 deletions src/analysis/useAnalysisFiles.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { act } from 'react-dom/test-utils';
import { useAnalysisFiles } from 'src/analysis/useAnalysisFiles';
import { AnalysisProvider } from 'src/libs/ajax/analysis-providers/AnalysisProvider';
import { defaultGoogleWorkspace } from 'src/testing/workspace-fixtures';

jest.mock('src/analysis/useAnalysisFiles', () => ({
useAnalysisFiles: jest.fn(),
}));

jest.mock('src/libs/ajax/analysis-providers/AnalysisProvider', () => ({
AnalysisProvider: {
listAnalyses: jest.fn(),
},
}));

jest.mock('src/libs/utils', () => ({
withBusyState: jest.fn((setter) => async (fn) => {
setter(true);
try {
return await fn();
} finally {
setter(false);
}
}),
maybeParseJSON: jest.fn(),
}));

describe('useAnalysisFiles - refreshFileStore', () => {
const mockWorkspace = defaultGoogleWorkspace;
const mockAnalyses = [{ name: 'test.ipynb', ext: '.ipynb', lastModified: 1690000000000 }];

let refreshFileStoreMock: () => any;

beforeEach(() => {
jest.clearAllMocks();

refreshFileStoreMock = jest.fn(async () => {
const analyses = await AnalysisProvider.listAnalyses(mockWorkspace.workspace);
return { status: 'Ready', files: analyses };
});

(useAnalysisFiles as jest.Mock).mockReturnValue({
refreshFileStore: refreshFileStoreMock,
loadedState: { status: 'Ready', files: [] },
});
});

it('calls refreshFileStore without rendering', async () => {
await act(async () => {
await refreshFileStoreMock();
});

expect(refreshFileStoreMock).toHaveBeenCalledTimes(1);
});

it('fetches analyses and updates state correctly', async () => {
(AnalysisProvider.listAnalyses as jest.Mock).mockResolvedValue(mockAnalyses);

await act(async () => {
const result = await refreshFileStoreMock();
expect(result.files).toEqual(mockAnalyses);
expect(result.status).toBe('Ready');
});

expect(AnalysisProvider.listAnalyses).toHaveBeenCalledWith(mockWorkspace.workspace);
});

it('handles errors gracefully when fetching analyses', async () => {
const mockError = new Error('Fetch failed');
(AnalysisProvider.listAnalyses as jest.Mock).mockRejectedValue(mockError);

await act(async () => {
await expect(refreshFileStoreMock()).rejects.toThrow(mockError);
});

expect(AnalysisProvider.listAnalyses).toHaveBeenCalledWith(mockWorkspace.workspace);
});
});
12 changes: 7 additions & 5 deletions src/analysis/useAnalysisFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from 'src/analysis/utils/file-utils';
import { getToolLabelFromFileExtension, ToolLabel } from 'src/analysis/utils/tool-utils';
import { AnalysisProvider } from 'src/libs/ajax/analysis-providers/AnalysisProvider';
import { reportError, withErrorReporting } from 'src/libs/error';
import { reportError } from 'src/libs/error';
import { useCancellation, useStore } from 'src/libs/react-utils';
import { workspaceStore } from 'src/libs/state';
import * as Utils from 'src/libs/utils';
Expand Down Expand Up @@ -56,13 +56,15 @@ export const useAnalysisFiles = (): AnalysisFileStore => {
const [pendingCreate, setPendingCreate] = useLoadedData<true>();
const [pendingDelete, setPendingDelete] = useLoadedData<true>();

const refresh = withHandlers(
[withErrorReporting('Error loading analysis files'), Utils.withBusyState(setLoading)],
async (): Promise<void> => {
const refresh = withHandlers([Utils.withBusyState(setLoading)], async (): Promise<void> => {
try {
const analysis = await AnalysisProvider.listAnalyses(workspace!.workspace, signal);
setAnalyses(analysis);
} catch (error) {
console.error('Error loading analysis files:', error);
throw error;
}
);
});

const createAnalysis = async (fullAnalysisName: string, toolLabel: ToolLabel, contents: any): Promise<void> => {
await setPendingCreate(async () => {
Expand Down
Loading