Skip to content

Conversation

mizdra
Copy link
Owner

@mizdra mizdra commented Aug 27, 2023

close: #17
follow up: #12

Before

import {
  defineBookFactory,
  lazy,
  defineAuthorFactory,
  defineAuthorFactoryWithTransientFields,
} from './index.js';

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')) ?? 0;
        // 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);

After

import {
  defineBookFactory,
  lazy,
  defineAuthorFactory,
  AuthorFactoryDefineOptions,
  AuthorFactoryInterface,
} from './index.js';

// Define factory with transient fields
type AuthorTransientFields = {
  bookCount: number;
};
function defineAuthorFactoryWithTransientFields<
  Options extends AuthorFactoryDefineOptions<AuthorTransientFields>,
>(options: Options): AuthorFactoryInterface<AuthorTransientFields, Options> {
  return defineAuthorFactory(options);
}

const BookFactory = defineBookFactory({
  defaultFields: {
    id: lazy(({ seq }) => `Book-${seq}`),
    title: lazy(({ seq }) => `ゆゆ式 ${seq}巻`),
    author: undefined,
  },
});
const AuthorFactory = defineAuthorFactoryWithTransientFields({
  defaultFields: {
    id: lazy(({ seq }) => `Author-${seq}`),
    name: '三上小又',
    books: lazy(async ({ get }) => {
      const bookCount = (await get('bookCount')) ?? 0;
      // eslint-disable-next-line max-nested-callbacks
      return Promise.all(Array.from({ length: bookCount }, async () => BookFactory.build()));
    }),
    bookCount: 0,
  },
});
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);

@mizdra mizdra added the Type: Change Change existing functionality. label Aug 27, 2023
@mizdra mizdra marked this pull request as draft August 27, 2023 10:00
@mizdra mizdra force-pushed the improve-transient-field-usability branch from 6607b3e to 145e778 Compare August 27, 2023 10:10
@mizdra mizdra force-pushed the improve-transient-field-usability branch from 145e778 to 92a574a Compare August 27, 2023 10:17
@mizdra mizdra marked this pull request as ready for review August 27, 2023 10:32
@mizdra mizdra merged commit 5843bc5 into main Aug 27, 2023
@mizdra mizdra deleted the improve-transient-field-usability branch August 27, 2023 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Change Change existing functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow Transient Fields in defineTypeFactory
1 participant