123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- import {browserHistory} from 'react-router';
- import {Organization} from 'sentry-fixture/organization';
- import {initializeOrg} from 'sentry-test/initializeOrg';
- import {render, screen, waitFor} from 'sentry-test/reactTestingLibrary';
- import DashboardDetail from 'sentry/views/dashboards/detail';
- import OrgDashboards from 'sentry/views/dashboards/orgDashboards';
- import {DashboardState} from 'sentry/views/dashboards/types';
- describe('OrgDashboards', () => {
- const api = new MockApiClient();
- const organization = Organization({
- features: ['dashboards-basic', 'dashboards-edit'],
- });
- let initialData;
- beforeEach(() => {
- initialData = initializeOrg({
- organization,
- projects: [],
- router: {
- location: TestStubs.location(),
- params: {orgId: 'org-slug'},
- },
- });
- const mockDashboard = {
- dateCreated: '2021-08-10T21:20:46.798237Z',
- id: '1',
- title: 'Test Dashboard',
- widgets: [],
- projects: [],
- filters: {},
- };
- MockApiClient.addMockResponse({
- url: `/organizations/org-slug/dashboards/1/`,
- method: 'GET',
- body: mockDashboard,
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/dashboards/',
- body: [mockDashboard],
- });
- });
- afterEach(() => {
- MockApiClient.clearMockResponses();
- jest.clearAllMocks();
- });
- it('redirects to add query params for page filters if any are saved', async () => {
- const mockDashboardWithFilters = {
- dateCreated: '2021-08-10T21:20:46.798237Z',
- id: '1',
- title: 'Test Dashboard',
- widgets: [],
- projects: [1, 2],
- environment: ['alpha'],
- period: '7d',
- filters: {},
- };
- MockApiClient.addMockResponse({
- url: `/organizations/org-slug/dashboards/1/`,
- method: 'GET',
- body: mockDashboardWithFilters,
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/dashboards/',
- body: [mockDashboardWithFilters],
- });
- render(
- <OrgDashboards
- api={api}
- location={TestStubs.location()}
- organization={initialData.organization}
- params={{orgId: 'org-slug', dashboardId: '1'}}
- >
- {({dashboard, dashboards}) => {
- return dashboard ? (
- <DashboardDetail
- api={api}
- initialState={DashboardState.VIEW}
- location={initialData.routerContext.location}
- router={initialData.router}
- dashboard={dashboard}
- dashboards={dashboards}
- {...initialData.router}
- />
- ) : (
- <div>loading</div>
- );
- }}
- </OrgDashboards>,
- {context: initialData.routerContext}
- );
- await waitFor(() =>
- expect(browserHistory.replace).toHaveBeenCalledWith(
- expect.objectContaining({
- query: expect.objectContaining({
- project: [1, 2],
- environment: ['alpha'],
- statsPeriod: '7d',
- }),
- })
- )
- );
- });
- it('ignores query params that are not page filters for redirection', async () => {
- const mockDashboardWithFilters = {
- dateCreated: '2021-08-10T21:20:46.798237Z',
- id: '1',
- title: 'Test Dashboard',
- widgets: [],
- projects: [1, 2],
- environment: ['alpha'],
- period: '7d',
- filters: {},
- };
- MockApiClient.addMockResponse({
- url: `/organizations/org-slug/dashboards/1/`,
- method: 'GET',
- body: mockDashboardWithFilters,
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/dashboards/',
- body: [mockDashboardWithFilters],
- });
- render(
- <OrgDashboards
- api={api}
- location={{
- ...TestStubs.location(),
- query: {
- // This query param is not a page filter, so it should not interfere
- // with the redirect logic
- sort: 'recentlyViewed',
- },
- }}
- organization={initialData.organization}
- params={{orgId: 'org-slug', dashboardId: '1'}}
- >
- {({dashboard, dashboards}) => {
- return dashboard ? (
- <DashboardDetail
- api={api}
- initialState={DashboardState.VIEW}
- location={initialData.router.location}
- router={initialData.router}
- dashboard={dashboard}
- dashboards={dashboards}
- {...initialData.router}
- />
- ) : (
- <div>loading</div>
- );
- }}
- </OrgDashboards>,
- {context: initialData.routerContext}
- );
- await waitFor(() =>
- expect(browserHistory.replace).toHaveBeenCalledWith(
- expect.objectContaining({
- query: expect.objectContaining({
- project: [1, 2],
- environment: ['alpha'],
- statsPeriod: '7d',
- }),
- })
- )
- );
- });
- it('does not add query params for page filters if one of the filters is defined', () => {
- initialData = initializeOrg({
- organization,
- projects: [],
- router: {
- location: {
- ...TestStubs.location(),
- query: {
- // project is supplied in the URL, so we should avoid redirecting
- project: ['1'],
- },
- },
- params: {orgId: 'org-slug'},
- },
- });
- const mockDashboardWithFilters = {
- dateCreated: '2021-08-10T21:20:46.798237Z',
- id: '1',
- title: 'Test Dashboard',
- widgets: [],
- projects: [1, 2],
- environment: ['alpha'],
- period: '7d',
- filters: {},
- };
- MockApiClient.addMockResponse({
- url: `/organizations/org-slug/dashboards/1/`,
- method: 'GET',
- body: mockDashboardWithFilters,
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/dashboards/',
- body: [mockDashboardWithFilters],
- });
- render(
- <OrgDashboards
- api={api}
- location={initialData.router.location}
- organization={initialData.organization}
- params={{orgId: 'org-slug', dashboardId: '1'}}
- >
- {({dashboard, dashboards}) => {
- return dashboard ? (
- <DashboardDetail
- api={api}
- initialState={DashboardState.VIEW}
- location={initialData.router.location}
- router={initialData.router}
- dashboard={dashboard}
- dashboards={dashboards}
- {...initialData.router}
- />
- ) : (
- <div>loading</div>
- );
- }}
- </OrgDashboards>,
- {context: initialData.routerContext}
- );
- expect(browserHistory.replace).not.toHaveBeenCalled();
- });
- it('does not add query params for page filters if none are saved', () => {
- render(
- <OrgDashboards
- api={api}
- location={TestStubs.location()}
- organization={initialData.organization}
- params={{orgId: 'org-slug', dashboardId: '1'}}
- >
- {({dashboard, dashboards}) => {
- return dashboard ? (
- <DashboardDetail
- api={api}
- initialState={DashboardState.VIEW}
- location={initialData.routerContext.location}
- router={initialData.router}
- dashboard={dashboard}
- dashboards={dashboards}
- {...initialData.router}
- />
- ) : (
- <div>loading</div>
- );
- }}
- </OrgDashboards>,
- {context: initialData.routerContext}
- );
- expect(browserHistory.replace).not.toHaveBeenCalled();
- });
- it('does not redirect to add query params if location is cleared manually', async () => {
- const mockDashboardWithFilters = {
- dateCreated: '2021-08-10T21:20:46.798237Z',
- id: '1',
- title: 'Test Dashboard',
- widgets: [],
- projects: [1],
- filters: {},
- };
- MockApiClient.addMockResponse({
- url: `/organizations/org-slug/dashboards/1/`,
- method: 'GET',
- body: mockDashboardWithFilters,
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/dashboards/',
- body: [mockDashboardWithFilters],
- });
- const {rerender} = render(
- <OrgDashboards
- api={api}
- location={TestStubs.location()}
- organization={initialData.organization}
- params={{orgId: 'org-slug', dashboardId: '1'}}
- >
- {({dashboard, dashboards}) => {
- return dashboard ? (
- <DashboardDetail
- api={api}
- initialState={DashboardState.VIEW}
- location={initialData.routerContext.location}
- router={initialData.router}
- dashboard={dashboard}
- dashboards={dashboards}
- {...initialData.router}
- />
- ) : (
- <div>loading</div>
- );
- }}
- </OrgDashboards>,
- {context: initialData.routerContext}
- );
- await waitFor(() => expect(browserHistory.replace).toHaveBeenCalledTimes(1));
- rerender(
- <OrgDashboards
- api={api}
- location={{...initialData.routerContext.location, query: {}}}
- organization={initialData.organization}
- params={{orgId: 'org-slug', dashboardId: '1'}}
- >
- {({dashboard, dashboards}) => {
- return dashboard ? (
- <DashboardDetail
- api={api}
- initialState={DashboardState.VIEW}
- location={initialData.routerContext.location}
- router={initialData.router}
- dashboard={dashboard}
- dashboards={dashboards}
- {...initialData.router}
- />
- ) : (
- <div>loading</div>
- );
- }}
- </OrgDashboards>
- );
- expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument();
- expect(browserHistory.replace).toHaveBeenCalledTimes(1);
- });
- });
|