123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- import {OrganizationFixture} from 'sentry-fixture/organization';
- import {
- ReservedBudgetFixture,
- ReservedBudgetMetricHistoryFixture,
- } from 'getsentry-test/fixtures/reservedBudget';
- import {Am3DsEnterpriseSubscriptionFixture} from 'getsentry-test/fixtures/subscription';
- import {renderGlobalModal, screen, userEvent} from 'sentry-test/reactTestingLibrary';
- import type {Subscription} from 'getsentry/types';
- import addGiftBudgetAction from './addGiftBudgetAction';
- describe('GiftBudgetAction', function () {
- const organization = OrganizationFixture();
- const subscription = Am3DsEnterpriseSubscriptionFixture({
- organization,
- });
- beforeEach(() => {
- MockApiClient.addMockResponse({
- url: `/customers/${organization.slug}/`,
- method: 'PUT',
- });
- });
- afterEach(() => {
- MockApiClient.clearMockResponses();
- });
- const openGiftBudgetModal = () => {
- addGiftBudgetAction({
- organization,
- subscription,
- onSuccess: () => {},
- });
- renderGlobalModal();
- };
- it('renders modal with budget information', function () {
- openGiftBudgetModal();
- expect(screen.getByText('Add Gift Budget')).toBeInTheDocument();
- expect(screen.getByText('Reserved Budget:')).toBeInTheDocument();
- expect(screen.getByText('$100,000')).toBeInTheDocument();
- expect(screen.getByText('Existing Free Budget:')).toBeInTheDocument();
- expect(screen.getByText('$0')).toBeInTheDocument();
- expect(screen.getByText(/accepted spans, stored spans/i)).toBeInTheDocument();
- });
- it('validates gift amount input', async function () {
- openGiftBudgetModal();
- const giftInput = screen.getByRole('spinbutton', {name: 'Gift Amount ($)'});
-
- await userEvent.clear(giftInput);
- await userEvent.type(giftInput, '-50');
- expect(screen.getByText(/Total Gift: \$0/)).toBeInTheDocument();
-
- await userEvent.clear(giftInput);
- await userEvent.type(giftInput, '15000');
- expect(screen.getByText(/Total Gift: \$10,000/)).toBeInTheDocument();
-
- await userEvent.clear(giftInput);
- await userEvent.type(giftInput, '500');
- expect(screen.getByText(/Total Gift: \$500/)).toBeInTheDocument();
- });
- it('requires notes field', function () {
- openGiftBudgetModal();
- const createButton = screen.getByRole('button', {name: /confirm/i});
- expect(createButton).toBeDisabled();
- });
- it('submits form with correct data', async function () {
- const updateMock = MockApiClient.addMockResponse({
- url: `/customers/${organization.slug}/`,
- method: 'PUT',
- body: {},
- });
- openGiftBudgetModal();
-
- await userEvent.type(
- screen.getByRole('spinbutton', {name: 'Gift Amount ($)'}),
- '500'
- );
- await userEvent.type(
- screen.getByRole('textbox', {name: 'TicketUrl'}),
- 'https://example.com'
- );
- await userEvent.type(screen.getByRole('textbox', {name: 'Notes'}), 'Test notes');
-
- await userEvent.click(screen.getByRole('button', {name: 'Confirm'}));
- expect(updateMock).toHaveBeenCalledWith(
- `/customers/${organization.slug}/`,
- expect.objectContaining({
- method: 'PUT',
- data: {
- freeReservedBudget: {
- id: '11',
- freeBudget: 50000,
- categories: ['spans', 'spansIndexed'],
- },
- ticketUrl: 'https://example.com',
- notes: 'Test notes',
- },
- })
- );
- });
- it('handles multiple reserved budgets', function () {
- const multiBudgetSub: Subscription = {
- ...subscription,
- reservedBudgets: [
- ReservedBudgetFixture({
- id: '11',
- reservedBudget: 100_000_00,
- totalReservedSpend: 60_000_00,
- freeBudget: 0,
- percentUsed: 0.6,
- categories: {
- spans: ReservedBudgetMetricHistoryFixture({
- reservedCpe: 1,
- reservedSpend: 40_000_00,
- }),
- },
- }),
- ReservedBudgetFixture({
- id: '22',
- reservedBudget: 100_000_00,
- totalReservedSpend: 60_000_00,
- freeBudget: 0,
- percentUsed: 0.6,
- categories: {
- spansIndexed: ReservedBudgetMetricHistoryFixture({
- reservedCpe: 2,
- reservedSpend: 20_000_00,
- }),
- },
- }),
- ],
- };
- addGiftBudgetAction({
- organization,
- subscription: multiBudgetSub,
- onSuccess: () => {},
- });
- renderGlobalModal();
- expect(
- screen.getByText('Select a reserved budget to add gift amount.')
- ).toBeInTheDocument();
- expect(screen.getAllByText(/Reserved Budget:/i)).toHaveLength(2);
- });
- });
|