Skip to content

Commit fd5fd39

Browse files
test: remove duplicate and low-value tests
- useScrollPosition: remove 3 redundant threshold tests, keep numeric threshold and default options tests for coverage - getDiffLineClasses: remove 2 repetitive CSS assertion tests - useDiff: remove 5 tests (redundant null checks, detects added/removed, unicode); consolidate lines array tests Result: 135 → 127 tests, maintains 100% coverage Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
1 parent c5c3e2b commit fd5fd39

3 files changed

Lines changed: 20 additions & 101 deletions

File tree

src/hooks/useDiff.test.ts

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,6 @@ describe('useDiff', () => {
88
expect(result.current).toBeNull();
99
});
1010

11-
it('returns null when original text is empty', () => {
12-
const { result } = renderHook(() => useDiff('', 'some text'));
13-
expect(result.current).toBeNull();
14-
});
15-
16-
it('returns null when modified text is empty', () => {
17-
const { result } = renderHook(() => useDiff('some text', ''));
18-
expect(result.current).toBeNull();
19-
});
20-
2111
it('returns hasChanges false when texts are identical', () => {
2212
const { result } = renderHook(() => useDiff('hello world', 'hello world'));
2313
expect(result.current).not.toBeNull();
@@ -44,32 +34,6 @@ describe('useDiff', () => {
4434
expect(hasUnchanged).toBe(true);
4535
});
4636

47-
it('detects added text correctly', () => {
48-
const { result } = renderHook(() => useDiff('hello', 'hello world'));
49-
expect(result.current?.hasChanges).toBe(true);
50-
51-
const addedSegments = result.current?.segments.filter(
52-
(s) => s.type === 'added',
53-
);
54-
expect(addedSegments?.length).toBeGreaterThan(0);
55-
});
56-
57-
it('detects removed text correctly', () => {
58-
const { result } = renderHook(() => useDiff('hello world', 'hello'));
59-
expect(result.current?.hasChanges).toBe(true);
60-
61-
const removedSegments = result.current?.segments.filter(
62-
(s) => s.type === 'removed',
63-
);
64-
expect(removedSegments?.length).toBeGreaterThan(0);
65-
});
66-
67-
it('handles special characters and unicode', () => {
68-
const { result } = renderHook(() => useDiff('café ☕', 'café 🍵'));
69-
expect(result.current).not.toBeNull();
70-
expect(result.current?.hasChanges).toBe(true);
71-
});
72-
7337
it('memoizes result for same inputs', () => {
7438
const { result, rerender } = renderHook(
7539
({ original, modified }) => useDiff(original, modified),
@@ -117,17 +81,15 @@ describe('useDiff', () => {
11781
);
11882
});
11983

120-
it('includes lines array in result', () => {
84+
it('returns correct lines array with line numbers', () => {
12185
const { result } = renderHook(() => useDiff('hello world', 'hello world'));
12286
expect(result.current?.lines).toBeDefined();
12387
expect(result.current?.lines.length).toBeGreaterThan(0);
124-
});
12588

126-
it('returns correct line numbers for line-level diff', () => {
127-
const { result } = renderHook(() =>
89+
const { result: diffResult } = renderHook(() =>
12890
useDiff('line1\nline2\n', 'line1\nchanged\n', 'lines'),
12991
);
130-
const lines = result.current?.lines ?? [];
92+
const lines = diffResult.current?.lines ?? [];
13193

13294
const unchanged = lines.filter((l) => l.type === 'unchanged');
13395
expect(unchanged[0]).toMatchObject({

src/hooks/useScrollPosition.test.ts

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('useScrollPosition', () => {
3333
}
3434
});
3535

36-
it('returns false when at top of page with 50vh threshold', () => {
36+
it('returns false when at top of page', () => {
3737
Object.defineProperty(window, 'scrollY', {
3838
value: 0,
3939
writable: true,
@@ -53,7 +53,7 @@ describe('useScrollPosition', () => {
5353
expect(result.current.scrollY).toBe(0);
5454
});
5555

56-
it('returns true when scrolled past 50vh threshold', () => {
56+
it('returns true when scrolled past threshold', () => {
5757
Object.defineProperty(window, 'scrollY', {
5858
value: 500,
5959
writable: true,
@@ -74,41 +74,6 @@ describe('useScrollPosition', () => {
7474
expect(result.current.scrollY).toBe(500);
7575
});
7676

77-
it('returns false when scrolled but not past 50vh threshold', () => {
78-
Object.defineProperty(window, 'scrollY', {
79-
value: 300,
80-
writable: true,
81-
configurable: true,
82-
});
83-
Object.defineProperty(window, 'innerHeight', {
84-
value: 800,
85-
writable: true,
86-
configurable: true,
87-
});
88-
89-
const { result } = renderHook(() =>
90-
useScrollPosition({ threshold: '50vh' }),
91-
);
92-
93-
// 50vh = 400px, scrollY = 300, so should NOT be past threshold
94-
expect(result.current.isScrolledPastThreshold).toBe(false);
95-
expect(result.current.scrollY).toBe(300);
96-
});
97-
98-
it('uses numeric threshold when provided', () => {
99-
Object.defineProperty(window, 'scrollY', {
100-
value: 250,
101-
writable: true,
102-
configurable: true,
103-
});
104-
105-
const { result } = renderHook(() => useScrollPosition({ threshold: 200 }));
106-
107-
// scrollY = 250, threshold = 200, so should be past threshold
108-
expect(result.current.isScrolledPastThreshold).toBe(true);
109-
expect(result.current.scrollY).toBe(250);
110-
});
111-
11277
it('updates when scroll position changes', () => {
11378
Object.defineProperty(window, 'innerHeight', {
11479
value: 800,
@@ -156,7 +121,21 @@ describe('useScrollPosition', () => {
156121
removeEventListenerSpy.mockRestore();
157122
});
158123

159-
it('defaults to 50vh threshold when no options provided', () => {
124+
it('uses numeric threshold when provided', () => {
125+
Object.defineProperty(window, 'scrollY', {
126+
value: 250,
127+
writable: true,
128+
configurable: true,
129+
});
130+
131+
const { result } = renderHook(() => useScrollPosition({ threshold: 200 }));
132+
133+
// scrollY = 250, threshold = 200, so should be past threshold
134+
expect(result.current.isScrolledPastThreshold).toBe(true);
135+
expect(result.current.scrollY).toBe(250);
136+
});
137+
138+
it('defaults to 50vh when no options provided', () => {
160139
Object.defineProperty(window, 'scrollY', {
161140
value: 0,
162141
writable: true,

src/utils/getDiffLineClasses.test.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,6 @@ describe('getDiffLineClasses', () => {
2323
);
2424
});
2525

26-
it('returns green classes for added lines', () => {
27-
const result = getDiffLineClasses('added');
28-
29-
expect(result.lineNumberClasses).toBe(
30-
'w-8 px-2 text-right font-mono text-sm leading-6 text-gray-500 dark:text-gray-400 bg-green-50 dark:bg-green-900/20',
31-
);
32-
expect(result.contentClasses).toBe(
33-
'pl-2 font-mono text-sm leading-6 dark:text-gray-100 bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-300',
34-
);
35-
});
36-
37-
it('returns red classes for removed lines', () => {
38-
const result = getDiffLineClasses('removed');
39-
40-
expect(result.lineNumberClasses).toBe(
41-
'w-8 px-2 text-right font-mono text-sm leading-6 text-gray-500 dark:text-gray-400 bg-red-50 dark:bg-red-900/20',
42-
);
43-
expect(result.contentClasses).toBe(
44-
'pl-2 font-mono text-sm leading-6 dark:text-gray-100 bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-300',
45-
);
46-
});
47-
4826
it('uses custom content base classes when provided', () => {
4927
const customBase =
5028
'min-w-0 flex-1 pl-2 font-mono text-sm leading-6 text-gray-900 dark:text-gray-100 whitespace-pre-wrap break-words';

0 commit comments

Comments
 (0)