Skip to content

Commit d7a7912

Browse files
committed
test: add tests, finish implementation
1 parent 3c0febc commit d7a7912

File tree

6 files changed

+325
-14
lines changed

6 files changed

+325
-14
lines changed

packages/helpers/helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const has = arr => Array.isArray(arr) && arr?.length > 0;
1+
export const has = arr => Array.isArray(arr) && arr?.length > 0;
22

33
/** Package */
44
export function hasModules(_package) {

packages/helpers/index.js

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export class CustomElementsJson {
99
_tagNames = new Map();
1010
_definitions = new Map();
1111
_mixins = new Map();
12+
_classLikes = new Map();
1213

1314
constructor(
1415
{ schemaVersion, readme, modules } = {
@@ -27,12 +28,16 @@ export class CustomElementsJson {
2728
this.loopAll((item) => {
2829
if (h.isClass(item)) {
2930
this._classes.set(item.name, item);
31+
this._classLikes.set(item.name, item);
3032
}
3133

3234
if (h.isMixin(item)) {
3335
this._mixins.set(item.name, item);
36+
this._classLikes.set(item.name, item);
3437
}
38+
});
3539

40+
this.loopAll((item) => {
3641
if (h.isCustomElementExport(item)) {
3742
this._tagNames.set(
3843
item.name,
@@ -92,29 +97,55 @@ export class CustomElementsJson {
9297
getInheritanceTree(className) {
9398
const tree = [];
9499

95-
let klass = this._classes.get(className);
100+
let klass = this._classLikes.get(className);
101+
const mixins = this.getMixins();
96102

97-
if (klass) {
103+
if(klass) {
98104
tree.push(klass);
99105

100-
klass?.mixins?.forEach((mixin) => {
101-
tree.push(this._mixins.get(mixin.name));
106+
klass?.mixins?.forEach(mixin => {
107+
let foundMixin = mixins.find(m => m.name === mixin.name);
108+
if(foundMixin) {
109+
tree.push(foundMixin);
110+
111+
while(h.has(foundMixin?.mixins)) {
112+
foundMixin?.mixins?.forEach(mixin => {
113+
foundMixin = mixins.find(m => m.name === mixin.name);
114+
if(foundMixin) {
115+
tree.push(foundMixin);
116+
}
117+
});
118+
}
119+
}
102120
});
103-
104-
while (this._classes.has(klass.superclass.name)) {
105-
const newKlass = this._classes.get(klass.superclass.name);
106-
107-
newKlass?.mixins?.forEach((mixin) => {
108-
tree.push(this._mixins.get(mixin.name));
121+
122+
while(this._classLikes.has(klass?.superclass?.name)) {
123+
const newKlass = this._classLikes.get(klass.superclass.name);
124+
125+
klass?.mixins?.forEach(mixin => {
126+
let foundMixin = mixins.find(m => m.name === mixin.name);
127+
if(foundMixin) {
128+
tree.push(foundMixin);
129+
130+
while(h.has(foundMixin?.mixins)) {
131+
foundMixin?.mixins?.forEach(mixin => {
132+
foundMixin = mixins.find(m => m.name === mixin.name);
133+
if(foundMixin) {
134+
tree.push(foundMixin);
135+
}
136+
});
137+
}
138+
}
109139
});
110140

111141
tree.push(newKlass);
112142
klass = newKlass;
113143
}
114-
115-
return tree;
116-
}
144+
145+
return [...new Map(tree.map(item => [item.name, item])).values()];
146+
}
117147
return [];
148+
118149
}
119150

120151
getModuleForClass(className) {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"schemaVersion": "0.1.0",
3+
"readme": "",
4+
"modules": [
5+
{
6+
"kind": "javascript-module",
7+
"path": "fixtures/-default/package/bar.js",
8+
"declarations": [
9+
{
10+
"kind": "class",
11+
"description": "",
12+
"name": "MyElement",
13+
"superclass": {
14+
"name": "HTMLElement"
15+
},
16+
"customElement": true,
17+
"tagName": "my-element"
18+
},
19+
{
20+
"kind": "class",
21+
"description": "",
22+
"name": "MyElement2",
23+
"superclass": {
24+
"name": "HTMLElement"
25+
},
26+
"customElement": true,
27+
"tagName": "my-element2"
28+
}
29+
],
30+
"exports": [
31+
{
32+
"kind": "custom-element-definition",
33+
"name": "my-element",
34+
"declaration": {
35+
"name": "MyElement",
36+
"module": "fixtures/-default/package/bar.js"
37+
}
38+
},
39+
{
40+
"kind": "custom-element-definition",
41+
"name": "my-element2",
42+
"declaration": {
43+
"name": "MyElement2",
44+
"module": "fixtures/-default/package/bar.js"
45+
}
46+
}
47+
]
48+
}
49+
]
50+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
{
2+
"schemaVersion": "0.1.0",
3+
"readme": "",
4+
"modules": [
5+
{
6+
"kind": "javascript-module",
7+
"path": "fixtures/-default/package/bar.js",
8+
"declarations": [
9+
{
10+
"kind": "mixin",
11+
"description": "",
12+
"name": "DedupeMixin",
13+
"parameters": [
14+
{
15+
"name": "klass"
16+
}
17+
]
18+
},
19+
{
20+
"kind": "mixin",
21+
"description": "",
22+
"name": "LocalizeMixin",
23+
"mixins": [
24+
{
25+
"name": "DedupeMixin",
26+
"module": "fixtures/-default/package/bar.js"
27+
}
28+
],
29+
"parameters": [
30+
{
31+
"name": "klass"
32+
}
33+
]
34+
},
35+
{
36+
"kind": "class",
37+
"description": "",
38+
"name": "LitElement",
39+
"superclass": {
40+
"name": "UpdatingElement"
41+
}
42+
},
43+
{
44+
"kind": "class",
45+
"description": "",
46+
"name": "MyComponent",
47+
"mixins": [
48+
{
49+
"name": "LocalizeMixin",
50+
"module": "fixtures/-default/package/bar.js"
51+
}
52+
],
53+
"superclass": {
54+
"name": "LitElement"
55+
},
56+
"customElement": true
57+
}
58+
],
59+
"exports": [
60+
{
61+
"kind": "js",
62+
"name": "DedupeMixin",
63+
"declaration": {
64+
"name": "DedupeMixin",
65+
"module": "fixtures/-default/package/bar.js"
66+
}
67+
},
68+
{
69+
"kind": "js",
70+
"name": "LocalizeMixin",
71+
"declaration": {
72+
"name": "LocalizeMixin",
73+
"module": "fixtures/-default/package/bar.js"
74+
}
75+
},
76+
{
77+
"kind": "js",
78+
"name": "LitElement",
79+
"declaration": {
80+
"name": "LitElement",
81+
"module": "fixtures/-default/package/bar.js"
82+
}
83+
},
84+
{
85+
"kind": "js",
86+
"name": "MyComponent",
87+
"declaration": {
88+
"name": "MyComponent",
89+
"module": "fixtures/-default/package/bar.js"
90+
}
91+
}
92+
]
93+
}
94+
]
95+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"schemaVersion": "0.1.0",
3+
"readme": "",
4+
"modules": [
5+
{
6+
"kind": "javascript-module",
7+
"path": "fixtures/-default/package/bar.js",
8+
"declarations": [
9+
{
10+
"kind": "class",
11+
"description": "",
12+
"name": "UpdatingElement",
13+
"superclass": {
14+
"name": "HTMLElement"
15+
},
16+
"customElement": true
17+
},
18+
{
19+
"kind": "class",
20+
"description": "",
21+
"name": "LitElement",
22+
"superclass": {
23+
"name": "UpdatingElement"
24+
}
25+
},
26+
{
27+
"kind": "class",
28+
"description": "",
29+
"name": "MyComponent",
30+
"superclass": {
31+
"name": "LitElement"
32+
},
33+
"customElement": true
34+
}
35+
],
36+
"exports": [
37+
{
38+
"kind": "js",
39+
"name": "UpdatingElement",
40+
"declaration": {
41+
"name": "UpdatingElement",
42+
"module": "fixtures/-default/package/bar.js"
43+
}
44+
},
45+
{
46+
"kind": "js",
47+
"name": "LitElement",
48+
"declaration": {
49+
"name": "LitElement",
50+
"module": "fixtures/-default/package/bar.js"
51+
}
52+
},
53+
{
54+
"kind": "js",
55+
"name": "MyComponent",
56+
"declaration": {
57+
"name": "MyComponent",
58+
"module": "fixtures/-default/package/bar.js"
59+
}
60+
}
61+
]
62+
}
63+
]
64+
}

packages/helpers/test/index.test.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import fs from 'fs';
2+
import { test } from 'uvu';
3+
import { expect } from 'chai';
4+
import { CustomElementsJson } from '../index.js';
5+
6+
7+
const classes = JSON.parse(fs.readFileSync('test/fixtures/classes.json').toString())
8+
const inheritanceSuperclass = JSON.parse(fs.readFileSync('test/fixtures/inheritance_superclass.json').toString())
9+
const inheritanceMixinsSuperclass = JSON.parse(fs.readFileSync('test/fixtures/inheritance_mixins_superclass.json').toString())
10+
11+
12+
test('getByTagName', () => {
13+
const customElementsJson = new CustomElementsJson(classes);
14+
expect(customElementsJson.getByTagName('my-element').name).to.equal('MyElement');
15+
expect(customElementsJson.getByTagName('my-element').tagName).to.equal('my-element');
16+
});
17+
18+
19+
test('getByClassName', () => {
20+
const customElementsJson = new CustomElementsJson(classes);
21+
expect(customElementsJson.getByClassName('MyElement').name).to.equal('MyElement');
22+
expect(customElementsJson.getByClassName('MyElement').tagName).to.equal('my-element');
23+
});
24+
25+
26+
27+
test('getClasses - gets all classes', () => {
28+
const customElementsJson = new CustomElementsJson(classes);
29+
expect(customElementsJson.getClasses().length).to.equal(2);
30+
});
31+
32+
test('getDefinitions - gets all definitions', () => {
33+
const customElementsJson = new CustomElementsJson(classes);
34+
expect(customElementsJson.getDefinitions().length).to.equal(2);
35+
});
36+
37+
38+
test('getMixins - gets all mixins', () => {
39+
const customElementsJson = new CustomElementsJson(inheritanceMixinsSuperclass);
40+
expect(customElementsJson.getMixins().length).to.equal(2);
41+
});
42+
43+
44+
test('getInheritanceTree - gets all superclasses', () => {
45+
const customElementsJson = new CustomElementsJson(inheritanceSuperclass);
46+
const result = customElementsJson.getInheritanceTree('MyComponent');
47+
48+
expect(result.length).to.equal(3);
49+
expect(result[0].name).to.equal('MyComponent');
50+
expect(result[1].name).to.equal('LitElement');
51+
expect(result[2].name).to.equal('UpdatingElement');
52+
});
53+
54+
test('getInheritanceTree - gets all superclasses and mixins', () => {
55+
const customElementsJson = new CustomElementsJson(inheritanceMixinsSuperclass);
56+
const result = customElementsJson.getInheritanceTree('MyComponent');
57+
58+
expect(result.length).to.equal(4);
59+
expect(result[0].name).to.equal('MyComponent');
60+
expect(result[1].name).to.equal('LocalizeMixin');
61+
expect(result[2].name).to.equal('DedupeMixin');
62+
expect(result[3].name).to.equal('LitElement');
63+
});
64+
65+
test('getInheritanceTree - returns empty array if class not found', () => {
66+
const customElementsJson = new CustomElementsJson(inheritanceMixinsSuperclass);
67+
expect(customElementsJson.getInheritanceTree('AsdfAsdf').length).to.equal(0);
68+
});
69+
70+
71+
test.run();

0 commit comments

Comments
 (0)