Skip to content

Commit

Permalink
More explicit types
Browse files Browse the repository at this point in the history
  • Loading branch information
Noah Bogart committed Jan 19, 2021
1 parent 11cbe7a commit 9be41c7
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 81 deletions.
4 changes: 2 additions & 2 deletions lib/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {Callback} from "./callback";

export class Assembler<T> {
attributeAssigner: AttributeAssigner<T>;
callbacks: Callback[];
callbacks: Callback<T>[];

constructor(attributeAssigner: AttributeAssigner<T>, callbacks: Callback[]) {
constructor(attributeAssigner: AttributeAssigner<T>, callbacks: Callback<T>[]) {
this.attributeAssigner = attributeAssigner;
this.callbacks = callbacks;
}
Expand Down
12 changes: 6 additions & 6 deletions lib/callback-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,36 @@ import {FixtureRiveter} from "./fixture-riveter";
import {isFunction} from "lodash";

export class CallbackHandler {
callbacks: Callback[];
callbacks: Callback<any>[];
fixtureRiveter: FixtureRiveter;

constructor(fixtureRiveter: FixtureRiveter) {
this.callbacks = [];
this.fixtureRiveter = fixtureRiveter;
}

addCallback(names: string[], block: callbackFunction): void {
addCallback<T>(names: string[], block: callbackFunction<T>): void {
for (const name of names) {
this.callbacks.push(new Callback(this.fixtureRiveter, name, block));
}
}

before(...rest: any[]): void {
before<T>(...rest: any[]): void {
const block = extractCallbackFunction(rest);
const names = rest.map((n: string) => {
const string = n.charAt(0).toUpperCase() + n.slice(1);
return `before${string}`;
});
this.addCallback(names, block);
this.addCallback<T>(names, block);
}

after(...rest: any[]): void {
after<T>(...rest: any[]): void {
const block = extractCallbackFunction(rest);
const names = rest.map((n: string) => {
const string = n.charAt(0).toUpperCase() + n.slice(1);
return `after${string}`;
});
this.addCallback(names, block);
this.addCallback<T>(names, block);
}
}

Expand Down
10 changes: 5 additions & 5 deletions lib/callback.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import {Evaluator} from "./evaluator";
import {FixtureRiveter} from "./fixture-riveter";

export type callbackFunction = (instance: any, evaluator: Evaluator) => any;
export type callbackFunction<T> = (instance: T, evaluator: Evaluator) => any;

export class Callback {
export class Callback<T> {
fixtureRiveter: FixtureRiveter;
name: string;
block: callbackFunction;
block: callbackFunction<T>;

constructor(fixtureRiveter: FixtureRiveter, name: string, block: callbackFunction) {
constructor(fixtureRiveter: FixtureRiveter, name: string, block: callbackFunction<T>) {
this.fixtureRiveter = fixtureRiveter;
this.name = name;
this.block = block;
}

async run(instance: any, evaluator: Evaluator): Promise<any> {
async run(instance: T, evaluator: Evaluator): Promise<any> {
return this.block.call(this.fixtureRiveter, instance, evaluator);
}
}
13 changes: 6 additions & 7 deletions lib/definition-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,16 @@ export class DefinitionProxy<T> {
block(addMethodMissing(proxy));
}

before(name: string, name2: string, name3: string, block: callbackFunction): void;
before(name: string, name2: string, block: callbackFunction): void;
before(name: string, block: callbackFunction): void;
before(name: string, block: callbackFunction<T>): void;
before(name: string, name2: string, block: callbackFunction<T>): void;
before(name: string, name2: string, name3: string, block: callbackFunction<T>): void;
before(...rest: any[]): void {
this.definition.before(...rest);
}

after(name: string, name2: string, name3: string, block: callbackFunction): void;
after(name: string, name2: string, block: callbackFunction): void;
after(name: string, block: callbackFunction): void;
after(name: string, ...block: any[]): void;
after(name: string, block: callbackFunction<T>): void;
after(name: string, name2: string, block: callbackFunction<T>): void;
after(name: string, name2: string, name3: string, block: callbackFunction<T>): void;
after(...rest: any[]): void {
this.definition.after(...rest);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ export class Definition<T> {
this.callbackHandler.after(...rest);
}

addCallback(names: string[], block: callbackFunction): void {
addCallback(names: string[], block: callbackFunction<T>): void {
this.callbackHandler.addCallback(names, block);
}

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

return [
Expand Down
13 changes: 10 additions & 3 deletions lib/fixture-options-parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {DefinitionProxy} from "./definition-proxy";
import {Evaluator} from "./evaluator";
import {Instance} from "./fixture-riveter";

import {first, isFunction, isPlainObject, last} from "lodash";

Expand All @@ -9,11 +10,17 @@ export interface FixtureOptions {
parent?: string;
}

type ConvertToFn<T, FN = () => Promise<any>> = {
[P in keyof T]: (fn: FN) => Promise<T[P]>;
type ConvertToFn<T, FN = () => any> = {
[P in keyof T]-?: <Q = undefined>(
fn: FN,
overrides?: Partial<Q extends undefined ? T extends Instance ? T : Instance : Q>,
) => Promise<T[P]>;
};

type EvaluatorFn<T> = (evaluator: ConvertToFn<T> & Evaluator) => Promise<any> | any;
type EvaluatorFn<T> =
((evaluator: ConvertToFn<T> & Evaluator) => Promise<any> | any) |
string[] |
Partial<T extends Instance ? T : Instance>;

export type AttrFunction<T> = (f: ConvertToFn<T, EvaluatorFn<T>> & Evaluator) => Promise<any> | any;

Expand Down
20 changes: 10 additions & 10 deletions lib/fixture-riveter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class FixtureRiveter {
return fixture;
}

registerFixture(fixture: Fixture<any>): void {
registerFixture<T>(fixture: Fixture<T>): void {
for (const name of fixture.names()) {
this.fixtures[name] = fixture;
}
Expand Down Expand Up @@ -201,25 +201,25 @@ export class FixtureRiveter {
this.strategyHandler.registerStrategy(strategyName, strategyClass);
}

before(name: string, name2: string, name3: string, block: callbackFunction): void;
before(name: string, name2: string, block: callbackFunction): void;
before(name: string, block?: callbackFunction): void;
before<T>(name: string, block?: callbackFunction<T>): void;
before<T>(name: string, name2: string, block: callbackFunction<T>): void;
before<T>(name: string, name2: string, name3: string, block: callbackFunction<T>): void;
before(...rest: any[]): void {
this.callbackHandler.before(...rest);
}

after(name: string, name2: string, name3: string, block: callbackFunction): void;
after(name: string, name2: string, block: callbackFunction): void;
after(name: string, block?: callbackFunction): void;
after<T>(name: string, block?: callbackFunction<T>): void;
after<T>(name: string, name2: string, block: callbackFunction<T>): void;
after<T>(name: string, name2: string, name3: string, block: callbackFunction<T>): void;
after(...rest: any[]): void {
this.callbackHandler.after(...rest);
}

addCallback(names: string[], block: callbackFunction): void {
addCallback<T>(names: string[], block: callbackFunction<T>): void {
this.callbackHandler.addCallback(names, block);
}

getCallbacks(): Callback[] {
getCallbacks<T>(): Callback<T>[] {
return this.callbackHandler.callbacks;
}

Expand Down Expand Up @@ -380,7 +380,7 @@ export class FixtureRiveter {
/* eslint-enable */
}

type Instance = Record<string, any>;
export type Instance = Record<string, any>;

type Pair<T> = [T, T];
type List<T> = T[];
Expand Down
2 changes: 1 addition & 1 deletion lib/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class Fixture<T> extends Definition<T> {
return attributesToKeep.concat(this.attributes);
}

getCallbacks(): Callback[] {
getCallbacks(): Callback<T>[] {
const globalCallbacks = this.fixtureRiveter.getCallbacks();
const parentCallbacks = this.parentFixture().getCallbacks();
const definedCallbacks = super.getCallbacks();
Expand Down
2 changes: 1 addition & 1 deletion lib/null-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class NullFixture<T> extends Definition<T> {
return [];
}

getCallbacks(): Callback[] {
getCallbacks(): Callback<T>[] {
return [];
}
}
34 changes: 28 additions & 6 deletions test/acceptance/associations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe("simple associations", function() {
id: number;
name: string;
age: number;
admin: boolean;
}

class Post extends ObjectionModel {
Expand All @@ -38,6 +39,7 @@ describe("simple associations", function() {
await createTable(User, {
name: "string",
age: "integer",
admin: "boolean",
});

await createTable(Post, {
Expand All @@ -48,13 +50,26 @@ describe("simple associations", function() {
fr = new FixtureRiveter();
fr.setAdapter(new ObjectionAdapter());

fr.fixture("user", User, (f: any) => {
fr.fixture("user", User, (f) => {
f.attr("name", () => "Noah");
f.attr("age", () => 32);

f.trait("admin", (t) => {
t.admin(() => true);
});
});
fr.fixture("post", Post, (f: any) => {

fr.fixture("post", Post, (f) => {
f.association("user");
f.attr("body", () => "Post body");

f.trait("admin", (t) => {
t.user(["admin"]);
});

f.trait("old admin", (t) => {
t.user<User>(["admin"], {age: 100});
});
});
});

Expand Down Expand Up @@ -86,6 +101,13 @@ describe("simple associations", function() {
expect(model.id).to.equal(post.userId);
expect(model.id).to.equal(post.user.id);
});

it("can handle traits and overrides", async function() {
const post = await fr.build("post", ["old admin"]);

expect(post.user.age).to.equal(100);
expect(post.user.admin).to.be.true;
});
});

describe("Complex associations", function() {
Expand Down Expand Up @@ -149,20 +171,20 @@ describe("Complex associations", function() {
fr = new FixtureRiveter();
fr.setAdapter(new ObjectionAdapter());

fr.fixture("user", User, (f: any) => {
fr.fixture("user", User, (f) => {
f.attr("name", () => "Noah");
f.fixture("userWithPosts", User, (ff: any) => {
f.fixture("userWithPosts", User, (ff) => {
ff.transient((t) => {
t.attr("postCount", () => 5);
});

ff.after("create", async(user, evaluator) => {
const posts = await fr.createList(
const posts = await fr.createList<Post>(
"post",
await evaluator.attr("postCount"),
{user},
);
await user.$appendRelated("posts", posts);
user.$appendRelated("posts", posts);
});
});
});
Expand Down

0 comments on commit 9be41c7

Please sign in to comment.