123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- import {render, screen} from 'sentry-test/reactTestingLibrary';
- import {Client} from 'sentry/api';
- import DiscoverQuery from 'sentry/utils/discover/discoverQuery';
- import EventView from 'sentry/utils/discover/eventView';
- describe('DiscoverQuery', function () {
- let location, api, eventView;
- beforeEach(() => {
- api = new Client();
- location = {
- pathname: '/events',
- query: {},
- };
- eventView = EventView.fromSavedQuery({
- id: '',
- name: 'test query',
- version: 2,
- fields: ['transaction', 'count()'],
- projects: [1],
- environment: ['dev'],
- });
- });
- describe('with eventsv2', function () {
- it('fetches data on mount', async function () {
- const getMock = MockApiClient.addMockResponse({
- url: '/organizations/test-org/eventsv2/',
- body: {
- meta: {transaction: 'string', count: 'number'},
- data: [{transaction: '/health', count: 1000}],
- },
- });
- render(
- <DiscoverQuery
- orgSlug="test-org"
- api={api}
- location={location}
- eventView={eventView}
- >
- {({tableData, isLoading}) => {
- if (isLoading) {
- return 'loading';
- }
- return <p>{tableData.data[0].transaction}</p>;
- }}
- </DiscoverQuery>
- );
- await tick();
- // Children should be rendered, and API should be called.
- expect(getMock).toHaveBeenCalledTimes(1);
- expect(await screen.findByText('/health')).toBeInTheDocument();
- });
- it('applies limit and cursor props', async function () {
- const getMock = MockApiClient.addMockResponse({
- url: '/organizations/test-org/eventsv2/',
- body: {
- meta: {transaction: 'string', count: 'number'},
- data: [{transaction: '/health', count: 1000}],
- },
- });
- render(
- <DiscoverQuery
- orgSlug="test-org"
- api={api}
- location={location}
- eventView={eventView}
- limit={3}
- cursor="1:0:1"
- >
- {({tableData, isLoading}) => {
- if (isLoading) {
- return 'loading';
- }
- return <p>{tableData.data[0].transaction}</p>;
- }}
- </DiscoverQuery>
- );
- await tick();
- expect(getMock).toHaveBeenCalledTimes(1);
- expect(getMock).toHaveBeenCalledWith(
- '/organizations/test-org/eventsv2/',
- expect.objectContaining({
- method: 'GET',
- query: expect.objectContaining({
- per_page: 3,
- cursor: '1:0:1',
- }),
- })
- );
- });
- it('parses string errors correctly', async function () {
- MockApiClient.addMockResponse({
- url: '/organizations/test-org/eventsv2/',
- body: {
- detail: 'Error Message',
- },
- statusCode: 400,
- });
- let errorValue;
- render(
- <DiscoverQuery
- orgSlug="test-org"
- api={api}
- location={location}
- eventView={eventView}
- setError={e => (errorValue = e)}
- >
- {({isLoading}) => {
- if (isLoading) {
- return 'loading';
- }
- return null;
- }}
- </DiscoverQuery>
- );
- await tick();
- expect(errorValue.message).toEqual('Error Message');
- });
- it('parses object errors correctly', async function () {
- MockApiClient.addMockResponse({
- url: '/organizations/test-org/eventsv2/',
- body: {
- detail: {
- code: '?',
- message: 'Object Error',
- extra: {},
- },
- },
- statusCode: 400,
- });
- let errorValue;
- render(
- <DiscoverQuery
- orgSlug="test-org"
- api={api}
- location={location}
- eventView={eventView}
- setError={e => (errorValue = e)}
- >
- {({isLoading}) => {
- if (isLoading) {
- return 'loading';
- }
- return null;
- }}
- </DiscoverQuery>
- );
- await tick();
- expect(errorValue.message).toEqual('Object Error');
- });
- });
- describe('with events', function () {
- it('fetches data on mount', async function () {
- const getMock = MockApiClient.addMockResponse({
- url: '/organizations/test-org/events/',
- body: {
- meta: {fields: {transaction: 'string', count: 'number'}},
- data: [{transaction: '/health', count: 1000}],
- },
- });
- render(
- <DiscoverQuery
- orgSlug="test-org"
- api={api}
- location={location}
- eventView={eventView}
- useEvents
- >
- {({tableData, isLoading}) => {
- if (isLoading) {
- return 'loading';
- }
- return <p>{tableData.data[0].transaction}</p>;
- }}
- </DiscoverQuery>
- );
- await tick();
- // Children should be rendered, and API should be called.
- expect(getMock).toHaveBeenCalledTimes(1);
- expect(await screen.findByText('/health')).toBeInTheDocument();
- });
- it('applies limit and cursor props', async function () {
- const getMock = MockApiClient.addMockResponse({
- url: '/organizations/test-org/events/',
- body: {
- meta: {fields: {transaction: 'string', count: 'number'}},
- data: [{transaction: '/health', count: 1000}],
- },
- });
- render(
- <DiscoverQuery
- orgSlug="test-org"
- api={api}
- location={location}
- eventView={eventView}
- limit={3}
- cursor="1:0:1"
- useEvents
- >
- {({tableData, isLoading}) => {
- if (isLoading) {
- return 'loading';
- }
- return <p>{tableData.data[0].transaction}</p>;
- }}
- </DiscoverQuery>
- );
- await tick();
- expect(getMock).toHaveBeenCalledTimes(1);
- expect(getMock).toHaveBeenCalledWith(
- '/organizations/test-org/events/',
- expect.objectContaining({
- method: 'GET',
- query: expect.objectContaining({
- per_page: 3,
- cursor: '1:0:1',
- }),
- })
- );
- });
- it('parses string errors correctly', async function () {
- MockApiClient.addMockResponse({
- url: '/organizations/test-org/events/',
- body: {
- detail: 'Error Message',
- },
- statusCode: 400,
- });
- let errorValue;
- render(
- <DiscoverQuery
- orgSlug="test-org"
- api={api}
- location={location}
- eventView={eventView}
- setError={e => (errorValue = e)}
- useEvents
- >
- {({isLoading}) => {
- if (isLoading) {
- return 'loading';
- }
- return null;
- }}
- </DiscoverQuery>
- );
- await tick();
- expect(errorValue.message).toEqual('Error Message');
- });
- it('parses object errors correctly', async function () {
- MockApiClient.addMockResponse({
- url: '/organizations/test-org/events/',
- body: {
- detail: {
- code: '?',
- message: 'Object Error',
- extra: {},
- },
- },
- statusCode: 400,
- });
- let errorValue;
- render(
- <DiscoverQuery
- orgSlug="test-org"
- api={api}
- location={location}
- eventView={eventView}
- setError={e => (errorValue = e)}
- useEvents
- >
- {({isLoading}) => {
- if (isLoading) {
- return 'loading';
- }
- return null;
- }}
- </DiscoverQuery>
- );
- await tick();
- expect(errorValue.message).toEqual('Object Error');
- });
- });
- });
|