Papupata Documentation

API Reference

class DeclaredAPI

method mock

Purpose

Causes API invocations to be omitted, instead returning a mock value

Availability

This functionality is available from papupata version 1.1.0 onwards.

Usage

This function is meant to help with testing components that use papupata. If you only wish to mock a single invocation, you might want to use the mockOnce method instead, as it automatically removes the mock implementation as soon as the invocation has been completed.

If you wish to restore the API to its normal state after mocking it, use the unmock method, or the unmockAll method of the API declaration

Parameters

NameTypeDescription
mockValueFunction or value

If a function, that function is invoked when the API is invoked and its return value is returned as the mock response from the API.

Otherwise the value given as this parameter is returned as the mock response from the API.

Returns

nothing

Examples

Value
import { APIDeclaration } from 'papupata'
const api = new APIDeclaration()
const myAPI = api.declarePostAPI('/do-stuff/:param')
  .response<string>()

myAPI.mock('test')

const value = await myAPI() // value is now "test"

myAPI.unmock()
Function
import { APIDeclaration } from 'papupata'
const api = new APIDeclaration()
const myAPI = api.declarePostAPI('/do-stuff/:param')
  .response<string>()

myAPI.mock(() => 'test')

const value = await myAPI() // value is now "test"

myAPI.unmock()