Skip to content

Commit

Permalink
Clean up getAttributes definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Noah Bogart committed Feb 16, 2021
1 parent 10c4c4c commit 1503cbb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 57 deletions.
24 changes: 11 additions & 13 deletions lib/definition.ts
Expand Up @@ -70,14 +70,10 @@ export class Definition<T> {
getAttributes(): Attribute[] {
this.compile();

if (this.attributes.length === 0) {
this.attributes = this.aggregateFromTraitsAndSelf(
"getAttributes",
() => this.declarationHandler.getAttributes(),
);
}

return this.attributes;
return this.aggregateFromTraitsAndSelf(
"getAttributes",
() => this.declarationHandler.getAttributes(),
);
}


Expand Down Expand Up @@ -136,23 +132,25 @@ export class Definition<T> {
].flat(2).filter(Boolean);
}

toBuild(): ((...args: any[]) => any)|undefined {
toBuild(): ((Model: any) => any) | undefined {
return last(this.aggregateFromTraitsAndSelf("toBuild", () => this._toBuild));
}

toSave(): ((...args: any[]) => any)|undefined {
toSave(): ((instance: any, Model?: any) => Promise<any>) | undefined {
return last(this.aggregateFromTraitsAndSelf("toSave", () => this._toSave));
}

toDestroy(): ((...args: any[]) => any)|undefined {
toDestroy(): ((instance: any, Model?: any) => Promise<any>) | undefined {
return last(this.aggregateFromTraitsAndSelf("toDestroy", () => this._toDestroy));
}

toRelate(): ((...args: any[]) => any)|undefined {
toRelate(): (
(instance: any, name: string, other: any, Model?: any) => Promise<any> | any
) | undefined {
return last(this.aggregateFromTraitsAndSelf("toRelate", () => this._toRelate));
}

toSet(): ((...args: any[]) => any)|undefined {
toSet(): ((instance: any, key: string, value: any) => Promise<any> | any) | undefined {
return last(this.aggregateFromTraitsAndSelf("toSet", () => this._toSet));
}
}
20 changes: 3 additions & 17 deletions lib/fixture.ts
Expand Up @@ -108,29 +108,15 @@ export class Fixture<T> extends Definition<T> {

getAttributes(): Attribute[] {
this.compile();

const parentAttributes = this.parentFixture().getAttributes();

if (!this.attributes || this.attributes.length === 0) {
this.attributes = this.aggregateFromTraitsAndSelf(
"getAttributes",
() => this.declarationHandler.getAttributes(),
);
}

return parentAttributes.concat(this.attributes);
const definedAttributes = super.getAttributes();
return parentAttributes.concat(definedAttributes);
}

getCallbacks(): Callback<T>[] {
this.compile();

const parentCallbacks = this.parentFixture().getCallbacks();

const definedCallbacks = this.aggregateFromTraitsAndSelf(
"getCallbacks",
() => this.callbackHandler.callbacks,
);

const definedCallbacks = super.getCallbacks();
return parentCallbacks.concat(definedCallbacks);
}

Expand Down
10 changes: 0 additions & 10 deletions lib/trait.ts
Expand Up @@ -43,14 +43,4 @@ export class Trait<T> extends Definition<T> {
}
return this.fixtureRiveter.getTrait(name);
}

getAttributes(): Attribute[] {
this.compile();

return [
this.getBaseTraits().map((t) => t.getAttributes()),
this.declarationHandler.getAttributes(),
this.getAdditionalTraits().map((t) => t.getAttributes()),
].flat(2).filter(Boolean);
}
}
18 changes: 1 addition & 17 deletions test/unit/fixture.ts
Expand Up @@ -137,22 +137,6 @@ describe("Fixture", function() {
});

describe("#getAttributes", function() {
beforeEach(function() {
fixtureRiveter = new FixtureRiveter();
});

it("concats child attributes to parent attributes", function() {
const fixture = new Fixture(fixtureRiveter, "dummy", DummyModel);
const childAttr = new DynamicAttribute("attr", false, () => true);
fixture.attributes = [childAttr];
fixture.compiled = true;

const parentAttr = new DynamicAttribute("parent", false, () => true);
sinon.stub(fixture, "parentFixture")
.returns({getAttributes: () => [parentAttr]} as any);

const result = fixture.getAttributes();
expect(result).to.deep.equal([parentAttr, childAttr]);
});
it("concats child attributes to parent attributes");
});
});

0 comments on commit 1503cbb

Please sign in to comment.