Skip to content

Commit 8429ac2

Browse files
committed
Add example and first schema test
1 parent 815250f commit 8429ac2

File tree

7 files changed

+237
-5
lines changed

7 files changed

+237
-5
lines changed

.github/workflows/tests.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
name: Test
2+
23
on:
34
push:
45
branches: [ main ]
@@ -17,3 +18,5 @@ jobs:
1718
- run: npm run build
1819
- name: Check schema.json is up-to-date
1920
run: git diff --exit-code
21+
- name: Run tests
22+
run: npm test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.DS_Store
22
node_modules/
3+
/test/

examples/simple-element.json

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"schemaVersion": "1.0.0",
3+
"readme": "README.md",
4+
"modules": [
5+
{
6+
"kind": "javascript-module",
7+
"path": "my-project/my-element.js",
8+
"declarations": [
9+
{
10+
"kind": "class",
11+
"description": "This is the description of the class",
12+
"name": "MyElement",
13+
"members": [
14+
{
15+
"kind": "field",
16+
"name": "disabled"
17+
},
18+
{
19+
"kind": "method",
20+
"name": "fire"
21+
}
22+
],
23+
"events": [
24+
{
25+
"name": "disabled-changed",
26+
"type": {
27+
"text": "Event"
28+
}
29+
}
30+
],
31+
"attributes": [
32+
{
33+
"name": "disabled"
34+
}
35+
],
36+
"superclass": {
37+
"name": "HTMLElement"
38+
},
39+
"tagName": "my-element",
40+
"customElement": true
41+
}
42+
],
43+
"exports": [
44+
{
45+
"kind": "js",
46+
"name": "MyElement",
47+
"declaration": {
48+
"name": "MyElement",
49+
"module": "my-project/my-element.js"
50+
}
51+
},
52+
{
53+
"kind": "custom-element-definition",
54+
"name": "my-element",
55+
"declaration": {
56+
"name": "MyElement",
57+
"module": "my-project/my-element.js"
58+
}
59+
}
60+
]
61+
}
62+
]
63+
}

package-lock.json

Lines changed: 131 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
"name": "custom-elements-manifest",
33
"version": "1.0.0",
44
"description": "A file format for describing custom elements",
5+
"type": "module",
56
"main": "schema.json",
67
"scripts": {
7-
"format": "prettier schema.d.ts --write",
8-
"build": "typescript-json-schema --required --ignoreErrors schema.d.ts -o schema.json Package",
8+
"format": "prettier schema.d.ts src/**/*.ts --write",
9+
"build": "npm run build:ts && npm run build:schema",
10+
"build:ts": "tsc",
11+
"build:schema": "typescript-json-schema --required --ignoreErrors schema.d.ts -o schema.json Package",
12+
"test": "node ./test/schema_test.js",
913
"prepublishOnly": "npm run build"
1014
},
1115
"files": [
@@ -24,8 +28,10 @@
2428
},
2529
"homepage": "https://github.com/webcomponents/custom-elements-manifest#readme",
2630
"devDependencies": {
31+
"jsonschema": "^1.4.0",
2732
"prettier": "^2.2.1",
2833
"typescript": "~4.5.5",
29-
"typescript-json-schema": "^0.53.0"
34+
"typescript-json-schema": "^0.53.0",
35+
"uvu": "^0.5.3"
3036
}
3137
}

src/test/schema_test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2022 The Polymer Project Authors. All rights reserved.
4+
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5+
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6+
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7+
* Code distributed by Google as part of the polymer project is also
8+
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9+
*/
10+
11+
import {test} from 'uvu';
12+
import * as assert from 'uvu/assert';
13+
import {Validator} from 'jsonschema';
14+
import * as fs from 'fs/promises';
15+
16+
const schema = JSON.parse(await fs.readFile('./schema.json', 'utf-8'));
17+
18+
test('Schema tests', async () => {
19+
// TODO: read multiple test files
20+
const example = JSON.parse(
21+
await fs.readFile('./examples/simple-element.json', 'utf-8')
22+
);
23+
const validator = new Validator();
24+
const result = validator.validate(example, schema);
25+
assert.is(result.errors.length, 0);
26+
});
27+
28+
test.run();

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"declaration": true,
77
"declarationMap": true,
88
"sourceMap": true,
9+
"rootDir": "./src/",
910
"outDir": "./",
1011
"strict": true,
1112
"noUnusedLocals": true,
@@ -14,6 +15,6 @@
1415
"noFallthroughCasesInSwitch": true,
1516
"moduleResolution": "node",
1617
},
17-
"include": ["*.ts"],
18+
"include": ["schema.d.ts", "src/**/*.ts"],
1819
"exclude": []
1920
}

0 commit comments

Comments
 (0)