Skip to content

Commit

Permalink
df
Browse files Browse the repository at this point in the history
  • Loading branch information
molszanski committed Nov 17, 2024
1 parent 7454a53 commit 08f6596
Showing 1 changed file with 44 additions and 39 deletions.
83 changes: 44 additions & 39 deletions iti/tests/update.api.spec.ts → iti/tests/update.api.vi.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect, vi, beforeEach } from "vitest"
import { createContainer } from "../src/iti"
import { wait } from "./_utils"

Expand Down Expand Up @@ -31,7 +32,7 @@ describe("Deleting and destructuring: ", () => {
// update B from null to a Primitive
r.upsert({ b: "new B" })
expect(r.get("b")).toBe("new B")
}, 100)
})

it("should be able to delete a token", () => {
let r = root.add({ a: "A", b: "B", c: "C" })
Expand All @@ -48,50 +49,49 @@ describe("Deleting and destructuring: ", () => {
}).toThrow()
})

it("should send containerUpdated event on overwrite", (cb) => {
;(async () => {
root.on("containerDeleted", async (k) => {
it("should send containerUpdated event on overwrite", async () => {
const cb = vi.fn()
root.on("containerDeleted", async (k) => {
expect(k.key).toBe("b")
root.on("containerUpserted", (k) => {
expect(k.key).toBe("b")
root.on("containerUpserted", (k) => {
expect(k.key).toBe("b")
expect(k.newContainer).toBe("new B")
cb()
})
await wait(5)
root.upsert({ b: "new B" })
expect(k.newContainer).toBe("new B")
cb()
})
await wait(5)
root.upsert({ b: "new B" })
})

root.add({ a: "A", b: "B" }).delete("b")
})()
}, 100)
root.add({ a: "A", b: "B" }).delete("b")
await wait(20)
expect(cb).toHaveBeenCalledTimes(1)
})

it("should send containerUpdated event on overwrite", (cb) => {
;(async () => {
const node = root.add(() => ({
a: "A",
b: "B",
}))
let f1 = jest.fn()
let f2 = jest.fn()
it("should send containerUpdated event on overwrite", async () => {
const node = root.add(() => ({
a: "A",
b: "B",
}))
const f1 = vi.fn()
const f2 = vi.fn()

node.subscribeToContainer("a", f1)
node.subscribeToContainerSet(["a", "b"], f2)
node.subscribeToContainer("a", f1)
node.subscribeToContainerSet(["a", "b"], f2)

node.delete("a")
node.delete("a")

await wait(10)
await wait(10)

expect(f1).toHaveBeenCalledTimes(1)
/**
* 2 becaus we have subscribed to two container, and this will provide us
* with two of those, hence two updates because two creations
*/
expect(f2).toHaveBeenCalledTimes(1)
cb()
})()
}, 100)
expect(f1).toHaveBeenCalledTimes(1)
/**
* 2 because we have subscribed to two container, and this will provide us
* with two of those, hence two updates because two creations
*/
expect(f2).toHaveBeenCalledTimes(1)
})

it("should send error if we remove a token some container listens to", (cb) => {
it("should send error if we remove a token some container listens to", async () => {
const cb = vi.fn()
const node = root.add(() => ({
a: "A",
b: "B",
Expand All @@ -101,9 +101,12 @@ describe("Deleting and destructuring: ", () => {
cb()
})
node.delete("a")
}, 100)
await wait(10)
expect(cb).toHaveBeenCalledTimes(1)
})

it("should send error if we remove a token some containerSet listens to", (cb) => {
it("should send error if we remove a token some containerSet listens to", async () => {
const cb = vi.fn()
const node = root.add(() => ({
a: "A",
b: "B",
Expand All @@ -113,5 +116,7 @@ describe("Deleting and destructuring: ", () => {
cb()
})
node.delete("a")
}, 100)
await wait(10)
expect(cb).toHaveBeenCalledTimes(1)
})
})

0 comments on commit 08f6596

Please sign in to comment.