From b11e3137146b153a8edeb3848ef373723afb256d Mon Sep 17 00:00:00 2001 From: vantage-ola Date: Mon, 12 Aug 2024 08:18:14 +0100 Subject: [PATCH] fix: type mismatch --- tracknow/web/src/tests/hooks/API.test.ts | 96 ++++++++++++------------ 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/tracknow/web/src/tests/hooks/API.test.ts b/tracknow/web/src/tests/hooks/API.test.ts index 73a1971..cc5e5c9 100644 --- a/tracknow/web/src/tests/hooks/API.test.ts +++ b/tracknow/web/src/tests/hooks/API.test.ts @@ -77,10 +77,10 @@ describe('API', () => { describe('loginUser', () => { it('should login user successfully', async () => { const mockResponse = { token: 'mock-token', user: { id: 1, name: 'User 1' } }; - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.MockedFunction).mockResolvedValueOnce({ ok: true, json: async () => mockResponse, - }); + } as Response); const result = await API.loginUser({ username: 'user1', password: 'password' }); @@ -99,9 +99,9 @@ describe('API', () => { }); it('should throw an error when login fails', async () => { - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: false, - }); + } as Response); await expect(API.loginUser({ username: 'user1', password: 'wrong' })).rejects.toThrow('Login Failed'); }); @@ -111,10 +111,10 @@ describe('API', () => { describe('getIdentity', () => { it('should get identity successfully', async () => { const mockIdentity = { id: 1, name: 'User 1' }; - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: true, json: async () => mockIdentity, - }); + } as Response); (localStorageMock.getItem as jest.MockedFunction).mockReturnValue('mock-token'); const result = await API.getIdentity(); @@ -134,9 +134,9 @@ describe('API', () => { }); it('should throw an error when user is not logged in', async () => { - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: false, - }); + } as Response); (localStorageMock.getItem as jest.MockedFunction).mockReturnValue('mock-token'); await expect(API.getIdentity()).rejects.toThrow('User not Logged in'); @@ -147,10 +147,10 @@ describe('API', () => { describe('CreateUser', () => { it('should create a user successfully', async () => { const mockResponse = { id: 1, name: 'New User' }; - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: true, json: async () => mockResponse, - }); + } as Response); const result = await API.CreateUser({ username: 'newuser', password: 'password' }); @@ -169,9 +169,9 @@ describe('API', () => { }); it('should throw an error when user creation fails', async () => { - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: false, - }); + } as Response); await expect(API.CreateUser({ username: 'newuser', password: 'password' })).rejects.toThrow('Failed to create user'); }); @@ -181,10 +181,10 @@ describe('API', () => { describe('handleLaptimes', () => { it('should create a laptime successfully', async () => { const mockResponse = { id: 1, time: '1:30.000' }; - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: true, json: async () => mockResponse, - }); + } as Response); (localStorageMock.getItem as jest.MockedFunction).mockReturnValue('mock-token'); @@ -207,10 +207,10 @@ describe('API', () => { it('should fetch laptimes successfully', async () => { const mockResponse = [{ id: 1, title: 'new laptime', simracing: false, comment: "my racing moments" }, { id: 2, title: 'new laptime 2', simracing: false, comment: "my racing moments 2" }]; - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: true, json: async () => mockResponse, - }); + } as Response); (localStorageMock.getItem as jest.MockedFunction).mockReturnValue('mock-token'); const result = await API.handleLaptimes(1); @@ -230,18 +230,18 @@ describe('API', () => { }); it('should throw an error when creating laptime fails', async () => { - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: false, - }); + } as Response); (localStorageMock.getItem as jest.MockedFunction).mockReturnValue('mock-token'); await expect(API.handleLaptimes(1, { title: 'new laptime', simracing: false, comment: "my racing moments" })).rejects.toThrow('Failed to create laptime'); }); it('should throw an error when fetching laptimes fails', async () => { - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: false, - }); + } as Response); (localStorageMock.getItem as jest.MockedFunction).mockReturnValue('mock-token'); await expect(API.handleLaptimes(1)).rejects.toThrow('Failed to fetch Personal laptimes'); @@ -252,10 +252,10 @@ describe('API', () => { describe('fetchMyLaptime', () => { it('should fetch a personal laptime successfully', async () => { const mockLaptime = { id: 1, title: 'new laptime', simracing: false, comment: "my racing moments" }; - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: true, json: async () => mockLaptime, - }); + } as Response); (localStorageMock.getItem as jest.MockedFunction).mockReturnValue('mock-token'); const result = await API.fetchMyLaptime(1); @@ -275,9 +275,9 @@ describe('API', () => { }); it('should throw an error when fetching personal laptime fails', async () => { - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: false, - }); + } as Response); (localStorageMock.getItem as jest.MockedFunction).mockReturnValue('mock-token'); await expect(API.fetchMyLaptime(1)).rejects.toThrow('Failed to fetch Personal Laptime. Does not exist.'); @@ -288,10 +288,10 @@ describe('API', () => { describe('fetchAUserLaptime', () => { it('should fetch a user\'s specific laptime successfully', async () => { const mockLaptime = { id: 1, title: 'new laptime', simracing: false, comment: "my racing moments" }; - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: true, json: async () => mockLaptime, - }); + } as Response); (localStorageMock.getItem as jest.MockedFunction).mockReturnValue('mock-token'); const result = await API.fetchAUserLaptime(1, 1); @@ -311,9 +311,9 @@ describe('API', () => { }); it('should throw an error when fetching a user\'s specific laptime fails', async () => { - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: false, - }); + } as Response); (localStorageMock.getItem as jest.MockedFunction).mockReturnValue('mock-token'); await expect(API.fetchAUserLaptime(1, 1)).rejects.toThrow('Failed to fetch #1 laptime'); @@ -324,10 +324,10 @@ describe('API', () => { describe('EditUserProfile', () => { it('should edit user profile successfully', async () => { const mockResponse = { id: 1, username: 'Updated User' }; - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: true, json: async () => mockResponse, - }); + } as Response); (localStorageMock.getItem as jest.MockedFunction).mockReturnValue('mock-token'); const result = await API.EditUserProfile(1, { username: 'Updated User' }); @@ -348,9 +348,9 @@ describe('API', () => { }); it('should throw an error when editing user profile fails', async () => { - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: false, - }); + } as Response); (localStorageMock.getItem as jest.MockedFunction).mockReturnValue('mock-token'); await expect(API.EditUserProfile(1, { username: 'Updated User' })).rejects.toThrow('Failed to Edit User profile'); @@ -361,10 +361,10 @@ describe('API', () => { describe('EditUserProfilePic', () => { it('should edit user profile picture successfully', async () => { const mockResponse = { id: 1, profile_picture_url: 'new-profile-picture.jpg' }; - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: true, json: async () => mockResponse, - }); + } as Response); (localStorageMock.getItem as jest.MockedFunction).mockReturnValue('mock-token'); const result = await API.EditUserProfilePic(1, { profile_picture_url: 'new-profile-picture.jpg' }); @@ -385,9 +385,9 @@ describe('API', () => { }); it('should throw an error when editing user profile picture fails', async () => { - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: false, - }); + } as Response); (localStorageMock.getItem as jest.MockedFunction).mockReturnValue('mock-token'); await expect(API.EditUserProfilePic(1, { profile_picture_url: 'new-profile-picture.jpg' })).rejects.toThrow('Failed to Edit User profile picture'); @@ -398,10 +398,10 @@ describe('API', () => { describe('fetchUsersLaptimes', () => { it('should fetch user\'s laptimes successfully', async () => { const mockLaptimes = [{ id: 1, title: 'new laptime', simracing: false, comment: "my racing moments" }, { id: 2, title: 'new laptime 2', simracing: false, comment: "my racing moments 2" }]; - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: true, json: async () => mockLaptimes, - }); + } as Response); (localStorageMock.getItem as jest.MockedFunction).mockReturnValue('mock-token'); const result = await API.fetchUsersLaptimes(1, 1); @@ -421,9 +421,9 @@ describe('API', () => { }); it('should throw an error when fetching user\'s laptimes fails', async () => { - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: false, - }); + } as Response); (localStorageMock.getItem as jest.MockedFunction).mockReturnValue('mock-token'); await expect(API.fetchUsersLaptimes(1, 1)).rejects.toThrow('Failed to fetch #1\'s laptimes!'); @@ -434,10 +434,10 @@ describe('API', () => { describe('fetchF1Teams', () => { it('should fetch Formula 1 teams successfully', async () => { const mockTeams = [{ id: 1, name: 'Team A' }, { id: 2, name: 'Team B' }]; - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: true, json: async () => mockTeams, - }); + } as Response); const result = await API.fetchF1Teams(); @@ -454,9 +454,9 @@ describe('API', () => { }); it('should throw an error when fetching Formula 1 teams fails', async () => { - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: false, - }); + } as Response); await expect(API.fetchF1Teams()).rejects.toThrow('Failed to get Formula 1 Team Standings'); }); @@ -466,10 +466,10 @@ describe('API', () => { describe('fetchF1Drivers', () => { it('should fetch Formula 1 drivers successfully', async () => { const mockDrivers = [{ id: 1, name: 'Driver A' }, { id: 2, name: 'Driver B' }]; - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: true, json: async () => mockDrivers, - }); + } as Response); const result = await API.fetchF1Drivers(); @@ -486,9 +486,9 @@ describe('API', () => { }); it('should throw an error when fetching Formula 1 drivers fails', async () => { - (global.fetch as jest.Mock).mockResolvedValueOnce({ + (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: false, - }); + } as Response); await expect(API.fetchF1Drivers()).rejects.toThrow('Failed to get Formula 1 Driver Standings'); });