Skip to content

Commit

Permalink
feature: classless (#35)
Browse files Browse the repository at this point in the history
Removed classes in favour of top-level functions to be better aligned with `[email protected]`

BREAKING CHANGE: removed classes
BREAKING CHANGE: simplified `io-ts` codec
  • Loading branch information
raveclassic committed Aug 23, 2019
1 parent b7ad152 commit 7351a88
Show file tree
Hide file tree
Showing 5 changed files with 1,084 additions and 1,567 deletions.
44 changes: 22 additions & 22 deletions src/__tests__/remote-data-io.spec.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import { createRemoteDataFromJSON } from '../remote-data-io';
import * as t from 'io-ts';
import { initial, pending, failure, success, progress } from '../remote-data';
import { right } from 'fp-ts/lib/Either';
import { none, some } from 'fp-ts/lib/Option';
import { number, string } from 'io-ts';

describe('RemoteDataFromJSONType', () => {
it('createRemoteDataFromJSON', () => {
const T = createRemoteDataFromJSON(t.string, t.number);
expect(T.decode({ type: 'Failure', error: 'error' })).toEqual(right(failure('error')));
expect(T.decode({ type: 'Initial' })).toEqual(right(initial));
expect(T.decode({ type: 'Pending', progress: null })).toEqual(right(pending));
expect(T.decode({ type: 'Pending', progress: { loaded: 2, total: null } })).toEqual(
const codec = createRemoteDataFromJSON(string, number);
expect(codec.decode({ _tag: 'RemoteFailure', error: 'error' })).toEqual(right(failure('error')));
expect(codec.decode({ _tag: 'RemoteInitial' })).toEqual(right(initial));
expect(codec.decode({ _tag: 'RemotePending', progress: null })).toEqual(right(pending));
expect(codec.decode({ _tag: 'RemotePending', progress: { loaded: 2, total: null } })).toEqual(
right(progress({ loaded: 2, total: none })),
);
expect(T.decode({ type: 'Pending', progress: { loaded: 2, total: 5 } })).toEqual(
expect(codec.decode({ _tag: 'RemotePending', progress: { loaded: 2, total: 5 } })).toEqual(
right(progress({ loaded: 2, total: some(5) })),
);
expect(T.decode({ type: 'Success', value: 42 })).toEqual(right(success(42)));
expect(T.encode(failure('error'))).toEqual({ type: 'Failure', error: 'error' });
expect(T.encode(initial)).toEqual({ type: 'Initial' });
expect(T.encode(pending)).toEqual({ type: 'Pending', progress: null });
expect(T.encode(progress({ loaded: 2, total: none }))).toEqual({
type: 'Pending',
expect(codec.decode({ _tag: 'RemoteSuccess', value: 42 })).toEqual(right(success(42)));
expect(codec.encode(failure('error'))).toEqual({ _tag: 'RemoteFailure', error: 'error' });
expect(codec.encode(initial)).toEqual({ _tag: 'RemoteInitial' });
expect(codec.encode(pending)).toEqual({ _tag: 'RemotePending', progress: null });
expect(codec.encode(progress({ loaded: 2, total: none }))).toEqual({
_tag: 'RemotePending',
progress: { loaded: 2, total: null },
});
expect(T.encode(progress({ loaded: 2, total: some(5) }))).toEqual({
type: 'Pending',
expect(codec.encode(progress({ loaded: 2, total: some(5) }))).toEqual({
_tag: 'RemotePending',
progress: { loaded: 2, total: 5 },
});
expect(T.encode(success(42))).toEqual({ type: 'Success', value: 42 });
expect(T.is(failure('error'))).toBe(true);
expect(T.is(initial)).toBe(true);
expect(T.is(pending)).toBe(true);
expect(T.is(success(42))).toBe(true);
expect(T.is(failure(1))).toBe(false);
expect(T.is(success('invalid'))).toBe(false);
expect(codec.encode(success(42))).toEqual({ _tag: 'RemoteSuccess', value: 42 });
expect(codec.is(failure('error'))).toBe(true);
expect(codec.is(initial)).toBe(true);
expect(codec.is(pending)).toBe(true);
expect(codec.is(success(42))).toBe(true);
expect(codec.is(failure(1))).toBe(false);
expect(codec.is(success('invalid'))).toBe(false);
});
});
17 changes: 14 additions & 3 deletions src/__tests__/remote-data-t.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { identity, constant } from 'fp-ts/lib/function';

import * as remoteDataT from '../remote-data-t';
import * as remoteData from '../remote-data';
import { pipe } from 'fp-ts/lib/pipeable';
import { fold } from '../remote-data';

describe('RemoteDataT', () => {
it('chain', async done => {
Expand All @@ -24,7 +26,10 @@ describe('RemoteDataT', () => {
const rdTask = success(task.of(42));
const rd = await rdTask();

rd.foldL(fail, fail, fail, (n: number) => expect(n).toEqual(42));
pipe(
rd,
fold(fail, fail, fail, (n: number) => expect(n).toEqual(42)),
);
done();
});

Expand All @@ -33,7 +38,10 @@ describe('RemoteDataT', () => {
const rdTask = failure(task.of(new Error('oops')));
const rd = await rdTask();

rd.foldL(fail, fail, (e: Error) => expect(e.message).toEqual('oops'), fail);
pipe(
rd,
fold(fail, fail, (e: Error) => expect(e.message).toEqual('oops'), fail),
);
done();
});

Expand All @@ -42,7 +50,10 @@ describe('RemoteDataT', () => {
const rdTask = fromRemoteData(remoteData.success(42));
const rd = await rdTask();

rd.foldL(fail, fail, fail, (n: number) => expect(n).toEqual(42));
pipe(
rd,
fold(fail, fail, fail, (n: number) => expect(n).toEqual(42)),
);
done();
});

Expand Down
Loading

0 comments on commit 7351a88

Please sign in to comment.