123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- import {TagsFixture} from 'sentry-fixture/tags';
- import {initializeOrg} from 'sentry-test/initializeOrg';
- import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
- import TagStore from 'sentry/stores/tagStore';
- import type {Tag, TagValue} from 'sentry/types/group';
- import {IsFieldValues} from 'sentry/utils/fields';
- import IssueListSearchBar from 'sentry/views/issueList/searchBar';
- describe('IssueListSearchBar', function () {
- let recentSearchMock;
- let defaultProps;
- const {router, organization} = initializeOrg();
- beforeEach(function () {
- TagStore.reset();
- TagStore.loadTagsSuccess(TagsFixture());
- defaultProps = {
- organization,
- query: '',
- onSearch: jest.fn(),
- };
- recentSearchMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/recent-searches/',
- method: 'GET',
- body: [],
- });
- });
- afterEach(function () {
- MockApiClient.clearMockResponses();
- });
- describe('updateAutoCompleteItems()', function () {
- it('sets state with complete tag', async function () {
- const tagValuesMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/tags/url/values/',
- method: 'GET',
- body: [],
- });
- render(<IssueListSearchBar {...defaultProps} />, {
- router,
- });
- await userEvent.click(screen.getByRole('textbox'));
- await userEvent.paste('url:"fu"');
- expect(tagValuesMock).toHaveBeenLastCalledWith(
- expect.anything(),
- expect.objectContaining({
- query: expect.objectContaining({
- query: 'fu',
- }),
- })
- );
- expect(screen.getByTestId('smart-search-dropdown')).toBeInTheDocument();
- });
- it('sets state when value has colon', async function () {
- const tagValuesMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/tags/url/values/',
- method: 'GET',
- body: [],
- });
- render(<IssueListSearchBar {...defaultProps} />, {
- router,
- });
- await userEvent.click(screen.getByRole('textbox'));
- await userEvent.paste('url:', {delay: null});
- expect(tagValuesMock).toHaveBeenCalled();
- });
- it('does not request values when tag is `timesSeen`', async function () {
- const tagValuesMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/tags/url/values/',
- method: 'GET',
- body: [],
- });
- render(<IssueListSearchBar {...defaultProps} />, {
- router,
- });
- await userEvent.click(screen.getByRole('textbox'));
- await userEvent.paste('timesSeen:', {delay: null});
- expect(tagValuesMock).not.toHaveBeenCalled();
- });
- });
- describe('Recent Searches', function () {
- it('saves search query as a recent search', async function () {
- const tagValuesMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/tags/url/values/',
- method: 'GET',
- body: [],
- });
- const saveRecentSearch = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/recent-searches/',
- method: 'POST',
- body: {},
- });
- const onSearch = jest.fn();
- render(<IssueListSearchBar {...defaultProps} onSearch={onSearch} />, {
- router,
- });
- await userEvent.click(screen.getByRole('textbox'));
- await userEvent.paste('url:"fu"');
- expect(tagValuesMock).toHaveBeenLastCalledWith(
- expect.anything(),
- expect.objectContaining({
- query: expect.objectContaining({
- query: 'fu',
- }),
- })
- );
- expect(screen.getByTestId('smart-search-dropdown')).toBeInTheDocument();
- await userEvent.keyboard('{Enter}');
- expect(onSearch).toHaveBeenCalledWith('url:"fu"');
- expect(saveRecentSearch).toHaveBeenCalledWith(
- expect.anything(),
- expect.objectContaining({
- data: {
- query: 'url:"fu"',
- type: 0,
- },
- })
- );
- });
- it('queries for recent searches', async function () {
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/tags/url/values/',
- method: 'GET',
- body: [],
- });
- render(<IssueListSearchBar {...defaultProps} />, {router});
- await userEvent.click(screen.getByRole('textbox'));
- await userEvent.paste('is:', {delay: null});
- expect(recentSearchMock).toHaveBeenCalledWith(
- expect.anything(),
- expect.objectContaining({
- query: {
- query: 'is:',
- limit: 3,
- type: 0,
- },
- })
- );
- });
- });
- describe('Tags and Fields', function () {
- const {router: routerWithFlag, organization: orgWithFlag} = initializeOrg();
- orgWithFlag.features = ['issue-stream-search-query-builder'];
- const newDefaultProps = {
- organization: orgWithFlag,
- query: '',
- statsPeriod: '7d',
- onSearch: jest.fn(),
- };
- it('displays the correct options for the is tag', async function () {
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/tags/',
- body: [],
- });
- render(<IssueListSearchBar {...newDefaultProps} />, {
- router: routerWithFlag,
- });
- await userEvent.click(screen.getByRole('combobox', {name: 'Add a search term'}));
- await userEvent.paste('is:', {delay: null});
- await userEvent.click(
- await screen.findByRole('button', {name: 'Edit value for filter: is'})
- );
- Object.values(IsFieldValues).forEach(value => {
- expect(screen.getByRole('option', {name: value})).toBeInTheDocument();
- });
- });
- it('displays the correct options under Event Tags', async function () {
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/tags/',
- body: [{key: 'someTag', name: 'Some Tag'}],
- });
- render(<IssueListSearchBar {...newDefaultProps} />, {
- router: routerWithFlag,
- });
- await userEvent.click(screen.getByRole('combobox', {name: 'Add a search term'}));
- await userEvent.click(screen.getByRole('button', {name: 'Event Tags'}));
- expect(await screen.findByRole('option', {name: 'someTag'})).toBeInTheDocument();
- });
- it('displays tags in the has filter', async function () {
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/tags/',
- body: [{key: 'someTag', name: 'Some Tag'}],
- });
- defaultProps.organization.features = ['issue-stream-search-query-builder'];
- render(<IssueListSearchBar {...newDefaultProps} />, {
- router: routerWithFlag,
- });
- await userEvent.click(screen.getByRole('combobox', {name: 'Add a search term'}));
- await userEvent.paste('has:', {delay: null});
- await userEvent.click(
- await screen.findByRole('button', {name: 'Edit value for filter: has'})
- );
- expect(await screen.findByRole('option', {name: 'someTag'})).toBeInTheDocument();
- });
- });
- describe('Tag Values', function () {
- const {router: routerWithFlag, organization: orgWithFlag} = initializeOrg();
- orgWithFlag.features = ['issue-stream-search-query-builder'];
- const newDefaultProps = {
- organization: orgWithFlag,
- query: '',
- statsPeriod: '7d',
- onSearch: jest.fn(),
- };
- it('displays the correct tag values for a key', async () => {
- const tagKey = 'random';
- const tagValue = 'randomValue';
- const tagValueResponse: TagValue[] = [
- {
- name: tagValue,
- value: tagValue,
- count: 1,
- firstSeen: '2021-01-01T00:00:00Z',
- lastSeen: '2021-01-01T00:00:00Z',
- email: 'a@sentry.io',
- username: 'a',
- id: '1',
- ip_address: '1',
- },
- ];
- const tag: Tag = {
- key: tagKey,
- name: tagKey,
- };
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/tags/',
- method: 'GET',
- body: [tag],
- });
- const tagValueMock = MockApiClient.addMockResponse({
- url: `/organizations/org-slug/tags/${tagKey}/values/`,
- method: 'GET',
- body: tagValueResponse,
- });
- render(<IssueListSearchBar {...newDefaultProps} />, {
- router: routerWithFlag,
- });
- await userEvent.click(screen.getByRole('combobox', {name: 'Add a search term'}));
- await userEvent.paste(tagKey, {delay: null});
- await userEvent.click(screen.getByRole('option', {name: tagKey}));
- expect(await screen.findByRole('option', {name: tagValue})).toBeInTheDocument();
- await waitFor(() => {
- // Expected twice since we make one request for values in events dataset
- // and another for values in IssuePlatform dataset.
- expect(tagValueMock).toHaveBeenCalledTimes(2);
- });
- });
- });
- });
|