Skip to main content
Version: 3.10.0

core.mock

Preface

The core.mock object provides methods related to the API mock served, so it contains methods related to routes, variants and collections.

caution

Use only the API methods described in this docs. Use other methods under your own risk, and take into account that they may change in minor versions without considering it as a breaking change.

API

onChange()

core.mock.onChange(callback): Add a callback to be executed when anything related to the API mock changes. So, it is triggered whenever collections or routes change, and also whenever mock settings change, such as mock.collections.selected or mock.routes.delay. It returns a function for removing the added callback.

  • callback() (Function): Function to be executed on change collections.
Example
// Add event listener and store the method allowing to remove it
const removeListener = core.mock.onChange(() => {
console.log("API mock has changed!");
});

// Remove event listener
removeListener();

createLoaders()

core.mock.createLoaders(): Return methods allowing to load routes and collections. Each method reloads only the collections or routes added by it, so, it allows to have many different sources for loading routes or collections without having conflicts between them.

It returns an object containing next methods:

  • loadRoutes(routes) - Load routes definitions with the same format that they are defined when using files. Each time this method is called, all previously loaded routes will be replaced by the new ones, but only those added using this method. Routes loaded by the files loader or other plugins will remain.
    • routes: <Array of routes> - Array of routes.
  • loadCollections(collections): Load collections definitions with the same format that they are defined when using files. Each time this method is called, all previously loaded collections will be replaced by the new ones, but only those added using this method. Collections loaded by the files loader or other plugins will remain.
    • collections: <Array of collections> - Array of collections.
Example
const { routes, collections } = require("./fixtures");

const { loadRoutes, loadCollections } = core.mock.createLoaders();

loadRoutes(routes);
loadCollections(collections);
caution

Note that, if you want your routes and collections to replace other defined previously, you should not call to createLoaders each different time you are going to use the methods. Instead of that, you should keep a reference to the loadRoutes and loadCollections methods and use always those references.

useRouteVariant()

core.mock.useRouteVariant(routeVariantId): Define a route variant to be used instead of the one defined in the current collection. The change is stored in memory only, so the original collection route variants are restored whenever the selected collection changes or routes or collections are reloaded.

  • routeVariantId (String): Route and variant id, with the format "[routeId]:[variantId]".

restoreRouteVariants()

core.mock.restoreRouteVariants(): Restore current collection route variants. It removes all variants defined with the useRouteVariant method.

customRouteVariants

core.mock.customRouteVariants: Getter returning an array of current custom route variants ids defined with the useRouteVariant method.

Routes API

info

The core.mock.routes object provides methods related to routes and variants.

plain

core.mock.routes.plain: Returns an array with all defined routes in plain format.

plainVariants

core.mock.routes.plainVariants: Returns an array with all defined variants in plain format.

Collections API

info

The core.mock.collections object provides access to methods related to collections.

select()

core.mock.collections.select(collectionId [,options]): Changes the current collection. The API mock will use the routes and variants defined in the selected collection.

  • collectionId (String): Collection id.
  • options (Object): Options object:
    • check (Boolean): When true, the method returns a promise that is resolved when the provided collection is set as the current one and it is effectively used by the mock server. Otherwise the promise is rejected after 2 seconds (in case the collection does not exists, for example).
Examples

// Async is not handled. The selected collection is set and it may take some milliseconds to the server to effectively use it
core.mock.collections.select("foo-collection-id");

// Async is handled. The selected collection is set and the promise is resolved when it is used by the server
core.mock.collections.select("foo-collection-id", { check: true }).then(() => {
console.log("The mock now is using the variants defined in foo-collection-id");
});
caution

Note that, if you use the check:true option, you should handle possible promise rejections.

selected

core.mock.collections.selected: Getter returning a collection instance correspondent to the currently selected collection.

plain

core.mock.collections.plain: Returns an array with current collections in plain format.

ids

core.mock.collections.ids: Getter returning an array with all collections ids.