-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Move suggested actions from Redux store to React hooks #5489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
compulim
merged 20 commits into
microsoft:main
from
compulim:port-store-suggested-actions
May 30, 2025
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6db9450
Add suggested actions test
compulim 9c79f6e
Add react-valibot and redux-store package
compulim ad3c7f3
Add entry
compulim e677a01
Sort
compulim 48c4687
Fix aria-keyshortcuts
compulim 9c88513
Add react-valibot deps
compulim 235211e
Use a single setRawState action
compulim f381623
Update comment
compulim 8464905
Test bidirectional replication
compulim 1da27b1
Wait for connected
compulim 1923964
Clean up
compulim c24e1a9
Safe handling actions
compulim 15d218f
No declare global
compulim a1704c0
chore: local lint rules stub
OEvgeny 208fd80
Clean up
compulim 0971f8e
Mark as readonly
compulim c49b996
Use base package
compulim f479b80
Merge pull request #5 from microsoft/poc/custom-eslint-rules
compulim 0459d1a
declare var process
compulim c3215c1
Deprecate useSuggestedActions()
compulim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <!doctype html> | ||
| <html lang="en-US"> | ||
| <head> | ||
| <link href="/assets/index.css" rel="stylesheet" type="text/css" /> | ||
| <script crossorigin="anonymous" src="/test-harness.js"></script> | ||
| <script crossorigin="anonymous" src="/test-page-object.js"></script> | ||
| <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script> | ||
| </head> | ||
| <body> | ||
| <main id="webchat"></main> | ||
| <script> | ||
| run(async function () { | ||
| const { | ||
| testHelpers: { createDirectLineEmulator } | ||
| } = window; | ||
|
|
||
| const { directLine, store } = createDirectLineEmulator(); | ||
|
|
||
| WebChat.renderWebChat({ directLine, store }, document.getElementById('webchat')); | ||
|
|
||
| await pageConditions.uiConnected(); | ||
| }); | ||
| </script> | ||
| </body> | ||
| </html> |
145 changes: 145 additions & 0 deletions
145
__tests__/html2/store/suggestedActions/hooks.useSuggestedActions.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| <!doctype html> | ||
| <html lang="en-US"> | ||
| <head> | ||
| <link href="/assets/index.css" rel="stylesheet" type="text/css" /> | ||
| <script crossorigin="anonymous" src="https://unpkg.com/react@16.8.6/umd/react.development.js"></script> | ||
| <script crossorigin="anonymous" src="https://unpkg.com/react-dom@16.8.6/umd/react-dom.development.js"></script> | ||
| <script crossorigin="anonymous" src="/test-harness.js"></script> | ||
| <script crossorigin="anonymous" src="/test-page-object.js"></script> | ||
| <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script> | ||
| </head> | ||
| <body> | ||
| <main id="webchat"></main> | ||
| <script type="importmap"> | ||
| { | ||
| "imports": { | ||
| "@testduet/wait-for": "https://unpkg.com/@testduet/wait-for@main/dist/wait-for.mjs" | ||
| } | ||
| } | ||
| </script> | ||
| <script type="module"> | ||
| import { waitFor } from '@testduet/wait-for'; | ||
| import renderHook from '../../hooks/private/renderHook.js'; | ||
|
|
||
| const { | ||
| React: { createElement }, | ||
| testHelpers: { createDirectLineEmulator }, | ||
| WebChat: { | ||
| Components: { BasicWebChat, Composer }, | ||
| hooks: { useSuggestedActions }, | ||
| testIds | ||
| } | ||
| } = window; | ||
|
|
||
| run(async function () { | ||
| const { directLine, store } = createDirectLineEmulator(); | ||
| const WebChatWrapper = ({ children }) => | ||
| createElement(Composer, { directLine, store }, createElement(BasicWebChat), children); | ||
|
|
||
| // WHEN: Render initially. | ||
| const renderResult = renderHook( | ||
| props => { | ||
| const state = useSuggestedActions(); | ||
|
|
||
| if (props) { | ||
| state[1](props.suggestedActions); | ||
| } else { | ||
| return state; | ||
| } | ||
| }, | ||
| { | ||
| legacyRoot: true, | ||
| wrapper: WebChatWrapper | ||
| } | ||
| ); | ||
|
|
||
| await pageConditions.uiConnected(); | ||
|
|
||
| // THEN: useSuggestedActions() getter should return empty array. | ||
| await waitFor(() => | ||
| expect(renderResult).toHaveProperty('result.current', [[], expect.anything(), { activity: undefined }]) | ||
| ); | ||
|
|
||
| // WHEN: An activity with 2 suggested actions is received. | ||
| await directLine.emulateIncomingActivity({ | ||
| from: { role: 'bot' }, | ||
| suggestedActions: { | ||
| actions: [ | ||
| { title: 'Hello, World!', type: 'imBack' }, | ||
| { title: 'Aloha!', type: 'imBack' } | ||
| ], | ||
| to: '' | ||
| }, | ||
| text: 'Hello, World!', | ||
| type: 'message' | ||
| }); | ||
|
|
||
| // THEN: useSuggestedActions() getter should return 2 suggested actions and origin activity. | ||
| renderResult.rerender(); | ||
|
|
||
| await waitFor(() => | ||
| expect(renderResult).toHaveProperty('result.current', [ | ||
| [ | ||
| { title: 'Hello, World!', type: 'imBack' }, | ||
| { title: 'Aloha!', type: 'imBack' } | ||
| ], | ||
| expect.any(Function), | ||
| { | ||
| activity: expect.objectContaining({ | ||
| from: { role: 'bot' }, | ||
| suggestedActions: { | ||
| actions: [ | ||
| { title: 'Hello, World!', type: 'imBack' }, | ||
| { title: 'Aloha!', type: 'imBack' } | ||
| ], | ||
| to: '' | ||
| }, | ||
| text: 'Hello, World!', | ||
| type: 'message' | ||
| }) | ||
| } | ||
| ]) | ||
| ); | ||
|
|
||
| // WHEN: useSuggestedActions() setter is called with 1 suggested action. | ||
| renderResult.rerender({ suggestedActions: [{ title: 'Good morning!', type: 'imBack' }] }); | ||
|
|
||
| // THEN: Should show 1 suggested action. | ||
| await waitFor(() => expect(pageElements.allByTestId(testIds.suggestedActionButton)).toHaveLength(1)); | ||
| expect(pageElements.allByTestId(testIds.suggestedActionButton)[0]).toHaveProperty( | ||
| 'textContent', | ||
| 'Good morning!' | ||
| ); | ||
|
|
||
| // THEN: Should return 1 suggested action. | ||
| renderResult.rerender(); | ||
| await waitFor(() => | ||
| expect(renderResult).toHaveProperty('result.current', [ | ||
| [{ title: 'Good morning!', type: 'imBack' }], | ||
| expect.any(Function), | ||
| { activity: undefined } | ||
| ]) | ||
| ); | ||
|
|
||
| // THEN: getState() should have 1 suggested action. | ||
| expect(store.getState().suggestedActions).toHaveLength(1); | ||
| expect(store.getState().suggestedActions[0]).toEqual({ title: 'Good morning!', type: 'imBack' }); | ||
|
|
||
| // WHEN: useSuggestedActions() is called with no suggested actions. | ||
| renderResult.rerender({ suggestedActions: [] }); | ||
|
|
||
| // THEN: Should hide suggested actions. | ||
| await waitFor(() => expect(pageElements.allByTestId(testIds.suggestedActionButton)).toHaveLength(0)); | ||
|
|
||
| // THEN: Should return 0 suggested actions. | ||
| renderResult.rerender(); | ||
| await waitFor(() => | ||
| expect(renderResult).toHaveProperty('result.current', [[], expect.any(Function), { activity: undefined }]) | ||
| ); | ||
|
|
||
| // THEN: getState() should have 1 suggested action. | ||
| expect(store.getState().suggestedActions).toHaveLength(0); | ||
| }); | ||
| </script> | ||
| </body> | ||
| </html> |
93 changes: 93 additions & 0 deletions
93
__tests__/html2/store/suggestedActions/redux.clearSuggestedActions.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| <!doctype html> | ||
| <html lang="en-US"> | ||
| <head> | ||
| <link href="/assets/index.css" rel="stylesheet" type="text/css" /> | ||
| <script crossorigin="anonymous" src="https://unpkg.com/react@16.8.6/umd/react.development.js"></script> | ||
| <script crossorigin="anonymous" src="https://unpkg.com/react-dom@16.8.6/umd/react-dom.development.js"></script> | ||
| <script crossorigin="anonymous" src="/test-harness.js"></script> | ||
| <script crossorigin="anonymous" src="/test-page-object.js"></script> | ||
| <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script> | ||
| </head> | ||
| <body> | ||
| <main id="webchat"></main> | ||
| <script type="importmap"> | ||
| { | ||
| "imports": { | ||
| "@testduet/wait-for": "https://esm.sh/@testduet/wait-for" | ||
| } | ||
| } | ||
| </script> | ||
| <script type="module"> | ||
| import { waitFor } from '@testduet/wait-for'; | ||
| import renderHook from '../../hooks/private/renderHook.js'; | ||
|
|
||
| run(async function () { | ||
| const { | ||
| React: { createElement }, | ||
| testHelpers: { createDirectLineEmulator }, | ||
| WebChat: { | ||
| Components: { BasicWebChat, Composer }, | ||
| hooks: { useSuggestedActions }, | ||
| testIds | ||
| } | ||
| } = window; | ||
|
|
||
| const { directLine, store } = createDirectLineEmulator(); | ||
|
|
||
| // WHEN: Render initially. | ||
| const renderResult = renderHook(() => useSuggestedActions(), { | ||
| legacyRoot: true, | ||
| wrapper: ({ children }) => | ||
| createElement(Composer, { directLine, store }, createElement(BasicWebChat), children) | ||
| }); | ||
|
|
||
| await pageConditions.uiConnected(); | ||
|
|
||
| // WHEN: Receive an activity with a suggested action. | ||
| await directLine.emulateIncomingActivity({ | ||
| from: { role: 'bot' }, | ||
| suggestedActions: { | ||
| actions: [{ title: 'Aloha!', type: 'imBack' }], | ||
| to: '' | ||
| }, | ||
| text: 'Hello, World!', | ||
| type: 'message' | ||
| }); | ||
|
|
||
| await pageConditions.numActivitiesShown(1); | ||
|
|
||
| // THEN: Should have one suggested action button shown. | ||
| expect(pageElements.allByTestId(testIds.suggestedActionButton)).toHaveLength(1); | ||
| expect(pageElements.allByTestId(testIds.suggestedActionButton)[0]).toHaveProperty('textContent', 'Aloha!'); | ||
|
|
||
| // THEN: getState() should have 1 suggested actions and origin activity. | ||
| expect(store.getState().suggestedActions).toHaveLength(1); | ||
| expect(store.getState().suggestedActions[0]).toEqual({ title: 'Aloha!', type: 'imBack' }); | ||
| expect(store.getState().suggestedActionsOriginActivity).toEqual({ | ||
| activity: expect.objectContaining({ | ||
| from: { role: 'bot' }, | ||
| suggestedActions: { | ||
| actions: [{ title: 'Aloha!', type: 'imBack' }], | ||
| to: '' | ||
| }, | ||
| text: 'Hello, World!', | ||
| type: 'message' | ||
| }) | ||
| }); | ||
|
|
||
| // WHEN: Dispatching "WEB_CHAT/CLEAR_SUGGESTED_ACTIONS" action. | ||
| store.dispatch({ type: 'WEB_CHAT/CLEAR_SUGGESTED_ACTIONS' }); | ||
|
|
||
| // THEN: Should not remove activity. | ||
| expect(pageElements.activities()).toHaveLength(1); | ||
|
|
||
| // THEN: Should have cleared suggested action. | ||
| await waitFor(() => expect(pageElements.allByTestId(testIds.suggestedActionButton)).toHaveLength(0)); | ||
|
|
||
| // THEN: useSuggestedActions() should have emptied. | ||
| renderResult.rerender(); | ||
| expect(renderResult).toHaveProperty('result.current', [[], expect.any(Function), { activity: undefined }]); | ||
| }); | ||
| </script> | ||
| </body> | ||
| </html> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.