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

feat: support groups in groups #21

Merged

Conversation

angeloashmore
Copy link
Member

@angeloashmore angeloashmore commented Jun 5, 2024

Caution

Complete before merging:

Resolves: DT-2135

Description

This PR adds support for group fields nested within group fields. This is an upcoming Prismic feature.

Checklist

  • If my changes require tests, I added them.
  • If my changes affect backward compatibility, it has been discussed.
  • If my changes require an update to the CONTRIBUTING.md guide, I updated it.

Preview

The following examples use this setup:

import { createMockFactory } from "@prismicio/mock";
const mock = createMockFactory({ seed: "foo" });

Model

const model = mock.model.group({
  fields: {
    nested: mock.model.group({
      fields: {
        boolean: mock.model.boolean(),
      },
    }),
  },
});
{
  "type": "Group",
  "config": {
    "label": "Vulputate",
    "fields": {
      "nested": {
        "type": "Group",
        "config": {
          "label": "Dolor",
          "fields": {
            "boolean": {
              "type": "Boolean",
              "config": {
                "label": "Amet"
              }
            }
          }
        }
      }
    }
  }
}

Value

const value = mock.value.group({ model });
[
  {
    "nested": [
      { "boolean": true },
      { "boolean": true },
      { "boolean": true },
      { "boolean": true },
      { "boolean": true }
    ]
  },
  {
    "nested": [
      { "boolean": true },
      { "boolean": true },
      { "boolean": false },
      { "boolean": true },
      { "boolean": true }
    ]
  },
  {
    "nested": [{ "boolean": true }]
  },
  {
    "nested": [{ "boolean": true }, { "boolean": true }]
  }
]

How to QA 1

  1. Create a new npm project with @prismicio/mock:

    mkdir prismic-mock-groups-in-group-test
    cd prismic-mock-groups-in-group-test
    echo "{}" > package.json
    npm install @prismicio/mock@alpha @prismicio/client@alpha --legacy-peer-deps
    touch index.js
  2. Copy this code into index.js.

    import { createMockFactory } from "@prismicio/mock";
    const mock = createMockFactory({ seed: "foo" });
    
    const model = mock.model.group({
      fields: {
        nested: mock.model.group({
          fields: {
            boolean: mock.model.boolean(),
          },
        }),
      },
    });
    console.log(JSON.stringify(model, null, 2));
    
    const value = mock.value.group({ model });
    console.log(JSON.stringify(value, null, 2));
  3. Run the example (vite-node is used to prevent ESM issues):

    npx vite-node ./index.js
    
  4. Verify the output is correct.

Footnotes

  1. Please use these labels when submitting a review:
    ❓ #ask: Ask a question.
    💡 #idea: Suggest an idea.
    ⚠️ #issue: Strongly suggest a change.
    🎉 #nice: Share a complement.

Copy link

github-actions bot commented Jun 5, 2024

size-limit report 📦

Path Size
dist/index.js 11.02 KB (0%)
dist/index.cjs 14.36 KB (0%)

Copy link
Member

@lihbr lihbr left a comment

Choose a reason for hiding this comment

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

LGTM! So it was only type updates here? Interesting!

@xrutayisire xrutayisire merged commit 301f0dd into main Jun 20, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants