Skip to content

Commit

Permalink
refactor(element): rewrite package to use decorators (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lodin committed Jul 27, 2018
1 parent 9b1d724 commit 2fea723
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 440 deletions.
32 changes: 6 additions & 26 deletions packages/element/__tests__/lifecycle/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import uuid from "uuid/v4";
import {defineAndMount} from "../../../../test/utils";
import CorpusculeElement, {
deriveStateFromProps,
PropertyDescriptorMap,
propertyMap,
property,
render,
shouldUpdate,
StateDescriptorMap, stateMap
shouldUpdate, state,
} from "../../src";

const methods = () => {
Expand All @@ -21,16 +19,11 @@ const methods = () => {
class Test extends CorpusculeElement {
public static is: string = `x-${uuid()}`;

public static get [propertyMap](): PropertyDescriptorMap<Test> {
return {
num: null,
};
}

public static [shouldUpdate](): boolean {
return false;
}

@property()
public num: number = 1;

protected [render](): TemplateResult {
Expand All @@ -57,16 +50,11 @@ const methods = () => {
class Test extends CorpusculeElement {
public static is: string = `x-${uuid()}`;

public static get [propertyMap](): PropertyDescriptorMap<Test> {
return {
num: null,
};
}

public static [shouldUpdate](): boolean {
return false;
}

@property()
public num: number = 1;

protected [render](): TemplateResult {
Expand All @@ -89,24 +77,16 @@ const methods = () => {
class Test extends CorpusculeElement {
public static is: string = `x-${uuid()}`;

public static get [propertyMap](): PropertyDescriptorMap<Test> {
return {
prop: null,
};
}

public static get [stateMap](): StateDescriptorMap<Test> {
return ["state"];
}

public static [deriveStateFromProps]({prop: nextProp}: any, {prop: prevProp}: any): any {
return {
state: nextProp < prevProp,
};
}

@property()
public prop: number = 1;

@state
public state: boolean = false;

protected [render](): TemplateResult {
Expand Down
62 changes: 9 additions & 53 deletions packages/element/__tests__/observableFields/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,15 @@ import {html} from "lit-html/lib/lit-extended";
// tslint:disable-next-line:no-implicit-dependencies
import uuid from "uuid/v4";
import {defineAndMount} from "../../../../test/utils";
import CorpusculeElement, {
AttributeDescriptorMap,
attributeMap,
render,
} from "../../src";
import CorpusculeElement, {attribute, render} from "../../src";

const attributes = () => {
describe("attributes", () => {
it("should update on attribute change", async () => {
class Test extends CorpusculeElement {
public static is: string = `x-${uuid()}`;

public static get [attributeMap](): AttributeDescriptorMap<Test> {
return {
index: ["idx", Number, {pure: true}],
};
}

@attribute("idx", Number, {pure: true})
public index?: number;

protected [render](): TemplateResult {
Expand Down Expand Up @@ -50,12 +41,7 @@ const attributes = () => {
class Test extends CorpusculeElement {
public static is: string = `x-${uuid()}`;

public static get [attributeMap](): AttributeDescriptorMap<Test> {
return {
index: ["idx", Number],
};
}

@attribute("idx", Number)
public index: number = 2;

protected [render](): TemplateResult {
Expand All @@ -76,12 +62,7 @@ const attributes = () => {
class Test extends CorpusculeElement {
public static is: string = `x-${uuid()}`;

public static get [attributeMap](): AttributeDescriptorMap<Test> {
return {
index: ["idx", Number],
};
}

@attribute("idx", Number)
public index: number = 1;

protected [render](): TemplateResult {
Expand All @@ -102,12 +83,7 @@ const attributes = () => {
class Test extends CorpusculeElement {
public static is: string = `x-${uuid()}`;

public static get [attributeMap](): AttributeDescriptorMap<Test> {
return {
index: ["idx", Number],
};
}

@attribute("idx", Number)
public index: number = 1;

protected [render](): TemplateResult {
Expand All @@ -130,12 +106,7 @@ const attributes = () => {
class Test extends CorpusculeElement {
public static is: string = `x-${uuid()}`;

public static get [attributeMap](): AttributeDescriptorMap<Test> {
return {
index: ["idx", Number],
};
}

@attribute("idx", Number)
public index: number = 1;

protected [render](): TemplateResult {
Expand All @@ -160,12 +131,7 @@ const attributes = () => {
class Test extends CorpusculeElement {
public static is: string = `x-${uuid()}`;

public static get [attributeMap](): AttributeDescriptorMap<Test> {
return {
index: ["idx", Number, {pure: false}],
};
}

@attribute("idx", Number, {pure: false})
public index: number = 1;

protected [render](): TemplateResult {
Expand All @@ -188,12 +154,7 @@ const attributes = () => {
class Test extends CorpusculeElement {
public static is: string = `x-${uuid()}`;

public static get [attributeMap](): AttributeDescriptorMap<Test> {
return {
has: ["has", Boolean],
};
}

@attribute("has", Boolean)
public has: boolean = false;

protected [render](): TemplateResult {
Expand Down Expand Up @@ -221,12 +182,7 @@ const attributes = () => {
class Test extends CorpusculeElement {
public static is: string = `x-${uuid()}`;

public static get [attributeMap](): AttributeDescriptorMap<Test> {
return {
index: ["idx", Number],
};
}

@attribute("idx", Number)
public index: number = 1;

protected [render](): TemplateResult {
Expand Down
88 changes: 19 additions & 69 deletions packages/element/__tests__/observableFields/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,20 @@ import {html} from "lit-html/lib/lit-extended";
// tslint:disable-next-line:no-implicit-dependencies
import uuid from "uuid/v4";
import {defineAndMount} from "../../../../test/utils";
import CorpusculeElement, {
ComputedDescriptorMap,
computedMap,
render,
} from "../../src";
import CorpusculeElement, {computed, render} from "../../src";

const computed = () => {
const testComputed = () => {
describe("computed", () => {
it("should memoize result of processed getter", async () => {
const spy = jasmine.createSpy("OnComputed");

class Test extends CorpusculeElement {
public static is: string = `x-${uuid()}`;

public static get [computedMap](): ComputedDescriptorMap<Test> {
return {
comp: ["first", "second"],
};
}

public first: number = 1;

public second: number = 2;

@computed("first", "second")
public get comp(): number {
spy();

Expand All @@ -54,16 +44,10 @@ const computed = () => {
class Test extends CorpusculeElement {
public static is: string = `x-${uuid()}`;

public static get [computedMap](): ComputedDescriptorMap<Test> {
return {
comp: ["first", "second"],
};
}

public first: number = 1;

public second: number = 2;

@computed("first", "second")
public get comp(): number {
spy();

Expand All @@ -90,68 +74,34 @@ const computed = () => {
expect(spy).toHaveBeenCalledTimes(2);
});

it("should throw an error if computed variable is not defined", () => {
class Test extends CorpusculeElement {
public static is: string = `x-${uuid()}`;

public static get [computedMap](): ComputedDescriptorMap {
return {
comp: ["first", "second"],
};
}

public first: number = 1;

public second: number = 2;

protected [render](): TemplateResult {
return html`<span id="node">Test content</span>`;
}
}

expect(() => defineAndMount(Test))
.toThrowError("Property \"comp\" is not defined or is not a getter");
});

it("should throw an error if computed variable is not a getter", () => {
class Test extends CorpusculeElement {
public static is: string = `x-${uuid()}`;
expect(() => {
class Test extends CorpusculeElement { // tslint:disable-line:no-unused-variable
public static is: string = `x-${uuid()}`;

public static get [computedMap](): ComputedDescriptorMap<Test> {
return {
comp: ["first", "second"],
};
}
public first: number = 1;
public second: number = 2;

public first: number = 1;

public second: number = 2;
@computed("first", "second")
public comp(): number {
return this.first + this.second;
}

public comp(): number {
return this.first + this.second;
}

protected [render](): TemplateResult {
return html`<span id="node">Test content</span>`;
protected [render](): TemplateResult {
return html`<span id="node">Test content</span>`;
}
}
}

expect(() => defineAndMount(Test))
})
.toThrowError("Property \"comp\" is not defined or is not a getter");
});

it("should allow to define property in any place of prototype chain", async () => {
class Parent extends CorpusculeElement {
public static is: string = `x-${uuid()}`;

public static get [computedMap](): ComputedDescriptorMap<Parent> {
return {
comp: ["first"],
};
}

public first: number = 1;

@computed("first")
public get comp(): number {
return this.first + 2;
}
Expand All @@ -172,4 +122,4 @@ const computed = () => {
});
};

export default computed;
export default testComputed;

0 comments on commit 2fea723

Please sign in to comment.