123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- import {
- deleteSavedSearch,
- fetchSavedSearches,
- pinSearch,
- unpinSearch,
- } from 'sentry/actionCreators/savedSearches';
- import {Client} from 'sentry/api';
- import SavedSearchesStore from 'sentry/stores/savedSearchesStore';
- describe('SavedSearchesStore', function () {
- let api;
- beforeAll(async function () {
- api = new Client();
- await SavedSearchesStore.reset();
- });
- beforeEach(function () {
- Client.addMockResponse({
- url: '/organizations/org-1/searches/',
- body: TestStubs.Searches(),
- });
- Client.addMockResponse({
- url: '/organizations/org-1/pinned-searches/',
- method: 'PUT',
- body: {},
- });
- Client.addMockResponse({
- url: '/organizations/org-1/pinned-searches/',
- method: 'DELETE',
- });
- });
- afterEach(async function () {
- Client.clearMockResponses();
- SavedSearchesStore.reset();
- await tick();
- });
- it('get', function () {
- expect(SavedSearchesStore.get()).toEqual({
- hasError: false,
- isLoading: true,
- savedSearches: [],
- });
- });
- it('fetching saved searches updates store', async function () {
- await fetchSavedSearches(api, 'org-1', {});
- await tick();
- expect(SavedSearchesStore.get().savedSearches).toHaveLength(2);
- expect(SavedSearchesStore.get().isLoading).toBe(false);
- });
- it('failed fetches do not corrupt store', async function () {
- Client.addMockResponse({
- url: '/organizations/org-1/searches/',
- body: '',
- });
- await fetchSavedSearches(api, 'org-1', {});
- await tick();
- expect(SavedSearchesStore.get().savedSearches).toHaveLength(0);
- expect(SavedSearchesStore.get().isLoading).toBe(false);
- });
- it('creates a new pin search', async function () {
- await fetchSavedSearches(api, 'org-1', {});
- await tick();
- Client.addMockResponse({
- url: '/organizations/org-1/pinned-searches/',
- method: 'PUT',
- body: {
- id: '123',
- query: 'level:info',
- sort: 'freq',
- isPinned: true,
- },
- });
- pinSearch(api, 'org-1', 0, 'level:info', 'freq');
- await tick();
- await tick();
- expect(SavedSearchesStore.get().savedSearches).toHaveLength(3);
- expect(SavedSearchesStore.get().savedSearches[0]).toEqual(
- expect.objectContaining({
- id: '123',
- isPinned: true,
- type: 0,
- query: 'level:info',
- name: 'My Pinned Search',
- sort: 'freq',
- })
- );
- });
- it('changes pinned search from a custom search to an existing search', async function () {
- const searches = TestStubs.Searches();
- Client.addMockResponse({
- url: '/organizations/org-1/searches/',
- body: [
- {
- id: null,
- isPinned: true,
- type: 0,
- query: 'assigned:me',
- sort: 'date',
- },
- ...searches,
- ],
- });
- Client.addMockResponse({
- url: '/organizations/org-1/pinned-searches/',
- method: 'PUT',
- body: {
- id: '1',
- isDefault: false,
- isGlobal: true,
- isOrgCustom: false,
- isPinned: true,
- query: 'is:unresolved',
- sort: 'date',
- name: 'Unresolved Issues',
- type: 0,
- },
- });
- await fetchSavedSearches(api, 'org-1', {});
- await tick();
- pinSearch(api, 'org-1', 0, searches[1].query, 'date');
- await tick();
- await tick();
- // Order should remain the same
- expect(SavedSearchesStore.get().savedSearches[1]).toEqual(
- expect.objectContaining({
- id: '1',
- isDefault: false,
- isGlobal: true,
- isOrgCustom: false,
- isPinned: true,
- type: 0,
- name: 'Unresolved Issues',
- query: 'is:unresolved',
- sort: 'date',
- })
- );
- // Saved custom search should be removed
- expect(SavedSearchesStore.get().savedSearches).toHaveLength(2);
- });
- it('changes pinned search from an existing search to another existing search', async function () {
- const searches = TestStubs.Searches();
- Client.addMockResponse({
- url: '/organizations/org-1/searches/',
- body: [{...searches[0], isPinned: true}, searches[1]],
- });
- Client.addMockResponse({
- url: '/organizations/org-1/pinned-searches/',
- method: 'PUT',
- body: {
- id: '1',
- isDefault: false,
- isGlobal: true,
- isOrgCustom: false,
- isPinned: true,
- query: 'is:unresolved',
- sort: 'date',
- name: 'Unresolved Issues',
- type: 0,
- },
- });
- await fetchSavedSearches(api, 'org-1', {});
- await tick();
- pinSearch(api, 'org-1', 0, searches[1].query, 'date');
- await tick();
- await tick();
- expect(SavedSearchesStore.get().savedSearches).toHaveLength(2);
- expect(SavedSearchesStore.get().savedSearches[0]).toEqual(
- expect.objectContaining({
- id: '2',
- isPinned: false,
- type: 0,
- name: 'Needs Triage',
- query: 'is:unresolved is:unassigned',
- sort: 'date',
- })
- );
- expect(SavedSearchesStore.get().savedSearches[1]).toEqual(
- expect.objectContaining({
- id: '1',
- isDefault: false,
- isGlobal: true,
- isOrgCustom: false,
- isPinned: true,
- type: 0,
- name: 'Unresolved Issues',
- query: 'is:unresolved',
- sort: 'date',
- })
- );
- });
- it('unpins a user custom search (not global, and not org custom)', async function () {
- const searches = TestStubs.Searches();
- Client.addMockResponse({
- url: '/organizations/org-1/searches/',
- body: [
- {
- id: null,
- isPinned: true,
- type: 0,
- query: 'assigned:me',
- sort: 'date',
- },
- ...searches,
- ],
- });
- await fetchSavedSearches(api, 'org-1', {});
- await tick();
- unpinSearch(api, 'org-1', 0, searches[0]);
- await tick();
- await tick();
- // Saved custom search should be removed
- expect(SavedSearchesStore.get().savedSearches).toHaveLength(2);
- expect(SavedSearchesStore.get().savedSearches[0]).toEqual(
- expect.objectContaining({
- id: '2',
- isPinned: false,
- type: 0,
- name: 'Needs Triage',
- query: 'is:unresolved is:unassigned',
- sort: 'date',
- })
- );
- expect(SavedSearchesStore.get().savedSearches[1]).toEqual(
- expect.objectContaining({
- id: '1',
- isPinned: false,
- type: 0,
- name: 'Unresolved Issues',
- query: 'is:unresolved',
- sort: 'date',
- })
- );
- });
- it('unpins an existing global saved search', async function () {
- const searches = TestStubs.Searches();
- Client.addMockResponse({
- url: '/organizations/org-1/searches/',
- body: [{...searches[0], isPinned: true}, searches[1]],
- });
- await fetchSavedSearches(api, 'org-1', {});
- await tick();
- unpinSearch(api, 'org-1', 0, searches[0]);
- await tick();
- await tick();
- expect(SavedSearchesStore.get().savedSearches).toHaveLength(2);
- expect(SavedSearchesStore.get().savedSearches[0]).toEqual(
- expect.objectContaining({
- id: '2',
- isPinned: false,
- type: 0,
- name: 'Needs Triage',
- query: 'is:unresolved is:unassigned',
- sort: 'date',
- })
- );
- expect(SavedSearchesStore.get().savedSearches[1]).toEqual(
- expect.objectContaining({
- id: '1',
- isPinned: false,
- type: 0,
- name: 'Unresolved Issues',
- query: 'is:unresolved',
- sort: 'date',
- })
- );
- });
- it('unpins an existing org saved search', async function () {
- const searches = TestStubs.Searches();
- Client.addMockResponse({
- url: '/organizations/org-1/searches/',
- body: [
- {...searches[0], isOrgCustom: true, isGlobal: false, isPinned: true},
- searches[1],
- ],
- });
- await fetchSavedSearches(api, 'org-1', {});
- await tick();
- unpinSearch(api, 'org-1', 0, searches[0]);
- await tick();
- await tick();
- expect(SavedSearchesStore.get().savedSearches).toHaveLength(2);
- expect(SavedSearchesStore.get().savedSearches[0]).toEqual(
- expect.objectContaining({
- id: '2',
- isPinned: false,
- type: 0,
- name: 'Needs Triage',
- query: 'is:unresolved is:unassigned',
- sort: 'date',
- })
- );
- expect(SavedSearchesStore.get().savedSearches[1]).toEqual(
- expect.objectContaining({
- id: '1',
- isPinned: false,
- type: 0,
- name: 'Unresolved Issues',
- query: 'is:unresolved',
- sort: 'date',
- })
- );
- });
- it('removes deleted saved searches', async function () {
- await fetchSavedSearches(api, 'org-1', {});
- await tick();
- const searches = SavedSearchesStore.get().savedSearches;
- Client.addMockResponse({
- url: `/organizations/org-1/searches/${searches[0].id}/`,
- method: 'DELETE',
- body: {},
- });
- await deleteSavedSearch(api, 'org-1', searches[0]);
- await tick();
- const newSearches = SavedSearchesStore.get().savedSearches;
- expect(newSearches.length).toBeLessThan(searches.length);
- expect(newSearches[0].id).not.toBe(searches[0].id);
- });
- });
|