import React from 'react';
import { render, screen } from '@testing-library/react';

jest.mock('@react-oauth/google', () => ({
  GoogleLogin: ({ text }: { text: string }) => <button data-testid="google-login">{text}</button>,
}));

import SocialLoginBlock from '../SocialLoginBlock';

const GOOGLE_CLIENT_ID_VAR = 'NEXT_PUBLIC_GOOGLE_CLIENT_ID';

function renderBlock() {
  render(
    <SocialLoginBlock
      label="Ou continuer avec"
      buttonText="signin_with"
      onSuccess={jest.fn()}
      onError={jest.fn()}
    />
  );
}

describe('SocialLoginBlock', () => {
  const originalClientId = process.env[GOOGLE_CLIENT_ID_VAR];

  afterEach(() => {
    if (originalClientId === undefined) {
      delete process.env[GOOGLE_CLIENT_ID_VAR];
    } else {
      process.env[GOOGLE_CLIENT_ID_VAR] = originalClientId;
    }
  });

  it('shouldOfferGoogleWhenAClientIdIsConfigured', () => {
    process.env[GOOGLE_CLIENT_ID_VAR] = 'client-id-de-test';

    renderBlock();

    expect(screen.getByText('Ou continuer avec')).toBeInTheDocument();
    expect(screen.getByTestId('google-login')).toHaveTextContent('signin_with');
  });

  // Sans fournisseur, un « Ou continuer avec » suivi de rien induit en erreur :
  // le bloc entier disparaît.
  it('shouldRenderNothingWithoutAnyProvider', () => {
    delete process.env[GOOGLE_CLIENT_ID_VAR];

    const { container } = render(
      <SocialLoginBlock
        label="Ou continuer avec"
        buttonText="signin_with"
        onSuccess={jest.fn()}
        onError={jest.fn()}
      />
    );

    expect(screen.queryByTestId('google-login')).not.toBeInTheDocument();
    expect(screen.queryByText('Ou continuer avec')).not.toBeInTheDocument();
    expect(container).toBeEmptyDOMElement();
  });
});
