From 32a3d6f21e26df5b202d507db8db25c4aad79b6d Mon Sep 17 00:00:00 2001 From: Nate Kelley Date: Fri, 9 May 2025 10:51:26 -0600 Subject: [PATCH] fix broken tests --- web/playwright-tests/collection-tests.test.ts | 3 +- .../dashboard-updates.test.ts | 18 +-- .../canSupportTrendline.test.ts | 112 ++++++++++++++++++ web/src/lib/messages.test.ts | 80 +++++++++++++ 4 files changed, 203 insertions(+), 10 deletions(-) create mode 100644 web/src/components/ui/charts/chartHooks/useDatasetOptions/useDataTrendlineOptions/canSupportTrendline.test.ts create mode 100644 web/src/lib/messages.test.ts diff --git a/web/playwright-tests/collection-tests.test.ts b/web/playwright-tests/collection-tests.test.ts index 1570f4ba3..ce62d0cc7 100644 --- a/web/playwright-tests/collection-tests.test.ts +++ b/web/playwright-tests/collection-tests.test.ts @@ -21,9 +21,10 @@ test('Can create a collection', async ({ page }) => { await page.getByRole('button').filter({ hasText: /^$/ }).nth(2).click(); await page.getByRole('menuitem', { name: 'Delete collection' }).click(); await page.getByRole('button', { name: 'Submit' }).click(); - await page.waitForTimeout(50); + await page.waitForTimeout(1000); await page.waitForLoadState('networkidle'); await page.waitForLoadState('domcontentloaded'); + await page.waitForLoadState('load'); expect(page.url()).toBe('http://localhost:3000/app/collections'); }); diff --git a/web/playwright-tests/dashboard-updates.test.ts b/web/playwright-tests/dashboard-updates.test.ts index efce372c1..1899b5706 100644 --- a/web/playwright-tests/dashboard-updates.test.ts +++ b/web/playwright-tests/dashboard-updates.test.ts @@ -152,16 +152,16 @@ test('Can edit name and description of a dashboard', async ({ page }) => { await page.waitForLoadState('networkidle'); await page.getByTestId('segmented-trigger-file').click(); await page.getByTestId('segmented-trigger-file').click(); - await page.waitForTimeout(2000); // Wait up to 2 seconds for the text to appear - await expect(page.getByText('Important Metrics SWAG')).toBeVisible({ timeout: 20000 }); // Wait up to 20 seconds for visibility + await expect(page.getByRole('code').getByText('Important Metrics SWAG')).toBeVisible({ + timeout: 5000 + }); await expect(page.locator('.current-line').first()).toBeVisible(); - - await page - .getByRole('textbox', { name: 'Editor content' }) - .fill( - "name: Important Metrics\ndescription: ''\nrows:\n- items:\n - id: 72e445a5-fb08-5b76-8c77-1642adf0cb72\n - id: 45848c7f-0d28-52a0-914e-f3fc1b7d4180\n - id: 117a2fc5-e3e8-5bb0-a29b-bcfa3da3adc0\n - id: b19d2606-6061-5d22-8628-78a4878310d4\n rowHeight: 320\n columnSizes:\n" - ); - await page.getByRole('button', { name: 'Save' }).click(); await page.getByTestId('segmented-trigger-dashboard').click(); + await page.getByRole('textbox', { name: 'New dashboard' }).click(); + await page.getByRole('textbox', { name: 'New dashboard' }).fill('Important Metrics'); + await page.getByRole('button', { name: 'Save' }).click(); + await expect(page.getByRole('textbox', { name: 'New dashboard' })).toHaveValue( + 'Important Metrics' + ); }); diff --git a/web/src/components/ui/charts/chartHooks/useDatasetOptions/useDataTrendlineOptions/canSupportTrendline.test.ts b/web/src/components/ui/charts/chartHooks/useDatasetOptions/useDataTrendlineOptions/canSupportTrendline.test.ts new file mode 100644 index 000000000..cd122f00f --- /dev/null +++ b/web/src/components/ui/charts/chartHooks/useDatasetOptions/useDataTrendlineOptions/canSupportTrendline.test.ts @@ -0,0 +1,112 @@ +import { canSupportTrendlineRecord } from './canSupportTrendline'; +import { isNumericColumnType } from '@/lib/messages'; +import { DEFAULT_COLUMN_LABEL_FORMAT } from '@/api/asset_interfaces/metric'; +import type { BusterChartProps, Trendline } from '@/api/asset_interfaces/metric/charts'; + +// Mock the isNumericColumnType function +jest.mock('@/lib/messages', () => ({ + isNumericColumnType: jest.fn() +})); + +const mockedIsNumericColumnType = isNumericColumnType as jest.MockedFunction< + typeof isNumericColumnType +>; + +describe('canSupportTrendlineRecord', () => { + const trendlineTypes: Trendline['type'][] = [ + 'linear_regression', + 'logarithmic_regression', + 'exponential_regression', + 'polynomial_regression', + 'min', + 'max', + 'median', + 'average' + ]; + + const columnId = 'test-column'; + const mockColumnLabelFormats: NonNullable = { + [columnId]: { + columnType: 'number', + style: 'number' + } + }; + + beforeEach(() => { + jest.clearAllMocks(); + }); + + test.each(trendlineTypes)( + 'returns true when %s trendline has a numeric column type', + (trendlineType) => { + // Arrange + mockedIsNumericColumnType.mockReturnValue(true); + const trendline: Trendline = { + type: trendlineType, + columnId, + show: true, + showTrendlineLabel: true, + trendlineLabel: 'Test Label' + }; + + // Act + const result = canSupportTrendlineRecord[trendlineType](mockColumnLabelFormats, trendline); + + // Assert + expect(result).toBe(true); + expect(mockedIsNumericColumnType).toHaveBeenCalledWith('number'); + } + ); + + test.each(trendlineTypes)( + 'returns false when %s trendline has a non-numeric column type', + (trendlineType) => { + // Arrange + mockedIsNumericColumnType.mockReturnValue(false); + const trendline: Trendline = { + type: trendlineType, + columnId, + show: true, + showTrendlineLabel: true, + trendlineLabel: 'Test Label' + }; + + // Act + const result = canSupportTrendlineRecord[trendlineType](mockColumnLabelFormats, trendline); + + // Assert + expect(result).toBe(false); + expect(mockedIsNumericColumnType).toHaveBeenCalledWith('number'); + } + ); + + test.each(trendlineTypes)( + 'uses DEFAULT_COLUMN_LABEL_FORMAT when column format not provided for %s', + (trendlineType) => { + // Arrange + mockedIsNumericColumnType.mockReturnValue(true); + const trendline: Trendline = { + type: trendlineType, + columnId: 'non-existent', + show: true, + showTrendlineLabel: true, + trendlineLabel: 'Test Label' + }; + + // Act + const result = canSupportTrendlineRecord[trendlineType](mockColumnLabelFormats, trendline); + + // Assert + expect(result).toBe(true); + expect(mockedIsNumericColumnType).toHaveBeenCalledWith( + DEFAULT_COLUMN_LABEL_FORMAT.columnType + ); + } + ); + + test('confirms all trendline types are tested', () => { + // This test ensures we've covered all trendline types in our tests + const allTrendlineTypes = Object.keys(canSupportTrendlineRecord) as Trendline['type'][]; + expect(allTrendlineTypes.sort()).toEqual(trendlineTypes.sort()); + }); +}); diff --git a/web/src/lib/messages.test.ts b/web/src/lib/messages.test.ts new file mode 100644 index 000000000..bfc110c71 --- /dev/null +++ b/web/src/lib/messages.test.ts @@ -0,0 +1,80 @@ +import { + simplifyColumnType, + isNumericColumnType, + isNumericColumnStyle, + isDateColumnType, + NUMBER_TYPES, + TEXT_TYPES, + DATE_TYPES +} from './messages'; +import type { SimplifiedColumnType } from '@/api/asset_interfaces/metric/'; +import type { ColumnLabelFormat } from '@/api/asset_interfaces/metric/charts'; + +describe('simplifyColumnType', () => { + test('should return "number" for numeric types', () => { + NUMBER_TYPES.forEach((type) => { + expect(simplifyColumnType(type)).toBe('number'); + }); + expect(simplifyColumnType('number')).toBe('number'); + }); + + test('should return "text" for text types', () => { + TEXT_TYPES.forEach((type) => { + expect(simplifyColumnType(type)).toBe('text'); + }); + expect(simplifyColumnType('text')).toBe('text'); + }); + + test('should return "date" for date types', () => { + DATE_TYPES.forEach((type) => { + expect(simplifyColumnType(type)).toBe('date'); + }); + expect(simplifyColumnType('date')).toBe('date'); + }); + + test('should return "text" for unknown types', () => { + expect(simplifyColumnType('unknown')).toBe('text'); + expect(simplifyColumnType('boolean')).toBe('text'); + expect(simplifyColumnType('')).toBe('text'); + }); +}); + +describe('isNumericColumnType', () => { + test('should return true for "number" type', () => { + expect(isNumericColumnType('number')).toBe(true); + }); + + test('should return false for non-number types', () => { + expect(isNumericColumnType('text')).toBe(false); + expect(isNumericColumnType('date')).toBe(false); + }); +}); + +describe('isNumericColumnStyle', () => { + test('should return true for numeric styles', () => { + expect(isNumericColumnStyle('number')).toBe(true); + expect(isNumericColumnStyle('percent')).toBe(true); + expect(isNumericColumnStyle('currency')).toBe(true); + }); + + test('should return false for non-numeric styles', () => { + expect(isNumericColumnStyle('string')).toBe(false); + expect(isNumericColumnStyle('date')).toBe(false); + expect(isNumericColumnStyle(undefined)).toBe(false); + }); +}); + +describe('isDateColumnType', () => { + test('should return true for "date" type', () => { + expect(isDateColumnType('date')).toBe(true); + }); + + test('should return false for non-date types', () => { + expect(isDateColumnType('number')).toBe(false); + expect(isDateColumnType('text')).toBe(false); + }); + + test('should return false for undefined', () => { + expect(isDateColumnType(undefined)).toBe(false); + }); +});