Skip to content

Commit

Permalink
added browser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wvanderp committed Dec 17, 2023
1 parent 2aac898 commit b63fdd5
Show file tree
Hide file tree
Showing 23 changed files with 13,516 additions and 396 deletions.
4 changes: 3 additions & 1 deletion .eslintIgnore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
node_moduals
.vscode
.nyc_output
coverage
coverage

tests/browser
18 changes: 18 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@
"prefer-arrow-callback": "off",
"sonarjs/no-duplicate-string": "off"
}
},
{
"files": [
"tests/browser/**/*"
],
"plugins": [
"cypress"
],
"extends": [
"plugin:cypress/recommended"
],
"env": {
"cypress/globals": true
},
"rules": {
"require-jsdoc": "off",
"jsdoc/require-jsdoc": "off"
}
}
],
"rules": {
Expand Down
1,049 changes: 684 additions & 365 deletions package-lock.json

Large diffs are not rendered by default.

30 changes: 17 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,27 @@
">0.3%, last 2 versions, not dead, not op_mini all"
],
"devDependencies": {
"@babel/preset-env": "^7.23.3",
"@babel/preset-env": "^7.23.6",
"@babel/preset-typescript": "^7.23.3",
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@types/deep-diff": "^1.0.5",
"@types/jest": "^29.5.8",
"@types/node": "^20.9.0",
"@types/jest": "^29.5.11",
"@types/node": "^20.10.4",
"@types/qs": "^6.9.10",
"@types/uuid": "^9.0.7",
"@typescript-eslint/eslint-plugin": "^6.11.0",
"@typescript-eslint/parser": "^6.11.0",
"@typescript-eslint/eslint-plugin": "^6.14.0",
"@typescript-eslint/parser": "^6.14.0",
"dotenv": "^16.3.1",
"eslint": "^8.53.0",
"eslint": "^8.56.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-airbnb-typescript": "^17.1.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-compat": "^4.2.0",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-cypress": "^2.15.1",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jest": "^27.6.0",
"eslint-plugin-jsdoc": "^46.9.0",
"eslint-plugin-jsonc": "^2.10.0",
"eslint-plugin-jsdoc": "^46.9.1",
"eslint-plugin-jsonc": "^2.11.1",
"eslint-plugin-no-secrets": "^0.8.9",
"eslint-plugin-pii": "^1.0.2",
"eslint-plugin-ramda": "^2.5.1",
Expand All @@ -52,11 +53,12 @@
"npm-run-all": "^4.1.5",
"rimraf": "^5.0.5",
"source-map-support": "^0.5.21",
"ts-node": "10.9.1",
"typedoc": "^0.25.3",
"typescript": "^5.2.2"
"ts-node": "10.9.2",
"typedoc": "^0.25.4",
"typescript": "^5.3.3"
},
"dependencies": {
"@aws-crypto/sha256-js": "^5.2.0",
"@wmde/wikibase-datamodel-types": "git+https://github.com/wvanderp/WikibaseDataModelTypes.git",
"axios": "1.6.2",
"diff-arrays-of-objects": "git+https://github.com/wvanderp/diff-arrays-of-objects.git",
Expand All @@ -70,9 +72,11 @@
"lint": "npm-run-all -c -s tsc eslint",
"tsc": "tsc --noEmit",
"eslint": "eslint --ext ts,js src/ tests/",
"prepublish": "npm run build",
"prepare": "cd tests/browser && npm install && cd ../..",
"prepublishOnly": "npm run build",
"test": "jest --testPathIgnorePatterns=\"integration/\"",
"test:all": "jest --runInBand",
"test:browser": "cd tests/browser && npm run test",
"coverage": "npm run test -- --coverage",
"coverage:all": "jest --coverage",
"clean": "rimraf lib/ .nyc_output/ coverage/ docs/"
Expand Down
6 changes: 2 additions & 4 deletions src/Item.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { createHash } from 'crypto';
import {
Item as WikidataItem,
Labels as WikidataLabels,
Expand Down Expand Up @@ -27,6 +26,7 @@ import statementDiff from './utils/diff/statementsDiff';
import { QString } from './types/strings';
import { isQString } from './utils/guards/strings';
import siteLinkDiff from './utils/diff/siteLinkDiff';
import sha256 from './utils/hash';

/**
* this type omits the id because if an item is new there will be no id
Expand Down Expand Up @@ -128,9 +128,7 @@ export default class Item {
*/
public get internalID(): string {
if (this._internalID === '') {
this._internalID = createHash('sha256')
.update(JSON.stringify(this.toJSON()))
.digest('hex');
this._internalID = sha256(JSON.stringify(this.toJSON()));
}

return this._internalID;
Expand Down
6 changes: 2 additions & 4 deletions src/Reference.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createHash } from 'crypto';
import { Reference as WikidataReference, ReferenceSnaks as wikidataReferenceSnaks, Snaks } from '@wmde/wikibase-datamodel-types';

import Snak from './Snak';
import arrayEqual, { arrayEqualWith } from './utils/arrayEqual';
import normalizeOutput from './utils/normalizeOutput';
import snakGenerator from './utils/snakGenerator';
import sha256 from './utils/hash';

/**
* Reduces an array of snaks into a object grouped by PropertyID
Expand Down Expand Up @@ -60,9 +60,7 @@ export default class Reference {
*/
public get internalID(): string {
if (this._internalID === '') {
this._internalID = createHash('sha256')
.update(JSON.stringify(this.toJSON()))
.digest('hex');
this._internalID = sha256(JSON.stringify(this.toJSON()));
}

return this._internalID;
Expand Down
6 changes: 2 additions & 4 deletions src/Snak.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createHash } from 'crypto';
import { Snaks as WikidataSnaks, SnakType as WikidataSnakType } from '@wmde/wikibase-datamodel-types';
import { PString } from './types/strings';
import { isPString } from './utils/guards/strings';
import sha256 from './utils/hash';

/**
* @abstract
Expand Down Expand Up @@ -51,9 +51,7 @@ export default abstract class Snak {
*/
public get internalID(): string {
if (this._internalID === '') {
this._internalID = createHash('sha256')
.update(JSON.stringify(this.toJSON()))
.digest('hex');
this._internalID = sha256(JSON.stringify(this.toJSON()));
}

return this._internalID;
Expand Down
6 changes: 2 additions & 4 deletions src/Statement.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { createHash } from 'crypto';
import { Statement as wikidataStatement, Qualifiers as wikidataQualifiers } from '@wmde/wikibase-datamodel-types';

import Reference from './Reference';
Expand All @@ -8,6 +7,7 @@ import { PString } from './types/strings';
import arrayEqual, { arrayEqualWith } from './utils/arrayEqual';
import normalizeOutput from './utils/normalizeOutput';
import snakGenerator from './utils/snakGenerator';
import sha256 from './utils/hash';

/**
* @class
Expand Down Expand Up @@ -60,9 +60,7 @@ export default class Statement {
*/
public get internalID(): string {
if (this._internalID === '') {
this._internalID = createHash('sha256')
.update(JSON.stringify(this.toJSON()))
.digest('hex');
this._internalID = sha256(JSON.stringify(this.toJSON()));
}

return this._internalID;
Expand Down
16 changes: 16 additions & 0 deletions src/utils/hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Sha256 } from '@aws-crypto/sha256-js';

/**
* Calculates the SHA256 hash of a string
*
* @param {string} input the string to hash
* @returns {string} the hash of the string
* @example
* const hash = sha256('hello');
* console.log(hash); // 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969
*/
export default function sha256(input: string): string {
const hash = new Sha256();
hash.update(input);
return Buffer.from(hash.digestSync()).toString('hex');
}
3 changes: 3 additions & 0 deletions tests/browser/.gitIgnore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.parcel-cache
.cache
dist
15 changes: 15 additions & 0 deletions tests/browser/cypress.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { defineConfig } = require('cypress');

const path = require('path');
const dotenvPath = path.resolve(__dirname, '..', '..', '.env');
require('dotenv').config({ path: dotenvPath });

module.exports = defineConfig({
env: {
...process.env,
},
e2e: {
setupNodeEvents(on, config) {},
},
});
6 changes: 6 additions & 0 deletions tests/browser/cypress/e2e/buildAndLoad.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
describe('template spec', () => {
it('passes', () => {
cy.visit('http://localhost:1234/buildAndLoad.html');
cy.get('body').should('contain', 'Douglas Adams');
});
});
17 changes: 17 additions & 0 deletions tests/browser/cypress/e2e/buildAndLoad.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<html>
<head>
<title>Build and Load</title>
</head>
<body>
<h1>test</h1>
<script type="module">
import { requestItem } from '../../../../';
const item = requestItem('Q42');
item.then((item) => {
document.body.innerHTML = item.findLabel('en').value;
});
</script>
<h1>result</h1>
</body>

</html>
20 changes: 20 additions & 0 deletions tests/browser/cypress/e2e/login.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
describe('template spec', () => {
it('passes', () => {
cy.visit('http://localhost:1234/login.html');

// the following code is injected into the page
// and executed in the browser
// the username and passwords are replaced by the .parcel-plugin-replace.js
// with the values from the .env file
const functionCall = `login('${Cypress.env('WIKIDATA_USERNAME')}', '${Cypress.env('WIKIDATA_PASSWORD')}')`;

cy.window().then((win) => {
try {
win.eval(functionCall);
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
}
});
});
});
10 changes: 10 additions & 0 deletions tests/browser/cypress/e2e/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<head>
<title>login</title>
<script type="module" src="./login.ts"></script>
</head>
<body>
<h1>test</h1>
</body>

</html>
21 changes: 21 additions & 0 deletions tests/browser/cypress/e2e/login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* eslint-disable import/no-relative-packages */
/* eslint-disable import/no-useless-path-segments */
import {
getToken
} from '../../../../';

declare global {
interface Window {
login: (username: string, password: string) => Promise<void>;
}
}

async function login(username: string, password: string): Promise<void> {
const token = await getToken(username, password);

if (token) {
document.body.innerHTML = `success: ${token}`;
}
}

window.login = login;
Empty file.
Loading

0 comments on commit b63fdd5

Please sign in to comment.