Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoInstallSign in
Socket

proxify-method

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

proxify-method

Project for flexible extension of the existing functionality

latest
Source
npmnpm
Version
0.0.14
Version published
Weekly downloads
2
-86.67%
Maintainers
1
Weekly downloads
 
Created
Source

proxify-method

Zero dependencies package. Main purpose of this package is to provide integration of the common assertions into class methods without refactoring and updates of the class methods, this package does not affect constructor, getters/setters and does not affect internal logic of the class methods. Package works with async function/Promises, and also sync common code.

npm downloads

Usage

ts example chainProxify js example chainProxify

js

chainProxify usage

// example with node-fetch and chai
const fetch = require('node-fetch');
const {chainProxify} = require('proxify-method');
const {expect} = require('chai');

function assertResponseBody(expectedBodyTextPart, {response}) {
  expect(response).to.include(expectedBodyTextPart)
}

function assertResponseBodyStrictEqual(expectedBody, {response}) {
  expect(response).to.equal(bodyTextPart)
}

class ControllerIterface {
  constructor() {
    // this two assertions will be available for every method in this class
    chainProxify('assertBodyIncludes', assertResponseBody)
      .chainProxify('strictAssertBody', assertResponseBodyStrictEqual)
      .initContextChainModel(this);
  }

  getDataFromServerController() {
    return fetch('http://someurl.that.returns.some.data')
    .then(result => result.text())
    // this {response: result} object will be second argument of the assert functions
    .then(result => ({response: result}))
  }

  postDataFromServerController() {
    return fetch('http://someurl.that.should_receive.some.data', {method: 'POST', body: 'string data'})
    .then(result => result.text())
    // this {response: result} object will be second argument of the assert functions
    .then(result => ({response: result}))
  }
}


async function test() {
  const controller = new ControllerIterface();

  const expectedGetBodyPart = 'some string';
  const expectedGetBodyFull = 'some string full'
  // can be done like TDD
  const {response: responseTddExample} = await controller.getDataFromServerController();
  expect(responseTddExample).to.include(expectedGetBodyPart);
  expect(responseTddExample).to.equal(expectedGetBodyFull);
  // ... usage of the response
  const resultTdd = responseTddExample;

  // can be done like BDD
  const {response: responseBddExample} = await controller.getDataFromServerController()
    .assertBodyIncludes(expectedGetBodyPart)
    .strictAssertBody(expectedGetBodyFull)
  // ... usage of the response
  const resultBdd = responseBddExample;
}

proxify usage

import {proxify} from 'proxify-method';

const obj = {a: 2, b: 3, c: 4};

function isPropExists(prop, value, targetObj) {
  expect(targetObj[prop]).toEqual(value);
}

const proxedObj = proxify(obj, {isPropExists: isPropExists});

const {a, b, c} = proxedObj
  .isPropExists('a', 2)
  .isPropExists('b', 3)
  .isPropExists('c', 4);

expect(a).toEqual(obj.a);
expect(b).toEqual(obj.b);
expect(c).toEqual(obj.c);
expect(proxedObj).toDeepEqual(obj);

Keywords

proxy object

FAQs

Package last updated on 28 Aug 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts