|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +import { |
| 9 | + LogicMounter, |
| 10 | + mockHttpValues, |
| 11 | + mockKibanaValues, |
| 12 | + mockFlashMessageHelpers, |
| 13 | +} from '../../../../__mocks__'; |
| 14 | +import '../../../__mocks__/engine_logic.mock'; |
| 15 | + |
| 16 | +import { nextTick } from '@kbn/test/jest'; |
| 17 | + |
| 18 | +import { CurationLogic } from './'; |
| 19 | + |
| 20 | +describe('CurationLogic', () => { |
| 21 | + const { mount } = new LogicMounter(CurationLogic); |
| 22 | + const { http } = mockHttpValues; |
| 23 | + const { navigateToUrl } = mockKibanaValues; |
| 24 | + const { clearFlashMessages, flashAPIErrors } = mockFlashMessageHelpers; |
| 25 | + |
| 26 | + const MOCK_CURATION_RESPONSE = { |
| 27 | + id: 'cur-123456789', |
| 28 | + last_updated: 'some timestamp', |
| 29 | + queries: ['some search'], |
| 30 | + promoted: [{ id: 'some-promoted-document' }], |
| 31 | + organic: [ |
| 32 | + { |
| 33 | + id: { raw: 'some-organic-document', snippet: null }, |
| 34 | + _meta: { id: 'some-organic-document', engine: 'some-engine' }, |
| 35 | + }, |
| 36 | + ], |
| 37 | + hidden: [{ id: 'some-hidden-document' }], |
| 38 | + }; |
| 39 | + |
| 40 | + const DEFAULT_VALUES = { |
| 41 | + dataLoading: true, |
| 42 | + curation: { |
| 43 | + id: '', |
| 44 | + last_updated: '', |
| 45 | + queries: [], |
| 46 | + promoted: [], |
| 47 | + organic: [], |
| 48 | + hidden: [], |
| 49 | + }, |
| 50 | + }; |
| 51 | + |
| 52 | + beforeEach(() => { |
| 53 | + jest.clearAllMocks(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('has expected default values', () => { |
| 57 | + mount(); |
| 58 | + expect(CurationLogic.values).toEqual(DEFAULT_VALUES); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('actions', () => { |
| 62 | + describe('onCurationLoad', () => { |
| 63 | + it('should set curation state & dataLoading to false', () => { |
| 64 | + mount(); |
| 65 | + |
| 66 | + CurationLogic.actions.onCurationLoad(MOCK_CURATION_RESPONSE); |
| 67 | + |
| 68 | + expect(CurationLogic.values).toEqual({ |
| 69 | + ...DEFAULT_VALUES, |
| 70 | + curation: MOCK_CURATION_RESPONSE, |
| 71 | + dataLoading: false, |
| 72 | + }); |
| 73 | + }); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('listeners', () => { |
| 78 | + describe('loadCuration', () => { |
| 79 | + it('should set dataLoading state', () => { |
| 80 | + mount({ dataLoading: false }, { curationId: 'cur-123456789' }); |
| 81 | + |
| 82 | + CurationLogic.actions.loadCuration(); |
| 83 | + |
| 84 | + expect(CurationLogic.values).toEqual({ |
| 85 | + ...DEFAULT_VALUES, |
| 86 | + dataLoading: true, |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should make an API call and set curation state', async () => { |
| 91 | + http.get.mockReturnValueOnce(Promise.resolve(MOCK_CURATION_RESPONSE)); |
| 92 | + mount({}, { curationId: 'cur-123456789' }); |
| 93 | + jest.spyOn(CurationLogic.actions, 'onCurationLoad'); |
| 94 | + |
| 95 | + CurationLogic.actions.loadCuration(); |
| 96 | + await nextTick(); |
| 97 | + |
| 98 | + expect(http.get).toHaveBeenCalledWith( |
| 99 | + '/api/app_search/engines/some-engine/curations/cur-123456789' |
| 100 | + ); |
| 101 | + expect(CurationLogic.actions.onCurationLoad).toHaveBeenCalledWith(MOCK_CURATION_RESPONSE); |
| 102 | + }); |
| 103 | + |
| 104 | + it('handles errors/404s with a redirect to the Curations view', async () => { |
| 105 | + http.get.mockReturnValueOnce(Promise.reject('error')); |
| 106 | + mount({}, { curationId: 'cur-404' }); |
| 107 | + |
| 108 | + CurationLogic.actions.loadCuration(); |
| 109 | + await nextTick(); |
| 110 | + |
| 111 | + expect(flashAPIErrors).toHaveBeenCalledWith('error', { isQueued: true }); |
| 112 | + expect(navigateToUrl).toHaveBeenCalledWith('/engines/some-engine/curations'); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe('updateCuration', () => { |
| 117 | + beforeAll(() => jest.useFakeTimers()); |
| 118 | + afterAll(() => jest.useRealTimers()); |
| 119 | + |
| 120 | + it('should make a PUT API call with queries and promoted/hidden IDs to update', async () => { |
| 121 | + http.put.mockReturnValueOnce(Promise.resolve(MOCK_CURATION_RESPONSE)); |
| 122 | + mount({}, { curationId: 'cur-123456789' }); |
| 123 | + jest.spyOn(CurationLogic.actions, 'onCurationLoad'); |
| 124 | + |
| 125 | + CurationLogic.actions.updateCuration(); |
| 126 | + jest.runAllTimers(); |
| 127 | + await nextTick(); |
| 128 | + |
| 129 | + expect(http.put).toHaveBeenCalledWith( |
| 130 | + '/api/app_search/engines/some-engine/curations/cur-123456789', |
| 131 | + { |
| 132 | + body: '{"queries":[],"query":"","promoted":[],"hidden":[]}', // Uses state currently in CurationLogic |
| 133 | + } |
| 134 | + ); |
| 135 | + expect(CurationLogic.actions.onCurationLoad).toHaveBeenCalledWith(MOCK_CURATION_RESPONSE); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should allow passing a custom queries param', async () => { |
| 139 | + http.put.mockReturnValueOnce(Promise.resolve(MOCK_CURATION_RESPONSE)); |
| 140 | + mount({}, { curationId: 'cur-123456789' }); |
| 141 | + jest.spyOn(CurationLogic.actions, 'onCurationLoad'); |
| 142 | + |
| 143 | + CurationLogic.actions.updateCuration({ queries: ['hello', 'world'] }); |
| 144 | + jest.runAllTimers(); |
| 145 | + await nextTick(); |
| 146 | + |
| 147 | + expect(http.put).toHaveBeenCalledWith( |
| 148 | + '/api/app_search/engines/some-engine/curations/cur-123456789', |
| 149 | + { |
| 150 | + body: '{"queries":["hello","world"],"query":"","promoted":[],"hidden":[]}', |
| 151 | + } |
| 152 | + ); |
| 153 | + expect(CurationLogic.actions.onCurationLoad).toHaveBeenCalledWith(MOCK_CURATION_RESPONSE); |
| 154 | + }); |
| 155 | + |
| 156 | + it('handles errors', async () => { |
| 157 | + http.put.mockReturnValueOnce(Promise.reject('error')); |
| 158 | + mount({}, { curationId: 'cur-123456789' }); |
| 159 | + |
| 160 | + CurationLogic.actions.updateCuration(); |
| 161 | + jest.runAllTimers(); |
| 162 | + await nextTick(); |
| 163 | + |
| 164 | + expect(clearFlashMessages).toHaveBeenCalled(); |
| 165 | + expect(flashAPIErrors).toHaveBeenCalledWith('error'); |
| 166 | + }); |
| 167 | + }); |
| 168 | + }); |
| 169 | +}); |
0 commit comments