Giter Site home page Giter Site logo

Comments (1)

SalahAdDin avatar SalahAdDin commented on June 15, 2024

Right now we have the following issue:

const targetType = chooseOne(ARTICLE_TYPES);
const targetContent =
  targetType === "live"
    ? mockArray(5, () => mockUpdatedInformation({}))
    : {
        root: mockRichText({}),
      };

export const articleFactory: ModelDefinition = {
  id: primaryKey(() => _articleId++),
  type: () => targetType,
  title: () => baseTitle,
  slug: () => slugify(baseTitle),
  author: nullable(manyOf("user")),
  editor: nullable(manyOf("user")),
  thumbnail: oneOf("image"),
  category: oneOf("category"),
  createdDate: () => faker.date.anytime(),
  publishedDate: () => faker.date.anytime(),
  introduction: () => faker.lorem.text(),
  summary: () => mockArray(3, () => faker.lorem.text()),
  // TODO: https://github.com/mswjs/data/issues/299
  content: () => targetContent,
  audio: {
    url: () => DEFAULT_AUDIO_URL,
    duration: () => 7264000,
    mimeType: () => chooseOne(AUDIO_MIME_TYPES),
  },
  hero: {
    url: () => faker.image.url({ width: mockWidth, height: mockHeight }),
    alt: () => faker.lorem.text(),
    width: () => mockWidth,
    height: () => mockHeight,
    mimeType: () => chooseOne(IMAGE_MIME_TYPES),
    caption: {
      source: () => faker.lorem.word(),
      description: () => faker.lorem.text(),
    },
  },
  source: nullable(() => faker.lorem.word()),
  tags: () => mockArray(6, () => faker.lorem.word()),
  continue: nullable(oneOf("continueArticle")),
  related: manyOf("relatedArticle"),
};

And we are creating the articles like this:

  /** Create an article. Faker will fill in any missing data */
  const createArticle = (article = {}) => db.article.create(article);

  /** Creating an article based on the figma design */
  createArticle({
    ...mockArticle({
      title: STANDARD_ARTICLE_BASE.title,
      introduction: STANDARD_ARTICLE_BASE.introduction,
      hero: {
        ...mockImage({ alt: STANDARD_ARTICLE_BASE.hero.text }),
        caption: mockCaption({
          source: STANDARD_ARTICLE_BASE.hero.source,
          description: STANDARD_ARTICLE_BASE.hero.text,
        }),
      },
      source: STANDARD_ARTICLE_BASE.source,
      tags: STANDARD_ARTICLE_BASE.tags,
    }),
    author: users[2],
    editor: chooseOne(users),
    thumbnail: chooseOne(imageGallery),
    category: figmaCategories[0],
    continue: figmaContinueArticles[0],
    related: figmaRelatedArticles.slice(0, 5),
    content: {
      root: mockArticleContent({ raw: STANDARD_ARTICLE_CONTENT }),
    },
  });

  /** Creating a live article based on the figma design */
  createArticle({
    ...mockLiveArticle({
      title: LIVE_ARTICLE_BASE.title,
      summary: LIVE_ARTICLE_BASE.summary,
      tags: LIVE_ARTICLE_BASE.tags,
    }),
    content: mockBELiveArticleContent(),
    editor: chooseOne(users),
    thumbnail: chooseOne(imageGallery),
    category: figmaCategories[1],
    related: figmaRelatedArticles.slice(4, 10),
  });

  /** Creating a featured article based on the figma design */
  createArticle({
    ...mockFeaturedArticle({
      title: FEATURED_ARTICLE_BASE.title,
      introduction: FEATURED_ARTICLE_BASE.introduction,
    }),
    type: "featured",
    display: chooseOne(DISPLAY_TYPES),
    category: figmaCategories[2],
    author: users[0],
    editor: chooseOne(users),
    thumbnail: chooseOne(imageGallery),
    continue: figmaContinueArticles[1],
    related: figmaRelatedArticles.slice(-2),
    content: {
      root: mockArticleContent({ raw: FEATURED_ARTICLE_CONTENT }),
    },
  });

So for a Live Article, the content should be an array of items, and it is:
image

But for a non Live Article, the content should be a rich-text object, and it does not work:
image

Before we had the content created with the mock function and not the content we passed to the function, that's not what we wanted.

As a solution for getting the right content, we have to set the content directly:

const targetContent = /* targetType === "live"
    ? mockArray(5, () => mockUpdatedInformation({}))
    : */ {
  root: mockRichText({}),
};

*
*
*
content: targetContent,

Yeah, content: () => targetContent, does not work also.

from data.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.