Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vilicvane/black-object
Blackbox mock with predefined interaction scripts.
https://github.com/vilicvane/black-object
mock typescript
Last synced: 20 days ago
JSON representation
Blackbox mock with predefined interaction scripts.
- Host: GitHub
- URL: https://github.com/vilicvane/black-object
- Owner: vilicvane
- License: mit
- Created: 2022-04-28T10:42:18.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-05-23T03:52:06.000Z (over 2 years ago)
- Last Synced: 2024-11-29T17:50:33.305Z (24 days ago)
- Topics: mock, typescript
- Language: TypeScript
- Homepage:
- Size: 89.8 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![NPM version](https://img.shields.io/npm/v/black-object?color=%23cb3837&style=flat-square)](https://www.npmjs.com/package/black-object)
[![Repository package.json version](https://img.shields.io/github/package-json/v/vilic/black-object?color=%230969da&label=repo&style=flat-square)](./package.json)
[![MIT license](https://img.shields.io/github/license/vilic/black-object?style=flat-square)](./LICENSE)# Black Object
Black Object is simple utility for creating a mock object with predefined interaction "scripts" (`call`, `get`, `set` and `property` update).
## Installation
```sh
npm install black-object
# or
yarn add black-object
```## Usage
```ts
import {assertScriptsCompleted, call, createBlackObject, x} from 'black-object';interface Adapter {
stepOne(value: number): void;
stepTwo(value: string): boolean;
}const adapter = createBlackObject([
['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'); // trueassertScriptsCompleted(adapter);
```### Create Black Object with fallback partial
```ts
const object = createBlackObject(
{
value: 123,
},
[
['foo', call([])],
['bar', call([x.number], value => value + 1)],
],
);object.value; // 123
object.foo();
object.bar(0); // 1object.value; // 123
```### Property `get` and `set`
Both `get` and `set` script work only once and does not change the property value.
```ts
const object = createBlackObject([
['foo', get(123)],
['foo', set(456)],
]);object.foo; // 123
object.foo = 456;
```### Update `property`
```ts
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.