Skip to content

Commit

Permalink
Add tests for non-error throws
Browse files Browse the repository at this point in the history
  • Loading branch information
jviide committed Oct 8, 2022
1 parent 90a0095 commit 6d0b577
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions packages/core/test/signal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ describe("computed()", () => {
expect(() => effect(() => a.value)).to.not.throw();
});

it("should store failures and recompute only after a dependency changes", () => {
it("should store thrown errors and recompute only after a dependency changes", () => {
const a = signal(0);
const spy = sinon.spy(() => {
a.value;
Expand All @@ -880,6 +880,39 @@ describe("computed()", () => {
expect(spy).to.be.calledTwice;
});

it("should store thrown non-errors and recompute only after a dependency changes", () => {
const a = signal(0);
const spy = sinon.spy();
const c = computed(() => {
a.value;
spy();
throw undefined;
});

try {
c.value;
expect.fail();
} catch (err) {
expect(err).to.be.undefined;
}
try {
c.value;
expect.fail();
} catch (err) {
expect(err).to.be.undefined;
}
expect(spy).to.be.calledOnce;

a.value = 1;
try {
c.value;
expect.fail();
} catch (err) {
expect(err).to.be.undefined;
}
expect(spy).to.be.calledTwice;
});

it("should conditionally unsubscribe from signals", () => {
const a = signal("a");
const b = signal("b");
Expand Down Expand Up @@ -1529,14 +1562,25 @@ describe("batch/transaction", () => {
expect(batch(() => 1)).to.equal(1);
});

it("should throw the error raised from the callback", () => {
it("should throw errors throws from the callback", () => {
expect(() =>
batch(() => {
throw Error("hello");
})
).to.throw("hello");
});

it("should throw non-errors thrown from the callback", () => {
try {
batch(() => {
throw undefined;
});
expect.fail();
} catch (err) {
expect(err).to.be.undefined;
}
});

it("should delay writes", () => {
const a = signal("a");
const b = signal("b");
Expand Down

0 comments on commit 6d0b577

Please sign in to comment.