Add tests

This commit is contained in:
Linus Groh
2019-09-27 21:13:40 +01:00
parent a2de94fd44
commit 600934183a
7 changed files with 1817 additions and 86 deletions

View File

@@ -1,7 +1,7 @@
module.exports = {
root: true,
env: {
node: true
node: true,
},
extends: ["plugin:vue/essential", "@vue/prettier"],
rules: {
@@ -17,6 +17,14 @@ module.exports = {
],
},
parserOptions: {
parser: "babel-eslint"
}
parser: "babel-eslint",
},
overrides: [
{
files: ["**/__tests__/*.{j,t}s?(x)"],
env: {
jest: true,
},
},
],
};

View File

@@ -18,31 +18,6 @@ pages, this is a more advanced interface with more functionality, all in one pla
### Manually
**For development:**
- Run `yarn install` to install dependencies
- Run `yarn serve` to compile for development and start the hot-reload server
- Run `yarn lint` to lint and fix files
You can use the [`corsProxy.js`](scripts/corsProxy.js) script to use your production
instance of OwnTracks for development without making changes to its CORS-Headers:
```console
$ yarn cors-proxy
```
If you have [basic authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#Basic_authentication_scheme) enabled:
```console
$ env OT_BASIC_AUTH_USERNAME=username OT_BASIC_AUTH_PASSWORD='P@$$w0rd' yarn cors-proxy
```
The default host and port it binds to is `0.0.0.0:8888`. Change using the `OT_PROXY_HOST`
and `OT_PROXY_PORT` environment variables.
Finally update `api.baseUrl` in your config to `"http://0.0.0.0:8888/https://owntracks.example.com"`.
**To deploy:**
- Run `yarn install --production` to install dependencies
- Run `yarn build` to compile and minify for production
- Copy the content of the `dist/` directory to your webroot
@@ -81,6 +56,30 @@ Copy [`public/config/config.default.js`](public/config/config.default.js) to
See [`docs/config.md`](docs/config.md) for all available options.
## Development
- Run `yarn install` to install dependencies
- Run `yarn serve` to compile for development and start the hot-reload server
- Run `yarn lint` to lint and fix files
- Run `yarn test` to run unit tests
You can use the [`corsProxy.js`](scripts/corsProxy.js) script to use your production
instance of OwnTracks for development without making changes to its CORS-Headers:
```console
$ yarn cors-proxy
```
If you have [basic authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#Basic_authentication_scheme) enabled:
```console
$ env OT_BASIC_AUTH_USERNAME=username OT_BASIC_AUTH_PASSWORD='P@$$w0rd' yarn cors-proxy
```
The default host and port it binds to is `0.0.0.0:8888`. Change using the `OT_PROXY_HOST`
and `OT_PROXY_PORT` environment variables.
Finally update `api.baseUrl` in your config to `"http://0.0.0.0:8888/https://owntracks.example.com"`.
## Features
- Last known (i.e. live) locations:

22
jest.config.js Normal file
View File

@@ -0,0 +1,22 @@
module.exports = {
moduleFileExtensions: ["js", "jsx", "json", "vue"],
transform: {
"^.+\\.vue$": "vue-jest",
".+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$":
"jest-transform-stub",
"^.+\\.jsx?$": "babel-jest",
},
transformIgnorePatterns: ["/node_modules/"],
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/$1",
},
snapshotSerializers: ["jest-serializer-vue"],
testMatch: [
"**/tests/**/*.test.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)",
],
testURL: "http://localhost/",
watchPlugins: [
"jest-watch-typeahead/filename",
"jest-watch-typeahead/testname",
],
};

View File

@@ -1,19 +1,11 @@
{
"name": "owntracks-ui",
"version": "2.0.0-alpha",
"license": "MIT",
"author": {
"name": "Linus Groh",
"email": "mail@linusgroh.de"
},
"repository": {
"type": "git",
"url": "https://github.com/owntracks/frontend.git"
},
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"test": "vue-cli-service test:unit",
"cors-proxy": "node scripts/corsProxy.js"
},
"dependencies": {
@@ -31,9 +23,13 @@
"devDependencies": {
"@vue/cli-plugin-babel": "^3.11.0",
"@vue/cli-plugin-eslint": "^3.11.0",
"@vue/cli-plugin-unit-jest": "^3.11.0",
"@vue/cli-service": "^3.11.0",
"@vue/eslint-config-prettier": "^5.0.0",
"@vue/test-utils": "1.0.0-beta.29",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "^10.0.3",
"babel-jest": "^23.6.0",
"cors-anywhere": "^0.4.1",
"eslint": "^6.4.0",
"eslint-plugin-prettier": "^3.1.1",
@@ -42,5 +38,14 @@
"node-sass": "^4.12.0",
"sass-loader": "^8.0.0",
"vue-template-compiler": "^2.5.21"
},
"license": "MIT",
"author": {
"name": "Linus Groh",
"email": "mail@linusgroh.de"
},
"repository": {
"type": "git",
"url": "https://github.com/owntracks/frontend.git"
}
}

5
tests/.eslintrc.js Normal file
View File

@@ -0,0 +1,5 @@
module.exports = {
env: {
jest: true,
},
};

92
tests/util.test.js Normal file
View File

@@ -0,0 +1,92 @@
import config from "@/config";
import {
getApiUrl,
isIsoDate,
degreesToRadians,
distanceBetweenCoordinates,
} from "@/util";
describe("getApiUrl", () => {
test("without base URL", () => {
// See testURL in jest.config.js
expect(getApiUrl("foo")).toBe("http://localhost/foo");
expect(getApiUrl("/foo")).toBe("http://localhost/foo");
expect(getApiUrl("/foo/bar")).toBe("http://localhost/foo/bar");
});
test("with base URL", () => {
config.api.baseUrl = "http://example.com/owntracks";
expect(getApiUrl("foo")).toBe("http://example.com/owntracks/foo");
expect(getApiUrl("/foo")).toBe("http://example.com/owntracks/foo");
expect(getApiUrl("/foo/bar")).toBe("http://example.com/owntracks/foo/bar");
});
});
describe("isIsoDate", () => {
test("no match", () => {
expect(isIsoDate("foo")).toBe(false);
expect(isIsoDate("2019")).toBe(false);
expect(isIsoDate("2019-09")).toBe(false);
expect(isIsoDate("2019.09.27")).toBe(false);
expect(isIsoDate("2019_09_27")).toBe(false);
expect(isIsoDate("2019/09/27")).toBe(false);
expect(isIsoDate("27-09-2019")).toBe(false);
expect(isIsoDate("27.09.2019")).toBe(false);
expect(isIsoDate("27_09_2019")).toBe(false);
expect(isIsoDate("27/09/2019")).toBe(false);
expect(isIsoDate("0000-00-00")).toBe(false);
expect(isIsoDate("1234-56-78")).toBe(false);
});
test("match", () => {
expect(isIsoDate("0000-01-01")).toBe(true);
expect(isIsoDate("2019-09-27")).toBe(true);
expect(isIsoDate("9999-12-31")).toBe(true);
});
});
describe("degreesToRadians", () => {
test("expected results", () => {
expect(degreesToRadians(0)).toBe(0);
expect(degreesToRadians(45)).toBe(0.7853981633974483);
expect(degreesToRadians(90)).toBe(1.5707963267948966);
expect(degreesToRadians(180)).toBe(3.141592653589793);
expect(degreesToRadians(360)).toBe(6.283185307179586);
expect(degreesToRadians(-180)).toBe(-3.141592653589793);
});
});
describe("distanceBetweenCoordinates", () => {
test("expected results", () => {
expect(
distanceBetweenCoordinates({ lat: 0, lng: 0 }, { lat: 0, lng: 0 })
).toBe(0);
// The Shard - Victoria Memorial
expect(
distanceBetweenCoordinates(
{ lat: 51.5046678, lng: -0.0870769 },
{ lat: 51.501752, lng: -0.1408258 }
)
// 3.74km according to Google Maps
).toBe(3734.363267904623);
// Gatwick Airport - Heathrow Airport
expect(
distanceBetweenCoordinates(
{ lat: 51.1526929, lng: -0.1752475 },
{ lat: 51.4720694, lng: -0.4499871 }
)
// 40km according to Google Maps
).toBe(40321.45758693094);
// Berlin - San Francisco
expect(
distanceBetweenCoordinates(
{ lat: 52.5067614, lng: 13.284651 },
{ lat: 37.7576948, lng: -122.4726193 }
)
// 9,102.73km according to Google Maps
).toBe(9105627.810109459);
});
});

1698
yarn.lock

File diff suppressed because it is too large Load Diff