@prosopo/procaptcha-common 2.12.6 → 2.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build$colon$cjs.log +20 -19
- package/.turbo/turbo-build$colon$tsc.log +18 -18
- package/.turbo/turbo-build.log +21 -20
- package/CHANGELOG.md +35 -0
- package/dist/cjs/index.cjs +3 -0
- package/dist/cjs/reactComponents/ChallengeSurface.cjs +164 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/reactComponents/ChallengeSurface.d.ts +21 -0
- package/dist/reactComponents/ChallengeSurface.d.ts.map +1 -0
- package/dist/reactComponents/ChallengeSurface.js +161 -0
- package/dist/reactComponents/ChallengeSurface.js.map +1 -0
- package/dist/tests/challengeSurface.unit.test.d.ts +2 -0
- package/dist/tests/challengeSurface.unit.test.d.ts.map +1 -0
- package/dist/tests/challengeSurface.unit.test.js +54 -0
- package/dist/tests/challengeSurface.unit.test.js.map +1 -0
- package/dist/tests/challengeSurfaceRender.unit.test.d.ts +2 -0
- package/dist/tests/challengeSurfaceRender.unit.test.d.ts.map +1 -0
- package/dist/tests/challengeSurfaceRender.unit.test.js +123 -0
- package/dist/tests/challengeSurfaceRender.unit.test.js.map +1 -0
- package/package.json +5 -5
- package/src/index.ts +1 -0
- package/src/reactComponents/ChallengeSurface.tsx +285 -0
- package/src/tests/challengeSurface.unit.test.ts +104 -0
- package/src/tests/challengeSurfaceRender.unit.test.tsx +207 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
// jsdom has no PointerEvent; the dismiss handler only reads event.target, so
|
|
16
|
+
// a plain Event of the same type is dispatched instead.
|
|
17
|
+
import { PlacementEnum, type PlacementType } from "@prosopo/types";
|
|
18
|
+
import { type Root, createRoot } from "react-dom/client";
|
|
19
|
+
import { act } from "react-dom/test-utils";
|
|
20
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
21
|
+
import { ChallengeSurface } from "../reactComponents/ChallengeSurface.js";
|
|
22
|
+
|
|
23
|
+
let container: HTMLDivElement;
|
|
24
|
+
let root: Root;
|
|
25
|
+
let anchor: HTMLDivElement;
|
|
26
|
+
|
|
27
|
+
const layer = (): HTMLElement | null =>
|
|
28
|
+
document.querySelector<HTMLElement>(".prosopo-challenge-surface");
|
|
29
|
+
|
|
30
|
+
const content = (): HTMLElement | null =>
|
|
31
|
+
document.querySelector<HTMLElement>(".prosopo-challenge-content");
|
|
32
|
+
|
|
33
|
+
interface RenderArgs {
|
|
34
|
+
placement?: PlacementType;
|
|
35
|
+
withAnchor?: boolean;
|
|
36
|
+
onDismiss?: () => void;
|
|
37
|
+
show?: boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const render = ({
|
|
41
|
+
placement,
|
|
42
|
+
withAnchor = true,
|
|
43
|
+
onDismiss,
|
|
44
|
+
show = true,
|
|
45
|
+
}: RenderArgs): void => {
|
|
46
|
+
act(() => {
|
|
47
|
+
root.render(
|
|
48
|
+
<ChallengeSurface
|
|
49
|
+
show={show}
|
|
50
|
+
placement={placement}
|
|
51
|
+
anchor={withAnchor ? anchor : null}
|
|
52
|
+
onDismiss={onDismiss}
|
|
53
|
+
>
|
|
54
|
+
<div data-testid="challenge">challenge</div>
|
|
55
|
+
</ChallengeSurface>,
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
beforeEach(() => {
|
|
61
|
+
container = document.createElement("div");
|
|
62
|
+
anchor = document.createElement("div");
|
|
63
|
+
document.body.append(container, anchor);
|
|
64
|
+
root = createRoot(container);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
afterEach(() => {
|
|
68
|
+
act(() => root.unmount());
|
|
69
|
+
container.remove();
|
|
70
|
+
anchor.remove();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe("where the surface renders", () => {
|
|
74
|
+
it("portals out of the mount container to the body", () => {
|
|
75
|
+
render({});
|
|
76
|
+
|
|
77
|
+
expect(container.querySelector(".prosopo-challenge-surface")).toBeNull();
|
|
78
|
+
expect(layer()?.parentElement).toBe(document.body);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("hides rather than unmounts when not shown", () => {
|
|
82
|
+
render({ show: false });
|
|
83
|
+
|
|
84
|
+
expect(layer()?.style.display).toBe("none");
|
|
85
|
+
expect(layer()?.textContent).toContain("challenge");
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe("popup", () => {
|
|
90
|
+
it("is the default placement", () => {
|
|
91
|
+
render({});
|
|
92
|
+
|
|
93
|
+
expect(layer()?.className).toContain("prosopo-challenge-surface--popup");
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("covers the page, so nothing behind it is reachable", () => {
|
|
97
|
+
render({ placement: PlacementEnum.popup });
|
|
98
|
+
|
|
99
|
+
expect(layer()?.style.display).toBe("flex");
|
|
100
|
+
expect(layer()?.style.pointerEvents).toBe("");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("ignores an outside click", () => {
|
|
104
|
+
const onDismiss = vi.fn();
|
|
105
|
+
render({ placement: PlacementEnum.popup, onDismiss });
|
|
106
|
+
|
|
107
|
+
act(() => {
|
|
108
|
+
document.body.dispatchEvent(new Event("pointerdown", { bubbles: true }));
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
expect(onDismiss).not.toHaveBeenCalled();
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe("float", () => {
|
|
116
|
+
it("leaves the page usable behind it", () => {
|
|
117
|
+
render({ placement: PlacementEnum.float });
|
|
118
|
+
|
|
119
|
+
expect(layer()?.className).toContain("prosopo-challenge-surface--float");
|
|
120
|
+
expect(layer()?.style.pointerEvents).toBe("none");
|
|
121
|
+
expect(content()?.style.pointerEvents).toBe("auto");
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("positions the panel absolutely so the page scrolls it", () => {
|
|
125
|
+
render({ placement: PlacementEnum.float });
|
|
126
|
+
|
|
127
|
+
// Viewport-relative positioning is what made the panel drift while
|
|
128
|
+
// scrolling; document-relative is what keeps it still.
|
|
129
|
+
expect(content()?.style.position).toBe("absolute");
|
|
130
|
+
expect(layer()?.style.position).toBe("absolute");
|
|
131
|
+
expect(layer()?.style.width).toBe("0px");
|
|
132
|
+
expect(layer()?.style.height).toBe("0px");
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("dismisses on a click outside the panel", () => {
|
|
136
|
+
const onDismiss = vi.fn();
|
|
137
|
+
render({ placement: PlacementEnum.float, onDismiss });
|
|
138
|
+
|
|
139
|
+
act(() => {
|
|
140
|
+
document.body.dispatchEvent(new Event("pointerdown", { bubbles: true }));
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
expect(onDismiss).toHaveBeenCalledTimes(1);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("does not dismiss on a click inside the panel", () => {
|
|
147
|
+
const onDismiss = vi.fn();
|
|
148
|
+
render({ placement: PlacementEnum.float, onDismiss });
|
|
149
|
+
|
|
150
|
+
act(() => {
|
|
151
|
+
content()?.dispatchEvent(new Event("pointerdown", { bubbles: true }));
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
expect(onDismiss).not.toHaveBeenCalled();
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("does not dismiss on a click on the anchor", () => {
|
|
158
|
+
const onDismiss = vi.fn();
|
|
159
|
+
render({ placement: PlacementEnum.float, onDismiss });
|
|
160
|
+
|
|
161
|
+
act(() => {
|
|
162
|
+
anchor.dispatchEvent(new Event("pointerdown", { bubbles: true }));
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
expect(onDismiss).not.toHaveBeenCalled();
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("falls back to popup with no anchor to attach to", () => {
|
|
169
|
+
render({ placement: PlacementEnum.float, withAnchor: false });
|
|
170
|
+
|
|
171
|
+
expect(layer()?.className).toContain("prosopo-challenge-surface--popup");
|
|
172
|
+
expect(layer()?.style.pointerEvents).toBe("");
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
describe("dismissing with the keyboard", () => {
|
|
177
|
+
it("closes on Escape in either placement", () => {
|
|
178
|
+
for (const placement of [PlacementEnum.popup, PlacementEnum.float]) {
|
|
179
|
+
const onDismiss = vi.fn();
|
|
180
|
+
render({ placement, onDismiss });
|
|
181
|
+
|
|
182
|
+
act(() => {
|
|
183
|
+
document.dispatchEvent(
|
|
184
|
+
new KeyboardEvent("keydown", { key: "Escape", bubbles: true }),
|
|
185
|
+
);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
expect(
|
|
189
|
+
onDismiss,
|
|
190
|
+
`${placement} should close on Escape`,
|
|
191
|
+
).toHaveBeenCalled();
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("ignores other keys", () => {
|
|
196
|
+
const onDismiss = vi.fn();
|
|
197
|
+
render({ placement: PlacementEnum.float, onDismiss });
|
|
198
|
+
|
|
199
|
+
act(() => {
|
|
200
|
+
document.dispatchEvent(
|
|
201
|
+
new KeyboardEvent("keydown", { key: "Enter", bubbles: true }),
|
|
202
|
+
);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
expect(onDismiss).not.toHaveBeenCalled();
|
|
206
|
+
});
|
|
207
|
+
});
|