From 2d938c8c3e7e97fde6191e7858fdc729a26a6079 Mon Sep 17 00:00:00 2001 From: Duyet Le Date: Mon, 4 Nov 2024 16:08:19 +0700 Subject: [PATCH] feat(test): add card-multi-metrics.cy.tsx --- README.md | 1 - .../generic-charts/card-multi-metrics.cy.tsx | 93 +++++++++++++++++++ .../generic-charts/card-multi-metrics.tsx | 15 ++- 3 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 components/generic-charts/card-multi-metrics.cy.tsx diff --git a/README.md b/README.md index 7f845e20..f420e33e 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,6 @@ [![Build and Test](https://github.com/duyet/clickhouse-monitoring/actions/workflows/ci.yml/badge.svg)](https://github.com/duyet/clickhouse-monitoring/actions/workflows/ci.yml) [![All-time uptime](https://img.shields.io/endpoint?url=https%3A%2F%2Fraw.githubusercontent.com%2Fduyet%2Fuptime%2FHEAD%2Fapi%2Fclickhouse-monitoring-vercel-app%2Fuptime.json)](https://duyet.github.io/uptime/history/clickhouse-monitoring-vercel-app) - The simple Next.js dashboard that relies on `system.*` tables to help monitor and provide an overview of your ClickHouse cluster. Features: diff --git a/components/generic-charts/card-multi-metrics.cy.tsx b/components/generic-charts/card-multi-metrics.cy.tsx new file mode 100644 index 00000000..154a1835 --- /dev/null +++ b/components/generic-charts/card-multi-metrics.cy.tsx @@ -0,0 +1,93 @@ +import { CardMultiMetrics } from './card-multi-metrics' + +describe('', () => { + const mockItems = [ + { + current: 100, + target: 200, + currentReadable: '100 users', + targetReadable: '200 users', + }, + { + current: 50, + target: 150, + currentReadable: '50 sessions', + targetReadable: '150 sessions', + }, + ] + + it('renders with default props', () => { + cy.mount() + + // Should have aria-description + cy.get('[aria-description="card-metrics"]').should('exist') + + // Should not render labels when no items + cy.contains('Current').should('not.exist') + cy.contains('Total').should('not.exist') + }) + + it('renders with primary content', () => { + cy.mount() + + cy.get('div.text-xl').contains('Dashboard Metrics').should('be.visible') + }) + + it('renders with custom labels and items', () => { + cy.mount( + + ) + + // Labels should be visible when items exist + cy.contains('Active').should('be.visible') + cy.contains('Maximum').should('be.visible') + }) + + it('renders with items data', () => { + cy.mount() + + // Check if readable values are displayed + cy.contains('100 users').should('be.visible') + cy.contains('200 users').should('be.visible') + cy.contains('50 sessions').should('be.visible') + cy.contains('150 sessions').should('be.visible') + + // Check for dotted separators + cy.get('hr.border-dotted').should('have.length', mockItems.length) + }) + + it('renders with custom className', () => { + const customClass = 'custom-test-class' + cy.mount() + + cy.get('[aria-description="card-metrics"]').should( + 'have.class', + customClass + ) + }) + + it('renders with ReactNode as primary content', () => { + cy.mount( + Custom Primary} + /> + ) + + cy.get('.test-primary').contains('Custom Primary').should('be.visible') + }) + + it('handles empty items array', () => { + cy.mount() + + // Should not render labels when items array is empty + cy.contains('Current').should('not.exist') + cy.contains('Total').should('not.exist') + + // Should not render any separators + cy.get('hr.border-dotted').should('not.exist') + }) +}) diff --git a/components/generic-charts/card-multi-metrics.tsx b/components/generic-charts/card-multi-metrics.tsx index 7e63cb20..9bb6e59d 100644 --- a/components/generic-charts/card-multi-metrics.tsx +++ b/components/generic-charts/card-multi-metrics.tsx @@ -21,14 +21,19 @@ export function CardMultiMetrics({ className, }: CardMultiMetricsProps) { return ( -
+
{primary}
-
- {currentLabel} - {targetLabel} -
+ {items.length ? ( +
+ {currentLabel} + {targetLabel} +
+ ) : null} {items.map((item, i) => { const _percent = (item.current / item.target) * 100