Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

2.4 KB
Raw Blame
import { ChakraProvider } from '@chakra-ui/react';
import { system } from '@theme/system';
import { act } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { afterEach, describe, expect, it } from 'vitest';

import { ProjectCover } from './ProjectCover';

const PIXEL = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
const BROKEN = 'data:image/gif;base64,not-an-image';

let host: HTMLDivElement | null = null;
let root: Root | null = null;
(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;

afterEach(async () => {
  await act(() => root?.unmount());
  host?.remove();
  host = null;
  root = null;
});

const render = async (coverUrl?: string) => {
  host = document.createElement('div');
  host.style.width = '320px';
  document.body.append(host);
  root = createRoot(host);

  await act(() => {
    root?.render(
      <ChakraProvider value={system}>
        <ProjectCover coverUrl={coverUrl} />
      </ChakraProvider>
    );
  });

  return host;
};

const waitFor = async (predicate: () => boolean) => {
  for (let attempt = 0; attempt < 50; attempt += 1) {
    if (predicate()) {
      return;
    }

    await act(async () => {
      await new Promise((resolve) => {
        setTimeout(resolve, 20);
      });
    });
  }

  throw new Error('condition never became true');
};

describe('ProjectCover', () => {
  it('renders the cover as a decorative image when one is supplied', async () => {
    const container = await render(PIXEL);
    const image = container.querySelector('img');

    expect(image).not.toBeNull();
    expect(image?.getAttribute('alt')).toBe('');
    expect(image?.getAttribute('src')).toBe(PIXEL);
  });

  it('falls back to the glyph when the cover image fails to load', async () => {
    const container = await render(BROKEN);

    await waitFor(() => container.querySelector('img') === null);
    expect(container.querySelector('svg')).not.toBeNull();
  });

  it('reserves the same box whether or not a cover is present', async () => {
    const withoutCover = (await render()).firstElementChild?.getBoundingClientRect();

    await act(() => root?.unmount());
    host?.remove();

    const withCover = (await render(PIXEL)).firstElementChild?.getBoundingClientRect();

    expect(withoutCover?.height).toBeGreaterThan(0);
    expect(withCover?.height).toBe(withoutCover?.height);
  });
});