0.0.1-3 • Published 2 years ago

black-object v0.0.1-3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

NPM version Repository package.json version MIT license

Black Object

Black Object is simple utility for creating a mock object with predefined interaction "scripts" (call, get, set and property update).

Installation

npm install black-object
# or
yarn add black-object

Usage

import {assertScriptsCompleted, call, createBlackObject, x} from 'black-object';

interface Adapter {
  stepOne(value: number): void;
  stepTwo(value: string): boolean;
}

const adapter = createBlackObject<Adapter>([
  ['stepOne', call([123])],
  ['stepTwo', call([x.string], true)],
]);

// Error would be thrown if the following interactions failed to match the
// scripts defined above.

adapter.stepOne(123);
adapter.stepTwo('abc'); // true

assertScriptsCompleted(adapter);

Create Black Object with fallback partial

const object = createBlackObject(
  {
    value: 123,
  },
  [
    ['foo', call([])],
    ['bar', call([x.number], value => value + 1)],
  ],
);

object.value; // 123

object.foo();
object.bar(0); // 1

object.value; // 123

Property get and set

Both get and set script work only once and does not change the property value.

const object = createBlackObject([
  ['foo', get(123)],
  ['foo', set(456)],
]);

object.foo; // 123
object.foo = 456;

Update property

const object = createBlackObject(
  {
    foo: 123,
  },
  [
    ['foo', set(x.number)],
    ['foo', property(456)],
  ],
);

object.foo = 456;
object.foo; // 456, actually updated by `property(456)` script.

License

MIT License.

0.0.1-3

2 years ago

0.0.1-2

2 years ago

0.0.1-1

2 years ago

0.0.1-0

2 years ago

0.0.0

2 years ago