Skip to content

Commit ab4bc19

Browse files
committed
tse
1 parent 9f82496 commit ab4bc19

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

packages/chrome-plugin/tests/hn.spec.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { test } from './fixtures';
1+
import { expect, test } from './fixtures';
22
import {
33
assertHarperHighlightBoxes,
4+
getHarperHighlightBoxes,
45
getTextarea,
56
replaceEditorContent,
67
testBasicSuggestionTextarea,
78
testCanIgnoreTextareaSuggestion,
9+
waitForAnyHarperHighlight,
810
} from './testUtils';
911

1012
const TEST_PAGE_URL = 'https://news.ycombinator.com/item?id=45453448';
@@ -42,5 +44,24 @@ test('Hacker News scrolls correctly', async ({ page }) => {
4244
'This is a test of the the Harper grammar checker, specifically if \n\n\n\n\n\n\n\n\n\n\n\n\nit scrolls beyo nd the height of the buffer.',
4345
);
4446

45-
await assertHarperHighlightBoxes(page, [{ x: 216.625, y: 217, width: 56, height: 19 }]);
47+
await waitForAnyHarperHighlight(page);
48+
49+
const [highlight] = await getHarperHighlightBoxes(page);
50+
const editorBox = await editor.boundingBox();
51+
const scrollTop = await editor.evaluate((el) => el.scrollTop);
52+
53+
expect(highlight).toBeDefined();
54+
expect(editorBox).not.toBeNull();
55+
expect(scrollTop).toBeGreaterThan(0);
56+
57+
if (highlight && editorBox) {
58+
const editorBottom = editorBox.y + editorBox.height;
59+
const highlightBottom = highlight.y + highlight.height;
60+
const relativeY = highlight.y - editorBox.y;
61+
const maxRelative = editorBox.height - highlight.height;
62+
63+
expect(highlight.x).toBeGreaterThanOrEqual(editorBox.x - 9);
64+
expect(highlightBottom).toBeLessThanOrEqual(editorBottom + 9);
65+
expect(relativeY).toBeGreaterThan(maxRelative * 0.6);
66+
}
4667
});

packages/chrome-plugin/tests/testUtils.ts

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,42 @@ export async function assertHarperHighlightBoxes(page: Page, boxes: Box[]): Prom
129129
const highlightCount = await highlights.count();
130130
expect(highlightCount).toBe(boxes.length);
131131

132+
const actualBoxes: Box[] = [];
133+
132134
for (let i = 0; i < highlightCount; i++) {
133135
const box = await highlights.nth(i).boundingBox();
136+
expect(box).not.toBeNull();
137+
if (box != null) {
138+
actualBoxes.push(box as Box);
139+
}
140+
}
134141

135-
console.log(`Expected: ${JSON.stringify(boxes[i])}`);
136-
console.log(`Got: ${JSON.stringify(box)}`);
142+
const expectedSorted = sortBoxes(boxes);
143+
const actualSorted = sortBoxes(actualBoxes);
137144

138-
assertBoxesClose(box, boxes[i]);
145+
for (let i = 0; i < expectedSorted.length; i++) {
146+
console.log(`Expected: ${JSON.stringify(expectedSorted[i])}`);
147+
console.log(`Got: ${JSON.stringify(actualSorted[i])}`);
148+
assertBoxesClose(actualSorted[i], expectedSorted[i]);
139149
}
140150
}
141151

152+
function sortBoxes(values: Box[]): Box[] {
153+
return [...values].sort((a, b) => {
154+
if (a == null || b == null) return 0;
155+
if (a.y !== b.y) {
156+
return a.y - b.y;
157+
}
158+
if (a.x !== b.x) {
159+
return a.x - b.x;
160+
}
161+
if (a.width !== b.width) {
162+
return a.width - b.width;
163+
}
164+
return a.height - b.height;
165+
});
166+
}
167+
142168
/** An assertion that checks to ensure that two boxes are _approximately_ equal.
143169
* Leaves wiggle room for floating point error. */
144170
export function assertBoxesClose(a: Box, b: Box) {
@@ -148,6 +174,19 @@ export function assertBoxesClose(a: Box, b: Box) {
148174
assertClose(a.height, b.height);
149175
}
150176

177+
export async function getHarperHighlightBoxes(page: Page): Promise<Box[]> {
178+
const highlights = getHarperHighlights(page);
179+
const count = await highlights.count();
180+
const boxes: Box[] = [];
181+
for (let i = 0; i < count; i++) {
182+
const box = await highlights.nth(i).boundingBox();
183+
if (box != null) {
184+
boxes.push(box as Box);
185+
}
186+
}
187+
return boxes;
188+
}
189+
151190
function assertClose(actual: number, expected: number) {
152191
expect(Math.abs(actual - expected)).toBeLessThanOrEqual(9);
153192
}

0 commit comments

Comments
 (0)