|
13 | 13 | <main id="webchat"></main> |
14 | 14 | <script type="text/babel" data-presets="env,stage-3,react"> |
15 | 15 | const { |
| 16 | + testHelpers: { createDirectLineEmulator }, |
16 | 17 | WebChat: { |
17 | 18 | Components: { BasicWebChat, Composer }, |
18 | 19 | hooks: { useActiveTyping } |
19 | 20 | } |
20 | 21 | } = window; |
21 | 22 |
|
22 | 23 | run(async function () { |
23 | | - const clock = lolex.createClock(); |
24 | | - |
25 | | - const directLine = WebChat.createDirectLine({ token: await testHelpers.token.fetchDirectLineToken() }); |
26 | | - const store = testHelpers.createStoreWithOptions({ ponyfill: clock }); |
| 24 | + const ponyfill = lolex.createClock(); |
| 25 | + const { directLine, store } = createDirectLineEmulator({ ponyfill }); |
27 | 26 |
|
28 | 27 | const RunFunction = ({ fn }) => { |
29 | 28 | fn(); |
|
34 | 33 | const renderWithFunction = fn => |
35 | 34 | new Promise(resolve => |
36 | 35 | ReactDOM.render( |
37 | | - <Composer directLine={directLine} ponyfill={clock} sendTypingIndicator={true} store={store}> |
| 36 | + <Composer directLine={directLine} ponyfill={ponyfill} sendTypingIndicator={true} store={store}> |
38 | 37 | <BasicWebChat /> |
39 | 38 | <RunFunction fn={() => resolve(fn && fn())} key={Date.now() + ''} /> |
40 | 39 | </Composer>, |
|
46 | 45 |
|
47 | 46 | await pageConditions.webChatRendered(); |
48 | 47 |
|
49 | | - clock.tick(600); |
| 48 | + ponyfill.tick(600); |
50 | 49 |
|
51 | 50 | // WHEN: At initial. |
52 | 51 | await pageConditions.uiConnected(); |
|
55 | 54 | await expect(renderWithFunction(() => useActiveTyping())).resolves.toEqual([{}]); |
56 | 55 |
|
57 | 56 | // WHEN: Type "a" without send. |
58 | | - await pageObjects.typeInSendBox('a'); |
| 57 | + await directLine.actPostActivity(() => pageObjects.typeInSendBox('a')); |
59 | 58 |
|
60 | 59 | // THEN: `useActiveTyping` should return current user. |
61 | 60 | await expect(renderWithFunction(() => Object.values(useActiveTyping()[0]))).resolves.toEqual([ |
|
69 | 68 | ]); |
70 | 69 |
|
71 | 70 | // WHEN: Type "b" at t=10. |
72 | | - clock.tick(10); |
| 71 | + ponyfill.tick(10); |
73 | 72 | await pageObjects.typeInSendBox('b'); |
74 | 73 |
|
75 | 74 | // We need to wait for 6000 ms because: |
|
78 | 77 | // 3. t=10: Typed letter "b" |
79 | 78 | // 4. t=10: Scheduled another typing indicator at t=3000 |
80 | 79 | // 5. t=3000: Send typing activity |
81 | | - clock.tick(3000); |
82 | | - |
83 | | - // TODO: Understand why we need to yield after clock.tick and see if we could eliminate it. |
84 | | - await 0; |
| 80 | + await directLine.actPostActivity(() => ponyfill.tick(3000)); |
85 | 81 |
|
86 | 82 | // THEN: `useActiveTyping` should return current user. |
87 | 83 | await expect(renderWithFunction(() => Object.values(useActiveTyping()[0]))).resolves.toEqual([ |
|
95 | 91 | ]); |
96 | 92 |
|
97 | 93 | // WHEN: After 8 seconds. |
98 | | - clock.tick(8000); |
| 94 | + ponyfill.tick(8000); |
99 | 95 |
|
100 | 96 | // THEN: `useActiveTyping` should return nothing. |
101 | 97 | await expect(renderWithFunction(() => useActiveTyping())).resolves.toEqual([{}]); |
|
0 commit comments