Skip to content

Commit 7d65141

Browse files
authored
Refactor test cases (#65)
2 parents 4f045b5 + 295d195 commit 7d65141

15 files changed

+412
-4517
lines changed

src/entrypoints/background.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { CommentEvent, CommentSpot } from '@/lib/enhancer'
2-
import { type DraftStats, statsFor } from '@/lib/enhancers/draftStats'
2+
import { type DraftStats, statsFor } from '@/lib/enhancers/draft-stats'
33
import type { GetTableRowsResponse, ToBackgroundMessage } from '@/lib/messages'
44
import {
55
CLOSE_MESSAGE_PORT,
File renamed without changes.

tests/corpus-fixture.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ vi.mock('overtype', () => {
3030
})
3131

3232
import { describe as baseDescribe, test as baseTest, expect } from 'vitest'
33+
import type { StrippedLocation } from '@/lib/enhancer'
34+
import { EnhancerRegistry } from '../src/lib/registries'
3335
import type { CORPUS } from './corpus/_corpus-index'
3436
import { cleanupDOM, setupDOM } from './corpus-utils'
3537

@@ -39,7 +41,7 @@ export const describe = baseDescribe
3941
export { expect }
4042

4143
// Fluent interface for any corpus type (HAR or HTML)
42-
export function forCorpus(corpusKey: keyof typeof CORPUS) {
44+
export function withCorpus(corpusKey: keyof typeof CORPUS) {
4345
return {
4446
it: (name: string, fn: () => void | Promise<void>) => {
4547
return baseTest(`${String(corpusKey)}:${name}`, async () => {
@@ -56,3 +58,46 @@ export function forCorpus(corpusKey: keyof typeof CORPUS) {
5658
},
5759
}
5860
}
61+
62+
// Helper function for detection tests
63+
export function detectedSpots() {
64+
const enhancers = new EnhancerRegistry()
65+
const textareas = document.querySelectorAll('textarea')
66+
const location: StrippedLocation = {
67+
host: window.location.host,
68+
pathname: window.location.pathname,
69+
}
70+
const detectionResults = []
71+
for (const textarea of textareas) {
72+
const enhanced = enhancers.tryToEnhance(textarea, location)
73+
const forValue = `id=${textarea.id} name=${textarea.name} className=${textarea.className}`
74+
detectionResults.push({
75+
for: forValue,
76+
spot: enhanced ? enhanced.spot : 'NO_SPOT',
77+
})
78+
}
79+
return detectionResults
80+
}
81+
82+
// Helper function for UI tests
83+
export function tableUI() {
84+
const enhancers = new EnhancerRegistry()
85+
const textareas = document.querySelectorAll('textarea')
86+
const location: StrippedLocation = {
87+
host: window.location.host,
88+
pathname: window.location.pathname,
89+
}
90+
const uiResults = []
91+
for (const textarea of textareas) {
92+
const enhanced = enhancers.tryToEnhance(textarea, location)
93+
const forValue = `id=${textarea.id} name=${textarea.name} className=${textarea.className}`
94+
if (enhanced) {
95+
uiResults.push({
96+
for: forValue,
97+
title: enhanced.enhancer.tableTitle(enhanced.spot),
98+
upperDecoration: enhanced.enhancer.tableUpperDecoration(enhanced.spot),
99+
})
100+
}
101+
}
102+
return uiResults
103+
}

tests/corpus/_corpus-index.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,22 @@ export interface CorpusEntry {
77
}
88

99
export const CORPUS: Record<string, CorpusEntry> = {
10-
// HAR corpus (initial page loads)
1110
gh_issue: {
12-
type: 'har',
11+
description: 'comment text box has some text',
12+
type: 'html',
1313
url: 'https://github.com/diffplug/selfie/issues/523',
1414
},
15+
// HAR corpus (initial page loads)
1516
gh_issue_edit: {
1617
description: 'editing an existing comment on an issue',
1718
type: 'html',
1819
url: 'https://github.com/diffplug/gitcasso/issues/56',
1920
},
20-
gh_issue_new_populated: {
21+
gh_issue_new: {
2122
description: 'a new issue wiht some fields filled out',
2223
type: 'html',
2324
url: 'https://github.com/diffplug/gitcasso/issues/new',
2425
},
25-
gh_issue_populated_comment: {
26-
description: 'comment text box has some text',
27-
type: 'html',
28-
url: 'https://github.com/diffplug/selfie/issues/523',
29-
},
30-
gh_new_issue: {
31-
type: 'har',
32-
url: 'https://github.com/diffplug/selfie/issues/new',
33-
},
34-
gh_new_pr: {
35-
type: 'har',
36-
url: 'https://github.com/diffplug/selfie/compare/main...cavia-porcellus:selfie:main?expand=1',
37-
},
3826
gh_pr: {
3927
type: 'har',
4028
url: 'https://github.com/diffplug/selfie/pull/517',
@@ -44,6 +32,10 @@ export const CORPUS: Record<string, CorpusEntry> = {
4432
type: 'html',
4533
url: 'https://github.com/diffplug/gitcasso/pull/58',
4634
},
35+
gh_pr_new: {
36+
type: 'har',
37+
url: 'https://github.com/diffplug/selfie/compare/main...cavia-porcellus:selfie:main?expand=1',
38+
},
4739
gh_project: {
4840
description: 'github project board initial load',
4941
type: 'html',

tests/corpus/gh_issue.har

Lines changed: 0 additions & 2885 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.

tests/corpus/gh_new_issue.har

Lines changed: 0 additions & 1327 deletions
This file was deleted.
File renamed without changes.
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`github detection > gh_issue:should detect correct spots 1`] = `
4+
[
5+
{
6+
"for": "id=:rn: name=null className=prc-Textarea-TextArea-13q4j overtype-input",
7+
"spot": {
8+
"domain": "github.com",
9+
"number": 523,
10+
"slug": "diffplug/selfie",
11+
"title": "[jvm] docs for VCR",
12+
"type": "GH_ISSUE_ADD_COMMENT",
13+
"unique_key": "github.com:diffplug/selfie:523",
14+
},
15+
},
16+
]
17+
`;
18+
19+
exports[`github detection > gh_issue_edit:should detect correct spots 1`] = `
20+
[
21+
{
22+
"for": "id=:rc3: name=null className=prc-Textarea-TextArea-13q4j focus-visible overtype-input",
23+
"spot": {
24+
"type": "GH_EDIT_COMMENT",
25+
"unique_key": "github.com:diffplug/gitcasso:56:edit-body",
26+
},
27+
},
28+
{
29+
"for": "id=:ra7: name=null className=prc-Textarea-TextArea-13q4j overtype-input",
30+
"spot": {
31+
"domain": "github.com",
32+
"number": 56,
33+
"slug": "diffplug/gitcasso",
34+
"title": "what about the draft?",
35+
"type": "GH_ISSUE_ADD_COMMENT",
36+
"unique_key": "github.com:diffplug/gitcasso:56",
37+
},
38+
},
39+
]
40+
`;
41+
42+
exports[`github detection > gh_issue_new:should detect correct spots 1`] = `
43+
[
44+
{
45+
"for": "id=:r34: name=null className=prc-Textarea-TextArea-13q4j focus-visible overtype-input",
46+
"spot": {
47+
"domain": "github.com",
48+
"slug": "diffplug/gitcasso",
49+
"title": "New issue title",
50+
"type": "GH_ISSUE_NEW_COMMENT",
51+
"unique_key": "github.com:diffplug/gitcasso:new",
52+
},
53+
},
54+
]
55+
`;
56+
57+
exports[`github detection > gh_pr:should detect correct spots 1`] = `
58+
[
59+
{
60+
"for": "id=feedback name=feedback className=form-control width-full mb-2",
61+
"spot": "NO_SPOT",
62+
},
63+
{
64+
"for": "id=new_comment_field name=comment[body] className=js-comment-field js-paste-markdown js-task-list-field js-quick-submit FormControl-textarea CommentBox-input js-size-to-fit size-to-fit js-session-resumable js-saved-reply-shortcut-comment-field overtype-input",
65+
"spot": {
66+
"domain": "github.com",
67+
"number": 517,
68+
"slug": "diffplug/selfie",
69+
"title": "Add "VCR" functionality
70+
#517",
71+
"type": "GH_PR_ADD_COMMENT",
72+
"unique_key": "github.com:diffplug/selfie:517",
73+
},
74+
},
75+
]
76+
`;
77+
78+
exports[`github detection > gh_pr_edit:should detect correct spots 1`] = `
79+
[
80+
{
81+
"for": "id=issue-3429313834-body name=pull_request[body] className=js-comment-field js-paste-markdown js-task-list-field js-quick-submit js-size-to-fit size-to-fit js-session-resumable CommentBox-input FormControl-textarea js-saved-reply-shortcut-comment-field focus-visible overtype-input",
82+
"spot": {
83+
"type": "GH_EDIT_COMMENT",
84+
"unique_key": "github.com:diffplug/gitcasso:58:edit-body",
85+
},
86+
},
87+
{
88+
"for": "id=new_comment_field name=comment[body] className=js-comment-field js-paste-markdown js-task-list-field js-quick-submit FormControl-textarea CommentBox-input js-size-to-fit size-to-fit js-session-resumable js-saved-reply-shortcut-comment-field overtype-input",
89+
"spot": {
90+
"domain": "github.com",
91+
"number": 58,
92+
"slug": "diffplug/gitcasso",
93+
"title": "Feat/expand corpus
94+
95+
#58",
96+
"type": "GH_PR_ADD_COMMENT",
97+
"unique_key": "github.com:diffplug/gitcasso:58",
98+
},
99+
},
100+
]
101+
`;
102+
103+
exports[`github detection > gh_pr_new:should detect correct spots 1`] = `
104+
[
105+
{
106+
"for": "id=feedback name=feedback className=form-control width-full mb-2",
107+
"spot": "NO_SPOT",
108+
},
109+
{
110+
"for": "id=pull_request_body name=pull_request[body] className=js-comment-field js-paste-markdown js-task-list-field js-quick-submit FormControl-textarea CommentBox-input js-size-to-fit size-to-fit js-session-resumable js-saved-reply-shortcut-comment-field CommentBox-input--large overtype-input",
111+
"spot": {
112+
"base": "main",
113+
"domain": "github.com",
114+
"head": "cavia-porcellus:selfie:main",
115+
"slug": "diffplug/selfie",
116+
"title": "Update README.md",
117+
"type": "GH_PR_NEW_COMMENT",
118+
"unique_key": "github.com:diffplug/selfie:main...cavia-porcellus:selfie:main",
119+
},
120+
},
121+
]
122+
`;
123+
124+
exports[`github detection > gh_project:should detect correct spots 1`] = `[]`;
125+
126+
exports[`github detection > gh_project_draft:should detect correct spots 1`] = `[]`;
127+
128+
exports[`github detection > gh_project_draft_edit:should detect correct spots 1`] = `
129+
[
130+
{
131+
"for": "id=:r5a: name=null className=prc-Textarea-TextArea-13q4j",
132+
"spot": "NO_SPOT",
133+
},
134+
]
135+
`;
136+
137+
exports[`github detection > gh_project_issue:should detect correct spots 1`] = `
138+
[
139+
{
140+
"for": "id=:rbs: name=null className=prc-Textarea-TextArea-13q4j",
141+
"spot": "NO_SPOT",
142+
},
143+
]
144+
`;
145+
146+
exports[`github detection > gh_project_issue_edit:should detect correct spots 1`] = `
147+
[
148+
{
149+
"for": "id=:rdh: name=null className=prc-Textarea-TextArea-13q4j",
150+
"spot": "NO_SPOT",
151+
},
152+
{
153+
"for": "id=:rbs: name=null className=prc-Textarea-TextArea-13q4j",
154+
"spot": "NO_SPOT",
155+
},
156+
]
157+
`;

0 commit comments

Comments
 (0)