123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- import {browserHistory} from 'react-router';
- import {Organization} from 'sentry-fixture/organization';
- 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 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 = Organization({
- features,
- });
- initialData = initializeOrg({
- organization,
- router: {
- location: TestStubs.location(),
- },
- });
- ProjectsStore.loadInitialData(organization.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: [],
- });
- 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',
- },
- });
- 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}
- />,
- {context: initialData.routerContext, 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}
- />,
- {context: initialData.routerContext, 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');
- });
- it('renders event view from URL params over homepage query', async () => {
- initialData = initializeOrg({
- organization,
- router: {
- location: {
- ...TestStubs.location(),
- 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}
- />,
- {context: initialData.routerContext, 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}
- />,
- {context: initialData.routerContext, 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}
- />,
- {context: initialData.routerContext, 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}
- />,
- {context: initialData.routerContext, 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: {
- ...TestStubs.location(),
- 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}
- />,
- {context: initialData.routerContext, 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: {
- ...TestStubs.location(),
- 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}
- />,
- {context: initialData.routerContext, 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: {
- ...TestStubs.location(),
- 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}
- />,
- {context: initialData.routerContext, organization: initialData.organization}
- );
- renderGlobalModal();
- // Simulate an update to the columns by changing the URL params
- const rerenderData = initializeOrg({
- organization,
- router: {
- location: {
- ...TestStubs.location(),
- 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: {
- ...TestStubs.location(),
- 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}
- />,
- {context: initialData.routerContext, organization: initialData.organization}
- );
- renderGlobalModal();
- // Simulate an update to the columns by changing the URL params
- const rerenderData = initializeOrg({
- organization,
- router: {
- location: {
- ...TestStubs.location(),
- 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([
- TestStubs.Project({id: 1}),
- TestStubs.Project({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}
- />,
- {context: initialData.routerContext, 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: {
- ...TestStubs.location(),
- 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}
- />,
- {context: initialData.routerContext, organization: initialData.organization}
- );
- await waitFor(() => expect(screen.getByTestId('set-as-default')).toBeEnabled());
- });
- });
|