barbiturat
4/14/2017 - 3:24 PM

Custom jsverify property for generating an alpha-numeric string

Custom jsverify property for generating an alpha-numeric string

jest.mock('./process-env');

import {equals} from 'ramda';
import * as jsc from 'jsverify';
// tslint:disable-next-line:no-require-imports
require('./testing/jasmineHelpers2'); // https://github.com/jsverify/jsverify#usage-with-jasmine

import {arbLatSymbols} from './testing/arbitrary';
import {getExtendedHeaders, handleError} from './requests';
import {getProcessEnv} from './process-env';

describe('getExtendedHeaders', () => {
  jsc.property('adds a proper "Authorization" field', alphanumericSymbolsArb(20, '---...,,,'), (apiKey) => {
    getProcessEnv.mockImplementationOnce(() => ({
      API_KEY: apiKey
    }));

    const extendedHeaders = getExtendedHeaders();
    const authorization = extendedHeaders.Authorization;

    return equals(authorization)(`Token token=${apiKey}`);
  });
});
import * as jsc from 'jsverify';

const getRandomString = (length: number, chars: string) => {
  let result = '';
  for (let i = length; i > 0; --i) {
    result += chars[Math.floor(Math.random() * chars.length)];
  }
  return result;
};

export const alphanumericSymbolsArb = (maxLength: number, additionalSymbols = '') => jsc.bless<string>({
  generator: jsc.generator.bless(() => {
      const stringLength = jsc.random(1, maxLength);

      return getRandomString(stringLength, `0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ${additionalSymbols}`);
    }
  ),
  show: (val) => val,
  shrink: jsc.shrink.noop
});