Skip to content

Commit

Permalink
fix: Added more thorough checks to RemoteData type guards
Browse files Browse the repository at this point in the history
  • Loading branch information
joanllenas committed May 17, 2021
1 parent 22dc191 commit 16dff7a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-remotedata",
"version": "6.0.0",
"version": "6.0.1",
"description": "RemoteData: Slaying a UI Antipattern with Angular",
"repository": {
"type": "git",
Expand Down
56 changes: 55 additions & 1 deletion projects/lib/src/lib/remote-data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,19 @@ import {
mapFailure,
isFailure,
isInProgress,
chain
chain,
isNotAsked,
isSuccess,
isRemoteData
} from './remote-data';

interface RemoteDataGuardsTestObj {
name: string;
fn: (rd: RemoteData<any, any>) => boolean;
truth: RemoteData<any, any>;
falses: any[];
}

describe('RemoteData', () => {
describe('notAsked', () => {
it('should have a "NotAsked" tag', () => {
Expand Down Expand Up @@ -138,4 +148,48 @@ describe('RemoteData', () => {
expect(ageResult).toEqual(failure('-3 is an invalid age'));
});
});

describe('Type Guard', () => {
([
{
name: 'isNotAsked',
fn: isNotAsked,
truth: notAsked(),
falses: [success('hola'), null, undefined, 'hola', {}]
},
{
name: 'isSuccess',
fn: isSuccess,
truth: success(1),
falses: [notAsked(), null, undefined, 'hola', {}]
},
{
name: 'isFailure',
fn: isFailure,
truth: failure('error!', 9),
falses: [success(9), null, undefined, 77, NaN]
},
{
name: 'isInProgress',
fn: isInProgress,
truth: inProgress(true),
falses: [failure('error!'), null, undefined, true, []]
},
{
name: 'isRemoteData',
fn: isRemoteData,
truth: notAsked(),
falses: [null, undefined, true, [], NaN, 8]
}
] as RemoteDataGuardsTestObj[]).forEach(test => {
describe(test.name, () => {
it('should be true', () => {
expect(test.fn(test.truth)).toBe(true);
});
it('should be false', () => {
test.falses.forEach(val => expect(test.fn(val)).toBe(false));
});
});
});
});
});
19 changes: 13 additions & 6 deletions projects/lib/src/lib/remote-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const isNotAsked = <T, E>(value: unknown): value is NotAsked => {
return (
value !== null &&
value !== undefined &&
(value as object).hasOwnProperty('tag') &&
(value as RemoteData<T, E>).tag === 'NotAsked'
);
};
Expand All @@ -65,6 +66,7 @@ export const isInProgress = <T, E>(value: unknown): value is InProgress<T> => {
return (
value !== null &&
value !== undefined &&
(value as object).hasOwnProperty('tag') &&
(value as RemoteData<T, E>).tag === 'InProgress'
);
};
Expand All @@ -73,6 +75,7 @@ export const isFailure = <T, E>(value: unknown): value is Failure<E, T> => {
return (
value !== null &&
value !== undefined &&
(value as object).hasOwnProperty('tag') &&
(value as RemoteData<T, E>).tag === 'Failure'
);
};
Expand All @@ -81,20 +84,24 @@ export const isSuccess = <T, E>(value: unknown): value is Success<T> => {
return (
value !== null &&
value !== undefined &&
(value as object).hasOwnProperty('tag') &&
(value as RemoteData<T, E>).tag === 'Success'
);
};

export const isRemoteData = <T, E = DefaultError>(
value: unknown
): value is RemoteData<T, E> => {
const hasRemoteDataTag = (tag: RemoteData<any, any>['tag']) =>
tag === 'NotAsked' ||
tag === 'InProgress' ||
tag === 'Success' ||
tag === 'Failure';
return (
(value !== null &&
value !== undefined &&
(value as RemoteData<T, E>).tag === 'NotAsked') ||
(value as RemoteData<T, E>).tag === 'InProgress' ||
(value as RemoteData<T, E>).tag === 'Success' ||
(value as RemoteData<T, E>).tag === 'Failure'
value !== null &&
value !== undefined &&
(value as object).hasOwnProperty('tag') &&
hasRemoteDataTag((value as RemoteData<T, E>).tag)
);
};

Expand Down

0 comments on commit 16dff7a

Please sign in to comment.