Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(core): #contextFactory - async dependencies #376

Merged
merged 1 commit into from
Oct 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion packages/core/src/context/specs/context.helper.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as R from 'fp-ts/lib/Reader';
import * as O from 'fp-ts/lib/Option';
import { size } from 'fp-ts/lib/Map';
import { pipe } from 'fp-ts/lib/function';
import { createContextToken } from '../context.token.factory';
import { createReader } from '../context.reader.factory';
import { contextFactory, constructContext } from '../context.helper';
import { bindTo, lookup, DerivedContextToken, bindEagerlyTo, createContext } from '../context';
import { bindTo, lookup, DerivedContextToken, bindEagerlyTo, createContext, Context } from '../context';
import { LoggerToken } from '../../logger';

describe('#contextFactory', () => {
Expand All @@ -26,6 +28,34 @@ describe('#contextFactory', () => {
expect(lookup(context)(token1)).toEqual(O.some('test_1'));
expect(lookup(context)(token2)).toEqual(O.some('test_2'));
});

test('resolves async dependencies', async () => {
// given
const token1 = createContextToken('token_1');
const token2 = createContextToken('token_2');
const token3 = createContextToken('token_3');

// 1. "raw" Reader
const dependency1 = pipe(R.ask<Context>(), R.map(async _ => token1.name));

// 2. createReader
const dependency2 = createReader(async _ => await Promise.resolve(token2.name));

// 3. async factory function
const dependency3 = async () => token3.name;

// when
const context = await contextFactory(
bindEagerlyTo(token1)(dependency1),
bindEagerlyTo(token2)(dependency2),
bindEagerlyTo(token3)(dependency3),
);

// then
expect(lookup(context)(token1)).toEqual(O.some(token1.name));
expect(lookup(context)(token2)).toEqual(O.some(token2.name));
expect(lookup(context)(token3)).toEqual(O.some(token3.name));
});
});

describe('#constructContext', () => {
Expand Down