https://github.com/vilicvane/black-object
Blackbox mock with predefined interaction scripts.
https://github.com/vilicvane/black-object
mock typescript
Last synced: 18 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 3 years ago)
- Default Branch: main
- Last Pushed: 2022-05-23T03:52:06.000Z (over 3 years ago)
- Last Synced: 2025-09-23T22:57:32.654Z (about 1 month 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
[](https://www.npmjs.com/package/black-object)
[](./package.json)
[](./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'); // true
assertScriptsCompleted(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); // 1
object.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.