Skip to content

Commit

Permalink
fix: Added pipe to module. More tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
joanllenas committed Oct 15, 2020
1 parent daa6597 commit bfb0431
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 69 deletions.
6 changes: 4 additions & 2 deletions projects/lib/src/lib/lib.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
GetFailureValuePipe,
GetInProgressPipe,
GetRemoteDataValuePipe,
AnyIsInProgressPipe
AnyIsInProgressPipe,
AnyIsNotAskedPipe
} from './pipes';

const declarations = [
Expand All @@ -24,7 +25,8 @@ const declarations = [
GetFailureValuePipe,
GetInProgressPipe,
GetRemoteDataValuePipe,
AnyIsInProgressPipe
AnyIsInProgressPipe,
AnyIsNotAskedPipe
];

@NgModule({
Expand Down
185 changes: 122 additions & 63 deletions projects/lib/src/lib/pipes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { ChangeDetectorRef, PipeTransform } from '@angular/core';
import { of } from 'rxjs';
import {
IsNotAskedPipe,
IsInProgressPipe,
IsFailurePipe,
IsSuccessPipe,
AnyIsInProgressPipe,
AnyIsNotAskedPipe,
GetRemoteDataValuePipe,
HasValuePipe,
GetRemoteDataValuePipe
IsFailurePipe,
IsInProgressPipe,
IsNotAskedPipe,
IsSuccessPipe
} from './pipes';
import { PipeTransform } from '@angular/core';
import {
NotAsked,
InProgress,
AnyRemoteData,
Failure,
Success,
AnyRemoteData
InProgress,
NotAsked,
Success
} from './remote-data';

describe('Boolean Pipes', () => {
Expand Down Expand Up @@ -80,60 +83,116 @@ describe('Boolean Pipes', () => {
});
});
});
describe('Value Pipes', () => {
([
{
PipeClass: GetRemoteDataValuePipe,
HasValuePipeClass: HasValuePipe,
rd: InProgress.of(false),
value: false,
hasValue: true
},
{
PipeClass: GetRemoteDataValuePipe,
HasValuePipeClass: HasValuePipe,
rd: InProgress.of(),
value: undefined,
hasValue: false
},
{
PipeClass: GetRemoteDataValuePipe,
HasValuePipeClass: HasValuePipe,
rd: Success.of('value'),
value: 'value',
hasValue: true
},
{
PipeClass: GetRemoteDataValuePipe,
HasValuePipeClass: HasValuePipe,
rd: Failure.of('error', 'value'),
value: 'value',
hasValue: true
},
{
PipeClass: GetRemoteDataValuePipe,
HasValuePipeClass: HasValuePipe,
rd: Failure.of('error'),
value: undefined,
hasValue: false
}
] as {
PipeClass: new () => PipeTransform;
HasValuePipeClass: new () => PipeTransform;
rd: AnyRemoteData;
value: any;
hasValue: boolean;
}[]).forEach(({ PipeClass, HasValuePipeClass, rd, value, hasValue }) => {
const pipeInstance = new PipeClass();
const hasValuePipeInstance = new HasValuePipeClass();
describe(`${pipeInstance.constructor.name}`, () => {
it(`hasValue should return ${hasValue} when instance is ${rd.constructor.name}`, () => {
expect(hasValuePipeInstance.transform(rd)).toBe(hasValue);
});
it(`should return ${value} when instance is ${rd.constructor.name}`, () => {
expect(pipeInstance.transform(rd)).toBe(value);
});
});

describe('Value Pipes', () => {
([
{
PipeClass: GetRemoteDataValuePipe,
HasValuePipeClass: HasValuePipe,
rd: InProgress.of(false),
value: false,
hasValue: true
},
{
PipeClass: GetRemoteDataValuePipe,
HasValuePipeClass: HasValuePipe,
rd: InProgress.of(),
value: undefined,
hasValue: false
},
{
PipeClass: GetRemoteDataValuePipe,
HasValuePipeClass: HasValuePipe,
rd: Success.of('value'),
value: 'value',
hasValue: true
},
{
PipeClass: GetRemoteDataValuePipe,
HasValuePipeClass: HasValuePipe,
rd: Failure.of('error', 'value'),
value: 'value',
hasValue: true
},
{
PipeClass: GetRemoteDataValuePipe,
HasValuePipeClass: HasValuePipe,
rd: Failure.of('error'),
value: undefined,
hasValue: false
}
] as {
PipeClass: new () => PipeTransform;
HasValuePipeClass: new () => PipeTransform;
rd: AnyRemoteData;
value: any;
hasValue: boolean;
}[]).forEach(({ PipeClass, HasValuePipeClass, rd, value, hasValue }) => {
const pipeInstance = new PipeClass();
const hasValuePipeInstance = new HasValuePipeClass();
describe(`${pipeInstance.constructor.name}`, () => {
it(`hasValue should return ${hasValue} when instance is ${rd.constructor.name}`, () => {
expect(hasValuePipeInstance.transform(rd)).toBe(hasValue);
});
it(`should return ${value} when instance is ${rd.constructor.name}`, () => {
expect(pipeInstance.transform(rd)).toBe(value);
});
});
});
});

// Mock Change Detector
const MyChangeDetector = class extends ChangeDetectorRef {
markForCheck(): void {}
detach(): void {}
detectChanges(): void {}
checkNoChanges(): void {}
reattach(): void {}
};

describe('AnyIsInProgressPipe', () => {
let cd: ChangeDetectorRef;
beforeEach(() => {
cd = new MyChangeDetector();
});
it('should return true when any is InProgress', () => {
expect(
new AnyIsInProgressPipe(cd).transform([
of(NotAsked.of()),
of(InProgress.of())
])
).toBe(true);
});
it('should return false when none is InProgress', () => {
expect(
new AnyIsInProgressPipe(cd).transform([
of(NotAsked.of()),
of(Failure.of('grr'))
])
).toBe(false);
});
});

describe('AnyIsNotAskedPipe', () => {
let cd: ChangeDetectorRef;
beforeEach(() => {
cd = new MyChangeDetector();
});
it('should return true when any is NotAsked', () => {
expect(
new AnyIsNotAskedPipe(cd).transform([
of(NotAsked.of()),
of(Failure.of('grr'))
])
).toBe(true);
});
it('should return false when none is NotAsked', () => {
expect(
new AnyIsNotAskedPipe(cd).transform([
of(Success.of('ok')),
of(Failure.of('grr'))
])
).toBe(false);
});
});
9 changes: 5 additions & 4 deletions projects/lib/src/lib/remote-data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ describe('RemoteData', () => {
});

describe('Failure', () => {
it('should be able to extract the wrapped error', () => {
it('should be able to extract the wrapped error and value', () => {
const err = 'Ouch!';
expect((Failure.of(err, '') as Failure<string, string>).error()).toBe(
err
);
const value = { type: 'DoStuff' };
const f = Failure.of(err, value) as Failure<string, typeof value>;
expect(f.error()).toBe(err);
expect(f.value()).toBe(value);
});
});
});

0 comments on commit bfb0431

Please sign in to comment.