|
2 | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | 3 | // found in the LICENSE file. |
4 | 4 |
|
5 | | -import {renderElementIntoDOM} from '../../../testing/DOMHelpers.js'; |
| 5 | +import * as Host from '../../../core/host/host.js'; |
6 | 6 | import { |
7 | 7 | describeWithEnvironment, |
8 | 8 | } from '../../../testing/EnvironmentHelpers.js'; |
9 | 9 | import * as Freestyler from '../ai_assistance.js'; |
10 | 10 |
|
11 | 11 | describeWithEnvironment('UserActionRow', () => { |
| 12 | + function createComponent(props: Freestyler.UserActionRowProps): |
| 13 | + [sinon.SinonStub<[Freestyler.ViewInput, Freestyler.ViewOutput, HTMLElement], void>, Freestyler.UserActionRow] { |
| 14 | + const view = sinon.stub<[Freestyler.ViewInput, Freestyler.ViewOutput, HTMLElement]>(); |
| 15 | + const component = new Freestyler.UserActionRow(undefined, view); |
| 16 | + Object.assign(component, props); |
| 17 | + component.wasShown(); |
| 18 | + return [view, component]; |
| 19 | + } |
| 20 | + |
12 | 21 | it('should show the feedback form when canShowFeedbackForm is true', async () => { |
13 | | - const component = new Freestyler.UserActionRow({ |
| 22 | + const [view] = createComponent({ |
14 | 23 | showRateButtons: true, |
15 | | - onFeedbackSubmit: sinon.stub(), |
16 | 24 | canShowFeedbackForm: true, |
17 | | - handleSuggestionClick: sinon.stub(), |
| 25 | + onSuggestionClick: sinon.stub(), |
| 26 | + onFeedbackSubmit: sinon.stub(), |
18 | 27 | }); |
19 | | - renderElementIntoDOM(component); |
20 | | - const button = component.shadowRoot!.querySelector('.rate-buttons devtools-button')! as HTMLElement; |
21 | | - button.click(); |
22 | 28 |
|
23 | | - assert(component.shadowRoot!.querySelector('.feedback-form')); |
| 29 | + assert.isTrue(view.calledOnce); |
| 30 | + |
| 31 | + { |
| 32 | + const [viewInput] = view.args[0]; |
| 33 | + expect(viewInput.isShowingFeedbackForm).equals(false); |
| 34 | + viewInput.onRatingClick(Host.AidaClient.Rating.POSITIVE); |
| 35 | + } |
| 36 | + |
| 37 | + assert.isTrue(view.calledTwice); |
| 38 | + { |
| 39 | + const [viewInput] = view.args[0]; |
| 40 | + expect(viewInput.isShowingFeedbackForm).equals(true); |
| 41 | + } |
24 | 42 | }); |
25 | 43 |
|
26 | 44 | it('should not show the feedback form when canShowFeedbackForm is false', async () => { |
27 | | - const component = new Freestyler.UserActionRow({ |
| 45 | + const [view] = createComponent({ |
28 | 46 | showRateButtons: true, |
29 | | - onFeedbackSubmit: sinon.stub(), |
30 | 47 | canShowFeedbackForm: false, |
31 | | - handleSuggestionClick: sinon.stub(), |
| 48 | + onSuggestionClick: sinon.stub(), |
| 49 | + onFeedbackSubmit: sinon.stub(), |
32 | 50 | }); |
33 | | - renderElementIntoDOM(component); |
34 | 51 |
|
35 | | - const button = component.shadowRoot!.querySelector('.rate-buttons devtools-button')! as HTMLElement; |
36 | | - button.click(); |
| 52 | + assert.isTrue(view.calledOnce); |
| 53 | + |
| 54 | + { |
| 55 | + const [viewInput] = view.args[0]; |
| 56 | + expect(viewInput.isShowingFeedbackForm).equals(false); |
| 57 | + viewInput.onRatingClick(Host.AidaClient.Rating.POSITIVE); |
| 58 | + } |
37 | 59 |
|
38 | | - assert.notExists(component.shadowRoot!.querySelector('.feedback-form')); |
| 60 | + assert.isTrue(view.calledTwice); |
| 61 | + { |
| 62 | + const [viewInput] = view.args[0]; |
| 63 | + expect(viewInput.isShowingFeedbackForm).equals(false); |
| 64 | + } |
39 | 65 | }); |
40 | 66 |
|
41 | 67 | it('should disable the submit button when the input is empty', async () => { |
42 | | - const component = new Freestyler.UserActionRow({ |
| 68 | + const [view] = createComponent({ |
43 | 69 | showRateButtons: true, |
44 | | - onFeedbackSubmit: sinon.stub(), |
45 | 70 | canShowFeedbackForm: true, |
46 | | - handleSuggestionClick: sinon.stub(), |
| 71 | + onSuggestionClick: sinon.stub(), |
| 72 | + onFeedbackSubmit: sinon.stub(), |
47 | 73 | }); |
48 | | - renderElementIntoDOM(component); |
49 | | - const button = component.shadowRoot!.querySelector('.rate-buttons devtools-button')! as HTMLElement; |
50 | | - button.click(); |
51 | | - |
52 | | - assert(component.shadowRoot!.querySelector('.feedback-form')); |
53 | | - const submitButton = component.shadowRoot!.querySelector('[aria-label="Submit"]') as HTMLButtonElement; |
54 | | - assert.isTrue(submitButton?.disabled); |
55 | | - const inputField = component.shadowRoot!.querySelector('.feedback-form input')! as HTMLInputElement; |
56 | | - inputField.value = 'test'; |
57 | | - inputField.dispatchEvent(new Event('input')); |
58 | | - assert.isFalse(submitButton?.disabled); |
| 74 | + |
| 75 | + assert.isTrue(view.calledOnce); |
| 76 | + |
| 77 | + { |
| 78 | + const [viewInput] = view.args[0]; |
| 79 | + expect(viewInput.isSubmitButtonDisabled).equals(false); |
| 80 | + viewInput.onRatingClick(Host.AidaClient.Rating.POSITIVE); |
| 81 | + } |
| 82 | + |
| 83 | + assert.isTrue(view.calledTwice); |
| 84 | + |
| 85 | + { |
| 86 | + const [viewInput] = view.args[0]; |
| 87 | + expect(viewInput.isSubmitButtonDisabled).equals(false); |
| 88 | + expect(viewInput.isShowingFeedbackForm).equals(true); |
| 89 | + viewInput.onInputChange('test'); |
| 90 | + viewInput.onSubmit(new SubmitEvent('submit')); |
| 91 | + } |
| 92 | + |
| 93 | + { |
| 94 | + const [viewInput] = view.args[0]; |
| 95 | + expect(viewInput.isSubmitButtonDisabled).equals(true); |
| 96 | + } |
59 | 97 | }); |
60 | 98 | }); |
0 commit comments