From 4ff9d81f3e47fc217232ee8b7d1d23d8c162a1b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Estrella?= Date: Wed, 29 Aug 2018 12:42:42 -0500 Subject: [PATCH] Add types to factory-girl --- types/factory-girl/factory-girl-tests.ts | 94 +++++++++++++++++++++++ types/factory-girl/index.d.ts | 95 ++++++++++++++++++++++++ types/factory-girl/tsconfig.json | 23 ++++++ types/factory-girl/tslint.json | 1 + 4 files changed, 213 insertions(+) create mode 100644 types/factory-girl/factory-girl-tests.ts create mode 100644 types/factory-girl/index.d.ts create mode 100644 types/factory-girl/tsconfig.json create mode 100644 types/factory-girl/tslint.json diff --git a/types/factory-girl/factory-girl-tests.ts b/types/factory-girl/factory-girl-tests.ts new file mode 100644 index 0000000000..e996df56af --- /dev/null +++ b/types/factory-girl/factory-girl-tests.ts @@ -0,0 +1,94 @@ +import * as factory from "factory-girl"; + +interface User { + username?: string; + score?: number; + email?: string; + roles?: Role[]; + creditCard?: any; + boss?: User; +} + +interface Role { + id?: number; + name?: string; +} + +interface SuperUser extends User { + superpower: string; +} + +// Testing setAdapter +factory.setAdapter("my-adapter", "my-adapter-name"); + +// Testing define with seq, assoc, assocAttrs, assocMany +factory.define("user", {}, { + username: "Bob", + score: factory.seq("User.score", score => score + 1), + email: factory.seq("User.email", num => `email-${1}@users.com`), + roles: factory.assocMany("Role", 3, "id"), + creditCard: factory.assocAttrs("CreditCard", "creditCard", { number: "1234" }), + boss: factory.assoc("User", "boss") +}, { + afterBuild: (model, attrs, options) => {}, + afterCreate: (model, attrs, options) => {} +}); + +// Testing extend, with and without options +factory.extend("user", "superuser", { superpower: "flight" }); + +factory.extend("user", "superuser", { superpower: "flight" }, { + afterBuild: (model, attrs, options) => {}, + afterCreate: (model, attrs, options) => {} +}); + +// Testing attrs, with and without attributes +factory.attrs("user").then(attrs => null); +factory.attrs("user", { score: 10 }).then(attrs => null); + +// Testing attrsMany, with and without attributes +factory.attrsMany("user", 2).then(attrs => null); +factory.attrsMany("user", 2, [{ score: 10 }]).then(attrs => null); + +// Testing build, with and without attributes +factory.build("user").then(user => user.username); +factory.build("user", { score: 10 }).then(user => user.username); + +// Testing buildMany, with and without attributes and options +factory.buildMany("user", 3) + .then(users => users.map(user => user.username)); + +factory.buildMany("user", 3, { username: "John McClane" }) + .then(users => users.map(user => user.username)); + +// Testing buildMany with a list of attributes +factory.buildMany("user", [ + { username: "Jake Blues" }, + { username: "Elwood Blues" } +]).then(users => users.map(user => user.username)); + +// Testing create, with and without attributes +factory.create("user").then(user => user.username); +factory.create("user", { score: 10 }).then(user => user.username); + +// Testing createMany, with and without attributes +factory.createMany("user", 3) + .then(users => users.map(user => user.username)); + +factory.createMany("user", 3, { username: "Rocky Balboa" }) + .then(users => users.map(user => user.username)); + +// Testing createMany with options +factory.createMany("user", 3, { username: "John Rambo" }, { + afterBuild: (model, attrs, options) => {}, + afterCreate: (model, attrs, options) => {} +}).then(users => users.map(user => user.username)); + +// Testing createMany with a list of attributes +factory.createMany("user", [ + { username: "Emmett Brown" }, + { username: "Marty McFly" } +]).then(users => users.map(user => user.username)); + +// Testing cleanUp +factory.cleanUp(); diff --git a/types/factory-girl/index.d.ts b/types/factory-girl/index.d.ts new file mode 100644 index 0000000000..ebd6583885 --- /dev/null +++ b/types/factory-girl/index.d.ts @@ -0,0 +1,95 @@ +// Type definitions for factory-girl 5.0 +// Project: https://github.com/aexmachina/factory-girl#readme +// Definitions by: Stack Builders +// Sebastián Estrella +// Luis Fernando Alvarez +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.8 + +declare const factory: factory.Static; + +declare namespace factory { + interface Static { + /** + * Associate the factory to other model + */ + assoc(model: string, attributes: string): any; + + /** + * Associate the factory to a model that's not persisted + */ + assocAttrs(name: string, key?: string, attributes?: any): any; + + /** + * Associate the factory to multiple other models + */ + assocMany(model: string, num: number, attributes: string): any[]; + + /** + * Generates and returns model attributes as an object hash instead of the model instance + */ + attrs(name: string, attrs?: Partial): Promise; + + /** + * Generates and returns a collection of model attributes as an object hash instead of the model instance + */ + attrsMany(name: string, num: number, attrs?: Array>): Promise; + + /** + * Builds a new model instance that is not persisted + */ + build(name: string, attrs?: Partial): Promise; + + /** + * Builds an array of model instances that are persisted + */ + buildMany(name: string, num: number, attrs?: Partial): Promise; + buildMany(name: string, attrs?: Array>): Promise; + + /** + * Destroys all of the created models + */ + cleanUp(): void; + + /** + * Builds a new model instance that is persisted + */ + create(name: string, attrs?: Partial): Promise; + + /** + * Builds an array of model instances that are persisted + */ + createMany(name: string, num: number, attrs?: Partial, buildOptions?: Options): Promise; + createMany(name: string, attrs?: Array>, buildOptions?: Options): Promise; + + /** + * Define a new factory with a set of options + */ + define(name: string, model: any, attrs: T, options?: Options): void; + + /** + * Extends a factory + */ + extend(parent: string, name: string, initializer: any, options?: Options): any; + + /** + * Generate values sequentially inside a factory + */ + seq(name: string, fn: (sequence: number) => T): T; + + /** + * Register an adapter, either as default or tied to a specific model + */ + setAdapter(adapter: any, name?: string): void; + } + + interface Options { + afterBuild?: Hook; + afterCreate?: Hook; + } + + type Hook = (model: any, attrs: T[], options: any) => void; +} + +export = factory; +export as namespace factory; diff --git a/types/factory-girl/tsconfig.json b/types/factory-girl/tsconfig.json new file mode 100644 index 0000000000..c6bb5f369e --- /dev/null +++ b/types/factory-girl/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "factory-girl-tests.ts" + ] +} diff --git a/types/factory-girl/tslint.json b/types/factory-girl/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/factory-girl/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }