fix of tests
This commit is contained in:
@@ -1,175 +1,213 @@
|
|||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import {
|
import {
|
||||||
listLocations,
|
listLocations,
|
||||||
getLocation,
|
getLocation,
|
||||||
addLocation,
|
addLocation,
|
||||||
updateLocation,
|
updateLocation,
|
||||||
deleteLocation
|
deleteLocation
|
||||||
} from '../api/locations';
|
} from '@/api/locations';
|
||||||
|
|
||||||
jest.mock('axios');
|
jest.mock('axios');
|
||||||
|
|
||||||
describe('Location API Functions', () => {
|
describe('Location API Functions', () => {
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
jest.clearAllMocks();
|
jest.clearAllMocks();
|
||||||
});
|
|
||||||
|
|
||||||
describe('listLocations', () => {
|
|
||||||
test('should fetch all locations successfully', async () => {
|
|
||||||
const mockLocations = [
|
|
||||||
{
|
|
||||||
"id": 1,
|
|
||||||
"name": "Warszawa",
|
|
||||||
"description": "Stolica Polski, położona w centralnej części kraju. Warszawa jest największym miastem w Polsce, znanym z bogatej historii, kultury i architektury. Warto zobaczyć Zamek Królewski, Stare Miasto oraz Muzeum Powstania Warszawskiego. Miasto oferuje wiele atrakcji turystycznych, w tym parki, muzea i teatry. Warszawa jest również ważnym ośrodkiem gospodarczym i kulturalnym.",
|
|
||||||
"image": "https://www.niesamowitapolska.eu/images/mazowieckie/warszawa/38607734_m.jpg",
|
|
||||||
"area": 517.21,
|
|
||||||
"population": 1790658
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": 2,
|
|
||||||
"name": "Kielce",
|
|
||||||
"description": "Stolica województwa świętokrzyskiego, położona w centralnej Polsce. Kielce to miasto w centralnej Polsce, znane z pięknych krajobrazów i bogatej historii. Warto odwiedzić Kielecki Park Etnograficzny, Muzeum Zabawek oraz Katedrę Wniebowzięcia Najświętszej Maryi Panny. Kielce są również znane z licznych festiwali i wydarzeń kulturalnych. Miasto oferuje wiele atrakcji turystycznych, w tym parki, muzea i teatry.",
|
|
||||||
"image": "https://as2.ftcdn.net/jpg/05/42/90/67/1000_F_542906717_cf5i6HeCJsPluuH5tqq5MbsSdfpopmtT.webp",
|
|
||||||
"area": 109.2,
|
|
||||||
"population": 196000
|
|
||||||
}
|
|
||||||
];
|
|
||||||
axios.get.mockResolvedValueOnce({ data: mockLocations });
|
|
||||||
|
|
||||||
const result = await listLocations();
|
|
||||||
|
|
||||||
expect(axios.get).toHaveBeenCalledWith('https://hopp.zikor.pl/locations/all');
|
|
||||||
expect(result).toEqual(mockLocations);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle error when fetching locations fails', async () => {
|
describe('listLocations', () => {
|
||||||
axios.get.mockRejectedValueOnce(new Error('Network error'));
|
test('should fetch all locations successfully', async () => {
|
||||||
|
const mockLocations = [
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"name": "Warszawa",
|
||||||
|
"description": "Stolica Polski, położona w centralnej części kraju. Warszawa jest największym miastem w Polsce, znanym z bogatej historii, kultury i architektury. Warto zobaczyć Zamek Królewski, Stare Miasto oraz Muzeum Powstania Warszawskiego. Miasto oferuje wiele atrakcji turystycznych, w tym parki, muzea i teatry. Warszawa jest również ważnym ośrodkiem gospodarczym i kulturalnym.",
|
||||||
|
"image": "https://www.niesamowitapolska.eu/images/mazowieckie/warszawa/38607734_m.jpg",
|
||||||
|
"imageSource": {
|
||||||
|
"uri": "https://www.niesamowitapolska.eu/images/mazowieckie/warszawa/38607734_m.jpg",
|
||||||
|
},
|
||||||
|
"area": 517.24,
|
||||||
|
"population": 1790658,
|
||||||
|
"longitude": 52.232887,
|
||||||
|
"latitude": 20.896273
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 2,
|
||||||
|
"name": "Kielce",
|
||||||
|
"description": "Stolica województwa świętokrzyskiego, położona w centralnej Polsce. Kielce to miasto w centralnej Polsce, znane z pięknych krajobrazów i bogatej historii. Warto odwiedzić Kielecki Park Etnograficzny, Muzeum Zabawek oraz Katedrę Wniebowzięcia Najświętszej Maryi Panny. Kielce są również znane z licznych festiwali i wydarzeń kulturalnych. Miasto oferuje wiele atrakcji turystycznych, w tym parki, muzea i teatry.",
|
||||||
|
"image": "https://as2.ftcdn.net/jpg/05/42/90/67/1000_F_542906717_cf5i6HeCJsPluuH5tqq5MbsSdfpopmtT.webp",
|
||||||
|
"imageSource": {
|
||||||
|
"uri": "https://as2.ftcdn.net/jpg/05/42/90/67/1000_F_542906717_cf5i6HeCJsPluuH5tqq5MbsSdfpopmtT.webp",
|
||||||
|
},
|
||||||
|
"area": 109.4,
|
||||||
|
"population": 196000,
|
||||||
|
"longitude": 50.85416,
|
||||||
|
"latitude": 20.533003
|
||||||
|
}
|
||||||
|
];
|
||||||
|
axios.get.mockResolvedValueOnce({data: mockLocations});
|
||||||
|
|
||||||
await expect(listLocations()).rejects.toThrow('Network error');
|
const result = await listLocations();
|
||||||
expect(axios.get).toHaveBeenCalledWith('https://hopp.zikor.pl/locations/all');
|
|
||||||
});
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('getLocation', () => {
|
console.log(result);
|
||||||
test('should fetch a location by id successfully', async () => {
|
|
||||||
const mockLocation = {
|
|
||||||
"id": 1,
|
|
||||||
"name": "Warszawa",
|
|
||||||
"description": "Stolica Polski, położona w centralnej części kraju.",
|
|
||||||
"image": "https://www.niesamowitapolska.eu/images/mazowieckie/warszawa/38607734_m.jpg",
|
|
||||||
"area": 517.21,
|
|
||||||
"population": 1790658
|
|
||||||
};
|
|
||||||
|
|
||||||
axios.get.mockResolvedValueOnce({ data: mockLocation });
|
|
||||||
|
|
||||||
const result = await getLocation(1);
|
expect(axios.get).toHaveBeenCalledWith('https://hopp.zikor.pl/locations/all');
|
||||||
|
expect(result).toEqual(mockLocations);
|
||||||
|
});
|
||||||
|
|
||||||
expect(axios.get).toHaveBeenCalledWith('https://hopp.zikor.pl/locations/1');
|
test('should handle error when fetching locations fails', async () => {
|
||||||
expect(result).toEqual(mockLocation);
|
axios.get.mockRejectedValueOnce(new Error('Network error'));
|
||||||
|
|
||||||
|
await expect(listLocations()).rejects.toThrow('Network error');
|
||||||
|
expect(axios.get).toHaveBeenCalledWith('https://hopp.zikor.pl/locations/all');
|
||||||
|
});
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('getLocation', () => {
|
||||||
|
test('should fetch a location by id successfully', async () => {
|
||||||
|
const mockLocation = [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
name: 'Warszawa',
|
||||||
|
description: 'Stolica Polski, położona w centralnej części kraju. Warszawa jest największym miastem w Polsce, znanym z bogatej historii, kultury i architektury. Warto zobaczyć Zamek Królewski, Stare Miasto oraz Muzeum Powstania Warszawskiego. Miasto oferuje wiele atrakcji turystycznych, w tym parki, muzea i teatry. Warszawa jest również ważnym ośrodkiem gospodarczym i kulturalnym.',
|
||||||
|
image: 'https://www.niesamowitapolska.eu/images/mazowieckie/warszawa/38607734_m.jpg',
|
||||||
|
imageSource: {
|
||||||
|
uri: 'https://www.niesamowitapolska.eu/images/mazowieckie/warszawa/38607734_m.jpg'
|
||||||
|
},
|
||||||
|
area: 517.24,
|
||||||
|
population: 1790658,
|
||||||
|
longitude: 52.232887,
|
||||||
|
latitude: 20.896273
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
const toCheck = [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
name: 'Warszawa',
|
||||||
|
description: 'Stolica Polski, położona w centralnej części kraju. Warszawa jest największym miastem w Polsce, znanym z bogatej historii, kultury i architektury. Warto zobaczyć Zamek Królewski, Stare Miasto oraz Muzeum Powstania Warszawskiego. Miasto oferuje wiele atrakcji turystycznych, w tym parki, muzea i teatry. Warszawa jest również ważnym ośrodkiem gospodarczym i kulturalnym.',
|
||||||
|
image: 'https://www.niesamowitapolska.eu/images/mazowieckie/warszawa/38607734_m.jpg',
|
||||||
|
imageSource: {
|
||||||
|
uri: 'https://www.niesamowitapolska.eu/images/mazowieckie/warszawa/38607734_m.jpg'
|
||||||
|
},
|
||||||
|
area: 517.24,
|
||||||
|
population: 1790658,
|
||||||
|
longitude: 52.232887,
|
||||||
|
latitude: 20.896273
|
||||||
|
},
|
||||||
|
{"image" : null}
|
||||||
|
];
|
||||||
|
|
||||||
|
axios.get.mockResolvedValueOnce({data: mockLocation});
|
||||||
|
|
||||||
|
const result = await getLocation(1);
|
||||||
|
|
||||||
|
console.log(result);
|
||||||
|
|
||||||
|
expect(axios.get).toHaveBeenCalledWith('https://hopp.zikor.pl/locations/1');
|
||||||
|
expect(result).toEqual(toCheck);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should handle error when fetching a location fails', async () => {
|
||||||
|
axios.get.mockRejectedValueOnce(new Error('Location not found'));
|
||||||
|
|
||||||
|
await expect(getLocation(999)).rejects.toThrow('Location not found');
|
||||||
|
expect(axios.get).toHaveBeenCalledWith('https://hopp.zikor.pl/locations/999');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle error when fetching a location fails', async () => {
|
describe('addLocation', () => {
|
||||||
axios.get.mockRejectedValueOnce(new Error('Location not found'));
|
test('should add a location successfully', async () => {
|
||||||
|
const newLocation = {
|
||||||
|
"name": "Gdańsk",
|
||||||
|
"description": "Miasto portowe na północy Polski.",
|
||||||
|
"image": "https://example.com/gdansk.jpg",
|
||||||
|
"area": 262.0,
|
||||||
|
"population": 470907
|
||||||
|
};
|
||||||
|
|
||||||
await expect(getLocation(999)).rejects.toThrow('Location not found');
|
const responseLocation = {
|
||||||
expect(axios.get).toHaveBeenCalledWith('https://hopp.zikor.pl/locations/999');
|
"id": 3,
|
||||||
});
|
...newLocation
|
||||||
});
|
};
|
||||||
|
|
||||||
describe('addLocation', () => {
|
axios.post.mockResolvedValueOnce({data: responseLocation});
|
||||||
test('should add a location successfully', async () => {
|
|
||||||
const newLocation = {
|
|
||||||
"name": "Gdańsk",
|
|
||||||
"description": "Miasto portowe na północy Polski.",
|
|
||||||
"image": "https://example.com/gdansk.jpg",
|
|
||||||
"area": 262.0,
|
|
||||||
"population": 470907
|
|
||||||
};
|
|
||||||
|
|
||||||
const responseLocation = {
|
|
||||||
"id": 3,
|
|
||||||
...newLocation
|
|
||||||
};
|
|
||||||
|
|
||||||
axios.post.mockResolvedValueOnce({ data: responseLocation });
|
|
||||||
|
|
||||||
const result = await addLocation(newLocation);
|
const result = await addLocation(newLocation);
|
||||||
|
|
||||||
expect(axios.post).toHaveBeenCalledWith(
|
expect(axios.post).toHaveBeenCalledWith(
|
||||||
'https://hopp.zikor.pl/locations/add',
|
'https://hopp.zikor.pl/locations/add',
|
||||||
newLocation,
|
newLocation,
|
||||||
{ headers: { "Content-Type": "application/json" } }
|
{headers: {"Content-Type": "application/json"}}
|
||||||
);
|
);
|
||||||
expect(result).toEqual(responseLocation);
|
expect(result).toEqual(responseLocation);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
describe('updateLocation', () => {
|
|
||||||
test('should update a location successfully', async () => {
|
|
||||||
const locationId = 2;
|
|
||||||
const updatedDetails = {
|
|
||||||
"name": "Kielce",
|
|
||||||
"description": "Updated description for Kielce",
|
|
||||||
"population": 200000
|
|
||||||
};
|
|
||||||
|
|
||||||
const responseLocation = {
|
|
||||||
"id": locationId,
|
|
||||||
"name": "Kielce",
|
|
||||||
"description": "Updated description for Kielce",
|
|
||||||
"image": "https://as2.ftcdn.net/jpg/05/42/90/67/1000_F_542906717_cf5i6HeCJsPluuH5tqq5MbsSdfpopmtT.webp",
|
|
||||||
"area": 109.2,
|
|
||||||
"population": 200000
|
|
||||||
};
|
|
||||||
|
|
||||||
axios.put.mockResolvedValueOnce({ data: responseLocation });
|
|
||||||
|
|
||||||
const result = await updateLocation(locationId, updatedDetails);
|
|
||||||
|
|
||||||
expect(axios.put).toHaveBeenCalledWith(
|
|
||||||
`https://hopp.zikor.pl/locations/${locationId}`,
|
|
||||||
updatedDetails,
|
|
||||||
{ headers: { "Content-Type": "application/json" } }
|
|
||||||
);
|
|
||||||
expect(result).toEqual(responseLocation);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle error when updating a location fails', async () => {
|
describe('updateLocation', () => {
|
||||||
const locationId = 999;
|
test('should update a location successfully', async () => {
|
||||||
const updatedDetails = { population: 300000 };
|
const locationId = 2;
|
||||||
|
const updatedDetails = {
|
||||||
axios.put.mockRejectedValueOnce(new Error('Failed to add location'));
|
"name": "Kielce",
|
||||||
|
"description": "Updated description for Kielce",
|
||||||
|
"population": 200000
|
||||||
|
};
|
||||||
|
|
||||||
await expect(updateLocation(locationId, updatedDetails)).rejects.toThrow('Failed to add location');
|
const responseLocation = {
|
||||||
expect(axios.put).toHaveBeenCalledWith(
|
"id": locationId,
|
||||||
`https://hopp.zikor.pl/locations/${locationId}`,
|
"name": "Kielce",
|
||||||
updatedDetails,
|
"description": "Updated description for Kielce",
|
||||||
{ headers: { "Content-Type": "application/json" } }
|
"image": "https://as2.ftcdn.net/jpg/05/42/90/67/1000_F_542906717_cf5i6HeCJsPluuH5tqq5MbsSdfpopmtT.webp",
|
||||||
);
|
"area": 109.2,
|
||||||
});
|
"population": 200000
|
||||||
});
|
};
|
||||||
|
|
||||||
describe('deleteLocation', () => {
|
axios.put.mockResolvedValueOnce({data: responseLocation});
|
||||||
test('should delete a location successfully', async () => {
|
|
||||||
const locationId = 2;
|
|
||||||
const responseData = { success: true, message: "Location deleted successfully" };
|
|
||||||
|
|
||||||
axios.delete.mockResolvedValueOnce({ data: responseData });
|
|
||||||
|
|
||||||
const result = await deleteLocation(locationId);
|
const result = await updateLocation(locationId, updatedDetails);
|
||||||
|
|
||||||
expect(axios.delete).toHaveBeenCalledWith(`https://hopp.zikor.pl/locations/${locationId}`);
|
expect(axios.put).toHaveBeenCalledWith(
|
||||||
expect(result).toEqual(responseData);
|
`https://hopp.zikor.pl/locations/${locationId}`,
|
||||||
|
updatedDetails,
|
||||||
|
{headers: {"Content-Type": "application/json"}}
|
||||||
|
);
|
||||||
|
expect(result).toEqual(responseLocation);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should handle error when updating a location fails', async () => {
|
||||||
|
const locationId = 999;
|
||||||
|
const updatedDetails = {population: 300000};
|
||||||
|
|
||||||
|
axios.put.mockRejectedValueOnce(new Error('Failed to add location'));
|
||||||
|
|
||||||
|
await expect(updateLocation(locationId, updatedDetails)).rejects.toThrow('Failed to add location');
|
||||||
|
expect(axios.put).toHaveBeenCalledWith(
|
||||||
|
`https://hopp.zikor.pl/locations/${locationId}`,
|
||||||
|
updatedDetails,
|
||||||
|
{headers: {"Content-Type": "application/json"}}
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle error when deleting a location fails', async () => {
|
describe('deleteLocation', () => {
|
||||||
const locationId = 999;
|
test('should delete a location successfully', async () => {
|
||||||
|
const locationId = 2;
|
||||||
axios.delete.mockRejectedValueOnce(new Error('Failed to delete location'));
|
const responseData = {success: true, message: "Location deleted successfully"};
|
||||||
|
|
||||||
await expect(deleteLocation(locationId)).rejects.toThrow('Failed to delete location');
|
axios.delete.mockResolvedValueOnce({data: responseData});
|
||||||
expect(axios.delete).toHaveBeenCalledWith(`https://hopp.zikor.pl/locations/${locationId}`);
|
|
||||||
|
const result = await deleteLocation(locationId);
|
||||||
|
|
||||||
|
expect(axios.delete).toHaveBeenCalledWith(`https://hopp.zikor.pl/locations/${locationId}`);
|
||||||
|
expect(result).toEqual(responseData);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should handle error when deleting a location fails', async () => {
|
||||||
|
const locationId = 999;
|
||||||
|
|
||||||
|
axios.delete.mockRejectedValueOnce(new Error('Failed to delete location'));
|
||||||
|
|
||||||
|
await expect(deleteLocation(locationId)).rejects.toThrow('Failed to delete location');
|
||||||
|
expect(axios.delete).toHaveBeenCalledWith(`https://hopp.zikor.pl/locations/${locationId}`);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
})
|
})
|
||||||
Reference in New Issue
Block a user