Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

4.5 KB
Raw Blame
/* eslint-disable react/jsx-props-no-spreading */
/* eslint-disable react/prop-types */
import { render, screen, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';

jest.mock('react-redux', () => ({
  useSelector: (fn) => fn({
    compare: { activeString: 'a' },
    palettes: { a: {} },
  }),
  connect: () => (Component) => Component,
}));

jest.mock('googleTagManager', () => ({
  pushEvent: jest.fn(),
}));

global.ResizeObserver = jest.fn().mockImplementation(() => ({
  observe: jest.fn(),
  unobserve: jest.fn(),
  disconnect: jest.fn(),
}));

// Mock util functions used by the component
jest.mock('../../modules/image-download/util', () => ({
  imageSizeValid: jest.fn(() => true),
  estimateMaxImageSize: jest.fn(() => ({ width: 8192, height: 8192 })),
  calculateScaleFactor: jest.fn(() => 2),
  calculateScaledResolution: jest.fn(() => 1),
  getDimensions: jest.fn(() => ({ width: 10, height: 20 })),
  getTruncatedGranuleDates: jest.fn(() => ({ truncated: true })),
  snapshot: jest.fn(),
  GRANULE_LIMIT: 42,
}));

// Simplified mocks for child components
jest.mock('../util/selector', () => (props) => (
  <select
    data-testid={props.id}
    value={props.value}
    onChange={(e) => props.onChange(props.optionName, e.target.value)}
  >
    {(props.optionArray || []).map((o) => (
      <option key={o} value={o}>{o}</option>
    ))}
  </select>
));

jest.mock('./grid', () => (props) => (
  <div>
    <button type="button" onClick={() => props.onClick(props.width, props.height)}>Download</button>
    <div data-testid="file-size">{props.fileSize}</div>
  </div>
));

jest.mock('../util/alert', () => (props) => (
  props.isOpen ? <div role="alert">{props.title}: {props.message}</div> : null
));

jest.mock('./lat-long-inputs', () => (props) => (
  <div>LatLong: {props.crs}</div>
));

jest.mock('./global-select', () => (props) => (
  <div>GlobalSelect</div>
));

import ImageDownloadPanel from './image-download-panel';
import * as util from '../../modules/image-download/util';

describe('ImageDownloadPanel', () => {
  const baseProps = {
    fileType: 'image/jpeg',
    isWorldfile: false,
    resolution: '1',
    getLayers: () => ([{ id: 'L1' }]),
    url: 'http://example.com/wms',
    lonlats: [0, 0],
    projection: { id: 'geographic', selected: { crs: 'EPSG:4326' } },
    date: new Date('2020-01-01T00:00:00Z'),
    markerCoordinates: null,
    onPanelChange: jest.fn(),
    fileTypeOptions: true,
    fileTypes: ['image/jpeg', 'application/vnd.google-earth.kmz'],
    worldFileOptions: true,
    datelineMessage: 'Crosses dateline',
    map: {
      getLayers: () => ({ getArray: () => [{ wv: { id: 'L1', granuleDates: [] } }] }),
      getView: () => ({
        getProjection: () => ({ getUnits: () => 'degrees', getMetersPerUnit: () => 111000 }),
        getCenter: () => [0, 0],
        getResolution: () => 1000,
        getZoom: () => 5,
        getZoomForResolution: () => 1,
      }),
    },
    viewExtent: null,
    resolutions: ['1', '2'],
    geoLatLong: null,
    onLatLongChange: jest.fn(),
    onResolutionChange: jest.fn(),
    onProgressChange: jest.fn(),
    getAnyOverZoom: jest.fn(() => false),
  };

  beforeEach(() => {
    jest.clearAllMocks();
    // ensure getDimensions returns predictable values per test
    util.getDimensions.mockReturnValue({ width: 100, height: 200 });
  });

  test('renders controls and alert, shows granule warning and debug url after download', () => {
    window.open = jest.fn();

    render(<ImageDownloadPanel {...baseProps} />);

    // resolution select present
    expect(screen.getByTestId('wv-image-resolution')).toBeInTheDocument();

    // filetype select present
    expect(screen.getByTestId('wv-image-format')).toBeInTheDocument();

    // dateline alert renders
    expect(screen.getByRole('alert')).toHaveTextContent('Crosses Dateline Alert');

    // granule warning appears
    expect(screen.getByText(/Warning: A snapshot will capture a max./)).toBeInTheDocument();

    // click download button
    fireEvent.click(screen.getByText('Download'));
  });

  test('worldfile select disabled for kmz', () => {
    const props = { ...baseProps, fileType: 'application/vnd.google-earth.kmz' };
    render(<ImageDownloadPanel {...props} />);

    // filetype select exists and worldfile select renders disabled select element
    const format = screen.getByTestId('wv-image-format');
    expect(format).toBeInTheDocument();

    // The rendered worldfile select in our DOM will be a plain select; ensure text present
    expect(screen.getByText(/Worldfile/)).toBeInTheDocument();
  });
});