jest-kefir
Version:
Jest plugin for asserting on Kefir Observables
148 lines (130 loc) • 5.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = jestKefir;
var _kefirTestUtils = _interopRequireDefault(require("kefir-test-utils"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const noop = () => {};
function jestKefir(Kefir) {
const helpers = (0, _kefirTestUtils.default)(Kefir);
const {
activate,
deactivate,
send,
error,
watch,
withFakeTime,
watchWithTime
} = helpers;
const extensions = {
toBeObservable(received) {
return {
pass: received instanceof Kefir.Observable,
message: () => `expected ${received} to be an instance of Kefir.Observable`
};
},
toBeProperty(received) {
return {
pass: received instanceof Kefir.Property,
message: () => `expected ${received} to be an instance of Kefir.Property`
};
},
toBeStream(received) {
return {
pass: received instanceof Kefir.Stream,
message: () => `expected ${received} to be an instance of Kefir.Stream`
};
},
toBePool(received) {
return {
pass: received instanceof Kefir.Pool,
message: () => `expected ${received} to be an instance of Kefir.Pool`
};
},
toBeActiveObservable(received) {
return {
pass: !!received._active,
message: () => `expected ${received} to be an active observable`
};
},
toEmit(received, expected, cb = noop) {
const {
log,
unwatch
} = watch(received);
cb();
unwatch();
const options = {
comment: 'Emitted values',
isNot: this.isNot,
promise: this.promise
};
const pass = this.equals(log, expected);
const message = pass ? () => this.utils.matcherHint('toEmit', undefined, undefined, options) + '\n\n' + `Expected: not ${this.utils.printExpected(expected)}\n` + `Received: ${this.utils.printReceived(log)}` : () => {
const diffString = this.utils.diff(expected, log, {
expand: this.expand
});
return this.utils.matcherHint('toEmit', undefined, undefined, options) + '\n\n' + (diffString && diffString.includes('- Expect') ? `Difference:\n\n${diffString}` : `Expected: ${this.utils.printExpected(expected)}\n` + `Received: ${this.utils.printReceived(log)}`);
};
return {
pass,
message
};
},
toEmitInTime(received, expected, cb = noop, {
timeLimit = 10000,
reverseSimultaneous = false
} = {}) {
let log = null;
withFakeTime((tick, clock) => {
log = watchWithTime(received);
cb(tick, clock);
tick(timeLimit);
}, reverseSimultaneous);
const options = {
comment: 'Emitted values',
isNot: this.isNot,
promise: this.promise
};
const pass = this.equals(log, expected);
const message = pass ? () => this.utils.matcherHint('toEmitInTime', undefined, undefined, options) + '\n\n' + `Expected: not ${this.utils.printExpected(expected)}\n` + `Received: ${this.utils.printReceived(log)}` : () => {
const diffString = this.utils.diff(expected, log, {
expand: this.expand
});
return this.utils.matcherHint('toEmitInTime', undefined, undefined, options) + '\n\n' + (diffString && diffString.includes('- Expect') ? `Difference:\n\n${diffString}` : `Expected: ${this.utils.printExpected(expected)}\n` + `Received: ${this.utils.printReceived(log)}`);
};
return {
pass,
message
};
},
toFlowErrors(received, source = received) {
const expected = [error(-2), error(-3)];
if (received instanceof Kefir.Property) {
activate(received);
send(source, [error(-1)]);
deactivate(received);
expected.unshift(error(-1, {
current: true
}));
}
const {
log,
unwatch
} = watch(received);
send(source, [error(-2), error(-3)]);
unwatch();
return {
pass: this.equals(log, expected),
message: () => `expected errors to flow`
};
}
};
return _objectSpread({}, helpers, {
extensions
});
}