Skip to content

Commit

Permalink
fix: type mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
vantage-ola committed Aug 12, 2024
1 parent 5ce6adc commit b11e313
Showing 1 changed file with 48 additions and 48 deletions.
96 changes: 48 additions & 48 deletions tracknow/web/src/tests/hooks/API.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof fetch>).mockResolvedValueOnce({
ok: true,
json: async () => mockResponse,
});
} as Response);

const result = await API.loginUser({ username: 'user1', password: 'password' });

Expand All @@ -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<typeof fetch>).mockResolvedValueOnce({
ok: false,
});
} as Response);

await expect(API.loginUser({ username: 'user1', password: 'wrong' })).rejects.toThrow('Login Failed');
});
Expand All @@ -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<typeof fetch>).mockResolvedValueOnce({
ok: true,
json: async () => mockIdentity,
});
} as Response);
(localStorageMock.getItem as jest.MockedFunction<typeof localStorageMock.getItem>).mockReturnValue('mock-token');

const result = await API.getIdentity();
Expand All @@ -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<typeof fetch>).mockResolvedValueOnce({
ok: false,
});
} as Response);
(localStorageMock.getItem as jest.MockedFunction<typeof localStorageMock.getItem>).mockReturnValue('mock-token');

await expect(API.getIdentity()).rejects.toThrow('User not Logged in');
Expand All @@ -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<typeof fetch>).mockResolvedValueOnce({
ok: true,
json: async () => mockResponse,
});
} as Response);

const result = await API.CreateUser({ username: 'newuser', password: 'password' });

Expand All @@ -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<typeof fetch>).mockResolvedValueOnce({
ok: false,
});
} as Response);

await expect(API.CreateUser({ username: 'newuser', password: 'password' })).rejects.toThrow('Failed to create user');
});
Expand All @@ -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<typeof fetch>).mockResolvedValueOnce({
ok: true,
json: async () => mockResponse,
});
} as Response);

(localStorageMock.getItem as jest.MockedFunction<typeof localStorageMock.getItem>).mockReturnValue('mock-token');

Expand All @@ -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<typeof fetch>).mockResolvedValueOnce({
ok: true,
json: async () => mockResponse,
});
} as Response);
(localStorageMock.getItem as jest.MockedFunction<typeof localStorageMock.getItem>).mockReturnValue('mock-token');

const result = await API.handleLaptimes(1);
Expand All @@ -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<typeof fetch>).mockResolvedValueOnce({
ok: false,
});
} as Response);
(localStorageMock.getItem as jest.MockedFunction<typeof localStorageMock.getItem>).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<typeof fetch>).mockResolvedValueOnce({
ok: false,
});
} as Response);
(localStorageMock.getItem as jest.MockedFunction<typeof localStorageMock.getItem>).mockReturnValue('mock-token');

await expect(API.handleLaptimes(1)).rejects.toThrow('Failed to fetch Personal laptimes');
Expand All @@ -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<typeof fetch>).mockResolvedValueOnce({
ok: true,
json: async () => mockLaptime,
});
} as Response);
(localStorageMock.getItem as jest.MockedFunction<typeof localStorageMock.getItem>).mockReturnValue('mock-token');

const result = await API.fetchMyLaptime(1);
Expand All @@ -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<typeof fetch>).mockResolvedValueOnce({
ok: false,
});
} as Response);
(localStorageMock.getItem as jest.MockedFunction<typeof localStorageMock.getItem>).mockReturnValue('mock-token');

await expect(API.fetchMyLaptime(1)).rejects.toThrow('Failed to fetch Personal Laptime. Does not exist.');
Expand All @@ -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<typeof fetch>).mockResolvedValueOnce({
ok: true,
json: async () => mockLaptime,
});
} as Response);
(localStorageMock.getItem as jest.MockedFunction<typeof localStorageMock.getItem>).mockReturnValue('mock-token');

const result = await API.fetchAUserLaptime(1, 1);
Expand All @@ -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<typeof fetch>).mockResolvedValueOnce({
ok: false,
});
} as Response);
(localStorageMock.getItem as jest.MockedFunction<typeof localStorageMock.getItem>).mockReturnValue('mock-token');

await expect(API.fetchAUserLaptime(1, 1)).rejects.toThrow('Failed to fetch #1 laptime');
Expand All @@ -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<typeof fetch>).mockResolvedValueOnce({
ok: true,
json: async () => mockResponse,
});
} as Response);
(localStorageMock.getItem as jest.MockedFunction<typeof localStorageMock.getItem>).mockReturnValue('mock-token');

const result = await API.EditUserProfile(1, { username: 'Updated User' });
Expand All @@ -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<typeof fetch>).mockResolvedValueOnce({
ok: false,
});
} as Response);
(localStorageMock.getItem as jest.MockedFunction<typeof localStorageMock.getItem>).mockReturnValue('mock-token');

await expect(API.EditUserProfile(1, { username: 'Updated User' })).rejects.toThrow('Failed to Edit User profile');
Expand All @@ -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<typeof fetch>).mockResolvedValueOnce({
ok: true,
json: async () => mockResponse,
});
} as Response);
(localStorageMock.getItem as jest.MockedFunction<typeof localStorageMock.getItem>).mockReturnValue('mock-token');

const result = await API.EditUserProfilePic(1, { profile_picture_url: 'new-profile-picture.jpg' });
Expand All @@ -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<typeof fetch>).mockResolvedValueOnce({
ok: false,
});
} as Response);
(localStorageMock.getItem as jest.MockedFunction<typeof localStorageMock.getItem>).mockReturnValue('mock-token');

await expect(API.EditUserProfilePic(1, { profile_picture_url: 'new-profile-picture.jpg' })).rejects.toThrow('Failed to Edit User profile picture');
Expand All @@ -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<typeof fetch>).mockResolvedValueOnce({
ok: true,
json: async () => mockLaptimes,
});
} as Response);
(localStorageMock.getItem as jest.MockedFunction<typeof localStorageMock.getItem>).mockReturnValue('mock-token');

const result = await API.fetchUsersLaptimes(1, 1);
Expand All @@ -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<typeof fetch>).mockResolvedValueOnce({
ok: false,
});
} as Response);
(localStorageMock.getItem as jest.MockedFunction<typeof localStorageMock.getItem>).mockReturnValue('mock-token');

await expect(API.fetchUsersLaptimes(1, 1)).rejects.toThrow('Failed to fetch #1\'s laptimes!');
Expand All @@ -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<typeof fetch>).mockResolvedValueOnce({
ok: true,
json: async () => mockTeams,
});
} as Response);

const result = await API.fetchF1Teams();

Expand All @@ -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<typeof fetch>).mockResolvedValueOnce({
ok: false,
});
} as Response);

await expect(API.fetchF1Teams()).rejects.toThrow('Failed to get Formula 1 Team Standings');
});
Expand All @@ -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<typeof fetch>).mockResolvedValueOnce({
ok: true,
json: async () => mockDrivers,
});
} as Response);

const result = await API.fetchF1Drivers();

Expand All @@ -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<typeof fetch>).mockResolvedValueOnce({
ok: false,
});
} as Response);

await expect(API.fetchF1Drivers()).rejects.toThrow('Failed to get Formula 1 Driver Standings');
});
Expand Down

0 comments on commit b11e313

Please sign in to comment.