prismicio / prismic-mock Goto Github PK
View Code? Open in Web Editor NEWGenerate mock Prismic documents, fields, Slices, and models for development and testing environments
License: Apache License 2.0
Generate mock Prismic documents, fields, Slices, and models for development and testing environments
License: Apache License 2.0
Allowing strings as seeds will make writing tests easier and more reliable.
For example, the following could be done with AVA:
import test from 'ava'
import * as mock from '@prismicio/mock'
test('my test', (t) => {
const model = mock.model.customType({ seed: t.title })
const document = mock.value.document({ seed: t.title, model })
// Do something with the model and document
})
The seed is now connected to the test's title which will be unique within the file.
When writing tests involving @prismicio/client
or the Prismic REST API directly, @prismicio/mock
can be used to mock return values. Currently this can be done by manually setting up a mock API within tests, such as through a custom request handler via Mock Service Worker (MSW).
Setting this up per-project can be a hassle as it requires intricate URL parameter and authentication checks.
A test-specific mock API solution could be provided by @prismicio/mock
. There are many ways to do this, such as building a full HTTP server or providing a mocked fetch
function.
Rather than build a completely custom solution, it makes sense to integrate with an existing HTTP mocking solution. Mock Service Worker, as mentioned above, is a common library made specifically for this purpose.
@prismicio/mock
could export MSW REST API handlers that cover most mocking cases. Its purpose is not to reimplement the Prismic REST API. Instead, it is just the bridge between an HTTP request and a set of provided results.
Preparing an API handler with MSW could look something like this:
import * as prismicM from "@prismicio/mock";
import * as mswNode from "msw/node";
const server = mswNode.setupServer();
const queryResponse = prismicM.api.query({
documents: [
prismicM.value.document(),
prismicM.value.document(),
],
});
server.use(prismicM.msw.queryHandler({ queryResponse }));
It uses the existing mock generators which allows users to fine-tune the results.
To restate, the provided handlers do not re-implement the REST API. A request with special parameters, such as predicates or fetchLinks
, is not processed by the handler. Instead, the user must prepare the response manually.
The handler can, however, validate against the URL to ensure the correct URL was requested.
The following libraries implement API mocking manually using MSW and @prismicio/mock
(or @prismicio/types
with manual mocks):
@prismicio/client
@prismicio/react
gatsby-source-prismic
gatsby-plugin-prismic-previews
Stripe provides a similar solution via a Docker container: https://github.com/stripe/stripe-mock
Rather than integrating with a specific language and framework, it exposes an API endpoint that returns static mock data. This makes it compatible with any solution, provided test requests are routed to the local URL.
Creating Rich Text and Title mock values is currently limited. It generates values for most block types, such as headings and images, but does not include some like links.
Rich Text and Title mock values can be customized using "pattern" option. Depending on the selected pattern, such as "short" and "long", the length of the value can be loosely determined. This gives some control of the output, but does not allow for custom patterns.
To give more control over the created mock value, access to individual block value generators can be provided by the library. This allows users to create custom values like:
This can be useful when you have a general idea of the type of content that will be provided by editors.
This requires all block types to be supported, including links.
Someone could use the individual value generators like this:
import * as prismicT from '@prismicio/types'
import * as prismicM from '@prismicio/mock'
const richTextValue: prismicT.RichTextField = [
prismicM.value.richTextBlocks.heading1({ pattern: 'medium' }),
prismicM.value.richTextBlocks.paragraph(),
prismicM.value.richTextBlocks.paragraph(),
]
Mock values can be constructed by hand. Some guidance is provided thanks to @prismicio/types
. Manually creating mock values is not ideal for obvious reasons.
N/A
All fields except Links return values that have been filled in. Most uses of the library will expect a filled value, but there are times were an empty value must be tested.
For example, if a React component displays an image from an Image field, it should be tested when an image is provided and not provided.
Link mock value generators accept a isFilled
option. All value generators could accept this. If isFilled
is false, an empty value should be returned.
For an image field, this is different from simply returning null
. Instead, it should look like this:
{
"url": null,
"dimensions": null,
"alt": null,
"copyright": null
}
The returned values can be modified manually. In the image example above, each property can be set to null
after a value is created.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.