Skip to content

Commit

Permalink
Refactor mock-data for weather-tests generation (#3000)
Browse files Browse the repository at this point in the history
Refactored the mock data generation for the tests so we can use plain
JSON files for the data and read it in a more general way.

Comments welcome!

Co-authored-by: veeck <michael@veeck.de>
  • Loading branch information
rejas and veeck authored Jan 11, 2023
1 parent 6e80e5a commit 877f8ad
Show file tree
Hide file tree
Showing 12 changed files with 1,475 additions and 1,392 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ _This release is scheduled to be released on 2023-04-01._
### Added

- Added increments for hourly forecasts in weather module (#2996)
- Added tests for hourly weather forecast

### Removed

Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = async () => {
return {
verbose: true,
testTimeout: 20000,
testSequencer: "<rootDir>/tests/configs/test_sequencer.js",
testSequencer: "<rootDir>/tests/utils/test_sequencer.js",
projects: [
{
displayName: "unit",
Expand Down
112 changes: 56 additions & 56 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@
},
"homepage": "https://magicmirror.builders",
"devDependencies": {
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-jest": "^27.2.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-jest": "^27.2.1",
"eslint-plugin-jsdoc": "^39.6.4",
"eslint-plugin-prettier": "^4.2.1",
"express-basic-auth": "^1.2.1",
"husky": "^8.0.2",
"husky": "^8.0.3",
"jest": "^29.3.1",
"jsdom": "^20.0.3",
"jsdom": "^21.0.0",
"lodash": "^4.17.21",
"playwright": "^1.29.1",
"prettier": "^2.8.1",
"playwright": "^1.29.2",
"prettier": "^2.8.2",
"pretty-quick": "^3.1.3",
"sinon": "^15.0.1",
"stylelint": "^14.16.1",
Expand Down
18 changes: 3 additions & 15 deletions tests/e2e/helpers/weather-functions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const helpers = require("./global-setup");
const path = require("path");
const fs = require("fs");
const { generateWeather, generateWeatherForecast, generateWeatherHourly } = require("../../mocks/weather_test");
const { injectMockData } = require("../../utils/weather_mocker");

exports.getText = async (element, result) => {
const elem = await helpers.waitForElement(element);
Expand All @@ -14,18 +12,8 @@ exports.getText = async (element, result) => {
).toBe(result);
};

exports.startApp = async (configFile, additionalMockData) => {
let mockWeather;
if (configFile.includes("forecast")) {
mockWeather = generateWeatherForecast(additionalMockData);
} else if (configFile.includes("hourly")) {
mockWeather = generateWeatherHourly(additionalMockData);
} else {
mockWeather = generateWeather(additionalMockData);
}
let content = fs.readFileSync(path.resolve(__dirname + "../../../../" + configFile)).toString();
content = content.replace("#####WEATHERDATA#####", mockWeather);
fs.writeFileSync(path.resolve(__dirname + "../../../../config/config.js"), content);
exports.startApp = async (configFileName, additionalMockData) => {
injectMockData(configFileName, additionalMockData);
await helpers.startApplication("");
await helpers.getDocument();
};
18 changes: 3 additions & 15 deletions tests/electron/helpers/weather-setup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const helpers = require("./global-setup");
const path = require("path");
const fs = require("fs");
const { generateWeather, generateWeatherForecast, generateWeatherHourly } = require("../../mocks/weather_test");
const { injectMockData } = require("../../utils/weather_mocker");

exports.getText = async (element, result) => {
const elem = await helpers.getElement(element);
Expand All @@ -15,17 +13,7 @@ exports.getText = async (element, result) => {
).toBe(result);
};

exports.startApp = async (configFile, systemDate) => {
let mockWeather;
if (configFile.includes("forecast")) {
mockWeather = generateWeatherForecast();
} else if (configFile.includes("hourly")) {
mockWeather = generateWeatherHourly();
} else {
mockWeather = generateWeather();
}
let content = fs.readFileSync(path.resolve(__dirname + "../../../../" + configFile)).toString();
content = content.replace("#####WEATHERDATA#####", mockWeather);
fs.writeFileSync(path.resolve(__dirname + "../../../../config/config.js"), content);
exports.startApp = async (configFileNameName, systemDate) => {
injectMockData(configFileNameName);
await helpers.startApplication("", systemDate);
};
48 changes: 48 additions & 0 deletions tests/mocks/weather_current.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"coord": {
"lon": 11.58,
"lat": 48.14
},
"weather": [
{
"id": 615,
"main": "Snow",
"description": "light rain and snow",
"icon": "13d"
},
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}
],
"base": "stations",
"main": {
"temp": 1.49,
"pressure": 1005,
"humidity": 93.7,
"temp_min": 1,
"temp_max": 2
},
"visibility": 7000,
"wind": {
"speed": 11.8,
"deg": 250
},
"clouds": {
"all": 75
},
"dt": 1547387400,
"sys": {
"type": 1,
"id": 1267,
"message": 0.0031,
"country": "DE",
"sunrise": 1547362817,
"sunset": 1547394301
},
"id": 2867714,
"name": "Munich",
"cod": 200
}
Loading

0 comments on commit 877f8ad

Please sign in to comment.