update tests

This commit is contained in:
Nate Kelley 2025-10-08 17:20:39 -06:00
parent 5b01949baa
commit 06cb498a46
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
5 changed files with 333 additions and 5 deletions

View File

@ -16,7 +16,6 @@ export const useIsAssetFileChanged = () => {
dashboardId: assetId, dashboardId: assetId,
enabled: assetType === 'dashboard_file', enabled: assetType === 'dashboard_file',
}); });
console.log('reportParams', assetId, assetType);
const reportParams = useIsReportFileChanged({ const reportParams = useIsReportFileChanged({
reportId: assetId, reportId: assetId,
enabled: assetType === 'report_file', enabled: assetType === 'report_file',

View File

@ -0,0 +1,134 @@
import { renderHook } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';
import { useSelectedAssetId } from './useSelectedAssetType';
// Mock @tanstack/react-router
vi.mock('@tanstack/react-router', () => ({
useParams: vi.fn(),
useMatches: vi.fn(),
useSearch: vi.fn(),
}));
import { useParams } from '@tanstack/react-router';
describe('useSelectedAssetId', () => {
it('should return metricId when present', () => {
vi.mocked(useParams).mockReturnValue({
metricId: 'metric-123',
dashboardId: undefined,
reportId: undefined,
chatId: undefined,
collectionId: undefined,
messageId: undefined,
});
const { result } = renderHook(() => useSelectedAssetId());
expect(result.current).toBe('metric-123');
});
it('should return dashboardId when present and metricId is not', () => {
vi.mocked(useParams).mockReturnValue({
metricId: undefined,
dashboardId: 'dashboard-456',
reportId: undefined,
chatId: undefined,
collectionId: undefined,
messageId: undefined,
});
const { result } = renderHook(() => useSelectedAssetId());
expect(result.current).toBe('dashboard-456');
});
it('should return reportId when present and metricId and dashboardId are not', () => {
vi.mocked(useParams).mockReturnValue({
metricId: undefined,
dashboardId: undefined,
reportId: 'report-789',
chatId: undefined,
collectionId: undefined,
messageId: undefined,
});
const { result } = renderHook(() => useSelectedAssetId());
expect(result.current).toBe('report-789');
});
it('should return chatId when present and other IDs (except messageId and collectionId) are not', () => {
vi.mocked(useParams).mockReturnValue({
metricId: undefined,
dashboardId: undefined,
reportId: undefined,
chatId: 'chat-abc',
collectionId: undefined,
messageId: undefined,
});
const { result } = renderHook(() => useSelectedAssetId());
expect(result.current).toBe('chat-abc');
});
it('should return collectionId when present and other IDs (except messageId) are not', () => {
vi.mocked(useParams).mockReturnValue({
metricId: undefined,
dashboardId: undefined,
reportId: undefined,
chatId: undefined,
collectionId: 'collection-def',
messageId: undefined,
});
const { result } = renderHook(() => useSelectedAssetId());
expect(result.current).toBe('collection-def');
});
it('should return messageId when present and all other IDs are not', () => {
vi.mocked(useParams).mockReturnValue({
metricId: undefined,
dashboardId: undefined,
reportId: undefined,
chatId: undefined,
collectionId: undefined,
messageId: 'message-ghi',
});
const { result } = renderHook(() => useSelectedAssetId());
expect(result.current).toBe('message-ghi');
});
it('should return null when no params are present', () => {
vi.mocked(useParams).mockReturnValue({
metricId: undefined,
dashboardId: undefined,
reportId: undefined,
chatId: undefined,
collectionId: undefined,
messageId: undefined,
});
const { result } = renderHook(() => useSelectedAssetId());
expect(result.current).toBeNull();
});
it('should prioritize metricId over all other params', () => {
vi.mocked(useParams).mockReturnValue({
metricId: 'metric-123',
dashboardId: 'dashboard-456',
reportId: 'report-789',
chatId: 'chat-abc',
collectionId: 'collection-def',
messageId: 'message-ghi',
});
const { result } = renderHook(() => useSelectedAssetId());
expect(result.current).toBe('metric-123');
});
});

View File

@ -0,0 +1,194 @@
import { renderHook } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';
import { useSelectedAssetType } from './useSelectedAssetType';
// Mock @tanstack/react-router
vi.mock('@tanstack/react-router', () => ({
useParams: vi.fn(),
useMatches: vi.fn(),
useSearch: vi.fn(),
}));
import { useParams } from '@tanstack/react-router';
describe('useSelectedAssetType', () => {
it('should return "metric_file" when metricId is present', () => {
vi.mocked(useParams).mockReturnValue({
metricId: 'metric-123',
dashboardId: undefined,
reportId: undefined,
chatId: undefined,
collectionId: undefined,
messageId: undefined,
});
const { result } = renderHook(() => useSelectedAssetType());
expect(result.current).toBe('metric_file');
});
it('should return "reasoning" when messageId is present', () => {
vi.mocked(useParams).mockReturnValue({
metricId: undefined,
dashboardId: undefined,
reportId: undefined,
chatId: undefined,
collectionId: undefined,
messageId: 'message-123',
});
const { result } = renderHook(() => useSelectedAssetType());
expect(result.current).toBe('reasoning');
});
it('should return "dashboard_file" when dashboardId is present', () => {
vi.mocked(useParams).mockReturnValue({
metricId: undefined,
dashboardId: 'dashboard-123',
reportId: undefined,
chatId: undefined,
collectionId: undefined,
messageId: undefined,
});
const { result } = renderHook(() => useSelectedAssetType());
expect(result.current).toBe('dashboard_file');
});
it('should return "report_file" when reportId is present', () => {
vi.mocked(useParams).mockReturnValue({
metricId: undefined,
dashboardId: undefined,
reportId: 'report-123',
chatId: undefined,
collectionId: undefined,
messageId: undefined,
});
const { result } = renderHook(() => useSelectedAssetType());
expect(result.current).toBe('report_file');
});
it('should return "chat" when chatId is present', () => {
vi.mocked(useParams).mockReturnValue({
metricId: undefined,
dashboardId: undefined,
reportId: undefined,
chatId: 'chat-123',
collectionId: undefined,
messageId: undefined,
});
const { result } = renderHook(() => useSelectedAssetType());
expect(result.current).toBe('chat');
});
it('should return "collection" when collectionId is present', () => {
vi.mocked(useParams).mockReturnValue({
metricId: undefined,
dashboardId: undefined,
reportId: undefined,
chatId: undefined,
collectionId: 'collection-123',
messageId: undefined,
});
const { result } = renderHook(() => useSelectedAssetType());
expect(result.current).toBe('collection');
});
it('should return "metric_file" as default when no params are present', () => {
vi.mocked(useParams).mockReturnValue({
metricId: undefined,
dashboardId: undefined,
reportId: undefined,
chatId: undefined,
collectionId: undefined,
messageId: undefined,
});
const { result } = renderHook(() => useSelectedAssetType());
expect(result.current).toBe('metric_file');
});
it('should prioritize metricId over all other params', () => {
vi.mocked(useParams).mockReturnValue({
metricId: 'metric-123',
dashboardId: 'dashboard-123',
reportId: 'report-123',
chatId: 'chat-123',
collectionId: 'collection-123',
messageId: 'message-123',
});
const { result } = renderHook(() => useSelectedAssetType());
expect(result.current).toBe('metric_file');
});
it('should prioritize messageId over dashboardId, reportId, chatId, and collectionId', () => {
vi.mocked(useParams).mockReturnValue({
metricId: undefined,
dashboardId: 'dashboard-123',
reportId: 'report-123',
chatId: 'chat-123',
collectionId: 'collection-123',
messageId: 'message-123',
});
const { result } = renderHook(() => useSelectedAssetType());
expect(result.current).toBe('reasoning');
});
it('should prioritize dashboardId over reportId, chatId, and collectionId', () => {
vi.mocked(useParams).mockReturnValue({
metricId: undefined,
dashboardId: 'dashboard-123',
reportId: 'report-123',
chatId: 'chat-123',
collectionId: 'collection-123',
messageId: undefined,
});
const { result } = renderHook(() => useSelectedAssetType());
expect(result.current).toBe('dashboard_file');
});
it('should prioritize reportId over chatId and collectionId', () => {
vi.mocked(useParams).mockReturnValue({
metricId: undefined,
dashboardId: undefined,
reportId: 'report-123',
chatId: 'chat-123',
collectionId: 'collection-123',
messageId: undefined,
});
const { result } = renderHook(() => useSelectedAssetType());
expect(result.current).toBe('report_file');
});
it('should prioritize chatId over collectionId', () => {
vi.mocked(useParams).mockReturnValue({
metricId: undefined,
dashboardId: undefined,
reportId: undefined,
chatId: 'chat-123',
collectionId: 'collection-123',
messageId: undefined,
});
const { result } = renderHook(() => useSelectedAssetType());
expect(result.current).toBe('chat');
});
});

View File

@ -46,6 +46,10 @@ export const useSelectedAssetId = () => {
return metricId; return metricId;
} }
if (messageId) {
return messageId;
}
if (dashboardId) { if (dashboardId) {
return dashboardId; return dashboardId;
} }
@ -62,10 +66,6 @@ export const useSelectedAssetId = () => {
return collectionId; return collectionId;
} }
if (messageId) {
return messageId;
}
return null; return null;
}; };

View File

@ -181,5 +181,6 @@ export const MOCK_CHAT = (chatIndex = 0): BusterChat => {
workspace_sharing: 'full_access', workspace_sharing: 'full_access',
workspace_member_count: 0, workspace_member_count: 0,
individual_permissions: [], individual_permissions: [],
screenshot_taken_at: null,
}; };
}; };