Created
September 3, 2021 19:13
-
-
Save tgrecojs/34ed14c1c9567f5e15e22faa9adeda6c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Either = (() => { | |
const Right = x => ({ | |
chain: f => f(x), | |
ap: other => other.map(x), | |
alt: other => Right(x), | |
extend: f => f(Right(x)), | |
concat: other => | |
other.fold( | |
x => other, | |
y => Right(x.concat(y)), | |
), | |
traverse: (of, f) => f(x).map(Right), | |
map: f => Right(f(x)), | |
fold: (_, g) => g(x), | |
inspect: () => `Right(${x})`, | |
}); | |
const Left = x => ({ | |
chain: _ => Left(x), | |
ap: _ => Left(x), | |
extend: _ => Left(x), | |
alt: other => other, | |
concat: _ => Left(x), | |
traverse: (of, _) => of(Left(x)), | |
map: _ => Left(x), | |
fold: (f, _) => f(x), | |
inspect: () => `Left(${x})`, | |
}); | |
const of = Right; | |
const tryCatch = f => { | |
try { | |
return Right(f()); | |
} catch (e) { | |
return Left(e); | |
} | |
}; | |
const fromNullable = x => (x != null ? Right(x) : Left(x)); | |
return { Right, Left, of, tryCatch, fromNullable }; | |
})(); | |
const { tryCatch } = Either; | |
const handleError = ( | |
uiMessage = 'Default error message for UI', | |
) => error => ({ | |
error, | |
uiMessage, | |
}); | |
const id = x => x; | |
test('assertIssuerKeywords:: without try...catch ## happy path', async t => { | |
const issuerKeywordRecord = harden({ | |
Central: linkIssuer, | |
Secondary: bldIssuer, | |
}); | |
const { zcf } = await setupZCFTest(issuerKeywordRecord); | |
t.deepEqual( | |
typeof assertIssuerKeywords(zcf, ['Central', 'Secondary']) === 'undefined', | |
true, | |
'test', | |
); | |
}); | |
test('assertIssuerKeywords:: with error', async t => { | |
const issuerKeywordRecord = harden({ | |
Central: linkIssuer, | |
}); | |
const { zcf } = await setupZCFTest(issuerKeywordRecord); | |
const safeAssertIssuerKeywords = tryCatch(() => | |
assertIssuerKeywords(zcf, ['Wrong']), | |
); | |
const displayString = 'Error with issuer keywords.'; | |
const actual = safeAssertIssuerKeywords.fold(handleError(displayString), id) | |
t.is(typeof actual.error, 'object', 'should return an object containing an Error object.') | |
t.deepEqual( | |
actual.uiMessage === displayString, | |
true, | |
'should handle the error correctly.', | |
); | |
}); | |
test('assertIssuerKeywords:: happy path', async t => { | |
const issuerKeywordRecord = harden({ | |
Central: linkIssuer, | |
}); | |
const { zcf } = await setupZCFTest(issuerKeywordRecord); | |
const safeAssertIssuerKeywords = tryCatch(() => assertIssuerKeywords(zcf, ['Central'])); | |
const actual = safeAssertIssuerKeywords.fold(handleError(), id) | |
t.deepEqual( | |
actual === undefined, | |
true, | |
'should return undefined.', | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment