Skip to content

Commit d56af56

Browse files
authored
_syncAttrs on constructor and connectedCallback (#43)
* _syncAttrs on constructor and connectedCallback * Tests * spelling * remove _syncAttrs on connectedCallback * fix server test
1 parent 7119b97 commit d56af56

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

Diff for: lib/component.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ class Component extends WebComponent {
201201
super();
202202

203203
this.panelID = cuid();
204+
205+
this.attrs = {};
206+
this._syncAttrs(); // constructor sync ensures default properties are present on this.attrs
207+
204208
this._config = Object.assign({}, {
205209
css: ``,
206210
helpers: {},
@@ -214,8 +218,8 @@ class Component extends WebComponent {
214218
// appState and isStateShared of child components will be overwritten by parent/root
215219
// when the component is connected to the hierarchy
216220
this.state = {};
217-
this.attrs = {};
218221
this.appState = this.getConfig(`appState`);
222+
219223
if (!this.appState) {
220224
this.appState = {};
221225
this.isStateShared = true;
@@ -288,7 +292,6 @@ class Component extends WebComponent {
288292
);
289293

290294
Object.assign(this.state, newState);
291-
Object.keys(this.constructor.attrsSchema).forEach(attr => this._updateAttr(attr));
292295

293296
if (Object.keys(this.getConfig(`routes`)).length) {
294297
this.router = new Router(this, {historyMethod: this.historyMethod});
@@ -420,6 +423,16 @@ class Component extends WebComponent {
420423
return state;
421424
}
422425

426+
/**
427+
* Syncs element attributes defined in attrsSchema with this.attrs
428+
*/
429+
_syncAttrs() {
430+
for (const attr of Object.keys(this.constructor.attrsSchema)) {
431+
this._updateAttr(attr);
432+
}
433+
return this.attrs;
434+
}
435+
423436
/**
424437
* Parses html attribute using type information from attrsSchema and updates this.attrs
425438
* @param {string} attr - attribute name

Diff for: test/browser/component.js

+12
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,18 @@ describe(`Simple Component instance with attrsSchema`, function() {
265265
`json-attr`,
266266
]);
267267
});
268+
269+
it(`has default attrs present after createElement`, function() {
270+
el = document.createElement(`attrs-reflection-app`);
271+
expect(el.attrs).to.deep.equal({
272+
'str-attr': `placeholder`,
273+
'bool-attr': false,
274+
'number-attr': 0,
275+
'json-attr': null,
276+
});
277+
// _config is initialised in constructor. defaultState should be able to access el.attrs
278+
expect(el._config.defaultState.str).to.equal(`placeholder`);
279+
});
268280
});
269281

270282
describe(`Nested Component instance`, function() {

Diff for: test/fixtures/attrs-reflection-app.js

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export class AttrsReflectionApp extends Component {
1414
template: scope => h(`div`, {class: {'attrs-reflection-app': true}},
1515
Object.entries(scope.$attrs).map(([attr, val]) => h(`p`, `${attr}: ${JSON.stringify(val)}`)),
1616
),
17+
defaultState: {
18+
str: this.attrs[`str-attr`],
19+
},
1720
};
1821
}
1922
}

Diff for: test/server/component.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,7 @@ describe(`Server-side component renderer`, function() {
178178
});
179179

180180
it(`throws error if there is a malformed attrsSchema type`, function() {
181-
const el = new BadAttrsSchemaApp();
182-
expect(() => el.connectedCallback()).to.throw(
181+
expect(() => new BadAttrsSchemaApp()).to.throw(
183182
`Unknown type: bool for attr: bad-attr in attrsSchema. Only (string | boolean | number | json) supported.`
184183
);
185184
});

0 commit comments

Comments
 (0)