KarenDouglas / bmi-calculator

MIT License
0 stars 0 forks source link

Implement Jest #2

Closed KarenDouglas closed 9 hours ago

KarenDouglas commented 4 weeks ago
  1. Test Calculator Logic
  2. Test Render with SnapSot Testing
  3. Interactive Elements: inputs
  4. images and Content
  5. animations
  6. Edge cases, for like extreme wieght , odd inputs
  7. input Validation
KarenDouglas commented 4 weeks ago

Some how tos: Sure! Let's go through each point and provide examples using Jest for unit testing and Playwright for end-to-end testing.

1. Calculator Logic:

// Jest Example
const { calculateBMI } = require('./bmiCalculator');

test('Calculate BMI', () => {
  const bmi = calculateBMI(70, 170); // Assuming height is in centimeters and weight in kilograms
  expect(bmi).toBeCloseTo(24.22, 2); // Expected BMI for these values
});

2. Rendering:

// Jest Example (Snapshot Testing)
import React from 'react';
import { render } from '@testing-library/react';
import Calculator from './Calculator';

test('Calculator UI Renders Correctly', () => {
  const { container } = render(<Calculator />);
  expect(container).toMatchSnapshot();
});

3. Interactive Elements:

// Jest Example (Mocking User Interaction)
import { fireEvent, screen } from '@testing-library/react';
import Calculator from './Calculator';

test('Clicking Calculate Button Triggers Calculation', () => {
  render(<Calculator />);
  const weightInput = screen.getByPlaceholderText('Enter weight');
  const heightInput = screen.getByPlaceholderText('Enter height');
  const calculateButton = screen.getByText('Calculate');

  fireEvent.change(weightInput, { target: { value: '70' } });
  fireEvent.change(heightInput, { target: { value: '170' } });
  fireEvent.click(calculateButton);

  const bmiResult = screen.getByTestId('bmi-result');
  expect(bmiResult.textContent).toBe('Your BMI: 24.22');
});

4. Images and Content:

// Jest Example (Content Testing)
test('Images and Content Load Correctly', () => {
  render(<Calculator />);
  const image = screen.getByAltText('BMI Calculator');
  expect(image).toBeInTheDocument();

  const content = screen.getByText('Calculate your BMI');
  expect(content).toBeInTheDocument();
});

5. Animations:

// Jest Example (Animation Testing)
test('Animations Start and Stop Correctly', () => {
  // Assuming your animation library provides methods to control animations
  const animation = document.getElementById('example-animation');
  expect(animation.isPlaying()).toBe(false); // Animation should not be playing initially

  fireEvent.click(screen.getByText('Start Animation'));
  expect(animation.isPlaying()).toBe(true); // Animation should start after clicking

  fireEvent.click(screen.getByText('Stop Animation'));
  expect(animation.isPlaying()).toBe(false); // Animation should stop after clicking
});

6. Responsive Design:

// Playwright Example (Viewport Testing)
const { chromium } = require('playwright');

test('Responsive Design', async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();
  await page.goto('http://yourwebsite.com');

  await page.setViewportSize({ width: 320, height: 568 }); // Mobile viewport
  await page.screenshot({ path: 'mobile.png' });

  await page.setViewportSize({ width: 1280, height: 800 }); // Desktop viewport
  await page.screenshot({ path: 'desktop.png' });

  await browser.close();
});

7. Edge Cases:

// Jest Example (Edge Case Testing)
test('Handle Extreme Values', () => {
  // Test with very low or high weight/height values
});

8. Accessibility:

// Playwright Example (Accessibility Testing)
test('Accessibility', async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();
  await page.goto('http://yourwebsite.com');

  const accessibilityReport = await page.accessibility.snapshot();
  // Analyze accessibilityReport for any issues
  console.log(accessibilityReport);

  await browser.close();
});

9. Input Validation:

// Jest Example (Input Validation Testing)
test('Input Validation', () => {
  // Test scenarios where users input invalid data and ensure correct error messages are displayed
});

10. Integration Testing:

// Jest Example (Integration Testing)
test('Integration Test', () => {
  // Test the interaction between the UI and BMI calculation function
});

These examples should give you a good starting point for testing different aspects of your BMI calculator using Jest for unit testing and Playwright for end-to-end testing. Adjust them according to your specific implementation and requirements.