-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshortUrl.test.js
More file actions
102 lines (81 loc) · 2.49 KB
/
Copy pathshortUrl.test.js
File metadata and controls
102 lines (81 loc) · 2.49 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
99
100
101
102
const axios = require('axios');
const { shortUrl } = require('./shortUrl');
jest.mock('axios');
describe('shortUrl', () => {
const originalEnv = process.env;
beforeEach(() => {
jest.resetModules(); // Clears any cache
process.env = { ...originalEnv }; // Preserves original env variables
});
afterAll(() => {
process.env = originalEnv; // Restores original env variables
});
it('should return short URL on success', async () => {
process.env.SHRTFLY_KEY = 'dummyKey';
const longUrl = 'https://example.com';
const alias = ''
const responseData = {
status: 'success',
result: {
original_url: 'https://example.com',
shorten_url: 'https://stfly.biz/7byhs',
stats_url: 'https://shrtfly.com/publisher/stats/16/7byhs',
},
};
axios.get.mockResolvedValue({ data: responseData });
const result = await shortUrl(longUrl);
expect(result).toEqual(responseData);
expect(axios.get).toHaveBeenCalledWith(
`https://shrtfly.com/api?api=dummyKey&url=${longUrl}&format=json&type=1`
);
});
it('should return API key error', async () => {
process.env.SHRTFLY_KEY = 'dummyKey';
const longUrl = 'https://example.com';
const responseData = {
status: 'error',
result: 'API key not valid. Please pass a valid API key.',
};
axios.get.mockResolvedValue({ data: responseData });
try {
await shortUrl(longUrl);
} catch (error) {
console.log("ERRORRR,", error)
expect(error).toEqual({
status: 'error',
result: 'API key not valid. Please pass a valid API key.',
});
}
});
it('should return URL error', async () => {
process.env.SHRTFLY_KEY = 'dummyKey';
const longUrl = 'https://example.com';
const responseData = {
status: 'error',
result: 'Please enter a valid URL.',
};
axios.get.mockResolvedValue({ data: responseData });
try {
await shortUrl(longUrl);
} catch (error) {
expect(error).toEqual({
status: 'error',
result: 'Please enter a valid URL.',
});
}
});
it('should handle axios request error', async () => {
process.env.SHRTFLY_KEY = 'dummyKey';
const longUrl = 'https://example.com';
const errorMessage = 'Network Error';
axios.get.mockRejectedValue(new Error(errorMessage));
try {
await shortUrl(longUrl);
} catch (error) {
expect(error).toEqual({
status: 'error',
message: errorMessage,
});
}
});
});