Skip to content

Commit

Permalink
Поправил тесты
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyAkkuratov committed May 30, 2024
1 parent 7839deb commit 2c84af3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/commonTestData.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const weather = {
};

export const ipInfo = {
region: "Moscow",
city: "Moscow",
};

export const testBlob = new Blob([
Expand Down
16 changes: 7 additions & 9 deletions src/pageTempaltes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ export const mainTemplate = `
<div>
<div class="info-block">
<form id="weatherForm" class="form-block">
<input id="userInput" class="form-input" placeholder="Type city and press enter" required autofocus>
<input id="userInput" class="form-input" placeholder="Type city and press enter" required="" autofocus="">
</form>
<div id="weather">
<img id="map"
src="${oopsImg}"
alt="Couldn't get image of map"></img>
<img id="map" src="${oopsImg}" alt="Couldn't get image of map">
</div>
<div id="info">
<span>Wait for city name</span>
Expand All @@ -26,17 +24,17 @@ export const mainTemplate = `

export const aboutTemplate = `
<div align="center">
<h3>Приложение &quot;Прогноз погоды&quot;</h3>
<h3>Приложение "Прогноз погоды"</h3>
<p>
Выполнение домашнего задания для лекции &quot;Современный инструментарий при разработке клиентских (и не только
приложений)&quot;
Выполнение домашнего задания для лекции "Современный инструментарий при разработке клиентских (и не только
приложений)"
</p>
<h2>О проекте</h2>
<p>Приложение &quot;Прогноз погоды&quot; это Web приложение разработанное на языке JavaScript. Оно предназначено для
<p>Приложение "Прогноз погоды" это Web приложение разработанное на языке JavaScript. Оно предназначено для
просмотра текущей погоды в конкретном городе.</p>
<p>Приложение показывает текущую температуру в градусах цельсия и общее состоянии погоды, обозначенное иконкой.</p>
<p>Так же приложение отображает компактную картинку с картой выбранного города.</p>
<a href="/">Main<a>
<a href="/">Main</a>
</div>
`;
22 changes: 13 additions & 9 deletions src/weatherApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ export default async function weatherApp(rootElement) {
}
}

async function updateWeather(cityNameParam, updateHistoryFlag) {
async function updateWeather(cityNameParam) {
const cityName = decodeURIComponent(cityNameParam);
const weather = await getWeather(cityName);
if (weather.cod === 200) {
if (updateHistoryFlag) await updateHistoryBlock(cityName);
await updateHistoryBlock(cityName);
const map = await getMap(weather.coord);
showMap(map);
showWeather(weather);
Expand Down Expand Up @@ -94,17 +94,17 @@ export default async function weatherApp(rootElement) {
router.navigate(`/weather/${cityName}`);
});

rootElement.querySelectorAll("a").forEach((link) =>
link.addEventListener("click", (event) => {
rootElement.addEventListener("click", (event) => {
if (event.target instanceof HTMLAnchorElement) {
event.preventDefault();
router.navigate(link.href);
}),
);
router.navigate(event.target.href);
}
});

router.addRoute({
path: /^\/weather\/(?<cityName>.+)$/,
onEnter: async (params) => {
updateWeather(params.cityName, true);
updateWeather(params.cityName);
},
});

Expand All @@ -113,7 +113,11 @@ export default async function weatherApp(rootElement) {
onEnter: () => {
rootElement.innerHTML = aboutTemplate;
},
onLeave: () => {
});

router.addRoute({
path: "/",
onEnter: () => {
rootElement.innerHTML = mainTemplate;
},
});
Expand Down
15 changes: 11 additions & 4 deletions src/weatherApp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import weatherApp from "./weatherApp";
import exteranlApi from "./externalRequests";
import { weather, ipInfo, testBlob } from "./commonTestData";
import { aboutTemplate, mainTemplate } from "./pageTempaltes";

jest.mock("./externalRequests", () => ({
getWeather: jest.fn(),
Expand Down Expand Up @@ -63,9 +64,6 @@ describe("Weather application tests", () => {
const map = el.querySelector("#map");
expect(map).not.toBe(null);
expect(map.alt).toBe("Couldn't get image of map");
expect(map.src).toBe(
"blob:http://localhost:8080/dbdb41a6-c771-4692-bdc1-594a6dd28ef5",
);
});

it("User see temperature on startup", () => {
Expand Down Expand Up @@ -99,6 +97,7 @@ describe("Weather application tests", () => {
"City_3",
"City_2",
"City_1",
"Moscow",
]);
});

Expand Down Expand Up @@ -165,7 +164,7 @@ describe("Weather application tests", () => {
// Надо подождать, когда все асинк функции закончатся, а потом продолжать выполнять синхронный код
await new Promise(process.nextTick);

expect(getAllHistoryParagraphs()).toStrictEqual(["DefaultCity"]);
expect(getAllHistoryParagraphs()).toStrictEqual(["DefaultCity", "Moscow"]);
});

it("History loads from localStorage", async () => {
Expand All @@ -179,4 +178,12 @@ describe("Weather application tests", () => {

expect(getAllHistoryParagraphs()).toStrictEqual(["Moscow"]);
});

it("Shoould open about and main pages when click on link", () => {
el.querySelector("a").click();
expect(el.innerHTML).toBe(aboutTemplate);

el.querySelector("a").click();
expect(el.innerHTML).toBe(mainTemplate);
});
});
2 changes: 1 addition & 1 deletion src/weatherAppError.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe("Weather application tests", () => {
const map = el.querySelector("#map");
expect(map).not.toBe(null);
expect(map.alt).toBe("Couldn't get image of map");
expect(map.src).toBe("http://localhost/");
expect(map.src).toBe("http://localhost/weather/Moscow");
});

it("Error with getInfoByIP", async () => {
Expand Down

0 comments on commit 2c84af3

Please sign in to comment.