mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-04 06:47:03 +00:00
Merge pull request #30135 from stackbuilders/factory_girl
Types for "factory-girl"
This commit is contained in:
@@ -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>("user", {}, {
|
||||
username: "Bob",
|
||||
score: factory.seq<number>("User.score", score => score + 1),
|
||||
email: factory.seq<string>("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>("user").then(attrs => null);
|
||||
factory.attrs<User>("user", { score: 10 }).then(attrs => null);
|
||||
|
||||
// Testing attrsMany, with and without attributes
|
||||
factory.attrsMany<User>("user", 2).then(attrs => null);
|
||||
factory.attrsMany<User>("user", 2, [{ score: 10 }]).then(attrs => null);
|
||||
|
||||
// Testing build, with and without attributes
|
||||
factory.build<User>("user").then(user => user.username);
|
||||
factory.build<User>("user", { score: 10 }).then(user => user.username);
|
||||
|
||||
// Testing buildMany, with and without attributes and options
|
||||
factory.buildMany<User>("user", 3)
|
||||
.then(users => users.map(user => user.username));
|
||||
|
||||
factory.buildMany<User>("user", 3, { username: "John McClane" })
|
||||
.then(users => users.map(user => user.username));
|
||||
|
||||
// Testing buildMany with a list of attributes
|
||||
factory.buildMany<User>("user", [
|
||||
{ username: "Jake Blues" },
|
||||
{ username: "Elwood Blues" }
|
||||
]).then(users => users.map(user => user.username));
|
||||
|
||||
// Testing create, with and without attributes
|
||||
factory.create<User>("user").then(user => user.username);
|
||||
factory.create<User>("user", { score: 10 }).then(user => user.username);
|
||||
|
||||
// Testing createMany, with and without attributes
|
||||
factory.createMany<User>("user", 3)
|
||||
.then(users => users.map(user => user.username));
|
||||
|
||||
factory.createMany<User>("user", 3, { username: "Rocky Balboa" })
|
||||
.then(users => users.map(user => user.username));
|
||||
|
||||
// Testing createMany with options
|
||||
factory.createMany<User>("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>("user", [
|
||||
{ username: "Emmett Brown" },
|
||||
{ username: "Marty McFly" }
|
||||
]).then(users => users.map(user => user.username));
|
||||
|
||||
// Testing cleanUp
|
||||
factory.cleanUp();
|
||||
Vendored
+95
@@ -0,0 +1,95 @@
|
||||
// Type definitions for factory-girl 5.0
|
||||
// Project: https://github.com/aexmachina/factory-girl#readme
|
||||
// Definitions by: Stack Builders <https://github.com/stackbuilders>
|
||||
// Sebastián Estrella <https://github.com/sestrella>
|
||||
// Luis Fernando Alvarez <https://github.com/elcuy>
|
||||
// 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<T>(name: string, attrs?: Partial<T>): Promise<T>;
|
||||
|
||||
/**
|
||||
* Generates and returns a collection of model attributes as an object hash instead of the model instance
|
||||
*/
|
||||
attrsMany<T>(name: string, num: number, attrs?: Array<Partial<T>>): Promise<T[]>;
|
||||
|
||||
/**
|
||||
* Builds a new model instance that is not persisted
|
||||
*/
|
||||
build<T>(name: string, attrs?: Partial<T>): Promise<T>;
|
||||
|
||||
/**
|
||||
* Builds an array of model instances that are persisted
|
||||
*/
|
||||
buildMany<T>(name: string, num: number, attrs?: Partial<T>): Promise<T[]>;
|
||||
buildMany<T>(name: string, attrs?: Array<Partial<T>>): Promise<T[]>;
|
||||
|
||||
/**
|
||||
* Destroys all of the created models
|
||||
*/
|
||||
cleanUp(): void;
|
||||
|
||||
/**
|
||||
* Builds a new model instance that is persisted
|
||||
*/
|
||||
create<T>(name: string, attrs?: Partial<T>): Promise<T>;
|
||||
|
||||
/**
|
||||
* Builds an array of model instances that are persisted
|
||||
*/
|
||||
createMany<T>(name: string, num: number, attrs?: Partial<T>, buildOptions?: Options<T>): Promise<T[]>;
|
||||
createMany<T>(name: string, attrs?: Array<Partial<T>>, buildOptions?: Options<T>): Promise<T[]>;
|
||||
|
||||
/**
|
||||
* Define a new factory with a set of options
|
||||
*/
|
||||
define<T>(name: string, model: any, attrs: T, options?: Options<T>): void;
|
||||
|
||||
/**
|
||||
* Extends a factory
|
||||
*/
|
||||
extend(parent: string, name: string, initializer: any, options?: Options<any>): any;
|
||||
|
||||
/**
|
||||
* Generate values sequentially inside a factory
|
||||
*/
|
||||
seq<T>(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<T> {
|
||||
afterBuild?: Hook<T>;
|
||||
afterCreate?: Hook<T>;
|
||||
}
|
||||
|
||||
type Hook<T> = (model: any, attrs: T[], options: any) => void;
|
||||
}
|
||||
|
||||
export = factory;
|
||||
export as namespace factory;
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user