How to Mock Rendering Parameters in Unit Test – Sitecore

Now that we created a cool components with rendering parameters (see Using Rendering Parameters to Inject Data into Hidden Input), we can write a unit test. Sitecore’s Rendering class has a publicly accessible string field called Parameters. This is the rendering parameter string get populated on page rendering. Mocking is relatively straight forward as all you need to do is to add the mock parameters to this field.

Here is the controller created in the previous post. We will write a unit test on this.

Here are the unit tests, covering scenarios for parameters present and absent. As you can see, the RenderingParameters class is instantiated with a query string with parameters, then added to the Parameters field in the Rendering object.

All you need to do is to create rendering context with the RenderingContext.EnterContext method and pass the mock rendering.

We are using NSubstitue to mock the repository through its interface. This should be straight forward. We are also using FluentAssertions for assertions.

Here is the repository created in the previous post.

Unit testing it is super easy. Just create the RenderingParameters object as in the controller test. Then, compare the results.

Front-End
TypeScript: type aliases to check type equality

This post is to analyse how the type equality check by using type aliases proposed by Matt Pocock in his twitter post. These type aliases allow us to elegantly express type equality checks in unit tests. All we need to do is to pass the output and expected types in …

Front-End
Fixing it.only type error in Jest

If you are getting a type error with it.only in Jest, it could be due to incorrect TypeScript typings or incompatible versions of Jest and TypeScript. To resolve this issue, you can try the following steps: Make sure you have the latest versions of Jest and its TypeScript typings installed …

Front-End
yup conditional validation example

Here’s an example of a Yup validation logic where the first input field is optional but, if filled, it must contain only alphabetic characters, and the second input field is required: import * as Yup from “yup”; const validationSchema = Yup.object().shape({ firstField: Yup.string().matches(/^[A-Za-z]*$/, { message: “First field must contain only …