generated from Jagoda11/react-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupTests.ts
74 lines (67 loc) Β· 1.86 KB
/
setupTests.ts
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
import '@testing-library/jest-dom'
import 'whatwg-fetch'
import { TextEncoder, TextDecoder } from 'util'
global.TextEncoder = TextEncoder as unknown as typeof global.TextEncoder
global.TextDecoder = TextDecoder as unknown as typeof global.TextDecoder
beforeEach(() => {
global.fetch = jest.fn((url) => {
// Mock: List Repositories
if (url.includes('/users/test-user/repos')) {
return Promise.resolve({
json: () =>
Promise.resolve([
{
id: 1,
name: 'repo-1',
language: 'JavaScript',
stargazers_count: 10,
},
{
id: 2,
name: 'repo-2',
language: 'TypeScript',
stargazers_count: 20,
},
]),
})
}
// Mock: Commit Activity
if (url.includes('/repos/test-user/repo-1/stats/commit_activity')) {
return Promise.resolve({
json: () =>
Promise.resolve([
{ week: 1630454400, total: 5 },
{ week: 1631059200, total: 10 },
]),
})
}
// Mock: Programming Languages
if (url.includes('/repos/test-user/repo-1/languages')) {
return Promise.resolve({
json: () =>
Promise.resolve({ JavaScript: 12345, HTML: 2345, CSS: 3456 }),
})
}
// Mock: User Information
if (url.includes('/users/test-user')) {
return Promise.resolve({
json: () =>
Promise.resolve({
login: 'test-user',
id: 123,
public_repos: 2,
followers: 50,
following: 10,
avatar_url: 'https://example.com/avatar.png',
}),
})
}
// Default Mock for Unhandled Requests
return Promise.resolve({
json: () => Promise.resolve({}),
})
}) as jest.Mock
})
afterEach(() => {
jest.clearAllMocks()
})