Skip to content

Commit

Permalink
test: .defaults().defaults() (#58)
Browse files Browse the repository at this point in the history
* test: `.defaults().defaults()`

* refactor: simplify constructor implementation
  • Loading branch information
gr2m authored Jul 8, 2021
1 parent bd9d789 commit 50341d7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ export class Base {
}
static defaults(defaults) {
return class extends this {
constructor(...args) {
super(Object.assign({}, defaults, args[0] || {}));
constructor(options) {
super({
...defaults,
...options,
});
}
};
}
Expand Down
7 changes: 7 additions & 0 deletions test/base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ test(".defaults({foo: 'bar'})", () => {
assert.equal(defaultsTest.options, { foo: "bar" });
assert.equal(mergedOptionsTest.options, { foo: "bar", baz: "daz" });
});
test(".defaults({one: 1}).defaults({two: 2})", () => {
const BaseWithDefaults = Base.defaults({ one: 1 }).defaults({ two: 2 });
const defaultsTest = new BaseWithDefaults();
const mergedOptionsTest = new BaseWithDefaults({ three: 3 });
assert.equal(defaultsTest.options, { one: 1, two: 2 });
assert.equal(mergedOptionsTest.options, { one: 1, two: 2, three: 3 });
});

test(".plugin().defaults()", () => {
const BaseWithPluginAndDefaults = Base.plugin(fooPlugin).defaults({
Expand Down

0 comments on commit 50341d7

Please sign in to comment.