|
| 1 | +<!doctype html> |
| 2 | +<html lang="en-US"> |
| 3 | + <head> |
| 4 | + <link href="/assets/index.css" rel="stylesheet" type="text/css" /> |
| 5 | + <script crossorigin="anonymous" src="https://unpkg.com/@babel/standalone/babel.min.js"></script> |
| 6 | + <script crossorigin=" anonymous" src=" https://unpkg.com/[email protected]/umd/react.production.min.js" ></script> |
| 7 | + <script crossorigin=" anonymous" src=" https://unpkg.com/[email protected]/umd/react-dom.production.min.js" ></script> |
| 8 | + <script crossorigin="anonymous" src="/test-harness.js"></script> |
| 9 | + <script crossorigin="anonymous" src="/test-page-object.js"></script> |
| 10 | + <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script> |
| 11 | + </head> |
| 12 | + <body> |
| 13 | + <main id="webchat"></main> |
| 14 | + <script type="text/babel"> |
| 15 | + run(async function () { |
| 16 | + const { |
| 17 | + React, |
| 18 | + React: { createContext, useContext, useMemo, useState }, |
| 19 | + ReactDOM: { render }, |
| 20 | + WebChat: { |
| 21 | + Components: { BasicWebChat, Composer }, |
| 22 | + hooks: { useActivities } |
| 23 | + } |
| 24 | + } = window; // Imports in UMD fashion. |
| 25 | + |
| 26 | + const AppContext = createContext({ showBorder: false }); |
| 27 | + |
| 28 | + const Border = ({ children }) => { |
| 29 | + const { showBorder } = useContext(AppContext); |
| 30 | + |
| 31 | + return ( |
| 32 | + <div |
| 33 | + style={{ |
| 34 | + outlineColor: 'Red', |
| 35 | + outlineOffset: -2, |
| 36 | + outlineStyle: 'dashed', |
| 37 | + outlineWidth: showBorder ? 2 : 0 |
| 38 | + }} |
| 39 | + > |
| 40 | + {children} |
| 41 | + </div> |
| 42 | + ); |
| 43 | + }; |
| 44 | + |
| 45 | + const { directLine, store } = testHelpers.createDirectLineEmulator(); |
| 46 | + |
| 47 | + const OnActivity = () => { |
| 48 | + const [activities] = useActivities(); |
| 49 | + const { setEndOfConversation, setShowBorder } = useContext(AppContext); |
| 50 | + |
| 51 | + const lastActivity = activities[activities.length - 1]; |
| 52 | + |
| 53 | + useMemo( |
| 54 | + () => setEndOfConversation(lastActivity?.inputHint === 'ignoring'), |
| 55 | + [lastActivity, setEndOfConversation] |
| 56 | + ); |
| 57 | + |
| 58 | + useMemo(() => setShowBorder(lastActivity?.from.role === 'bot'), [lastActivity, setShowBorder]); |
| 59 | + |
| 60 | + return false; |
| 61 | + }; |
| 62 | + |
| 63 | + const App = () => { |
| 64 | + const [showBorder, setShowBorder] = useState(false); |
| 65 | + const [endOfConversation, setEndOfConversation] = useState(false); |
| 66 | + const context = useMemo( |
| 67 | + () => ({ endOfConversation, setEndOfConversation, setShowBorder, showBorder }), |
| 68 | + [endOfConversation, setEndOfConversation, setShowBorder, showBorder] |
| 69 | + ); |
| 70 | + |
| 71 | + const attentionMiddleware = useMemo( |
| 72 | + () => |
| 73 | + Object.freeze([ |
| 74 | + () => next => request => { |
| 75 | + const NextComponent = next(request); |
| 76 | + |
| 77 | + return props => ( |
| 78 | + <Border> |
| 79 | + <NextComponent {...props} /> |
| 80 | + </Border> |
| 81 | + ); |
| 82 | + } |
| 83 | + ]), |
| 84 | + [] |
| 85 | + ); |
| 86 | + |
| 87 | + const endOfConversationMiddleware = useMemo( |
| 88 | + () => Object.freeze([() => () => () => () => <div>Thank you. You can close the chat now.</div>]), |
| 89 | + [] |
| 90 | + ); |
| 91 | + |
| 92 | + const sendBoxMiddleware = useMemo( |
| 93 | + () => (endOfConversation ? endOfConversationMiddleware : attentionMiddleware), |
| 94 | + [attentionMiddleware, endOfConversation, endOfConversationMiddleware] |
| 95 | + ); |
| 96 | + |
| 97 | + return ( |
| 98 | + <AppContext.Provider value={context}> |
| 99 | + <Composer directLine={directLine} store={store} sendBoxMiddleware={sendBoxMiddleware}> |
| 100 | + <OnActivity /> |
| 101 | + <BasicWebChat /> |
| 102 | + </Composer> |
| 103 | + </AppContext.Provider> |
| 104 | + ); |
| 105 | + }; |
| 106 | + |
| 107 | + render(<App />, document.getElementById('webchat')); |
| 108 | + |
| 109 | + await pageConditions.uiConnected(); |
| 110 | + |
| 111 | + // SCENARIO: If the last message is from bot, show a border around the send box. |
| 112 | + |
| 113 | + // WHEN: Received a message from bot. |
| 114 | + await directLine.emulateIncomingActivity( |
| 115 | + 'Culpa qui aliqua officia pariatur sit commodo in occaecat deserunt excepteur ad irure.' |
| 116 | + ); |
| 117 | + |
| 118 | + await pageConditions.numActivitiesShown(1); |
| 119 | + |
| 120 | + // THEN: Should render a border around the default send box. |
| 121 | + await host.snapshot(); |
| 122 | + |
| 123 | + // WHEN: Send a message. |
| 124 | + await (await directLine.emulateOutgoingActivity('Hello, World!')).resolveAll(); |
| 125 | + |
| 126 | + // THEN: Should not render a border around the default send box because the last message is not from bot. |
| 127 | + await host.snapshot(); |
| 128 | + |
| 129 | + // WHEN: Received a message from bot with inputHint of "ignoring". |
| 130 | + await directLine.emulateIncomingActivity({ |
| 131 | + inputHint: 'ignoring', |
| 132 | + text: 'Irure incididunt excepteur incididunt sunt occaecat excepteur.', |
| 133 | + type: 'message' |
| 134 | + }); |
| 135 | + |
| 136 | + // THEN: Should hide the send box because it changed to a different set of middleware. |
| 137 | + await host.snapshot(); |
| 138 | + }); |
| 139 | + </script> |
| 140 | + </body> |
| 141 | +</html> |
0 commit comments