0.1.0 • Published 3 years ago

v1-test-helper v0.1.0

Weekly downloads
4
License
ISC
Repository
-
Last release
3 years ago

Installation

npm install v1-test-helper

Creating an NockHelper v1-test-helper object

You must supply a url as parameter to constructor.

import { NockHelper } from "v1-test-helper";
const nockHelper = new NockHelper('SPARKPOST_URL');

Methods

Note: The following methods are supported.

  • rootUrl()
  • cleanAll()
  • headers(headers?: any)
  • integration(integration?: any)
  • publicKey(publicKey?: string)
  • authPayload(authPayload?: any)
  • addGetCall(url: string, code: number, payload: any)
  • addPostCall(url: string, code: number, payload: any)
  • addAuthenticatedCall(url: string, code: number, payload: any)
  • addAuthenticatedOnlyCall(url: string, code: number)

Basic get usage example

import axios from "axios";
import { NockHelper } from "v1-test-helper";
axios.defaults.adapter = require("axios/lib/adapters/http");

const url = "http://localhost:3000";
const nockHelper = new NockHelper(url);

describe("Testing things", () => {
  test("testing thing1", () => { 
    nock.addGetCall("/api/v1/update", 200, {
      thing1: "thing1",
      thing2: "thing2",  
    });
    
    // add your test call ...
  });
});

Basic post usage example

import axios from "axios";
import { NockHelper } from "v1-test-helper";
axios.defaults.adapter = require("axios/lib/adapters/http");

const url = "http://localhost:3000";
const nockHelper = new NockHelper(url);

describe("Testing things", () => {
  test("testing thing1", () => { 
    nockHelper.addPostCall("/api/v1/update", 200, {
      thing1: "thing1",
      thing2: "thing2",  
    });
    // add your test call ...
  });
});

Authenticated SDQ post calls require that you set the authLoad and integration object

import axios from "axios";
import { NockHelper } from "v1-test-helper";
axios.defaults.adapter = require("axios/lib/adapters/http");

const url = "http://localhost:3000";
const nockHelper = new NockHelper(url);

describe("Testing things", () => {
  test("testing thing1", () => { 
    nockHelper.authPayload({
      callback: "https://somewhere-out-there.com",
      api_token: "dfadfasdf3223424"
    });
    
    nockHelper.integration({
      id: 2,
      name: "acyou",
      public_key: "321"
    });
    
    nockHelper.addAuthenticatedCall("/api/v1/update", 200, {
      thing1: "thing1",
      thing2: "thing2",  
    });
    
    // add your test call ...
  });
});

Sometimes you may just want to make an authenticated SDQ post call then follow with a specific addPostCall

import axios from "axios";
import { NockHelper } from "v1-test-helper";
axios.defaults.adapter = require("axios/lib/adapters/http");

const url = "http://localhost:3000";
const nockHelper = new NockHelper(url);

describe("Testing things", () => {
  test("testing thing1", () => { 
    nockHelper.authPayload({
      callback: "https://somewhere-out-there.com",
      api_token: "dfadfasdf3223424"
    });
    nockHelper.integration({
      id: 2,
      name: "acyou",
      public_key: "321"
    });
    
    nockHelper.addAuthenticatedOnlyCall("/api/v1/update", 200);
    
    nockHelper.addPostCall("/api/v1/update", 200, {
      thing1: "thing1",
      thing2: "thing2",  
    });

    // add your test call ...
  });
});

Development

Note: You MUST run build command to ensure you're lastest updates are available in the /lib directory for any process using this repo.

// code changes, add and commit ...
git add/commit ...

// Now build against new updates, then add/commit new /lib
npm run build
git add/commit /lib

git push ...