|
| 1 | +<!doctype html> |
| 2 | +<html lang="en-US"> |
| 3 | + <head> |
| 4 | + <link href="/assets/index.css" rel="stylesheet" type="text/css" /> |
| 5 | + </head> |
| 6 | + <body> |
| 7 | + <main id="webchat"></main> |
| 8 | + <script type="importmap"> |
| 9 | + { |
| 10 | + "imports": { |
| 11 | + "@testduet/wait-for": "https://esm.sh/@testduet/wait-for", |
| 12 | + "botframework-webchat": "/__dist__/packages/bundle/static/botframework-webchat.js", |
| 13 | + "botframework-webchat/": "/__dist__/packages/bundle/static/botframework-webchat/", |
| 14 | + "react": "/__dist__/packages/bundle/static/react.js", |
| 15 | + "react-dom": "/__dist__/packages/bundle/static/react-dom.js", |
| 16 | + "react-dom/client": "/__dist__/packages/bundle/static/react-dom/client.js" |
| 17 | + } |
| 18 | + } |
| 19 | + </script> |
| 20 | + <script type="module"> |
| 21 | + import '/test-harness.mjs'; |
| 22 | + import '/test-page-object.mjs'; |
| 23 | + |
| 24 | + import { waitFor } from '@testduet/wait-for'; |
| 25 | + import { createStoreWithOptions, ReactWebChat } from 'botframework-webchat'; |
| 26 | + import { createElement } from 'react'; |
| 27 | + import { createRoot } from 'react-dom/client'; |
| 28 | + |
| 29 | + // TODO: This is for `createDirectLineEmulator` only, should find ways to eliminate this line. |
| 30 | + window.WebChat = { createStoreWithOptions }; |
| 31 | + |
| 32 | + run(async function () { |
| 33 | + const { directLine, store } = testHelpers.createDirectLineEmulator(); |
| 34 | + |
| 35 | + createRoot(document.getElementById('webchat')).render( |
| 36 | + createElement(ReactWebChat, { directLine, store, styleOptions: { hideSendBox: true } }) |
| 37 | + ); |
| 38 | + |
| 39 | + await pageConditions.uiConnected(); |
| 40 | + |
| 41 | + // GIVEN: A carousel of hero cards, each with a button. |
| 42 | + await directLine.emulateIncomingActivity({ |
| 43 | + attachmentLayout: 'carousel', |
| 44 | + attachments: [1, 2, 3].map(index => ({ |
| 45 | + content: { |
| 46 | + buttons: [{ title: `Buy item ${index}`, type: 'imBack', value: `Buy item ${index}` }], |
| 47 | + text: 'Price: $XXX.XX USD', |
| 48 | + title: `Details about item ${index}` |
| 49 | + }, |
| 50 | + contentType: 'application/vnd.microsoft.card.hero' |
| 51 | + })), |
| 52 | + from: { role: 'bot' }, |
| 53 | + type: 'message' |
| 54 | + }); |
| 55 | + |
| 56 | + await pageConditions.numActivitiesShown(1); |
| 57 | + |
| 58 | + const carouselLayout = document.querySelector('.carousel-layout'); |
| 59 | + const carouselFilmstrip = carouselLayout.querySelector('.carousel-filmstrip'); |
| 60 | + const cards = Array.from(carouselFilmstrip.querySelectorAll('[aria-roledescription="attachment"]')); |
| 61 | + const flippers = Array.from(carouselLayout.querySelectorAll('.react-film__flipper')); |
| 62 | + const buttons = cards.map(card => card.querySelector('button')); |
| 63 | + |
| 64 | + const isCardInView = card => { |
| 65 | + const cardRect = card.getBoundingClientRect(); |
| 66 | + const filmstripRect = carouselFilmstrip.getBoundingClientRect(); |
| 67 | + |
| 68 | + return cardRect.left >= filmstripRect.left && cardRect.right <= filmstripRect.right; |
| 69 | + }; |
| 70 | + |
| 71 | + // The flipper is hidden when its body has slid out of the flipper button (1s delay after scroll stops, then 300ms slide). |
| 72 | + const isFlipperHidden = flipper => { |
| 73 | + const bodyRect = flipper.querySelector('.react-film__flipper__body').getBoundingClientRect(); |
| 74 | + const buttonRect = flipper.getBoundingClientRect(); |
| 75 | + |
| 76 | + return bodyRect.right <= buttonRect.left || bodyRect.left >= buttonRect.right; |
| 77 | + }; |
| 78 | + |
| 79 | + const expectCardInView = async index => { |
| 80 | + await waitFor(() => expect(isCardInView(cards[index])).toBe(true)); |
| 81 | + |
| 82 | + // Wait for the scroll and the flippers to settle. |
| 83 | + await waitFor( |
| 84 | + () => { |
| 85 | + expect(carouselLayout.querySelector('.react-film__main--scrolling')).toBeNull(); |
| 86 | + expect(flippers.every(isFlipperHidden)).toBe(true); |
| 87 | + }, |
| 88 | + { timeout: 5000 } |
| 89 | + ); |
| 90 | + |
| 91 | + expect(isCardInView(cards[index])).toBe(true); |
| 92 | + }; |
| 93 | + |
| 94 | + expect(cards).toHaveLength(3); |
| 95 | + expect(carouselFilmstrip.scrollLeft).toBe(0); |
| 96 | + expect(isCardInView(cards[1])).toBe(false); // The second card is partially visible. |
| 97 | + |
| 98 | + // WHEN: Focus on the first card via keyboard. |
| 99 | + pageObjects.focusTranscript(); |
| 100 | + await host.sendKeys('ENTER'); |
| 101 | + |
| 102 | + // THEN: The first card has focus, the carousel is at the start. |
| 103 | + expect(document.activeElement).toBe(cards[0]); |
| 104 | + await expectCardInView(0); |
| 105 | + expect(carouselFilmstrip.scrollLeft).toBe(0); |
| 106 | + await host.snapshot('local'); |
| 107 | + |
| 108 | + // WHEN: Press TAB. |
| 109 | + await host.sendTab(); |
| 110 | + |
| 111 | + // THEN: The "Buy item 1" button has focus, the carousel is at the start. |
| 112 | + expect(document.activeElement).toBe(buttons[0]); |
| 113 | + await expectCardInView(0); |
| 114 | + expect(carouselFilmstrip.scrollLeft).toBe(0); |
| 115 | + await host.snapshot('local'); |
| 116 | + |
| 117 | + // WHEN: Press TAB. |
| 118 | + await host.sendTab(); |
| 119 | + |
| 120 | + // THEN: The second card has focus and is scrolled fully into view, as RIGHT would do. |
| 121 | + expect(document.activeElement).toBe(cards[1]); |
| 122 | + await expectCardInView(1); |
| 123 | + expect(isCardInView(cards[0])).toBe(false); |
| 124 | + await host.snapshot('local'); |
| 125 | + |
| 126 | + // WHEN: Press SHIFT-TAB. |
| 127 | + await host.sendShiftTab(); |
| 128 | + |
| 129 | + // THEN: The "Buy item 1" button has focus, the first card is scrolled back into view. |
| 130 | + expect(document.activeElement).toBe(buttons[0]); |
| 131 | + await expectCardInView(0); |
| 132 | + expect(carouselFilmstrip.scrollLeft).toBe(0); |
| 133 | + await host.snapshot('local'); |
| 134 | + }); |
| 135 | + </script> |
| 136 | + </body> |
| 137 | +</html> |
0 commit comments