|
| 1 | +// create a basic test for filters bar |
| 2 | + |
| 3 | +import {LocationFixture} from 'sentry-fixture/locationFixture'; |
| 4 | +import {OrganizationFixture} from 'sentry-fixture/organization'; |
| 5 | +import {ReleaseFixture} from 'sentry-fixture/release'; |
| 6 | +import {TagsFixture} from 'sentry-fixture/tags'; |
| 7 | + |
| 8 | +import {render, screen, waitForElementToBeRemoved} from 'sentry-test/reactTestingLibrary'; |
| 9 | + |
| 10 | +import type {Organization} from 'sentry/types/organization'; |
| 11 | +import {FieldKind} from 'sentry/utils/fields'; |
| 12 | +import FiltersBar, {type FiltersBarProps} from 'sentry/views/dashboards/filtersBar'; |
| 13 | +import { |
| 14 | + DashboardFilterKeys, |
| 15 | + WidgetType, |
| 16 | + type GlobalFilter, |
| 17 | +} from 'sentry/views/dashboards/types'; |
| 18 | +import {PrebuiltDashboardId} from 'sentry/views/dashboards/utils/prebuiltConfigs'; |
| 19 | + |
| 20 | +describe('FiltersBar', () => { |
| 21 | + let organization: Organization; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + mockNetworkRequests(); |
| 25 | + |
| 26 | + organization = OrganizationFixture({ |
| 27 | + features: ['dashboards-basic', 'dashboards-edit', 'dashboards-global-filters'], |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + MockApiClient.clearMockResponses(); |
| 33 | + jest.clearAllMocks(); |
| 34 | + }); |
| 35 | + |
| 36 | + const renderFilterBar = (overrides: Partial<FiltersBarProps> = {}) => { |
| 37 | + const props: FiltersBarProps = { |
| 38 | + filters: {}, |
| 39 | + hasUnsavedChanges: false, |
| 40 | + isEditingDashboard: false, |
| 41 | + isPreview: false, |
| 42 | + location: LocationFixture(), |
| 43 | + onDashboardFilterChange: () => {}, |
| 44 | + ...overrides, |
| 45 | + }; |
| 46 | + |
| 47 | + return render(<FiltersBar {...props} />, {organization}); |
| 48 | + }; |
| 49 | + |
| 50 | + it('should render basic global filter', async () => { |
| 51 | + const newLocation = LocationFixture({ |
| 52 | + query: { |
| 53 | + [DashboardFilterKeys.GLOBAL_FILTER]: JSON.stringify({ |
| 54 | + dataset: WidgetType.SPANS, |
| 55 | + tag: {key: 'browser.name', name: 'Browser Name', kind: FieldKind.FIELD}, |
| 56 | + value: `browser.name:[Chrome]`, |
| 57 | + } satisfies GlobalFilter), |
| 58 | + }, |
| 59 | + }); |
| 60 | + renderFilterBar({location: newLocation}); |
| 61 | + await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator')); |
| 62 | + expect( |
| 63 | + screen.getByRole('button', {name: /browser\.name.*Chrome/i}) |
| 64 | + ).toBeInTheDocument(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should render save button with unsaved changes', async () => { |
| 68 | + const newLocation = LocationFixture({ |
| 69 | + query: { |
| 70 | + [DashboardFilterKeys.GLOBAL_FILTER]: JSON.stringify({ |
| 71 | + dataset: WidgetType.SPANS, |
| 72 | + tag: {key: 'browser.name', name: 'Browser Name', kind: FieldKind.FIELD}, |
| 73 | + value: `browser.name:[Chrome]`, |
| 74 | + } satisfies GlobalFilter), |
| 75 | + }, |
| 76 | + }); |
| 77 | + renderFilterBar({location: newLocation, hasUnsavedChanges: true}); |
| 78 | + await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator')); |
| 79 | + expect(screen.getByRole('button', {name: 'Save'})).toBeInTheDocument(); |
| 80 | + expect(screen.getByRole('button', {name: 'Cancel'})).toBeInTheDocument(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should not render save button with temporary filter', async () => { |
| 84 | + const newLocation = LocationFixture({ |
| 85 | + query: { |
| 86 | + [DashboardFilterKeys.GLOBAL_FILTER]: JSON.stringify({ |
| 87 | + dataset: WidgetType.SPANS, |
| 88 | + tag: {key: 'browser.name', name: 'Browser Name', kind: FieldKind.FIELD}, |
| 89 | + value: `browser.name:[Chrome]`, |
| 90 | + isTemporary: true, |
| 91 | + } satisfies GlobalFilter), |
| 92 | + }, |
| 93 | + }); |
| 94 | + |
| 95 | + renderFilterBar({location: newLocation}); |
| 96 | + await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator')); |
| 97 | + expect( |
| 98 | + screen.getByRole('button', {name: /browser\.name.*Chrome/i}) |
| 99 | + ).toBeInTheDocument(); |
| 100 | + expect(screen.queryByRole('button', {name: 'Save'})).not.toBeInTheDocument(); |
| 101 | + expect(screen.queryByRole('button', {name: 'Cancel'})).not.toBeInTheDocument(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should not render save button on prebuilt dashboard', async () => { |
| 105 | + const newLocation = LocationFixture({ |
| 106 | + query: { |
| 107 | + [DashboardFilterKeys.GLOBAL_FILTER]: JSON.stringify({ |
| 108 | + dataset: WidgetType.SPANS, |
| 109 | + tag: {key: 'browser.name', name: 'Browser Name', kind: FieldKind.FIELD}, |
| 110 | + value: `browser.name:[Chrome]`, |
| 111 | + } satisfies GlobalFilter), |
| 112 | + }, |
| 113 | + }); |
| 114 | + renderFilterBar({ |
| 115 | + location: newLocation, |
| 116 | + prebuiltDashboardId: PrebuiltDashboardId.FRONTEND_SESSION_HEALTH, |
| 117 | + }); |
| 118 | + await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator')); |
| 119 | + expect( |
| 120 | + screen.getByRole('button', {name: /browser\.name.*Chrome/i}) |
| 121 | + ).toBeInTheDocument(); |
| 122 | + expect(screen.queryByRole('button', {name: 'Save'})).not.toBeInTheDocument(); |
| 123 | + expect(screen.queryByRole('button', {name: 'Cancel'})).not.toBeInTheDocument(); |
| 124 | + }); |
| 125 | +}); |
| 126 | + |
| 127 | +const mockNetworkRequests = () => { |
| 128 | + MockApiClient.addMockResponse({ |
| 129 | + url: '/organizations/org-slug/releases/', |
| 130 | + body: [ReleaseFixture()], |
| 131 | + }); |
| 132 | + MockApiClient.addMockResponse({ |
| 133 | + url: '/organizations/org-slug/tags/', |
| 134 | + body: TagsFixture(), |
| 135 | + }); |
| 136 | + MockApiClient.addMockResponse({ |
| 137 | + url: `/organizations/org-slug/measurements-meta/`, |
| 138 | + body: { |
| 139 | + 'measurements.custom.measurement': { |
| 140 | + functions: ['p99'], |
| 141 | + }, |
| 142 | + 'measurements.another.custom.measurement': { |
| 143 | + functions: ['p99'], |
| 144 | + }, |
| 145 | + }, |
| 146 | + }); |
| 147 | + |
| 148 | + const mockSearchResponse = [ |
| 149 | + { |
| 150 | + key: 'browser.name', |
| 151 | + value: 'Chrome', |
| 152 | + name: 'Chrome', |
| 153 | + first_seen: null, |
| 154 | + last_seen: null, |
| 155 | + times_seen: null, |
| 156 | + }, |
| 157 | + { |
| 158 | + key: 'browser.name', |
| 159 | + value: 'Firefox', |
| 160 | + name: 'Firefox', |
| 161 | + first_seen: null, |
| 162 | + last_seen: null, |
| 163 | + times_seen: null, |
| 164 | + }, |
| 165 | + ]; |
| 166 | + |
| 167 | + MockApiClient.addMockResponse({ |
| 168 | + url: `/organizations/org-slug/trace-items/attributes/browser.name/values/`, |
| 169 | + body: mockSearchResponse, |
| 170 | + match: [MockApiClient.matchQuery({attributeType: 'string'})], |
| 171 | + }); |
| 172 | +}; |
0 commit comments