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

Transient Fields #12

Merged
merged 6 commits into from
Aug 27, 2023
Merged

Transient Fields #12

merged 6 commits into from
Aug 27, 2023

Conversation

mizdra
Copy link
Owner

@mizdra mizdra commented Aug 20, 2023

ref: #1

Example

const BookFactory = defineBookFactory({
  defaultFields: {
    id: lazy(({ seq }) => `Book-${seq}`),
    title: lazy(({ seq }) => `ゆゆ式 ${seq}巻`),
    author: undefined,
  },
});
const AuthorFactory = defineAuthorFactoryWithTransientFields(
  {
    bookCount: 0,
  },
  {
    defaultFields: {
      id: lazy(({ seq }) => `Author-${seq}`),
      name: '三上小又',
      books: lazy(async ({ get }) => {
        const bookCount = await get('bookCount');
        // eslint-disable-next-line max-nested-callbacks
        return Promise.all(Array.from({ length: bookCount }, async () => BookFactory.build()));
      }),
    },
  },
);
const author1 = await AuthorFactory.build();
expect(author1).toStrictEqual({
  id: 'Author-0',
  name: '三上小又',
  books: [],
});
assertType<{
  id: string;
  name: string;
  books: { id: string; title: string; author: undefined }[];
}>(author1);
expectTypeOf(author1).not.toBeNever();

const author2 = await AuthorFactory.build({ bookCount: 3 });
expect(author2).toStrictEqual({
  id: 'Author-1',
  name: '三上小又',
  books: [
    { id: 'Book-0', title: 'ゆゆ式 0巻', author: undefined },
    { id: 'Book-1', title: 'ゆゆ式 1巻', author: undefined },
    { id: 'Book-2', title: 'ゆゆ式 2巻', author: undefined },
  ],
});
assertType<{
  id: string;
  name: string;
  books: {
    id: string;
    title: string;
    author: undefined;
  }[];
}>(author2);

@mizdra mizdra added the Type: Add Add new features. label Aug 20, 2023
@mizdra mizdra mentioned this pull request Aug 20, 2023
26 tasks
@mizdra mizdra force-pushed the transient-fields branch 3 times, most recently from ee74ea0 to 0005380 Compare August 23, 2023 16:20
@mizdra mizdra marked this pull request as ready for review August 27, 2023 00:55
@mizdra mizdra merged commit 8b84580 into main Aug 27, 2023
5 checks passed
@mizdra mizdra deleted the transient-fields branch August 27, 2023 03:12
* @param options
* @returns factory {@link AuthorFactoryInterface}
*/
export function defineAuthorFactoryWithTransientFields<
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really wanted to allow transientFields in the defineAuthorFactory, but I couldn't. For more information: #17

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Add Add new features.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant