Skip to content

Commit

Permalink
Merge pull request #431 from abraham/guards
Browse files Browse the repository at this point in the history
Add type guards
  • Loading branch information
abraham authored Feb 19, 2022
2 parents e66b71f + 716b9c2 commit be55d41
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import { Failure, fold, Initialized, Kinds, Pending, Success } from './index';
import {
Failure,
fold,
Initialized,
isFailure,
isInitialized,
isPending,
isSuccess,
Kinds,
Pending,
Success,
} from './index';

test('Kinds', () => {
expect(Kinds).toEqual({
Expand All @@ -12,11 +23,15 @@ test('Kinds', () => {
test('Initialized', () => {
expect(new Initialized()).toBeInstanceOf(Initialized);
expect(new Initialized().kind).toEqual('Initialized');
expect(isInitialized(new Initialized())).toBeTruthy();
expect(isInitialized(new Pending())).toBeFalsy();
});

test('Pending', () => {
expect(new Pending()).toBeInstanceOf(Pending);
expect(new Pending().kind).toEqual('Pending');
expect(isPending(new Pending())).toBeTruthy();
expect(isPending(new Initialized())).toBeFalsy();
});

test('Success', () => {
Expand All @@ -25,10 +40,16 @@ test('Success', () => {
expect(state).toBeInstanceOf(Success);
expect(state.kind).toEqual('Success');
expect(state.data).toEqual(data);
expect(isSuccess(state)).toBeTruthy();
expect(isSuccess(new Initialized())).toBeFalsy();
});

test('Success without data', () => {
expect(() => new Success()).toThrowError('Parameter "data" is required');
expect(() => new Success(null)).toThrowError('Parameter "data" is required');
expect(() => new Success(undefined)).toThrowError(
'Parameter "data" is required',
);
});

test('Failure', () => {
Expand All @@ -37,10 +58,16 @@ test('Failure', () => {
expect(state).toBeInstanceOf(Failure);
expect(state.kind).toEqual('Failure');
expect(state.error).toEqual(error);
expect(isFailure(state)).toBeTruthy();
expect(isFailure(new Initialized())).toBeFalsy();
});

test('Failure without error', () => {
expect(() => new Failure()).toThrowError('Parameter "error" is required');
expect(() => new Failure(null)).toThrowError('Parameter "error" is required');
expect(() => new Failure(undefined)).toThrowError(
'Parameter "error" is required',
);
});

test('fold initialized', () => {
Expand Down
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,12 @@ export function fold<T, E, D>(
}
};
}

export const isInitialized = (state: unknown): state is Initialized =>
state instanceof Initialized;
export const isPending = (state: unknown): state is Pending =>
state instanceof Pending;
export const isFailure = <E>(state: unknown): state is Failure<E> =>
state instanceof Failure;
export const isSuccess = <D>(state: unknown): state is Success<D> =>
state instanceof Success;

0 comments on commit be55d41

Please sign in to comment.