diff --git a/packages/primitives/package.json b/packages/primitives/package.json index b8cdbc0..9d3c48e 100644 --- a/packages/primitives/package.json +++ b/packages/primitives/package.json @@ -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/*" + ] } } diff --git a/packages/primitives/src/boolean.ts b/packages/primitives/src/boolean.ts new file mode 100644 index 0000000..a22eb42 --- /dev/null +++ b/packages/primitives/src/boolean.ts @@ -0,0 +1,7 @@ +/** + * Boolean FHIR Primitive Runtime Type + */ + +import { BooleanType, boolean } from "io-ts"; + +export { BooleanType as Boolean, boolean }; diff --git a/packages/primitives/src/index.ts b/packages/primitives/src/index.ts index 37f9aa5..6507868 100644 --- a/packages/primitives/src/index.ts +++ b/packages/primitives/src/index.ts @@ -1,3 +1,7 @@ /** * FHIR Primitive Runtime Types */ + +import { Boolean, boolean } from "./boolean"; + +export { Boolean, boolean }; diff --git a/packages/primitives/test/boolean.test.ts b/packages/primitives/test/boolean.test.ts new file mode 100644 index 0000000..21d7395 --- /dev/null +++ b/packages/primitives/test/boolean.test.ts @@ -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); + }); +}); diff --git a/packages/primitives/test/helpers.ts b/packages/primitives/test/helpers.ts new file mode 100644 index 0000000..f559342 --- /dev/null +++ b/packages/primitives/test/helpers.ts @@ -0,0 +1,32 @@ +/** + * Test helpers + */ + +import { PathReporter } from "io-ts/lib/PathReporter"; +import * as t from "io-ts"; + +export const assertSuccess = (validation: t.Validation): void => { + expect(validation.isRight()).toBe(true); +}; + +export const assertFailure = ( + validation: t.Validation, + descriptions: Array +): void => { + expect(validation.isLeft()); + expect(PathReporter.report(validation)).toEqual(descriptions); +}; + +export const assertStrictEqual = ( + validation: t.Validation, + value: any +): void => { + expect(validation.fold(t.identity, t.identity)).toBe(value); +}; + +export const assertDeepEqual = ( + validation: t.Validation, + value: any +): void => { + expect(validation.fold(t.identity, t.identity)).toEqual(value); +};