|
| 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 | + formatPromptGroups, |
| 10 | + getAllPromptIds, |
| 11 | + promptGroups, |
| 12 | + StarterPrompts, |
| 13 | +} from './starter_prompts'; |
| 14 | +import { fireEvent, render } from '@testing-library/react'; |
| 15 | +import { TestProviders } from '../../mock/test_providers/test_providers'; |
| 16 | +import React from 'react'; |
| 17 | +import { useFindPrompts } from '../../..'; |
| 18 | +const mockResponse = [ |
| 19 | + { |
| 20 | + promptId: 'starterPromptTitle1', |
| 21 | + prompt: 'starterPromptTitle1 from API yall', |
| 22 | + }, |
| 23 | + { |
| 24 | + promptId: 'starterPromptDescription1', |
| 25 | + prompt: 'starterPromptDescription1 from API yall', |
| 26 | + }, |
| 27 | + { |
| 28 | + promptId: 'starterPromptIcon1', |
| 29 | + prompt: 'starterPromptIcon1 from API yall', |
| 30 | + }, |
| 31 | + { |
| 32 | + promptId: 'starterPromptPrompt1', |
| 33 | + prompt: 'starterPromptPrompt1 from API yall', |
| 34 | + }, |
| 35 | + { |
| 36 | + promptId: 'starterPromptDescription2', |
| 37 | + prompt: 'starterPromptDescription2 from API yall', |
| 38 | + }, |
| 39 | + { |
| 40 | + promptId: 'starterPromptTitle2', |
| 41 | + prompt: 'starterPromptTitle2 from API yall', |
| 42 | + }, |
| 43 | + { |
| 44 | + promptId: 'starterPromptIcon2', |
| 45 | + prompt: 'starterPromptIcon2 from API yall', |
| 46 | + }, |
| 47 | + { |
| 48 | + promptId: 'starterPromptPrompt2', |
| 49 | + prompt: 'starterPromptPrompt2 from API yall', |
| 50 | + }, |
| 51 | + { |
| 52 | + promptId: 'starterPromptDescription3', |
| 53 | + prompt: 'starterPromptDescription3 from API yall', |
| 54 | + }, |
| 55 | + { |
| 56 | + promptId: 'starterPromptTitle3', |
| 57 | + prompt: 'starterPromptTitle3 from API yall', |
| 58 | + }, |
| 59 | + { |
| 60 | + promptId: 'starterPromptIcon3', |
| 61 | + prompt: 'starterPromptIcon3 from API yall', |
| 62 | + }, |
| 63 | + { |
| 64 | + promptId: 'starterPromptPrompt3', |
| 65 | + prompt: 'starterPromptPrompt3 from API yall', |
| 66 | + }, |
| 67 | + { |
| 68 | + promptId: 'starterPromptDescription4', |
| 69 | + prompt: 'starterPromptDescription4 from API yall', |
| 70 | + }, |
| 71 | + { |
| 72 | + promptId: 'starterPromptTitle4', |
| 73 | + prompt: 'starterPromptTitle4 from API yall', |
| 74 | + }, |
| 75 | + { |
| 76 | + promptId: 'starterPromptPrompt4', |
| 77 | + prompt: 'starterPromptPrompt4 from API yall', |
| 78 | + }, |
| 79 | +]; |
| 80 | + |
| 81 | +const testProps = { |
| 82 | + setUserPrompt: jest.fn(), |
| 83 | +}; |
| 84 | + |
| 85 | +jest.mock('../../..', () => { |
| 86 | + return { |
| 87 | + useFindPrompts: jest.fn(), |
| 88 | + useAssistantContext: () => ({ |
| 89 | + assistantAvailability: { |
| 90 | + isAssistantEnabled: true, |
| 91 | + }, |
| 92 | + http: { fetch: {} }, |
| 93 | + }), |
| 94 | + }; |
| 95 | +}); |
| 96 | + |
| 97 | +describe('StarterPrompts', () => { |
| 98 | + it('should return an empty array if no prompts are provided', () => { |
| 99 | + expect(getAllPromptIds(promptGroups)).toEqual([ |
| 100 | + 'starterPromptTitle1', |
| 101 | + 'starterPromptDescription1', |
| 102 | + 'starterPromptIcon1', |
| 103 | + 'starterPromptPrompt1', |
| 104 | + 'starterPromptDescription2', |
| 105 | + 'starterPromptTitle2', |
| 106 | + 'starterPromptIcon2', |
| 107 | + 'starterPromptPrompt2', |
| 108 | + 'starterPromptDescription3', |
| 109 | + 'starterPromptTitle3', |
| 110 | + 'starterPromptIcon3', |
| 111 | + 'starterPromptPrompt3', |
| 112 | + 'starterPromptDescription4', |
| 113 | + 'starterPromptTitle4', |
| 114 | + 'starterPromptIcon4', |
| 115 | + 'starterPromptPrompt4', |
| 116 | + ]); |
| 117 | + }); |
| 118 | + it('should return the correct prompt groups with fetched prompts', () => { |
| 119 | + const response = formatPromptGroups(mockResponse); |
| 120 | + expect(response).toEqual([ |
| 121 | + { |
| 122 | + description: 'starterPromptDescription1 from API yall', |
| 123 | + icon: 'starterPromptIcon1 from API yall', |
| 124 | + prompt: 'starterPromptPrompt1 from API yall', |
| 125 | + title: 'starterPromptTitle1 from API yall', |
| 126 | + }, |
| 127 | + { |
| 128 | + description: 'starterPromptDescription2 from API yall', |
| 129 | + icon: 'starterPromptIcon2 from API yall', |
| 130 | + prompt: 'starterPromptPrompt2 from API yall', |
| 131 | + title: 'starterPromptTitle2 from API yall', |
| 132 | + }, |
| 133 | + { |
| 134 | + description: 'starterPromptDescription3 from API yall', |
| 135 | + icon: 'starterPromptIcon3 from API yall', |
| 136 | + prompt: 'starterPromptPrompt3 from API yall', |
| 137 | + title: 'starterPromptTitle3 from API yall', |
| 138 | + }, |
| 139 | + // starterPrompt Group4 should not exist because starterPromptIcon4 is not in the mockResponse |
| 140 | + ]); |
| 141 | + }); |
| 142 | + it('the component renders correctly with valid props', () => { |
| 143 | + (useFindPrompts as jest.Mock).mockReturnValue({ data: { prompts: mockResponse } }); |
| 144 | + const { getByTestId } = render( |
| 145 | + <TestProviders> |
| 146 | + <StarterPrompts {...testProps} /> |
| 147 | + </TestProviders> |
| 148 | + ); |
| 149 | + expect(getByTestId('starterPromptPrompt2 from API yall')).toBeInTheDocument(); |
| 150 | + }); |
| 151 | + it('calls setUserPrompt when a prompt is selected', () => { |
| 152 | + (useFindPrompts as jest.Mock).mockReturnValue({ data: { prompts: mockResponse } }); |
| 153 | + const { getByTestId } = render( |
| 154 | + <TestProviders> |
| 155 | + <StarterPrompts {...testProps} /> |
| 156 | + </TestProviders> |
| 157 | + ); |
| 158 | + fireEvent.click(getByTestId('starterPromptPrompt2 from API yall')); |
| 159 | + expect(testProps.setUserPrompt).toHaveBeenCalledWith('starterPromptPrompt2 from API yall'); |
| 160 | + }); |
| 161 | +}); |
0 commit comments