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

Add type guards #431

Merged
merged 1 commit into from
Feb 19, 2022
Merged
Show file tree
Hide file tree
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
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;