Skip to content

Instantly share code, notes, and snippets.

Pierre Criulanscy PCreations

Block or report user

Report or block PCreations

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View updatingBar.test.js
const { MyVeryUsefulProject } = require('../index');
const { getFooOfId, saveFoo } = require('../infrastructure/inMemory');
describe('Feature: updating bar', () => {
describe('scenario: updating bar with a value not containing a "?"', () => {
describe('given a foo object in database with the id foo1 and a bar value of "initial bar value"', () => {
describe('when updating the bar value to "some new value"', async (done) => {
test('then the foo object with id foo1 should have its bar value set to "some new value"', () => {
const inMemoryDatabase = {
foo1: {
View index.js
const { updateBarUseCaseHandler } = require('./application/updateBarUseCaseHandler');
const MyVeryUsefulProject = ({ getFooOfId, saveFoo }) => ({
updateBar: updateBarUseCaseHandler({ getFooOfId, saveFoo }),
});
module.exports = {
MyVeryUsefulProject,
}
View inMemoryDatabase.js
const { getFooOfId: createGetFooOfId } = require('../domain/foo/getFooOfId');
const { saveFoo: createSaveFoo } = require('../domain/foo/saveFoo');
const getFooOfId = inMemoryDatabase => createGetFooOfId(fooId => inMemoryDatabase[fooId]);
const saveFoo = inMemoryDatabase => createSaveFoo(fooState => inMemoryDatabase[fooState.id] = fooState);
module.exports = {
getFooOfId,
saveFoo,
View updateBarUseCaseHandler.js
const { updateBar } = require('../domain/foo/behaviors');
const updateBarUseCaseHandler = ({ getFooOfId, saveFoo }) => async ({ fooId, bar }) => {
if (typeof fooId !== typeof '') {
throw new Error('fooId must be string');
}
const fooData = getFooOfId();
const newFooData = updateBar({ fooData, bar });
await saveFoo(newFooData);
}
View getFooOfId.js
const { FooData } = require('./data');
const getFooOfId = (getFooOfIdImpl = async (fooId) => { throw new Error(`Can't retrieved foo of id ${fooId} : missing implementation`) }) => async fooId => {
try {
const fooData = await getFooOfIdImpl(fooId);
return FooData(fooData);
} catch (err) {
throw new Error(`Unable to retrieve Foo with id ${fooId}`);
}
}
View behaviors.js
const { FooData } = require('./data');
const updateBar = ({ fooData, bar }) => {
if (typeof bar !== typeof '') {
throw new Error('bar should be a valid string');
}
return FooData({
...fooData,
bar,
foobaz: bar.indexOf('?') === -1 ? fooData.foobaz : 42,
View data.js
const FooData = ({ id, foo, bar, foobaz }) => Object.freeze({
id,
foo,
bar,
foobaz
});
module.exports = {
FooData,
}
@PCreations
PCreations / jestTdd.js
Created Oct 27, 2018
LIttle helpers methods to help you write unit test with jest in a more "behavior oriented" way
View jestTdd.js
const given = (name, cb = () => {}) => [cb, describe.bind(describe, `given ${name}`)];
const and = (name, cb = () => {}) => [cb, describe.bind(describe, `and ${name}`)];
const when = (name, cb = () => {}) => [cb, describe.bind(describe, `when ${name}`)];
const then = (name, cb = () => {}) => [cb, test.bind(test, `then ${name}`)];
const feature = (name, getSteps) => {
const steps = [[() => {}, describe.bind(describe, 'Feature: basic synchronous command')], ...getSteps()];
const lastStep = steps.pop();
const [cb, step] = steps.reduceRight(([combinedCb, combinedStep], currentFn) => {
const [cb, step] = currentFn;
View createRxAdapter.js
import React from "react";
import { Subject, Obsevable } from "rxjs/Rx";
import { Map, fromJS, is } from "immutable";
const createRxAdapter = ({
initialState = {
messages: {},
areMessagesLoading: false,
users: {}
},
View createUnistoreAdapter.js
import React from "react";
import createUnistore from "unistore";
import { Provider, connect } from "unistore/react";
const createUnistoreAdapter = ({
initialState = {
messages: {},
areMessagesLoading: false,
users: {}
},
You can’t perform that action at this time.