Skip to content

Commit

Permalink
Showing 5 changed files with 99 additions and 1 deletion.
24 changes: 23 additions & 1 deletion packages/primitives/package.json
Original file line number Diff line number Diff line change
@@ -15,8 +15,30 @@
"files": [
"src"
],
"scripts": {},
"scripts": {
"coverage": "jest --coverage",
"test": "jest"
},
"devDependencies": {
"typescript": "^3.2.2"
},
"dependencies": {
"fp-ts": "^1.12.3",
"io-ts": "^1.5.2"
},
"jest": {
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|\\.(test))\\.(ts|tsx|js)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json"
],
"collectCoverageFrom": [
"src/*"
]
}
}
7 changes: 7 additions & 0 deletions packages/primitives/src/boolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Boolean FHIR Primitive Runtime Type
*/

import { BooleanType, boolean } from "io-ts";

export { BooleanType as Boolean, boolean };
4 changes: 4 additions & 0 deletions packages/primitives/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* FHIR Primitive Runtime Types
*/

import { Boolean, boolean } from "./boolean";

export { Boolean, boolean };
33 changes: 33 additions & 0 deletions packages/primitives/test/boolean.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Tests for Boolean Runtime Type
*/

import { boolean } from "../src";
import { assertSuccess, assertFailure, assertStrictEqual } from "./helpers";

describe("boolean", () => {
it("should succeed validating a valid value", () => {
const T = boolean;
assertSuccess(T.decode(true));
});

it("should return the same reference if validation succeeded and nothing changed", () => {
const T = boolean;
const value = false;
assertStrictEqual(T.decode(value), value);
});

it("should fail validating an invalid value", () => {
const T = boolean;
assertFailure(T.decode("abc"), [
'Invalid value "abc" supplied to : boolean'
]);
});

it("should type guard", () => {
const T = boolean;
expect(T.is(false)).toEqual(true);
expect(T.is("b")).toEqual(false);
expect(T.is(undefined)).toEqual(false);
});
});
32 changes: 32 additions & 0 deletions packages/primitives/test/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Test helpers
*/

import { PathReporter } from "io-ts/lib/PathReporter";
import * as t from "io-ts";

export const assertSuccess = <T>(validation: t.Validation<T>): void => {
expect(validation.isRight()).toBe(true);
};

export const assertFailure = <T>(
validation: t.Validation<T>,
descriptions: Array<string>
): void => {
expect(validation.isLeft());
expect(PathReporter.report(validation)).toEqual(descriptions);
};

export const assertStrictEqual = <T>(
validation: t.Validation<T>,
value: any
): void => {
expect(validation.fold<any>(t.identity, t.identity)).toBe(value);
};

export const assertDeepEqual = <T>(
validation: t.Validation<T>,
value: any
): void => {
expect(validation.fold<any>(t.identity, t.identity)).toEqual(value);
};

0 comments on commit a8a2257

Please sign in to comment.