Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate features like traits #27

Closed
Tracked by #5
Quramy opened this issue Nov 23, 2022 · 1 comment · Fixed by #116
Closed
Tracked by #5

Investigate features like traits #27

Quramy opened this issue Nov 23, 2022 · 1 comment · Fixed by #116
Labels
enhancement New feature or request

Comments

@Quramy
Copy link
Owner

Quramy commented Nov 23, 2022

https://github.com/thoughtbot/factory_bot/blob/main/GETTING_STARTED.md#defining-traits

@Quramy Quramy mentioned this issue Nov 23, 2022
16 tasks
@Quramy Quramy added the enhancement New feature or request label Nov 25, 2022
@Quramy
Copy link
Owner Author

Quramy commented Nov 25, 2022

Type definition design

import { Resolver } from "@quramy/prisma-fabbrica/lib/helpers";

type UserFactoryDefineInput = {
  id?: string;
  name?: string;
  status?: "Active" | "Withdrawal";
};

type UserFactoryTraitOptions = {
  data?: Resolver<Partial<UserFactoryDefineInput>>;
};

type UserFactoryDefineOptions = {
  defaultData?: Resolver<UserFactoryDefineInput>;
  traits?: Record<string, UserFactoryTraitOptions>;
};

declare function defineUserFactory<TOptions extends UserFactoryDefineOptions>(
  options: TOptions,
): { create: (_: any, traits?: (keyof TOptions["traits"])[]) => null };

const UserFactory = defineUserFactory({
  defaultData: {
    status: "Active",
  },
  traits: {
    withdrawal: {
      data: async () => ({
        name: "***",
        status: "Withdrawal",
      }),
    },
  },
});

// Without traits
UserFactory.create({});

// With trait
UserFactory.create({ id: "quramy" }, ["withdrawal"]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant