Papupata Documentation

API Reference

class DeclaredAPI

method mockOnce

Purpose

Causes an API invocation 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. When using this function instead of mock you do not need to call unmock afterwards, assuming the API was invoked, as it is automatically called upon the invocation.

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.mockOnce('test')

const value1 = await myAPI() // value1 is now "test"
const value2 = await myAPI() // value is now obtained by invoking the actual API
Function
import { APIDeclaration } from 'papupata'
const api = new APIDeclaration()
const myAPI = api.declarePostAPI('/do-stuff/:param')
  .response<string>()

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

const value1 = await myAPI() // value1 is now "test"
const value2 = await myAPI() // value is now obtained by invoking the actual API

myAPI.unmock()