Skip to content

Commit

Permalink
feat: add requireSync to computedAsync for better typings (#255)
Browse files Browse the repository at this point in the history
* feat: add require-sync

* fix

* fix: improve impl

* feat: improve types

* fix: remove unnecessary assertion

* fix: code improvements
  • Loading branch information
eneajaho authored Jan 25, 2024
1 parent 12b9bae commit 5e428be
Show file tree
Hide file tree
Showing 2 changed files with 295 additions and 67 deletions.
62 changes: 58 additions & 4 deletions libs/ngxtension/computed-async/src/computed-async.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,59 @@ describe(computedAsync.name, () => {
}));
});

describe('works with requireSync', () => {
it('returns undefined for sync observables if not enabled', fakeAsync(() => {
TestBed.runInInjectionContext(() => {
const value = signal(1);
const s = computedAsync(() => of(value()));
expect(s()).toEqual(undefined); // initial value
});
}));
it('returns correct value and doesnt throw error if enabled', fakeAsync(() => {
TestBed.runInInjectionContext(() => {
const value = signal(1);
const s = computedAsync(() => of(value()), { requireSync: true });
expect(s()).toEqual(1); // initial value
});
}));
it('returns correct value and doesnt throw error with initial value provided', () => {
TestBed.runInInjectionContext(() => {
const s = computedAsync(() => of(1), { initialValue: 2 });
expect(s()).toEqual(2); // initial value
TestBed.flushEffects();
expect(s()).toEqual(1);
});
});
it('returns correct value and doesnt throw error with normal variable when enabled', fakeAsync(() => {
TestBed.runInInjectionContext(() => {
const s = computedAsync(() => 1, { requireSync: true });
expect(s()).toEqual(1); // initial value
});
}));
it('throws error for promises', () => {
TestBed.runInInjectionContext(() => {
const value = signal(1);
expect(() => {
const s: never = computedAsync(() => promise(value()), {
requireSync: true,
});
s;
}).toThrow(/Promises cannot be used with requireSync/i);
});
});
});

describe('works with promises', () => {
it('waits for them to resolve', fakeAsync(() => {
TestBed.runInInjectionContext(() => {
const logs: number[] = [];
const value = signal(1);

const s = computedAsync(() => {
return promise(value(), 100).then((v) => logs.push(v));
return promise(value(), 100).then((v) => {
logs.push(v);
return v;
});
});
expect(s()).toEqual(undefined); // initial value
TestBed.flushEffects();
Expand Down Expand Up @@ -353,23 +398,31 @@ describe(computedAsync.name, () => {
return of(1);
});

// : Signal<number | null>
// : Signal<number | null | undefined>
const c = computedAsync(
() => {
return 1;
},
{ initialValue: null },
);

// : Signal<string>
// : Signal<string | undefined>
const d = computedAsync(
() => {
return '';
},
{ initialValue: '' },
);

// : Signal<string | undefined>
// : Signal<string>
const d1 = computedAsync(
() => {
return '';
},
{ initialValue: '', requireSync: true },
);

// : Signal<string | undefined>
const e = computedAsync(
() => {
return '';
Expand All @@ -381,6 +434,7 @@ describe(computedAsync.name, () => {
expect(b).toBeTruthy();
expect(c).toBeTruthy();
expect(d).toBeTruthy();
expect(d1).toBeTruthy();
expect(e).toBeTruthy();
});
});
Expand Down
Loading

0 comments on commit 5e428be

Please sign in to comment.