scope-not-honoured-notice.test.tsx2.4 KBView on GitHub
import { ScopeNotHonouredNotice } from '@/modules/administeredUser/components/ScopeNotHonouredNotice';
import { render, screen } from '@testing-library/react';

/**
 * The guard that stands between a route regression and an admin editing their
 * own conversation types under a teammate's name.
 *
 * `aop.listAopsForUser` shipped for a while accepting no member scope at all, so
 * a scoped call resolved happily and returned the CALLER's rows. The route now
 * takes `targetUserId`, which the server's own `aop-list-scope` test proves it
 * applies. These cases pin the client half: that the notice stays silent on
 * correctly-scoped rows, and that it still fires if a route ever regresses to
 * answering with the caller's.
 */

const ADMIN = 'user_admin';
const MATE = 'user_mate';

const administered = {
  targetUser: { id: MATE, name: 'Dana Mate', email: '<email>' },
  isAdministeringOther: true,
};

let mockAdministered: {
  targetUser: { id: string; name: string | null; email: string | null } | null;
  isAdministeringOther: boolean;
} = administered;

jest.mock('@/modules/administeredUser/context', () => ({
  useAdministeredUser: () => mockAdministered,
}));

const NOTICE = /did not apply the member scope/i;

beforeEach(() => {
  mockAdministered = administered;
});

describe('ScopeNotHonouredNotice', () => {
  it('stays silent on rows that belong to the administered user', () => {
    // The shape `aop.listAopsForUser` returns for an org admin naming a teammate.
    render(<ScopeNotHonouredNotice rows={[{ userId: MATE }, { userId: MATE }]} />);
    expect(screen.queryByText(NOTICE)).not.toBeInTheDocument();
  });

  it("fires when the rows are the ADMIN's own, which is the regression it exists for", () => {
    render(<ScopeNotHonouredNotice rows={[{ userId: ADMIN }, { userId: ADMIN }]} />);
    expect(screen.getByText(NOTICE)).toBeInTheDocument();
    expect(screen.getByText(/Dana Mate/)).toBeInTheDocument();
  });

  it('stays silent while administering yourself, whoever owns the rows', () => {
    mockAdministered = { targetUser: null, isAdministeringOther: false };
    render(<ScopeNotHonouredNotice rows={[{ userId: ADMIN }]} />);
    expect(screen.queryByText(NOTICE)).not.toBeInTheDocument();
  });

  it('stays silent on an empty list, which carries no evidence either way', () => {
    render(<ScopeNotHonouredNotice rows={[]} />);
    expect(screen.queryByText(NOTICE)).not.toBeInTheDocument();
  });
});