Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Nov 14, 2024
1 parent 2e83355 commit 7d9bd0e
Showing 1 changed file with 90 additions and 101 deletions.
191 changes: 90 additions & 101 deletions packages/signals/signals/src/core/buffer/__tests__/buffer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,123 +12,112 @@ describe(getSignalBuffer, () => {
})
await buffer.clear()
})

it('should instantiate without throwing an error', () => {
expect(buffer).toBeTruthy()
})
it('should add and clear', async () => {
const mockSignal = createInteractionSignal({
eventType: 'submit',
target: {},
describe('indexDB', () => {
it('should instantiate without throwing an error', () => {
expect(buffer).toBeTruthy()
})
await buffer.add(mockSignal)
await expect(buffer.getAll()).resolves.toEqual([mockSignal])
await buffer.clear()
await expect(buffer.getAll()).resolves.toHaveLength(0)
})

it('should delete older signals when maxBufferSize is exceeded', async () => {
const signals = range(15).map((_, idx) =>
createInteractionSignal({
idx: idx,
eventType: 'change',
target: {},
})
)

for (const signal of signals) {
await buffer.add(signal)
}

const storedSignals = await buffer.getAll()
expect(storedSignals).toHaveLength(10)
expect(storedSignals).toEqual(signals.slice(-10).reverse())
})

it('should delete older signals on initialize if current number exceeds maxBufferSize', async () => {
const signals = range(15).map((_, idx) =>
createInteractionSignal({
idx: idx,
eventType: 'change',
it('should add and clear', async () => {
const mockSignal = createInteractionSignal({
eventType: 'submit',
target: {},
})
)

for (const signal of signals) {
await buffer.add(signal)
}

// Re-initialize buffer
buffer = getSignalBuffer({
maxBufferSize: 10,
})

const storedSignals = await buffer.getAll()
expect(storedSignals).toHaveLength(10)
expect(storedSignals).toEqual(signals.slice(-10).reverse())
})

it('should clear signal buffer if there is a new session according to session storage', async () => {
const mockSignal = createInteractionSignal({
eventType: 'submit',
target: {},
await buffer.add(mockSignal)
await expect(buffer.getAll()).resolves.toEqual([mockSignal])
await buffer.clear()
await expect(buffer.getAll()).resolves.toHaveLength(0)
})
await buffer.add(mockSignal)
await expect(buffer.getAll()).resolves.toEqual([mockSignal])

// Simulate a new session by clearing session storage and re-initializing the buffer
sessionStorage.clear()
await sleep(100)
buffer = getSignalBuffer({
maxBufferSize: 10,
it('should delete older signals when maxBufferSize is exceeded', async () => {
const signals = range(15).map((_, idx) =>
createInteractionSignal({
idx: idx,
eventType: 'change',
target: {},
})
)

for (const signal of signals) {
await buffer.add(signal)
}

const storedSignals = await buffer.getAll()
expect(storedSignals).toHaveLength(10)
expect(storedSignals).toEqual(signals.slice(-10).reverse())
})

await expect(buffer.getAll()).resolves.toHaveLength(0)
})
})

describe('SignalStoreSessionStorage', () => {
let buffer: SignalBuffer
it('should delete older signals on initialize if current number exceeds maxBufferSize', async () => {
const signals = range(15).map((_, idx) =>
createInteractionSignal({
idx: idx,
eventType: 'change',
target: {},
})
)

for (const signal of signals) {
await buffer.add(signal)
}

// Re-initialize buffer
buffer = getSignalBuffer({
maxBufferSize: 10,
})

beforeEach(async () => {
sessionStorage.clear()
buffer = getSignalBuffer({
maxBufferSize: 10,
storageType: 'session',
const storedSignals = await buffer.getAll()
expect(storedSignals).toHaveLength(10)
expect(storedSignals).toEqual(signals.slice(-10).reverse())
})
await buffer.clear()
})

it('should instantiate without throwing an error', () => {
expect(buffer).toBeTruthy()
})
it('should clear signal buffer if there is a new session according to session storage', async () => {
const mockSignal = createInteractionSignal({
eventType: 'submit',
target: {},
})
await buffer.add(mockSignal)
await expect(buffer.getAll()).resolves.toEqual([mockSignal])

// Simulate a new session by clearing session storage and re-initializing the buffer
sessionStorage.clear()
await sleep(100)
buffer = getSignalBuffer({
maxBufferSize: 10,
})

it('should add and clear', async () => {
const mockSignal = createInteractionSignal({
eventType: 'submit',
target: {},
await expect(buffer.getAll()).resolves.toHaveLength(0)
})
await buffer.add(mockSignal)
await expect(buffer.getAll()).resolves.toEqual([mockSignal])
await buffer.clear()
await expect(buffer.getAll()).resolves.toHaveLength(0)
})
describe('sessionStorage', () => {
it('should instantiate without throwing an error', () => {
expect(buffer).toBeTruthy()
})

it('should delete older signals when maxBufferSize is exceeded', async () => {
const signals = range(15).map((_, idx) =>
createInteractionSignal({
idx: idx,
eventType: 'change',
it('should add and clear', async () => {
const mockSignal = createInteractionSignal({
eventType: 'submit',
target: {},
})
)

for (const signal of signals) {
await buffer.add(signal)
}
await buffer.add(mockSignal)
await expect(buffer.getAll()).resolves.toEqual([mockSignal])
await buffer.clear()
await expect(buffer.getAll()).resolves.toHaveLength(0)
})

const storedSignals = await buffer.getAll()
expect(storedSignals).toHaveLength(10)
expect(storedSignals).toEqual(signals.slice(-10).reverse())
it('should delete older signals when maxBufferSize is exceeded', async () => {
const signals = range(15).map((_, idx) =>
createInteractionSignal({
idx: idx,
eventType: 'change',
target: {},
})
)

for (const signal of signals) {
await buffer.add(signal)
}

const storedSignals = await buffer.getAll()
expect(storedSignals).toHaveLength(10)
expect(storedSignals).toEqual(signals.slice(-10).reverse())
})
})
})

0 comments on commit 7d9bd0e

Please sign in to comment.