Skip to content

Instantly share code, notes, and snippets.

@gdekefir
gdekefir / test-observable-property.ts
Created April 14, 2017 15:10
Use rxjs's marble-testing together with jsverify
import {TestScheduler} from 'rxjs';
import {Arbitrary} from 'jsverify';
import {equals} from 'ramda';
import * as jsc from 'jsverify';
// tslint:disable-next-line:no-require-imports
require('./jasmineHelpers2'); // https://github.com/jsverify/jsverify#usage-with-jasmine
type TestPropertyHandler = (propertyScheduler: TestScheduler, ...args: any[]) => void;
export function testObservableProperty(description: string, arbitrary1: Arbitrary<any>, handler: TestPropertyHandler): void;
@gdekefir
gdekefir / arbitrary.ts
Last active February 18, 2018 22:20
Custom jsverify property for generating an alpha-numeric string
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;
};
@gdekefir
gdekefir / requests.spec.ts
Created April 14, 2017 20:24
Mocking Observable.ajax.get
jest.mock('./process-env');
import Mock = jest.Mock;
import {Observable} from 'rxjs';
import {get} from './requests';
import {getProcessEnv} from './process-env';
describe('get', () => {
const originalAjaxGet = Observable.ajax.get;
interface ReadonlyArray<T> {
/**
* Gets the length of the array. This is a number one higher than the highest element defined in an array.
*/
readonly length: number;
/**
* Returns a string representation of an array.
*/
toString(): string;
toLocaleString(): string;
@gdekefir
gdekefir / action-names.ts
Created April 19, 2017 10:20
Action names
export const REQUESTING_LOGIN = 'REQUESTING_LOGIN';
export const REQUESTING_ADMIN_TOKEN = 'REQUESTING_ADMIN_TOKEN';
export const FETCHING_TABLES = 'FETCHING_TABLES';
export const FETCHING_TABLE_SESSIONS_HISTORY = 'FETCHING_TABLE_SESSIONS_HISTORY';
export const PENDING_TABLES = 'PENDING_TABLES';
export const PENDING_TABLE_SESSIONS = 'PENDING_TABLE_SESSIONS';
import {Observable} from 'rxjs';
import {Epic} from 'redux-observable';
import {Store} from 'redux';
import {pipe, clone, merge, keys, map, concat, uniq} from 'ramda';
// tslint:disable-next-line:no-require-imports
const t = require('tcomb-validation');
import {FETCHING_TABLE_SESSIONS_HISTORY} from '../constants/action-names';
import {
get, isAjaxResponseDefined,
import Mock = jest.Mock;
import {expectEpic} from './expect-epic';
import {getEpic} from './test-epic';
describe('expectEpic', () => {
const actions = {
FETCH_FOO: 'FETCH_FOO',
FETCH_FOO_FULFILLED: 'FETCH_FOO_FULFILLED',
equals(
pipe(
chain(f1),
chain(f2),
chain(f3)
)(whatever),
chain(pipeK(f1, f2, f3))(whatever)
)
const accumMaker = (pred, reducer, memo = 0) => (...args) => {
const currentRes = reducer(...[memo, ...args]);
return pred(currentRes) ? currentRes : (...nextArgs) => accumMaker(pred, reducer, currentRes)(...nextArgs)
};
// return itself until sum is lower than 10
const accumulator = accumMaker((val) => val >= 10, function (memo, ...args) {
return [memo, ...args].reduce((previousValue, val) => previousValue + val, 0);
});
accumulator(5)(1)(7); // 13
@gdekefir
gdekefir / new_gist_file.js
Created June 3, 2017 14:20
Derivative of R.props for deep fields
const obj1 = {
a: { b: { c: 1 } },
x: 2
};
const dotPath = compose(path, split('.'));
const dotPaths = map(dotPath);
const propsDotPath = (paths, obj) =>
ap(dotPaths(paths), [obj])