123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675 |
- import {LocationFixture} from 'sentry-fixture/locationFixture';
- import {OrganizationFixture} from 'sentry-fixture/organization';
- import {ProjectFixture} from 'sentry-fixture/project';
- import {initializeOrg} from 'sentry-test/initializeOrg';
- import {
- render,
- renderGlobalModal,
- screen,
- userEvent,
- waitFor,
- } from 'sentry-test/reactTestingLibrary';
- import * as pageFilterUtils from 'sentry/components/organizations/pageFilters/persistence';
- import ProjectsStore from 'sentry/stores/projectsStore';
- import {browserHistory} from 'sentry/utils/browserHistory';
- import EventView from 'sentry/utils/discover/eventView';
- import {DEFAULT_EVENT_VIEW} from './data';
- import Homepage from './homepage';
- describe('Discover > Homepage', () => {
- const features = ['global-views', 'discover-query'];
- let initialData, organization, mockHomepage, measurementsMetaMock;
- beforeEach(() => {
- organization = OrganizationFixture({
- features,
- });
- initialData = initializeOrg({
- organization,
- router: {
- location: LocationFixture(),
- },
- });
- ProjectsStore.loadInitialData(initialData.projects);
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events/',
- body: [],
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events-meta/',
- body: {
- count: 2,
- },
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events-stats/',
- body: {data: [[123, []]]},
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/tags/',
- body: [],
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/releases/stats/',
- body: [],
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/dynamic-sampling/custom-rules/',
- body: '',
- });
- mockHomepage = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/discover/homepage/',
- method: 'GET',
- statusCode: 200,
- body: {
- id: '2',
- name: 'homepage query',
- projects: [],
- version: 2,
- expired: false,
- dateCreated: '2021-04-08T17:53:25.195782Z',
- dateUpdated: '2021-04-09T12:13:18.567264Z',
- createdBy: {
- id: '2',
- },
- environment: ['alpha'],
- fields: ['environment'],
- widths: ['-1'],
- range: '24h',
- orderby: '-environment',
- display: 'previous',
- query: 'event.type:error',
- queryDataset: 'discover',
- },
- });
- measurementsMetaMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/measurements-meta/',
- method: 'GET',
- body: {},
- });
- });
- it('renders the Discover banner', async () => {
- render(
- <Homepage
- organization={organization}
- location={initialData.router.location}
- router={initialData.router}
- setSavedQuery={jest.fn()}
- loading={false}
- />,
- {router: initialData.router, organization: initialData.organization}
- );
- await screen.findByText('Discover Trends');
- screen.getByText('Get a Tour');
- expect(screen.queryByText('Build a new query')).not.toBeInTheDocument();
- });
- it('fetches from the homepage URL and renders fields, async page filters, async and chart information', async () => {
- render(
- <Homepage
- organization={organization}
- location={initialData.router.location}
- router={initialData.router}
- setSavedQuery={jest.fn()}
- loading={false}
- />,
- {router: initialData.router, organization: initialData.organization}
- );
- expect(mockHomepage).toHaveBeenCalled();
- await screen.findByText('environment');
- // Only the environment field
- expect(screen.getAllByTestId('grid-head-cell').length).toEqual(1);
- screen.getByText('Previous Period');
- screen.getByText('event.type:error');
- expect(screen.queryByText('Dataset')).not.toBeInTheDocument();
- });
- it('renders event view from URL params over homepage query', async () => {
- initialData = initializeOrg({
- organization,
- router: {
- location: {
- ...LocationFixture(),
- query: {
- ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
- field: ['project'],
- },
- },
- },
- });
- render(
- <Homepage
- organization={organization}
- location={initialData.router.location}
- router={initialData.router}
- setSavedQuery={jest.fn()}
- loading={false}
- />,
- {router: initialData.router, organization: initialData.organization}
- );
- expect(mockHomepage).toHaveBeenCalled();
- await screen.findByText('project');
- // This is the field in the mocked response for the homepage
- expect(screen.queryByText('environment')).not.toBeInTheDocument();
- });
- it('applies URL changes with the homepage pathname', async () => {
- render(
- <Homepage
- organization={organization}
- location={initialData.router.location}
- router={initialData.router}
- setSavedQuery={jest.fn()}
- loading={false}
- />,
- {router: initialData.router, organization: initialData.organization}
- );
- renderGlobalModal();
- await userEvent.click(await screen.findByText('Columns'));
- await userEvent.click(screen.getByTestId('label'));
- await userEvent.click(screen.getByText('event.type'));
- await userEvent.click(screen.getByText('Apply'));
- expect(browserHistory.push).toHaveBeenCalledWith(
- expect.objectContaining({
- pathname: '/organizations/org-slug/discover/homepage/',
- query: expect.objectContaining({
- field: 'event.type',
- }),
- })
- );
- });
- it('does not show an editable header or author information', async () => {
- render(
- <Homepage
- organization={organization}
- location={initialData.router.location}
- router={initialData.router}
- setSavedQuery={jest.fn()}
- loading={false}
- />,
- {router: initialData.router, organization: initialData.organization}
- );
- await waitFor(() => {
- expect(measurementsMetaMock).toHaveBeenCalled();
- });
- // 'Discover' is the header for the homepage
- expect(screen.getByText('Discover')).toBeInTheDocument();
- expect(screen.queryByText(/Created by:/)).not.toBeInTheDocument();
- expect(screen.queryByText(/Last edited:/)).not.toBeInTheDocument();
- });
- it('shows the Remove Default button on initial load', async () => {
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/discover/homepage/',
- method: 'GET',
- statusCode: 200,
- body: {
- id: '2',
- name: 'homepage query',
- projects: [],
- version: 2,
- expired: false,
- dateCreated: '2021-04-08T17:53:25.195782Z',
- dateUpdated: '2021-04-09T12:13:18.567264Z',
- createdBy: {
- id: '2',
- },
- environment: [],
- fields: ['environment'],
- widths: ['-1'],
- range: '14d',
- orderby: '-environment',
- display: 'previous',
- query: 'event.type:error',
- topEvents: '5',
- },
- });
- render(
- <Homepage
- organization={organization}
- location={initialData.router.location}
- router={initialData.router}
- setSavedQuery={jest.fn()}
- loading={false}
- />,
- {router: initialData.router, organization: initialData.organization}
- );
- expect(await screen.findByText('Remove Default')).toBeInTheDocument();
- expect(screen.queryByText('Set as Default')).not.toBeInTheDocument();
- });
- it('Disables the Set as Default button when no saved homepage', async () => {
- initialData = initializeOrg({
- organization,
- router: {
- location: {
- ...LocationFixture(),
- query: {
- ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
- },
- },
- },
- });
- mockHomepage = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/discover/homepage/',
- method: 'GET',
- statusCode: 200,
- });
- render(
- <Homepage
- organization={organization}
- location={initialData.router.location}
- router={initialData.router}
- setSavedQuery={jest.fn()}
- loading={false}
- />,
- {router: initialData.router, organization: initialData.organization}
- );
- expect(mockHomepage).toHaveBeenCalled();
- expect(screen.getByRole('button', {name: /set as default/i})).toBeDisabled();
- await waitFor(() => {
- expect(measurementsMetaMock).toHaveBeenCalled();
- });
- });
- it('follows absolute date selection', async () => {
- initialData = initializeOrg({
- organization,
- router: {
- location: {
- ...LocationFixture(),
- query: {
- ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
- },
- },
- },
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/discover/homepage/',
- method: 'GET',
- statusCode: 200,
- });
- render(
- <Homepage
- organization={organization}
- location={initialData.router.location}
- router={initialData.router}
- setSavedQuery={jest.fn()}
- loading={false}
- />,
- {router: initialData.router, organization: initialData.organization}
- );
- await userEvent.click(await screen.findByText('24H'));
- await userEvent.click(await screen.findByText('Absolute date'));
- await userEvent.click(screen.getByText('Apply'));
- expect(screen.queryByText('14D')).not.toBeInTheDocument();
- });
- it('renders changes to the discover query when no homepage', async () => {
- initialData = initializeOrg({
- organization,
- router: {
- location: {
- ...LocationFixture(),
- query: {
- ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
- field: ['title'],
- },
- },
- },
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/discover/homepage/',
- method: 'GET',
- statusCode: 200,
- body: '',
- });
- const {rerender} = render(
- <Homepage
- organization={organization}
- location={initialData.router.location}
- router={initialData.router}
- setSavedQuery={jest.fn()}
- loading={false}
- />,
- {router: initialData.router, organization: initialData.organization}
- );
- renderGlobalModal();
- // Simulate an update to the columns by changing the URL params
- const rerenderData = initializeOrg({
- organization,
- router: {
- location: {
- ...LocationFixture(),
- query: {
- ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
- field: ['event.type'],
- },
- },
- },
- });
- rerender(
- <Homepage
- organization={organization}
- location={rerenderData.router.location}
- router={rerenderData.router}
- setSavedQuery={jest.fn()}
- loading={false}
- />
- );
- await waitFor(() => {
- expect(measurementsMetaMock).toHaveBeenCalled();
- });
- expect(screen.getByText('event.type')).toBeInTheDocument();
- });
- it('renders changes to the discover query when loaded with valid event view in url params', async () => {
- initialData = initializeOrg({
- organization,
- router: {
- location: {
- ...LocationFixture(),
- query: {
- ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
- field: ['title'],
- },
- },
- },
- });
- const {rerender} = render(
- <Homepage
- organization={organization}
- location={initialData.router.location}
- router={initialData.router}
- setSavedQuery={jest.fn()}
- loading={false}
- />,
- {router: initialData.router, organization: initialData.organization}
- );
- renderGlobalModal();
- // Simulate an update to the columns by changing the URL params
- const rerenderData = initializeOrg({
- organization,
- router: {
- location: {
- ...LocationFixture(),
- query: {
- ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
- field: ['event.type'],
- },
- },
- },
- });
- rerender(
- <Homepage
- organization={organization}
- location={rerenderData.router.location}
- router={rerenderData.router}
- setSavedQuery={jest.fn()}
- loading={false}
- />
- );
- await waitFor(() => {
- expect(measurementsMetaMock).toHaveBeenCalled();
- });
- expect(screen.getByText('event.type')).toBeInTheDocument();
- });
- it('overrides homepage filters with pinned filters if they exist', async () => {
- ProjectsStore.loadInitialData([ProjectFixture({id: '1'}), ProjectFixture({id: '2'})]);
- jest.spyOn(pageFilterUtils, 'getPageFilterStorage').mockReturnValueOnce({
- pinnedFilters: new Set(['projects']),
- state: {
- project: [2],
- environment: [],
- start: null,
- end: null,
- period: '14d',
- utc: null,
- },
- });
- render(
- <Homepage
- organization={organization}
- location={initialData.router.location}
- router={initialData.router}
- setSavedQuery={jest.fn()}
- loading={false}
- />,
- {router: initialData.router, organization: initialData.organization}
- );
- await waitFor(() => {
- expect(measurementsMetaMock).toHaveBeenCalled();
- });
- expect(screen.getByText('project-slug')).toBeInTheDocument();
- });
- it('allows users to set the All Events query as default', async () => {
- initialData = initializeOrg({
- organization,
- router: {
- location: {
- ...LocationFixture(),
- query: {
- ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
- },
- },
- },
- });
- mockHomepage = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/discover/homepage/',
- method: 'GET',
- statusCode: 200,
- });
- render(
- <Homepage
- organization={organization}
- location={initialData.router.location}
- router={initialData.router}
- setSavedQuery={jest.fn()}
- loading={false}
- />,
- {router: initialData.router, organization: initialData.organization}
- );
- await waitFor(() => expect(screen.getByTestId('set-as-default')).toBeEnabled());
- });
- it('uses split decision for homepage query', async () => {
- organization = OrganizationFixture({
- features: [
- 'discover-basic',
- 'discover-query',
- 'performance-discover-dataset-selector',
- ],
- });
- initialData = initializeOrg({
- organization: organization,
- router: {
- location: LocationFixture(),
- },
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events/',
- body: {
- meta: {
- discoverSplitDecision: 'error-events',
- },
- data: [],
- },
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/discover/homepage/',
- method: 'GET',
- statusCode: 200,
- body: {
- id: '2',
- name: 'homepage query',
- projects: [],
- version: 2,
- expired: false,
- dateCreated: '2021-04-08T17:53:25.195782Z',
- dateUpdated: '2021-04-09T12:13:18.567264Z',
- createdBy: {
- id: '2',
- },
- environment: [],
- fields: ['environment'],
- widths: ['-1'],
- range: '14d',
- orderby: '-environment',
- display: 'previous',
- query: 'event.type:error',
- topEvents: '5',
- queryDataset: 'discover',
- },
- });
- render(
- <Homepage
- organization={organization}
- location={initialData.router.location}
- router={initialData.router}
- setSavedQuery={jest.fn()}
- loading={false}
- />,
- {router: initialData.router, organization: initialData.organization}
- );
- expect(await screen.findByText('Remove Default')).toBeInTheDocument();
- expect(screen.queryByText('Set as Default')).not.toBeInTheDocument();
- await screen.findByText('environment');
- expect(screen.getAllByTestId('grid-head-cell').length).toEqual(1);
- screen.getByText('event.type:error');
- expect(screen.getByRole('tab', {name: 'Errors'})).toHaveAttribute(
- 'aria-selected',
- 'true'
- );
- expect(
- screen.getByText(
- "We're splitting our datasets up to make it a bit easier to digest. We defaulted this query to Errors. Edit as you see fit."
- )
- ).toBeInTheDocument();
- });
- it('saves homepage with dataset selection', async () => {
- organization = OrganizationFixture({
- features: [
- 'discover-basic',
- 'discover-query',
- 'performance-discover-dataset-selector',
- ],
- });
- initialData = initializeOrg({
- organization: organization,
- router: {
- location: LocationFixture(),
- },
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events/',
- body: {
- meta: {
- discoverSplitDecision: 'error-events',
- },
- data: [],
- },
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/discover/homepage/',
- method: 'GET',
- statusCode: 200,
- body: {
- id: '2',
- name: 'homepage query',
- projects: [],
- version: 2,
- expired: false,
- dateCreated: '2021-04-08T17:53:25.195782Z',
- dateUpdated: '2021-04-09T12:13:18.567264Z',
- createdBy: {
- id: '2',
- },
- environment: [],
- fields: ['environment'],
- widths: ['-1'],
- range: '14d',
- orderby: '-environment',
- display: 'previous',
- query: 'event.type:error',
- topEvents: '5',
- queryDataset: 'discover',
- },
- });
- render(
- <Homepage
- organization={organization}
- location={initialData.router.location}
- router={initialData.router}
- setSavedQuery={jest.fn()}
- loading={false}
- />,
- {router: initialData.router, organization: initialData.organization}
- );
- expect(await screen.findByText('Remove Default')).toBeInTheDocument();
- expect(screen.queryByText('Set as Default')).not.toBeInTheDocument();
- await userEvent.click(screen.getByRole('tab', {name: 'Transactions'}));
- expect(initialData.router.push).toHaveBeenCalledWith(
- expect.objectContaining({
- query: expect.objectContaining({
- dataset: 'transactions',
- name: 'homepage query',
- project: undefined,
- query: '',
- field: 'environment',
- queryDataset: 'transaction-like',
- }),
- })
- );
- });
- });
|