Skip to content

Commit

Permalink
_syncAttrs on constructor and connectedCallback (#43)
Browse files Browse the repository at this point in the history
* _syncAttrs on constructor and connectedCallback

* Tests

* spelling

* remove _syncAttrs on connectedCallback

* fix server test
  • Loading branch information
nojvek authored Sep 10, 2018
1 parent 7119b97 commit d56af56
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
17 changes: 15 additions & 2 deletions lib/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ class Component extends WebComponent {
super();

this.panelID = cuid();

this.attrs = {};
this._syncAttrs(); // constructor sync ensures default properties are present on this.attrs

this._config = Object.assign({}, {
css: ``,
helpers: {},
Expand All @@ -214,8 +218,8 @@ class Component extends WebComponent {
// appState and isStateShared of child components will be overwritten by parent/root
// when the component is connected to the hierarchy
this.state = {};
this.attrs = {};
this.appState = this.getConfig(`appState`);

if (!this.appState) {
this.appState = {};
this.isStateShared = true;
Expand Down Expand Up @@ -288,7 +292,6 @@ class Component extends WebComponent {
);

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

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

/**
* Syncs element attributes defined in attrsSchema with this.attrs
*/
_syncAttrs() {
for (const attr of Object.keys(this.constructor.attrsSchema)) {
this._updateAttr(attr);
}
return this.attrs;
}

/**
* Parses html attribute using type information from attrsSchema and updates this.attrs
* @param {string} attr - attribute name
Expand Down
12 changes: 12 additions & 0 deletions test/browser/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,18 @@ describe(`Simple Component instance with attrsSchema`, function() {
`json-attr`,
]);
});

it(`has default attrs present after createElement`, function() {
el = document.createElement(`attrs-reflection-app`);
expect(el.attrs).to.deep.equal({
'str-attr': `placeholder`,
'bool-attr': false,
'number-attr': 0,
'json-attr': null,
});
// _config is initialised in constructor. defaultState should be able to access el.attrs
expect(el._config.defaultState.str).to.equal(`placeholder`);
});
});

describe(`Nested Component instance`, function() {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/attrs-reflection-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export class AttrsReflectionApp extends Component {
template: scope => h(`div`, {class: {'attrs-reflection-app': true}},
Object.entries(scope.$attrs).map(([attr, val]) => h(`p`, `${attr}: ${JSON.stringify(val)}`)),
),
defaultState: {
str: this.attrs[`str-attr`],
},
};
}
}
3 changes: 1 addition & 2 deletions test/server/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ describe(`Server-side component renderer`, function() {
});

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

0 comments on commit d56af56

Please sign in to comment.