123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- import {Fragment} from 'react';
- import {render, waitFor} from 'sentry-test/reactTestingLibrary';
- import EventsGeoRequest, {
- EventsGeoRequestProps,
- } from 'sentry/components/charts/eventsGeoRequest';
- import * as genericDiscoverQuery from 'sentry/utils/discover/genericDiscoverQuery';
- describe('EventsRequest', function () {
- const project = TestStubs.Project();
- const organization = TestStubs.Organization();
- const makeProps = (
- partial: Partial<EventsGeoRequestProps> = {}
- ): EventsGeoRequestProps => {
- return {
- api: new MockApiClient(),
- organization,
- yAxis: ['count()'],
- query: 'event.type:transaction',
- projects: [parseInt(project.id, 10)],
- period: '24h',
- start: new Date(),
- end: new Date(),
- environments: [],
- children: jest.fn(() => <Fragment />),
- ...partial,
- };
- };
- describe('with props changes', () => {
- afterEach(() => {
- jest.restoreAllMocks();
- });
- it('renders with loading state', async () => {
- jest
- .spyOn(genericDiscoverQuery, 'doDiscoverQuery')
- .mockResolvedValue([{data: 'test'}, undefined, undefined]);
- const mock = jest.fn(() => <Fragment />);
- render(<EventsGeoRequest {...makeProps()}>{mock}</EventsGeoRequest>);
- await waitFor(() => {
- expect(mock).toHaveBeenNthCalledWith(
- 1,
- expect.objectContaining({
- errored: false,
- loading: true,
- reloading: false,
- tableData: undefined,
- })
- );
- });
- });
- it('makes requests', async () => {
- jest
- .spyOn(genericDiscoverQuery, 'doDiscoverQuery')
- .mockResolvedValue([{data: 'test'}, undefined, undefined]);
- const mock = jest.fn(() => <Fragment />);
- render(<EventsGeoRequest {...makeProps()}>{mock}</EventsGeoRequest>);
- await waitFor(() => {
- expect(mock).toHaveBeenLastCalledWith(
- expect.objectContaining({
- errored: false,
- loading: false,
- reloading: false,
- tableData: [{data: 'test'}],
- })
- );
- });
- });
- it('renders with error if request errors', async () => {
- jest.spyOn(genericDiscoverQuery, 'doDiscoverQuery').mockRejectedValue({});
- const mock = jest.fn(() => <Fragment />);
- render(<EventsGeoRequest {...makeProps()}>{mock}</EventsGeoRequest>);
- await waitFor(() => {
- expect(mock).toHaveBeenLastCalledWith(
- expect.objectContaining({
- errored: true,
- loading: false,
- reloading: false,
- tableData: undefined,
- })
- );
- });
- });
- it.each([{query: 'event.type:error'}, {yAxis: 'failure_count()'}, {period: '12h'}])(
- 'rerenders if %j prop changes',
- async (rerenderProps: Partial<EventsGeoRequestProps>) => {
- jest
- .spyOn(genericDiscoverQuery, 'doDiscoverQuery')
- .mockResolvedValue([{data: 'test'}, undefined, undefined]);
- const mock = jest.fn(() => <Fragment />);
- const {rerender} = render(
- <EventsGeoRequest {...makeProps()}>{mock}</EventsGeoRequest>
- );
- rerender(
- <EventsGeoRequest {...makeProps(rerenderProps)}>{mock}</EventsGeoRequest>
- );
- await waitFor(() => {
- expect(genericDiscoverQuery.doDiscoverQuery).toHaveBeenNthCalledWith(
- 2,
- expect.anything(),
- expect.anything(),
- expect.objectContaining(
- rerenderProps.period ? {statsPeriod: rerenderProps.period} : rerenderProps
- )
- );
- });
- }
- );
- });
- });
|