Options
All
  • Public
  • Public/Protected
  • All
Menu

The objects Contract are an expectation that gather multiple expectations 😵.

For 'simple' cases you may not need a Contract, sticking with the functions expect* like in the examples provided here is totally fine. This Contract class will help to go a bit further, in particular in terms of formalizing required & optionals expectations.

Let consider the following example (and assume Material, Mesh, Option1, Option2 are known classes):

// the next line is to avoid to write an expect statement for each and every types.
let expectInstanceOf = ( T, attName ) => {
     let isInstance = expect({when: (d) => d instanceof T})
     return expectAnyOf( {when:[
         isInstance,
         expectAttribute({name:attName, when: isInstance})
     ]
})

let contract = contract({
      requireds: {   
          mat:expectSingle<Material>({when: expectInstanceOf(Material, 'material' ) }), 
          meshes:expectCount<Mesh>({count: 2, when:  expectInstanceOf(Mesh, 'mesh')}), 
      },
      optionals: {
          options1 : expectSingle<Option1>({when: expectInstanceOf(Option1, 'option')})
      }
  })

Using this contract in an input will ensure we always get the following normalized data-structure in the triggered callback:

type dataType = {
     mat: Material,
     meshes: [Mesh, Mesh],
     options1: Option1 | undefined,
}

Hierarchy

  • Contract

Implements

Index

Constructors

Properties

Methods

Constructors

constructor

  • new Contract(description: string, requireds: {}, optionals?: {}): Contract

Properties

Readonly description

description: string

description of the expectation

Readonly optionals

optionals: {} = ...

Type declaration

Readonly requireds

requireds: {}

Type declaration

Methods

resolve

Generated using TypeDoc