All files / src/features/profile PairedWatchCard.tsx

100% Statements 8/8
75% Branches 3/4
100% Functions 4/4
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92                                  1x 8x 8x           8x                                                                 1x 1x                 1x                       2x                      
/**
 * PairedWatchCard Component
 * Story W2.1: Pairing Code Backend & Web Frontend — AC4
 * Displays a paired watch with device name, paired date, and Unpair button.
 */
 
import React, { useState } from 'react';
import { Box, Button, Typography } from '@mui/material';
import WatchIcon from '@mui/icons-material/Watch';
import type { PairedWatch } from '@/types/watch';
 
interface PairedWatchCardProps {
  watch: PairedWatch;
  onUnpair: (watchId: string) => void;
  isUnpairing?: boolean;
}
 
const PairedWatchCard: React.FC<PairedWatchCardProps> = ({ watch, onUnpair, isUnpairing }) => {
  const [confirmPending, setConfirmPending] = useState(false);
  const formattedDate = new Date(watch.pairedAt).toLocaleDateString('en-GB', {
    year: 'numeric',
    month: 'short',
    day: 'numeric',
  });
 
  return (
    <Box
      sx={{
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'space-between',
        p: 1.5,
        border: '1px solid',
        borderColor: 'divider',
        borderRadius: 1,
        mb: 1,
      }}
      data-testid={`paired-watch-card-${watch.id}`}
    >
      <Box sx={{ display: 'flex', alignItems: 'center', gap: 1.5 }}>
        <WatchIcon color="action" />
        <Box>
          <Typography variant="body2" fontWeight="medium" data-testid="watch-device-name">
            {watch.deviceName || 'Apple Watch'}
          </Typography>
          <Typography variant="caption" color="text.secondary" data-testid="watch-paired-date">
            Paired {formattedDate}
          </Typography>
        </Box>
      </Box>
      {confirmPending ? (
        <Box sx={{ display: 'flex', gap: 1 }}>
          <Button
            size="small"
            variant="contained"
            color="error"
            disabled={isUnpairing}
            onClick={() => {
              setConfirmPending(false);
              onUnpair(watch.id);
            }}
            data-testid={`unpair-confirm-button-${watch.id}`}
          >
            Confirm
          </Button>
          <Button
            size="small"
            variant="outlined"
            onClick={() => setConfirmPending(false)}
            data-testid={`unpair-cancel-button-${watch.id}`}
          >
            Cancel
          </Button>
        </Box>
      ) : (
        <Button
          size="small"
          variant="outlined"
          color="error"
          disabled={isUnpairing}
          onClick={() => setConfirmPending(true)}
          data-testid={`unpair-button-${watch.id}`}
        >
          Unpair
        </Button>
      )}
    </Box>
  );
};
 
export default PairedWatchCard;