Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

13.4 KB
Raw Blame
import { cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'

import { resumeKeaLoadersErrors, silenceKeaLoadersErrors } from '~/initKea'
import { initKeaTests } from '~/test/init'

import {
    dataQualityChecksHealthList,
    dataQualityChecksList,
    dataQualityChecksScheduleRetrieve,
    dataQualityChecksSubjectsList,
    dataQualityRunsCreate,
    dataQualityRunsRetrieve,
    dataQualityChecksCheckTypesList,
    dataQualityChecksRunsList,
} from 'products/data_quality/frontend/generated/api'
import type { DataQualityOverviewCheckApi } from 'products/data_quality/frontend/generated/api.schemas'

import { DataQualityOverview } from './DataQualityOverview'
import { subjectDisclosureId } from './dataQualityOverviewLogic'

jest.mock('lib/api', () => ({
    __esModule: true,
    default: {},
    ApiConfig: { getCurrentTeamId: jest.fn(() => 1) },
    ApiError: class ApiError extends Error {},
}))

jest.mock('lib/lemon-ui/LemonToast/LemonToast', () => ({
    lemonToast: { success: jest.fn(), error: jest.fn(), info: jest.fn(), warning: jest.fn() },
}))

jest.mock('products/data_quality/frontend/generated/api', () => ({
    dataQualityChecksList: jest.fn(),
    dataQualityChecksHealthList: jest.fn(),

    dataQualityRunsCreate: jest.fn(),
    dataQualityRunsRetrieve: jest.fn(),
    dataQualityChecksRunsList: jest.fn(),
    dataQualityChecksDestroy: jest.fn(),
    dataQualityChecksCheckTypesList: jest.fn(),
    dataQualityChecksCreate: jest.fn(),
    dataQualityChecksPartialUpdate: jest.fn(),
    dataQualityChecksScheduleRetrieve: jest.fn(),
    dataQualityChecksSchedulePartialUpdate: jest.fn(),
    dataQualityChecksSchedulesList: jest.fn(() => Promise.resolve([])),
    dataQualityChecksSubjectsList: jest.fn(),
}))

function buildCheck(
    id: string,
    subject: string,
    overrides: Partial<DataQualityOverviewCheckApi> = {}
): DataQualityOverviewCheckApi {
    return {
        id,
        name: `${subject}_${id}`,
        subject_type: 'view',
        subject_uuid: `uuid-${subject}`,
        subject_name: subject,
        check_type: 'not_null',
        column_name: 'id',
        severity: 'error',
        enabled: true,
        last_status: 'failed',
        last_run_at: null,
        subject_node_id: null,
        subject_source_id: null,
        subject_schema_id: null,
        ...overrides,
    } as DataQualityOverviewCheckApi
}

function failingHealth(subject: string): Record<string, unknown> {
    return {
        subject_type: 'view',
        subject_uuid: `uuid-${subject}`,
        health: 'failing',
        checks_total: 1,
        checks_failing: 1,
    }
}

function queryAll(selector: string): HTMLElement[] {
    return [...document.querySelectorAll<HTMLElement>(selector)]
}

function runSubjectButtons(): HTMLElement[] {
    return queryAll('[data-attr="data-quality-overview-run-subject"]')
}

function settingsLinkHref(): string | null | undefined {
    return document.querySelector('[data-attr="data-quality-overview-settings"]')?.getAttribute('href')
}

function isSpinning(button: HTMLElement): boolean {
    return !!button.querySelector('.LemonIcon--spin, [class*="Spinner"]')
}

describe('DataQualityOverview', () => {
    beforeEach(() => {
        jest.clearAllMocks()
        initKeaTests()
        silenceKeaLoadersErrors()
        ;(dataQualityChecksList as jest.Mock).mockResolvedValue({
            results: [buildCheck('check-1', 'orders'), buildCheck('check-2', 'customers')],
        })
        ;(dataQualityChecksHealthList as jest.Mock).mockResolvedValue([
            failingHealth('orders'),
            failingHealth('customers'),
        ])
        ;(dataQualityChecksSubjectsList as jest.Mock).mockResolvedValue([])
        ;(dataQualityRunsCreate as jest.Mock).mockResolvedValue({
            id: 'suite-1',
            status: 'running',
            trigger: 'manual',
            checks_passed: 0,
            checks_failed: 0,
            checks_errored: 0,
            checks_skipped: 0,
            error: '',
        })
        ;(dataQualityRunsRetrieve as jest.Mock).mockResolvedValue({ id: 'suite-1', status: 'running' })
        ;(dataQualityChecksRunsList as jest.Mock).mockResolvedValue([
            {
                id: 'run-1',
                status: 'failed',
                compiled_query: 'SELECT 1',
                duration_ms: 5,
                observed_value: null,
                failed_row_count: null,
                error: '',
                started_at: null,
            },
        ])
        ;(dataQualityChecksCheckTypesList as jest.Mock).mockResolvedValue([
            { check_type: 'not_null', description: '', requires_column: true, config_schema: {} },
        ])
    })

    afterEach(() => {
        resumeKeaLoadersErrors()
        cleanup()
    })

    async function renderOverview(): Promise<void> {
        render(<DataQualityOverview />)
        await waitFor(() => expect(runSubjectButtons()).toHaveLength(2))
    }

    it('spins only the subject a run was started from, and disables the rest without a spinner', async () => {
        // The reported regression: one shared loading flag put a spinner on every run control, so a
        // run on one table looked like a run on the whole project.
        await renderOverview()
        fireEvent.click(runSubjectButtons()[0])

        await waitFor(() => expect(isSpinning(runSubjectButtons()[0])).toBe(true))
        const runAll = document.querySelector('[data-attr="data-quality-overview-run-all"]') as HTMLElement
        expect(isSpinning(runSubjectButtons()[1])).toBe(false)
        expect(runSubjectButtons()[1].getAttribute('aria-disabled')).toEqual('true')
        expect(isSpinning(runAll)).toBe(false)
        expect(runAll.getAttribute('aria-disabled')).toEqual('true')
    })

    it('renders no check rows for a collapsed subject', async () => {
        await renderOverview()
        expect(screen.getByText('orders_check-1')).toBeTruthy()

        fireEvent.click(document.getElementById(subjectDisclosureId('view:uuid-orders'))!)

        await waitFor(() => expect(screen.queryByText('orders_check-1')).toBeNull())
        expect(queryAll('[data-attr="data-quality-overview-check-actions"]')).toHaveLength(1)
    })

    it('keeps the disclosure, the subject link and the run action as separate controls', async () => {
        ;(dataQualityChecksList as jest.Mock).mockResolvedValue({
            results: [buildCheck('check-1', 'orders', { subject_node_id: 'node-1' })],
        })
        ;(dataQualityChecksHealthList as jest.Mock).mockResolvedValue([failingHealth('orders')])
        render(<DataQualityOverview />)
        await waitFor(() => expect(runSubjectButtons()).toHaveLength(1))

        const disclosure = queryAll('[data-attr="data-quality-subject-disclosure"]')[0]
        expect(within(disclosure.parentElement!).getByText('Failing')).toBeTruthy()
        // Suffix match: the rendered href carries the /project/:id prefix, so an exact match on the
        // path would find nothing and the assertion below would pass on a null link.
        const link = document.querySelector('a[href$="/models/node-1/data-quality"]')
        expect(link).not.toBeNull()
        expect(disclosure.contains(link)).toBe(false)
        expect(disclosure.contains(runSubjectButtons()[0])).toBe(false)
        expect(disclosure.getAttribute('aria-expanded')).toEqual('true')
        expect(disclosure.getAttribute('aria-controls')).toEqual('data-quality-subject-checks-view:uuid-orders')
    })

    it('renders the subject name as text when it has no page of its own', async () => {
        await renderOverview()

        expect(document.querySelector('a[href^="/models/"]')).toBeNull()
        expect(screen.getAllByText('orders').length).toBeGreaterThan(0)
    })

    it('groups a PostHog table under its own tag, with a schedule and a link to its node', async () => {
        const check = buildCheck('check-events', 'events', {
            subject_type: 'posthog_table',
            subject_uuid: 'uuid-events',
            subject_node_id: 'node-events',
        })
        ;(dataQualityChecksList as jest.Mock).mockResolvedValue({ results: [check] })
        ;(dataQualityChecksHealthList as jest.Mock).mockResolvedValue([failingHealth('events')])
        ;(dataQualityChecksScheduleRetrieve as jest.Mock).mockResolvedValue({
            id: 'schedule-1',
            enabled: true,
            interval: '24hour',
            next_run_at: null,
            last_run_at: null,
            last_suite_run: null,
        })
        render(<DataQualityOverview />)
        await waitFor(() => expect(runSubjectButtons()).toHaveLength(1))

        expect(screen.getByText('PostHog')).toBeTruthy()
        expect(document.querySelector('a[href*="/models/node-events"]')).not.toBeNull()

        fireEvent.click(queryAll('[data-attr="data-quality-subject-disclosure"]')[0])

        expect(await screen.findByText('Run automatically')).toBeTruthy()
    })

    it('shows only the creation path from the illustrated first-use state', async () => {
        ;(dataQualityChecksList as jest.Mock).mockResolvedValue({ results: [] })
        ;(dataQualityChecksHealthList as jest.Mock).mockResolvedValue([])
        render(<DataQualityOverview />)

        expect(await screen.findByText('No checks yet')).toBeTruthy()
        expect(screen.queryByText('No checks match these filters.')).toBeNull()
        expect(document.querySelector('[data-attr="data-quality-overview-new-check"]')).not.toBeNull()
        expect(document.querySelector('[data-attr="data-quality-overview-browse"]')).toBeNull()
        expect(document.querySelector('[data-attr="data-quality-overview-empty-state"] img')).not.toBeNull()
        expect(screen.queryByPlaceholderText('Search checks')).toBeNull()
        expect(document.querySelector('[data-attr="data-quality-overview-run-all"]')).toBeNull()

        fireEvent.click(document.querySelector('[data-attr="data-quality-overview-first-check"]')!)

        expect(await screen.findByText('Table, view, or metric')).toBeTruthy()
        expect(document.querySelector('.ReactModal__Content')?.textContent).toContain('Browse tables and views')
    })

    it('links to the data quality settings from both the populated and the empty toolbar', async () => {
        await renderOverview()

        expect(settingsLinkHref()).toMatch(/\/settings\/environment-data-quality$/)

        cleanup()
        ;(dataQualityChecksList as jest.Mock).mockResolvedValue({ results: [] })
        ;(dataQualityChecksHealthList as jest.Mock).mockResolvedValue([])
        render(<DataQualityOverview />)
        await screen.findByText('No checks yet')

        expect(settingsLinkHref()).toMatch(/\/settings\/environment-data-quality$/)
    })

    it('keeps the existing subject-scoped editor free of a subject picker', async () => {
        await renderOverview()

        fireEvent.click(queryAll('[data-attr="data-quality-overview-check-actions"]')[0])
        fireEvent.click(await screen.findByText('Edit'))

        expect(await screen.findByText('Check type')).toBeTruthy()
        expect(screen.queryByText('Table, view, or metric')).toBeNull()
    })

    it('shows a retry when the subject picker catalog cannot load', async () => {
        ;(dataQualityChecksList as jest.Mock).mockResolvedValue({ results: [] })
        ;(dataQualityChecksHealthList as jest.Mock).mockResolvedValue([])
        ;(dataQualityChecksSubjectsList as jest.Mock).mockRejectedValue(new Error('down'))
        render(<DataQualityOverview />)
        await screen.findByText('No checks yet')

        fireEvent.click(document.querySelector('[data-attr="data-quality-overview-first-check"]')!)

        expect(await screen.findByText("Couldn't load what you can check.")).toBeTruthy()
        expect(screen.getByText('Retry')).toBeTruthy()
    })

    it('shows a retry when the check type catalog cannot load', async () => {
        ;(dataQualityChecksCheckTypesList as jest.Mock).mockRejectedValueOnce(new Error('down'))
        await renderOverview()

        fireEvent.click(queryAll('[data-attr="data-quality-overview-check-actions"]')[0])
        fireEvent.click(await screen.findByText('Edit'))

        expect(await screen.findByText("Couldn't load check types.")).toBeTruthy()
        expect(screen.getByText('Retry')).toBeTruthy()
    })

    it('shows a retry rather than an empty page when the first load fails', async () => {
        ;(dataQualityChecksList as jest.Mock).mockRejectedValue(new Error('down'))
        render(<DataQualityOverview />)

        expect(await screen.findByText("Couldn't load data quality checks.")).toBeTruthy()
        expect(screen.queryByText('No checks yet')).toBeNull()
    })

    it('separates the filtered-empty state from the first-use one', async () => {
        await renderOverview()

        fireEvent.change(screen.getByPlaceholderText('Search checks'), { target: { value: 'nothing matches this' } })

        expect(await screen.findByText('No checks match these filters.')).toBeTruthy()
        expect(screen.queryByText('No checks yet')).toBeNull()
        fireEvent.click(screen.getByText('Clear filters'))
        await waitFor(() => expect(runSubjectButtons()).toHaveLength(2))
    })

    it('returns focus to the row action when a delete is canceled', async () => {
        await renderOverview()
        const trigger = queryAll('[data-attr="data-quality-overview-check-actions"]')[0]

        fireEvent.click(trigger)
        fireEvent.click(await screen.findByText('Delete'))
        fireEvent.click(await screen.findByText('Cancel'))

        await waitFor(() => expect(document.activeElement).toBe(trigger))
    })
})