|
| 1 | +function setupBasicPage() { |
| 2 | + document.body.innerHTML = ` |
| 3 | + <nav>Nav Text</nav> |
| 4 | + <section> |
| 5 | + <h1>First Section Heading</h1> |
| 6 | + <p>First Section Text</p> |
| 7 | + <article> |
| 8 | + <header> |
| 9 | + <h1>Article Header Heading</h1> |
| 10 | + <p>Article Header Text</p> |
| 11 | + </header> |
| 12 | + <p>Article Text</p> |
| 13 | + </article> |
| 14 | + </section> |
| 15 | + <section> |
| 16 | + <h1>Second Section Heading</h1> |
| 17 | + <p>Second Section Text</p> |
| 18 | + </section> |
| 19 | + <section aria-hidden="true"> |
| 20 | + <h1>Hidden Section Heading</h1> |
| 21 | + <p>Hidden Section Text</p> |
| 22 | + </section> |
| 23 | + <footer>Footer</footer> |
| 24 | + `; |
| 25 | +} |
| 26 | + |
| 27 | +describe("matchers", () => { |
| 28 | + beforeEach(() => { |
| 29 | + setupBasicPage(); |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + document.body.innerHTML = ``; |
| 34 | + }); |
| 35 | + |
| 36 | + test("toMatchScreenReaderSnapshot on the whole body", async () => { |
| 37 | + await expect(document.body).toMatchScreenReaderSnapshot(); |
| 38 | + }); |
| 39 | + |
| 40 | + test("toMatchScreenReaderInlineSnapshot on the whole body", async () => { |
| 41 | + await expect(document.body).toMatchScreenReaderInlineSnapshot(` |
| 42 | +[ |
| 43 | + "document", |
| 44 | + "navigation", |
| 45 | + "Nav Text", |
| 46 | + "end of navigation", |
| 47 | + "region", |
| 48 | + "heading, First Section Heading, level 1", |
| 49 | + "paragraph", |
| 50 | + "First Section Text", |
| 51 | + "end of paragraph", |
| 52 | + "article", |
| 53 | + "banner", |
| 54 | + "heading, Article Header Heading, level 1", |
| 55 | + "paragraph", |
| 56 | + "Article Header Text", |
| 57 | + "end of paragraph", |
| 58 | + "end of banner", |
| 59 | + "paragraph", |
| 60 | + "Article Text", |
| 61 | + "end of paragraph", |
| 62 | + "end of article", |
| 63 | + "end of region", |
| 64 | + "region", |
| 65 | + "heading, Second Section Heading, level 1", |
| 66 | + "paragraph", |
| 67 | + "Second Section Text", |
| 68 | + "end of paragraph", |
| 69 | + "end of region", |
| 70 | + "contentinfo", |
| 71 | + "Footer", |
| 72 | + "end of contentinfo", |
| 73 | + "end of document", |
| 74 | +] |
| 75 | +`); |
| 76 | + }); |
| 77 | + |
| 78 | + test("toMatchScreenReaderSnapshot on an element", async () => { |
| 79 | + await expect( |
| 80 | + document.getElementsByTagName("section")[1] |
| 81 | + ).toMatchScreenReaderSnapshot(); |
| 82 | + }); |
| 83 | + |
| 84 | + test("toMatchScreenReaderInlineSnapshot on an element", async () => { |
| 85 | + await expect(document.getElementsByTagName("section")[1]). |
| 86 | +toMatchScreenReaderInlineSnapshot(` |
| 87 | +[ |
| 88 | + "region", |
| 89 | + "heading, Second Section Heading, level 1", |
| 90 | + "paragraph", |
| 91 | + "Second Section Text", |
| 92 | + "end of paragraph", |
| 93 | + "end of region", |
| 94 | +] |
| 95 | +`); |
| 96 | + }); |
| 97 | + |
| 98 | + test("toMatchScreenReaderSnapshot on a text node", async () => { |
| 99 | + await expect( |
| 100 | + document.getElementsByTagName("nav")[0].firstChild |
| 101 | + ).toMatchScreenReaderSnapshot(); |
| 102 | + }); |
| 103 | + |
| 104 | + test("toMatchScreenReaderInlineSnapshot on a text node", async () => { |
| 105 | + await expect(document.getElementsByTagName("nav")[0].firstChild) |
| 106 | + .toMatchScreenReaderInlineSnapshot(` |
| 107 | +[ |
| 108 | + "Nav Text", |
| 109 | +] |
| 110 | +`); |
| 111 | + }); |
| 112 | + |
| 113 | + test("toMatchScreenReaderSnapshot on a hidden node", async () => { |
| 114 | + await expect( |
| 115 | + document.querySelector('[aria-hidden="true"]') |
| 116 | + ).toMatchScreenReaderSnapshot(); |
| 117 | + }); |
| 118 | + |
| 119 | + test("toMatchScreenReaderInlineSnapshot on a hidden node", async () => { |
| 120 | + await expect( |
| 121 | + document.querySelector('[aria-hidden="true"]') |
| 122 | + ).toMatchScreenReaderInlineSnapshot(`[]`); |
| 123 | + }); |
| 124 | + |
| 125 | + test("toMatchScreenReaderSnapshot on a null node", async () => { |
| 126 | + await expect(null).toMatchScreenReaderSnapshot(); |
| 127 | + }); |
| 128 | + |
| 129 | + test("toMatchScreenReaderInlineSnapshot on a null node", async () => { |
| 130 | + await expect(null).toMatchScreenReaderInlineSnapshot(`[]`); |
| 131 | + }); |
| 132 | + |
| 133 | + test("snapshot matchers handles parallel assertions", async () => { |
| 134 | + await Promise.all([ |
| 135 | + expect( |
| 136 | + document.getElementsByTagName("section")[1] |
| 137 | + ).toMatchScreenReaderSnapshot(), |
| 138 | + expect(document.getElementsByTagName("section")[1]). |
| 139 | +toMatchScreenReaderInlineSnapshot(` |
| 140 | +[ |
| 141 | + "region", |
| 142 | + "heading, Second Section Heading, level 1", |
| 143 | + "paragraph", |
| 144 | + "Second Section Text", |
| 145 | + "end of paragraph", |
| 146 | + "end of region", |
| 147 | +] |
| 148 | +`), |
| 149 | + ]); |
| 150 | + }); |
| 151 | +}); |
0 commit comments