123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661 |
- import {OrganizationFixture} from 'sentry-fixture/organization';
- import {SubscriptionFixture} from 'getsentry-test/fixtures/subscription';
- import {
- fireEvent,
- render,
- renderGlobalModal,
- screen,
- userEvent,
- } from 'sentry-test/reactTestingLibrary';
- import SubscriptionStore from 'getsentry/stores/subscriptionStore';
- import {OnDemandBudgetMode, PlanTier, type Subscription} from 'getsentry/types';
- import {OnDemandSettings} from './onDemandSettings';
- describe('edit on-demand budget', () => {
- const organization = OrganizationFixture({
- features: ['ondemand-budgets'],
- });
- const onDemandOrg = OrganizationFixture({
- features: ['ondemand-budgets'],
- access: ['org:billing'],
- });
- beforeEach(() => {
- MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/monitor-count/`,
- method: 'GET',
- body: {enabledMonitorCount: 0, disabledMonitorCount: 0},
- });
- });
- it('allows VC partner accounts to edit on-demand budget without payment source', function () {
- const subscription = SubscriptionFixture({
- plan: 'am3_business',
- planTier: PlanTier.AM3,
- isFree: false,
- isTrial: false,
- supportsOnDemand: true,
- organization: onDemandOrg,
- paymentSource: null,
- partner: {
- externalId: 'x123x',
- name: 'VC Org',
- partnership: {
- id: 'VC',
- displayName: 'VC',
- supportNote: '',
- },
- isActive: true,
- },
- onDemandBudgets: {
- enabled: true,
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 4500,
- onDemandSpendUsed: 0,
- },
- });
- SubscriptionStore.set(onDemandOrg.slug, subscription);
- render(<OnDemandSettings organization={organization} subscription={subscription} />, {
- organization: onDemandOrg,
- });
- // Should show Edit button even without payment source
- expect(screen.getByText('Edit')).toBeInTheDocument();
- expect(screen.getByText('Pay-as-you-go Budget')).toBeInTheDocument();
- expect(screen.getByText('$45')).toBeInTheDocument();
- });
- it('requires payment source for non-VC accounts', function () {
- const subscription = SubscriptionFixture({
- plan: 'am3_business',
- planTier: PlanTier.AM3,
- isFree: false,
- isTrial: false,
- supportsOnDemand: true,
- organization: onDemandOrg,
- paymentSource: null,
- onDemandBudgets: {
- enabled: true,
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 4500,
- onDemandSpendUsed: 0,
- },
- });
- SubscriptionStore.set(onDemandOrg.slug, subscription);
- render(<OnDemandSettings organization={organization} subscription={subscription} />, {
- organization: onDemandOrg,
- });
- // Should show Add Credit Card message
- expect(screen.getByText('Add Credit Card')).toBeInTheDocument();
- expect(
- screen.getByText(
- "To set a pay-as-you-go budget, you'll need a valid credit card on file."
- )
- ).toBeInTheDocument();
- });
- it('switch from shared budget to per-category budget', async function () {
- MockApiClient.addMockResponse({
- url: `/customers/${organization.slug}/ondemand-budgets/`,
- method: 'POST',
- statusCode: 200,
- body: {
- enabled: true,
- budgetMode: OnDemandBudgetMode.PER_CATEGORY,
- errorsBudget: 1000,
- transactionsBudget: 2000,
- attachmentsBudget: 3000,
- budgets: {errors: 1000, transactions: 2000, attachments: 3000},
- },
- });
- const subscription = SubscriptionFixture({
- plan: 'am1_business',
- planTier: PlanTier.AM1,
- isFree: false,
- isTrial: false,
- supportsOnDemand: true,
- organization: onDemandOrg,
- onDemandBudgets: {
- enabled: true,
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 4500,
- onDemandSpendUsed: 0,
- },
- });
- SubscriptionStore.set(onDemandOrg.slug, subscription);
- MockApiClient.addMockResponse({
- url: `/subscriptions/${onDemandOrg.slug}/`,
- method: 'GET',
- statusCode: 200,
- body: {
- ...subscription,
- onDemandMaxSpend: 1000 + 2000 + 3000,
- onDemandSpendUsed: 100 + 200 + 300,
- onDemandBudgets: {
- enabled: true,
- budgetMode: OnDemandBudgetMode.PER_CATEGORY,
- errorsBudget: 1000,
- transactionsBudget: 2000,
- attachmentsBudget: 3000,
- budgets: {
- errors: 1000,
- transactions: 2000,
- attachments: 3000,
- monitorSeats: 4000,
- },
- errorSpendUsed: 100,
- transactionSpendUsed: 200,
- attachmentSpendUsed: 300,
- usedSpends: {
- errors: 100,
- transactions: 200,
- attachments: 300,
- monitorSeats: 400,
- },
- },
- },
- });
- const {rerender} = render(
- <OnDemandSettings organization={organization} subscription={subscription} />,
- {
- organization: onDemandOrg,
- }
- );
- const {waitForModalToHide} = renderGlobalModal();
- expect(await screen.findByTestId('shared-budget-info')).toBeInTheDocument();
- expect(screen.getByText('$45')).toBeInTheDocument();
- await userEvent.click(screen.getByText('Edit'));
- expect(await screen.findByText('Edit On-Demand Budgets')).toBeInTheDocument();
- expect(screen.getByTestId('shared-budget-radio')).toBeChecked();
- expect(screen.getByRole('textbox', {name: 'Shared max budget'})).toHaveValue('45');
- // Select per-category budget strategy
- await userEvent.click(screen.getByTestId('per-category-budget-radio'));
- expect(screen.getByTestId('per-category-budget-radio')).toBeChecked();
- expect(screen.getByTestId('shared-budget-radio')).not.toBeChecked();
- expect(
- screen.queryByRole('textbox', {name: 'Shared max budget'})
- ).not.toBeInTheDocument();
- // Shared budget should split 50:50 between transactions and errors (whole dollars, remainder added to errors)
- expect(screen.getByRole('textbox', {name: 'Errors budget'})).toHaveValue('23');
- expect(screen.getByRole('textbox', {name: 'Transactions budget'})).toHaveValue('22');
- expect(screen.getByRole('textbox', {name: 'Attachments budget'})).toHaveValue('0');
- expect(screen.getByRole('textbox', {name: 'Cron monitors budget'})).toHaveValue('0');
- fireEvent.change(screen.getByRole('textbox', {name: 'Errors budget'}), {
- target: {value: '10'},
- });
- fireEvent.change(screen.getByRole('textbox', {name: 'Transactions budget'}), {
- target: {value: '20'},
- });
- fireEvent.change(screen.getByRole('textbox', {name: 'Attachments budget'}), {
- target: {value: '30'},
- });
- fireEvent.change(screen.getByRole('textbox', {name: 'Cron monitors budget'}), {
- target: {value: '40'},
- });
- await userEvent.click(screen.getByLabelText('Save'));
- await waitForModalToHide();
- const updatedSubscription = await new Promise<Subscription>(resolve => {
- SubscriptionStore.get(organization.slug, resolve);
- });
- expect(updatedSubscription.onDemandMaxSpend).toBe(1000 + 2000 + 3000);
- rerender(
- <OnDemandSettings organization={organization} subscription={updatedSubscription} />
- );
- expect(await screen.findByText('$10')).toBeInTheDocument();
- expect(screen.getByText('$20')).toBeInTheDocument();
- expect(screen.getByText('$30')).toBeInTheDocument();
- expect(screen.getByText('$40')).toBeInTheDocument();
- expect(screen.getByTestId('per-category-budget-info')).toBeInTheDocument();
- });
- it('switch from shared budget to per-category budget with sub-$1.00 budget', async function () {
- MockApiClient.addMockResponse({
- url: `/customers/${organization.slug}/ondemand-budgets/`,
- method: 'POST',
- statusCode: 200,
- body: {
- enabled: true,
- budgetMode: OnDemandBudgetMode.PER_CATEGORY,
- errorsBudget: 100,
- transactionsBudget: 0,
- attachmentsBudget: 3000,
- budgets: {
- errors: 100,
- transactions: 0,
- attachments: 2400,
- replays: 300,
- monitorSeats: 200,
- },
- },
- });
- const subscription = SubscriptionFixture({
- plan: 'am1_business',
- planTier: PlanTier.AM1,
- isFree: false,
- isTrial: false,
- supportsOnDemand: true,
- organization: onDemandOrg,
- onDemandBudgets: {
- enabled: true,
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 76,
- onDemandSpendUsed: 0,
- },
- });
- SubscriptionStore.set(onDemandOrg.slug, subscription);
- MockApiClient.addMockResponse({
- url: `/subscriptions/${onDemandOrg.slug}/`,
- method: 'GET',
- statusCode: 200,
- body: {
- ...subscription,
- onDemandMaxSpend: 100 + 3000,
- onDemandSpendUsed: 76,
- onDemandBudgets: {
- enabled: true,
- budgetMode: OnDemandBudgetMode.PER_CATEGORY,
- errorsBudget: 100,
- transactionsBudget: 0,
- attachmentsBudget: 3000,
- budgets: {
- errors: 100,
- transactions: 0,
- attachments: 2400,
- replays: 300,
- monitorSeats: 200,
- },
- errorSpendUsed: 76,
- transactionSpendUsed: 0,
- attachmentSpendUsed: 0,
- usedSpends: {
- errors: 76,
- transactions: 0,
- attachments: 0,
- replays: 0,
- monitorSeats: 100,
- },
- },
- },
- });
- const {rerender} = render(
- <OnDemandSettings organization={organization} subscription={subscription} />,
- {
- organization: onDemandOrg,
- }
- );
- const {waitForModalToHide} = renderGlobalModal();
- expect(await screen.findByTestId('shared-budget-info')).toBeInTheDocument();
- expect(screen.getByText('$0.76')).toBeInTheDocument();
- await userEvent.click(screen.getByText('Edit'));
- expect(await screen.findByText('Edit On-Demand Budgets')).toBeInTheDocument();
- expect(screen.getByTestId('shared-budget-radio')).toBeChecked();
- expect(screen.getByRole('textbox', {name: 'Shared max budget'})).toHaveValue('0.76');
- // Select per-category budget strategy
- await userEvent.click(screen.getByTestId('per-category-budget-radio'));
- expect(screen.getByTestId('per-category-budget-radio')).toBeChecked();
- expect(screen.getByTestId('shared-budget-radio')).not.toBeChecked();
- expect(
- screen.queryByRole('textbox', {name: 'Shared max budget'})
- ).not.toBeInTheDocument();
- // Shared budget should split 50:50 between transactions and errors (whole dollars, remainder added to errors)
- expect(screen.getByRole('textbox', {name: 'Errors budget'})).toHaveValue('1');
- expect(screen.getByRole('textbox', {name: 'Transactions budget'})).toHaveValue('0');
- expect(screen.getByRole('textbox', {name: 'Attachments budget'})).toHaveValue('0');
- expect(screen.getByRole('textbox', {name: 'Cron monitors budget'})).toHaveValue('0');
- fireEvent.change(screen.getByRole('textbox', {name: 'Attachments budget'}), {
- target: {value: '30'},
- });
- await userEvent.click(screen.getByLabelText('Save'));
- await waitForModalToHide();
- const updatedSubscription = await new Promise<Subscription>(resolve => {
- SubscriptionStore.get(onDemandOrg.slug, resolve);
- });
- expect(updatedSubscription.onDemandMaxSpend).toBe(3100);
- rerender(
- <OnDemandSettings organization={organization} subscription={updatedSubscription} />
- );
- expect(await screen.findByText('$3')).toBeInTheDocument();
- expect(screen.getByText('$24')).toBeInTheDocument();
- expect(screen.getByText('$2')).toBeInTheDocument();
- expect(screen.getByTestId('per-category-budget-info')).toBeInTheDocument();
- });
- it('switch from per-category budget to shared budget', async function () {
- MockApiClient.addMockResponse({
- url: `/customers/${onDemandOrg.slug}/ondemand-budgets/`,
- method: 'POST',
- statusCode: 200,
- body: {
- enabled: true,
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 4200,
- },
- });
- const subscription = SubscriptionFixture({
- plan: 'am1_business',
- planTier: PlanTier.AM1,
- isFree: false,
- isTrial: false,
- supportsOnDemand: true,
- organization: onDemandOrg,
- onDemandBudgets: {
- enabled: true,
- budgetMode: OnDemandBudgetMode.PER_CATEGORY,
- errorsBudget: 1000,
- transactionsBudget: 2000,
- attachmentsBudget: 3000,
- replaysBudget: 0,
- budgets: {
- errors: 1000,
- transactions: 2000,
- attachments: 3000,
- replays: 0,
- monitorSeats: 5000,
- },
- attachmentSpendUsed: 0,
- errorSpendUsed: 0,
- transactionSpendUsed: 0,
- usedSpends: {},
- },
- });
- SubscriptionStore.set(onDemandOrg.slug, subscription);
- MockApiClient.addMockResponse({
- url: `/subscriptions/${onDemandOrg.slug}/`,
- method: 'GET',
- statusCode: 200,
- body: {
- ...subscription,
- onDemandMaxSpend: 4200,
- onDemandSpendUsed: 2022,
- onDemandBudgets: {
- enabled: true,
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 4200,
- onDemandSpendUsed: 2022,
- },
- },
- });
- const {rerender} = render(
- <OnDemandSettings organization={organization} subscription={subscription} />,
- {
- organization: onDemandOrg,
- }
- );
- const {waitForModalToHide} = renderGlobalModal();
- expect(await screen.findByTestId('per-category-budget-info')).toBeInTheDocument();
- expect(screen.getByText('$10')).toBeInTheDocument();
- expect(screen.getByText('$20')).toBeInTheDocument();
- expect(screen.getByText('$30')).toBeInTheDocument();
- await userEvent.click(screen.getByText('Edit'));
- expect(await screen.findByText('Edit On-Demand Budgets')).toBeInTheDocument();
- expect(screen.getByTestId('per-category-budget-radio')).toBeChecked();
- expect(screen.getByRole('textbox', {name: 'Errors budget'})).toHaveValue('10');
- expect(screen.getByRole('textbox', {name: 'Transactions budget'})).toHaveValue('20');
- expect(screen.getByRole('textbox', {name: 'Attachments budget'})).toHaveValue('30');
- expect(screen.getByRole('textbox', {name: 'Cron monitors budget'})).toHaveValue('50');
- // Select shared budget strategy
- await userEvent.click(screen.getByTestId('shared-budget-radio'));
- expect(screen.getByTestId('shared-budget-radio')).toBeChecked();
- expect(screen.getByTestId('per-category-budget-radio')).not.toBeChecked();
- expect(
- screen.queryByRole('textbox', {name: 'Errors budget'})
- ).not.toBeInTheDocument();
- expect(
- screen.queryByRole('textbox', {name: 'Transactions budget'})
- ).not.toBeInTheDocument();
- expect(
- screen.queryByRole('textbox', {name: 'Attachments budget'})
- ).not.toBeInTheDocument();
- expect(
- screen.queryByRole('textbox', {name: 'Cron Monitors budget'})
- ).not.toBeInTheDocument();
- // Default shared budget should be total of the current per-category budget.
- expect(screen.getByRole('textbox', {name: 'Shared max budget'})).toHaveValue('110');
- fireEvent.change(screen.getByRole('textbox', {name: 'Shared max budget'}), {
- target: {value: '42'},
- });
- await userEvent.click(screen.getByLabelText('Save'));
- await waitForModalToHide();
- const updatedSubscription = await new Promise<Subscription>(resolve => {
- SubscriptionStore.get(onDemandOrg.slug, resolve);
- });
- expect(updatedSubscription.onDemandMaxSpend).toBe(4200);
- rerender(
- <OnDemandSettings organization={organization} subscription={updatedSubscription} />
- );
- expect(await screen.findByText('$42')).toBeInTheDocument();
- expect(screen.getByTestId('shared-budget-info')).toBeInTheDocument();
- });
- it('disable shared on-demand budget', async function () {
- MockApiClient.addMockResponse({
- url: `/customers/${onDemandOrg.slug}/ondemand-budgets/`,
- method: 'POST',
- statusCode: 200,
- body: {
- enabled: false,
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 0,
- },
- });
- const subscription = SubscriptionFixture({
- plan: 'am1_business',
- planTier: PlanTier.AM1,
- isFree: false,
- isTrial: false,
- supportsOnDemand: true,
- organization: onDemandOrg,
- onDemandBudgets: {
- enabled: true,
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 4200,
- onDemandSpendUsed: 0,
- },
- });
- SubscriptionStore.set(onDemandOrg.slug, subscription);
- MockApiClient.addMockResponse({
- url: `/subscriptions/${onDemandOrg.slug}/`,
- method: 'GET',
- statusCode: 200,
- body: {
- ...subscription,
- onDemandMaxSpend: 0,
- onDemandSpendUsed: 0,
- onDemandBudgets: {
- enabled: false,
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 0,
- onDemandSpendUsed: 0,
- },
- },
- });
- const {rerender} = render(
- <OnDemandSettings organization={organization} subscription={subscription} />,
- {
- organization: onDemandOrg,
- }
- );
- const {waitForModalToHide} = renderGlobalModal();
- expect(await screen.findByTestId('shared-budget-info')).toBeInTheDocument();
- expect(screen.getByText('$42')).toBeInTheDocument();
- await userEvent.click(screen.getByText('Edit'));
- expect(await screen.findByText('Edit On-Demand Budgets')).toBeInTheDocument();
- expect(screen.getByTestId('shared-budget-radio')).toBeChecked();
- expect(screen.getByRole('textbox', {name: 'Shared max budget'})).toHaveValue('42');
- // Disable on-demand budgets
- fireEvent.change(screen.getByRole('textbox', {name: 'Shared max budget'}), {
- target: {value: '0'},
- });
- await userEvent.click(screen.getByLabelText('Save'));
- await waitForModalToHide();
- const updatedSubscription = await new Promise<Subscription>(resolve => {
- SubscriptionStore.get(onDemandOrg.slug, resolve);
- });
- expect(updatedSubscription.onDemandMaxSpend).toBe(0);
- rerender(
- <OnDemandSettings organization={organization} subscription={updatedSubscription} />
- );
- expect(await screen.findByText('Set Up On-Demand')).toBeInTheDocument();
- expect(screen.queryByTestId('per-category-budget-info')).not.toBeInTheDocument();
- expect(screen.queryByTestId('shared-budget-info')).not.toBeInTheDocument();
- });
- it('disable per-category on-demand budget', async function () {
- MockApiClient.addMockResponse({
- url: `/customers/${onDemandOrg.slug}/ondemand-budgets/`,
- method: 'POST',
- statusCode: 200,
- body: {
- enabled: false,
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 0,
- },
- });
- const subscription = SubscriptionFixture({
- plan: 'am1_business',
- planTier: PlanTier.AM1,
- isFree: false,
- isTrial: false,
- supportsOnDemand: true,
- organization: onDemandOrg,
- onDemandBudgets: {
- enabled: true,
- budgetMode: OnDemandBudgetMode.PER_CATEGORY,
- errorsBudget: 1000,
- transactionsBudget: 2000,
- attachmentsBudget: 3000,
- replaysBudget: 0,
- budgets: {
- errors: 1000,
- transactions: 2000,
- attachments: 3000,
- replays: 0,
- monitorSeats: 5000,
- },
- attachmentSpendUsed: 0,
- errorSpendUsed: 0,
- transactionSpendUsed: 0,
- usedSpends: {},
- },
- });
- SubscriptionStore.set(onDemandOrg.slug, subscription);
- MockApiClient.addMockResponse({
- url: `/subscriptions/${onDemandOrg.slug}/`,
- method: 'GET',
- statusCode: 200,
- body: {
- ...subscription,
- onDemandMaxSpend: 0,
- onDemandSpendUsed: 0,
- onDemandBudgets: {
- enabled: false,
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 0,
- onDemandSpendUsed: 0,
- },
- },
- });
- const {rerender} = render(
- <OnDemandSettings organization={organization} subscription={subscription} />,
- {
- organization: onDemandOrg,
- }
- );
- const {waitForModalToHide} = renderGlobalModal();
- expect(await screen.findByTestId('per-category-budget-info')).toBeInTheDocument();
- expect(screen.getByText('$10')).toBeInTheDocument();
- expect(screen.getByText('$20')).toBeInTheDocument();
- expect(screen.getByText('$30')).toBeInTheDocument();
- await userEvent.click(screen.getByText('Edit'));
- expect(await screen.findByText('Edit On-Demand Budgets')).toBeInTheDocument();
- expect(screen.getByTestId('per-category-budget-radio')).toBeChecked();
- expect(screen.getByRole('textbox', {name: 'Errors budget'})).toHaveValue('10');
- expect(screen.getByRole('textbox', {name: 'Transactions budget'})).toHaveValue('20');
- expect(screen.getByRole('textbox', {name: 'Attachments budget'})).toHaveValue('30');
- expect(screen.getByRole('textbox', {name: 'Cron monitors budget'})).toHaveValue('50');
- // Disable on-demand budgets
- fireEvent.change(screen.getByRole('textbox', {name: 'Errors budget'}), {
- target: {value: '0'},
- });
- fireEvent.change(screen.getByRole('textbox', {name: 'Transactions budget'}), {
- target: {value: '0'},
- });
- fireEvent.change(screen.getByRole('textbox', {name: 'Attachments budget'}), {
- target: {value: '0'},
- });
- fireEvent.change(screen.getByRole('textbox', {name: 'Cron monitors budget'}), {
- target: {value: '0'},
- });
- await userEvent.click(screen.getByLabelText('Save'));
- await waitForModalToHide();
- const updatedSubscription = await new Promise<Subscription>(resolve => {
- SubscriptionStore.get(onDemandOrg.slug, resolve);
- });
- expect(updatedSubscription.onDemandMaxSpend).toBe(0);
- rerender(
- <OnDemandSettings organization={organization} subscription={updatedSubscription} />
- );
- expect(await screen.findByText('Set Up On-Demand')).toBeInTheDocument();
- expect(screen.queryByTestId('per-category-budget-info')).not.toBeInTheDocument();
- expect(screen.queryByTestId('shared-budget-info')).not.toBeInTheDocument();
- });
- });
|