|
| 1 | +import { type PropsOf, component$ } from "@qwik.dev/core"; |
| 2 | +import { page } from "@vitest/browser/context"; |
| 3 | +import { expect, test } from "vitest"; |
| 4 | +import { render } from "vitest-browser-qwik"; |
| 5 | +import { QRCode } from ".."; |
| 6 | + |
| 7 | +// Top-level locator constants using data-testid |
| 8 | +const Root = page.getByTestId("root"); |
| 9 | +const Frame = page.getByTestId("frame"); |
| 10 | +const Svg = page.getByTestId("svg"); |
| 11 | +const Path = page.getByTestId("path"); |
| 12 | +const Overlay = page.getByTestId("overlay"); |
| 13 | +const OverlayImg = page.getByTestId("overlay-img"); |
| 14 | + |
| 15 | +const Basic = component$((props: PropsOf<typeof QRCode.Root>) => { |
| 16 | + return ( |
| 17 | + <QRCode.Root value="https://qwik.dev" data-testid="root" {...props}> |
| 18 | + <QRCode.Frame data-testid="frame"> |
| 19 | + <QRCode.PatternSvg width={200} height={200} data-testid="svg"> |
| 20 | + <QRCode.PatternPath fill="black" data-testid="path" /> |
| 21 | + </QRCode.PatternSvg> |
| 22 | + </QRCode.Frame> |
| 23 | + </QRCode.Root> |
| 24 | + ); |
| 25 | +}); |
| 26 | + |
| 27 | +const WithOverlay = component$(() => { |
| 28 | + return ( |
| 29 | + <QRCode.Root value="https://qwik.dev" data-testid="root"> |
| 30 | + <QRCode.Frame data-testid="frame"> |
| 31 | + <QRCode.PatternSvg width={200} height={200} data-testid="svg"> |
| 32 | + <QRCode.PatternPath fill="black" data-testid="path" /> |
| 33 | + </QRCode.PatternSvg> |
| 34 | + <QRCode.Overlay data-testid="overlay"> |
| 35 | + <img |
| 36 | + src="https://qwik.dev/favicon.svg" |
| 37 | + alt="Qwik logo" |
| 38 | + width={40} |
| 39 | + height={40} |
| 40 | + data-testid="overlay-img" |
| 41 | + /> |
| 42 | + </QRCode.Overlay> |
| 43 | + </QRCode.Frame> |
| 44 | + </QRCode.Root> |
| 45 | + ); |
| 46 | +}); |
| 47 | + |
| 48 | +const WithCustomColors = component$(() => { |
| 49 | + return ( |
| 50 | + <QRCode.Root value="https://qwik.dev" data-testid="root"> |
| 51 | + <QRCode.Frame data-testid="frame" style={{ backgroundColor: "rgb(255, 255, 0)" }}> |
| 52 | + <QRCode.PatternSvg width={200} height={200} data-testid="svg"> |
| 53 | + <QRCode.PatternPath fill="blue" data-testid="path" /> |
| 54 | + </QRCode.PatternSvg> |
| 55 | + <QRCode.Overlay data-testid="overlay"> |
| 56 | + <img |
| 57 | + src="https://qwik.dev/favicon.svg" |
| 58 | + alt="Qwik logo" |
| 59 | + width={40} |
| 60 | + height={40} |
| 61 | + data-testid="overlay-img" |
| 62 | + /> |
| 63 | + </QRCode.Overlay> |
| 64 | + </QRCode.Frame> |
| 65 | + </QRCode.Root> |
| 66 | + ); |
| 67 | +}); |
| 68 | + |
| 69 | +const Multiple = component$(() => { |
| 70 | + return ( |
| 71 | + <div> |
| 72 | + <QRCode.Root value="https://qwik.dev" data-testid="root"> |
| 73 | + <QRCode.Frame data-testid="frame"> |
| 74 | + <QRCode.PatternSvg width={200} height={200} data-testid="svg"> |
| 75 | + <QRCode.PatternPath fill="black" data-testid="path" /> |
| 76 | + </QRCode.PatternSvg> |
| 77 | + </QRCode.Frame> |
| 78 | + </QRCode.Root> |
| 79 | + <QRCode.Root value="https://builder.io" data-testid="root"> |
| 80 | + <QRCode.Frame data-testid="frame"> |
| 81 | + <QRCode.PatternSvg width={200} height={200} data-testid="svg"> |
| 82 | + <QRCode.PatternPath fill="black" data-testid="path" /> |
| 83 | + </QRCode.PatternSvg> |
| 84 | + </QRCode.Frame> |
| 85 | + </QRCode.Root> |
| 86 | + </div> |
| 87 | + ); |
| 88 | +}); |
| 89 | + |
| 90 | +test("SVG should be visible", async () => { |
| 91 | + render(<Basic />); |
| 92 | + await expect.element(Svg).toBeVisible(); |
| 93 | +}); |
| 94 | + |
| 95 | +test("should have correct size", async () => { |
| 96 | + render(<Basic />); |
| 97 | + |
| 98 | + await expect.element(Svg).toHaveAttribute("width", "200"); |
| 99 | + await expect.element(Svg).toHaveAttribute("height", "200"); |
| 100 | +}); |
| 101 | + |
| 102 | +test("should have path with QR code data", async () => { |
| 103 | + render(<Basic />); |
| 104 | + |
| 105 | + await expect.element(Path).toBeVisible(); |
| 106 | + await expect.element(Path).toHaveAttribute("d"); |
| 107 | + await expect.element(Path).toHaveAttribute("fill", "black"); |
| 108 | +}); |
| 109 | + |
| 110 | +test("overlay image should be present", async () => { |
| 111 | + render(<WithOverlay />); |
| 112 | + |
| 113 | + await expect.element(OverlayImg).toBeVisible(); |
| 114 | + await expect.element(OverlayImg).toHaveAttribute("src"); |
| 115 | + await expect.element(OverlayImg).toHaveAttribute("width"); |
| 116 | + await expect.element(OverlayImg).toHaveAttribute("height"); |
| 117 | +}); |
| 118 | + |
| 119 | +test("should use the specified custom colors", async () => { |
| 120 | + render(<WithCustomColors />); |
| 121 | + |
| 122 | + await expect.element(Frame).toHaveStyle({ backgroundColor: "rgb(255, 255, 0)" }); |
| 123 | + await expect.element(Path).toHaveAttribute("fill", "blue"); |
| 124 | +}); |
| 125 | + |
| 126 | +test("multiple QR codes should be visible and unique", async () => { |
| 127 | + render(<Multiple />); |
| 128 | + |
| 129 | + const allSvgs = page.getByTestId("svg"); |
| 130 | + |
| 131 | + await expect.element(allSvgs.nth(0)).toBeVisible(); |
| 132 | + await expect.element(allSvgs.nth(1)).toBeVisible(); |
| 133 | + |
| 134 | + // Get the path data to verify they're different |
| 135 | + const firstPathElement = await allSvgs.nth(0).element(); |
| 136 | + const secondPathElement = await allSvgs.nth(1).element(); |
| 137 | + |
| 138 | + const firstPath = firstPathElement.querySelector("[data-testid='path']"); |
| 139 | + const secondPath = secondPathElement.querySelector("[data-testid='path']"); |
| 140 | + |
| 141 | + const firstD = firstPath?.getAttribute("d"); |
| 142 | + const secondD = secondPath?.getAttribute("d"); |
| 143 | + |
| 144 | + expect(firstD).toBeTruthy(); |
| 145 | + expect(secondD).toBeTruthy(); |
| 146 | + expect(firstD).not.toEqual(secondD); |
| 147 | +}); |
| 148 | + |
| 149 | +test("should have proper ARIA attributes", async () => { |
| 150 | + render(<Basic />); |
| 151 | + |
| 152 | + await expect.element(Root).toHaveAttribute("role", "img"); |
| 153 | + await expect.element(Root).toHaveAttribute("aria-label"); |
| 154 | + await expect.element(Svg).toHaveAttribute("aria-hidden", "true"); |
| 155 | +}); |
0 commit comments