-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpopup.test.js
More file actions
98 lines (82 loc) · 2.62 KB
/
Copy pathpopup.test.js
File metadata and controls
98 lines (82 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { describe, it, expect, vi, beforeEach } from 'vitest';
// Mock Chrome APIs
const mockChrome = {
tabs: {
query: vi.fn(),
},
scripting: {
executeScript: vi.fn(),
},
tabCapture: {
capture: vi.fn(),
},
downloads: {
download: vi.fn(),
},
runtime: {
lastError: null,
},
};
global.chrome = mockChrome;
// Extract testable logic into separate functions
import { calculateScrollParams, createSmoothScrollFunction } from './scroll-utils.js';
describe('Scrollywood', () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe('calculateScrollParams', () => {
it('should calculate correct scroll parameters for a given duration', () => {
const params = calculateScrollParams({
scrollHeight: 5000,
windowHeight: 1000,
duration: 60,
});
expect(params.totalScrollDistance).toBe(4000); // 5000 - 1000
expect(params.scrollsPerSecond).toBe(60); // 60fps
expect(params.pixelsPerFrame).toBeCloseTo(4000 / (60 * 60), 2);
});
it('should handle short pages', () => {
const params = calculateScrollParams({
scrollHeight: 500,
windowHeight: 1000,
duration: 30,
});
expect(params.totalScrollDistance).toBe(0); // Can't scroll, page fits
});
});
describe('createSmoothScrollFunction', () => {
it('should return a function that calculates scroll position based on progress', () => {
const scrollFn = createSmoothScrollFunction(4000); // 4000px total
expect(scrollFn(0)).toBe(0);
expect(scrollFn(0.5)).toBe(2000);
expect(scrollFn(1)).toBe(4000);
});
});
describe('Chrome API integration', () => {
it('should query the active tab', async () => {
mockChrome.tabs.query.mockResolvedValue([{ id: 123 }]);
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
expect(tab.id).toBe(123);
expect(mockChrome.tabs.query).toHaveBeenCalledWith({
active: true,
currentWindow: true,
});
});
it('should capture tab with correct options', async () => {
const mockStream = { getTracks: () => [] };
mockChrome.tabCapture.capture.mockImplementation((options, callback) => {
callback(mockStream);
});
const capturedStream = await new Promise((resolve) => {
chrome.tabCapture.capture({ audio: false, video: true }, (stream) => {
resolve(stream);
});
});
expect(capturedStream).toBe(mockStream);
expect(mockChrome.tabCapture.capture).toHaveBeenCalledWith(
{ audio: false, video: true },
expect.any(Function)
);
});
});
});