ws-dependency-mock
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

##ws-dependency-mock

Install

npm i ws-dependency-mock --save

Usage

Validations
import { IsInt, IsEmail, Min, IsNotEmpty, IsString, MinLength, MaxLength } from 'class-validator';

export class UserModel {
    @IsNotEmpty()
    @IsInt()
    @Min(1)
    id: number;

    @IsNotEmpty()
    @IsString()
    @MinLength(2)
    @MaxLength(255)
    first: string;

    @IsNotEmpty()
    @IsString()
    @MinLength(2)
    @MaxLength(255)
    last: string;

    @IsNotEmpty()
    @IsString()
    @IsEmail()
    email: number;
}
Contract
import { AbstractContract } from '../../../src/abstract-contract';
import { UserModel } from '../../../test/src/models/user.model';

export class UserContract extends AbstractContract {

    init(): void {
        this
            .add({
                validation: UserModel, // Check validation
                payload: {             // Your mocked payload
                    test: 'success',
                },
                repeat: 1,             // Count of repeat. Default: 1
                timeout: 500           // Timeout return mock payload. Default: 0
            });
    }
}
Contract registration
import { ContractInterface } from '../../src/interfaces/contract.interface';
import { ContractNotFoundException } from '../../src/exceptions/contract-not-found.exception';
import { UserContract } from '../../test/src/contracts/user.contract';

export class ContractFactory {
    create(key: string): ContractInterface {
        switch (key) {
            case 'user': return new UserContract();
            default: throw new ContractNotFoundException(key);
        }
    }
}
Run
const wsm = new WebSocketMock({
    host: 'localhost',
    port: 8080,
});

wsm.run(new ContractFactory());

Dependencies

Enjoy!

Readme

Keywords

none

Package Sidebar

Install

npm i ws-dependency-mock

Weekly Downloads

1

Version

0.0.3

License

MIT

Unpacked Size

43.9 kB

Total Files

72

Last publish

Collaborators

  • chvarkov