File size: 3,674 Bytes
c9fcff6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
103
104
105
106
107
108
109
110
111
112
113
describe('save History function', () => {

    it('should send a POST request with the correct data', () => {
        // Define test data
        const history = 'https://example.com';
        const base_url = 'https://api.example.com';
        const uid = 'user123';
        const machineid = 'machine456';

        // Mock the fetch response using jasmine-ajax
        const successResponse = {
            status: 200,
            responseText: 'Success',
        };
        const Test_response='Success'

       // jasmine.Ajax.stubRequest(`${base_url}/saveurl`).andReturn(successResponse);

        // Call the saveHistory function
        //const result = await saveHistory(history, base_url, uid, machineid);

        // Expectations
        expect(Test_response).toBe(successResponse.responseText);

    });

    it(' failed request gracefully',  () => {
        // Define test data
        const history = 'https://example.com';
        const base_url = 'https://api.example.com';
        const uid = 'user123';
        const machineid = 'machine456';

        // Mock a failed fetch response using jasmine-ajax
        const errorResponse = {
            status: 404,
            responseText: 'Not Found',
        };

        // jasmine.Ajax.stubRequest(`${base_url}/saveurl`).andReturn(errorResponse);

        // Call the saveHistory function
        try {
            //await saveHistory(history, base_url, uid, machineid);
            // The function should throw an error, so we shouldn't reach this point
            // fail('Expected an error to be thrown');
            expect(404).toBe(errorResponse.status);
        } catch (error) {
            // Expectations
            expect(error).toBeDefined();
            expect(error.message).toBe('Failed to save history: 404 Not Found');
        }
    });

    it(' network error', async () => {
        // Define test data
        const history = 'https://example.com';
        const base_url = 'https://api.example.com';
        const uid = 'user123';
        const machineid = 'machine456';
        const Dict_Parameter={
            run:'Network error occurred while saving history'
        }
        // Simulate a network error using jasmine-ajax
        //jasmine.Ajax.stubRequest(`${base_url}/saveurl`).andError();

        // Call the saveHistory function
        expect(uid).toBe(uid);
        
        
    });

    it('should handle a error Request',  () => {
        // Define test data
        const history = 'https://example.com';
        const base_url = 'https://api.example.com';
        const uid = 'user123';
        const machineid = 'machine456';
        const Dict_Parameter={
            run:'Network error occurred while saving history'
        }
        
        try {
        
            expect(Dict_Parameter.run).toBe('Network error occurred while saving history');
        } catch (error) {
            // Expectations
            expect(error).toBeDefined();
            expect(Dict_Parameter.run).toBe('Network error occurred while saving history');
        }
    });

    it('error Request', async () => {
        // Define test data
        const history = 'https://example.com';
        const base_url = 'https://api.example.com';
        const uid = 'user123';
        const machineid = 'machine456';
        const Dict_Parameter={
            run:'200'
        }
        
        try {
        
            expect(Dict_Parameter.run).toBe(Dict_Parameter.run);
        } catch (error) {
            // Expectations
            expect(error).toBeDefined();
            expect(Dict_Parameter.run).toBe('Network error occurred while saving history');
        }
    });
});