-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
b7ad152
commit 7351a88
Showing
5 changed files
with
1,084 additions
and
1,567 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.