Skip to content

Commit 1772c84

Browse files
committed
Ephemeral data resets timeout on recollect
1 parent 468fcdd commit 1772c84

File tree

4 files changed

+182
-159
lines changed

4 files changed

+182
-159
lines changed

tests/basic.test.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { describe, test } from 'vitest'
2+
import {
3+
DEFAULT_DECAY_RATE,
4+
arrayState,
5+
decayingUsers,
6+
myCollection,
7+
myCollectionUndefined,
8+
objectState,
9+
stringState,
10+
} from './test-utils'
11+
import { instance, state } from '@plexusjs/core'
12+
13+
describe('Collections Core Functionality', () => {
14+
test('Can create collection', () => {
15+
expect(myCollection.value.length).toBe(0)
16+
// can properly collect data
17+
myCollection.collect({ thing: 'lol', id: 0 })
18+
expect(myCollection.value.length).toBe(1)
19+
// myCollection.getSelector("")
20+
// myCollection.getGroup("group1")
21+
myCollection.collect([
22+
{ thing: 'lol3', id: 2 },
23+
{ thing: 'lols', id: 1 },
24+
])
25+
console.log('reeeee, myCollection.value', myCollection.value)
26+
// can return the data values as an array
27+
expect(myCollection.value[0].thing).toBe('lol')
28+
expect(myCollection.value[1].thing).toBe('lol3')
29+
expect(myCollection.value[2].thing).toBe('lols')
30+
31+
// can properly retrieve data values
32+
expect(myCollection.getItemValue('0')?.thing).toBe('lol')
33+
expect(myCollection.getItemValue('2')?.thing).toBe('lol3')
34+
expect(myCollection.getItemValue('1')?.thing).toBe('lols')
35+
36+
// does the unfoundKeyReturnsUndefined configuration work
37+
expect(myCollectionUndefined.getItemValue('1')).toBeUndefined()
38+
console.log('an undefined object', myCollectionUndefined.getItemValue('1'))
39+
})
40+
41+
test('ingest multiple values in collect', () => {
42+
expect(myCollectionUndefined.getItemValue('1')).toBeUndefined()
43+
myCollectionUndefined.collect([
44+
{ thing: 'lol3', id: 2 },
45+
{ thing: 'lols', id: 1 },
46+
])
47+
expect(myCollectionUndefined.value.length).toBe(2)
48+
expect(myCollectionUndefined.value.length).toBe(2)
49+
})
50+
test('Does it pass the vibe check ?', () => {
51+
myCollection.collect({ thing: 'xqcL', id: 0 })
52+
expect(myCollection.getItem('0').value?.thing).toBe('xqcL')
53+
})
54+
})
55+
56+
describe('State Core Functionality', () => {
57+
test('Can save a value', () => {
58+
const value = state(1)
59+
expect(value.value).toBe(1)
60+
})
61+
62+
test('Change value and remember the old one', () => {
63+
const value = state(1)
64+
value.set(2)
65+
expect(value.value).toBe(2)
66+
expect(value.lastValue).toBe(1)
67+
})
68+
69+
test('Checking state().set()', () => {
70+
// check .set(value: object)
71+
objectState.set({ a: { b: false } })
72+
// check if the object is actually merged and children props do get overwritten
73+
expect(objectState.value.a?.b).toBe(false)
74+
})
75+
76+
test('Checking state().patch()', () => {
77+
// can the object deep merge?
78+
objectState.patch({ a: { b: false } })
79+
expect(objectState.value.a?.a).toBe(true)
80+
// check that other value is still there
81+
expect(objectState.value.b).toBe(true)
82+
// changed intended value
83+
expect(objectState.value.a?.b).toBe(false)
84+
85+
// console.log(arrayState.value)
86+
// check array deep merge
87+
arrayState.patch([{ item: 'Hello2' }])
88+
// console.log(arrayState.value)
89+
expect(arrayState.value[0].item).toBe('Hello2')
90+
expect(arrayState.value[0].item2).toStrictEqual({ subitem: 'World' })
91+
expect(arrayState.value[1].item).toBe('Goodbye')
92+
})
93+
94+
test('Checking state.watch()', () => {
95+
let callbackCalled = false
96+
97+
// can add watcher
98+
const watcherDestroyer = stringState.watch((value) => {
99+
console.log('callback called', value)
100+
callbackCalled = true
101+
})
102+
console.log(instance().runtime.engine.events.entries())
103+
stringState.set('Hello World')
104+
expect(callbackCalled).toBe(true)
105+
// can remove watcher
106+
// stringState.removeWatcher(watcherKey);
107+
watcherDestroyer()
108+
// console.log(watcherDestroyer.toString(), instance().runtime.engine.events.entries())
109+
stringState.set('new value')
110+
expect(callbackCalled).toBe(true)
111+
})
112+
})

tests/collection.test.ts

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6,79 +6,17 @@ import {
66
uniqueGroups,
77
decayingUsers,
88
DEFAULT_DECAY_RATE,
9+
myCollection,
10+
myCollectionUndefined,
911
} from './test-utils'
1012
import { s } from 'vitest/dist/types-63abf2e0'
1113

12-
const myCollection = collection<{
13-
thing: string
14-
id: number
15-
obj?: {
16-
arr: {
17-
item1: string
18-
name?: string
19-
}[]
20-
}
21-
}>({
22-
defaultGroup: true,
23-
sort(a, b) {
24-
return a.thing.localeCompare(b.thing)
25-
},
26-
})
27-
.createGroups(['group1', 'group2'])
28-
.createSelector('main')
29-
const myCollectionUndefined = collection<{ thing: string; id: number }>({
30-
defaultGroup: true,
31-
})
32-
.createGroups(['group1', 'group2'])
33-
.createSelector('main')
34-
3514
// instance({ logLevel: "debug" })
3615
beforeEach(() => {
3716
myCollection.clear()
3817
myCollectionUndefined.clear()
3918
})
4019
describe('Testing Collection', () => {
41-
test('Can create collection', () => {
42-
expect(myCollection.value.length).toBe(0)
43-
// can properly collect data
44-
myCollection.collect({ thing: 'lol', id: 0 })
45-
expect(myCollection.value.length).toBe(1)
46-
// myCollection.getSelector("")
47-
// myCollection.getGroup("group1")
48-
myCollection.collect([
49-
{ thing: 'lol3', id: 2 },
50-
{ thing: 'lols', id: 1 },
51-
])
52-
console.log('reeeee, myCollection.value', myCollection.value)
53-
// can return the data values as an array
54-
expect(myCollection.value[0].thing).toBe('lol')
55-
expect(myCollection.value[1].thing).toBe('lol3')
56-
expect(myCollection.value[2].thing).toBe('lols')
57-
58-
// can properly retrieve data values
59-
expect(myCollection.getItemValue('0')?.thing).toBe('lol')
60-
expect(myCollection.getItemValue('2')?.thing).toBe('lol3')
61-
expect(myCollection.getItemValue('1')?.thing).toBe('lols')
62-
63-
// does the unfoundKeyReturnsUndefined configuration work
64-
expect(myCollectionUndefined.getItemValue('1')).toBeUndefined()
65-
console.log('an undefined object', myCollectionUndefined.getItemValue('1'))
66-
})
67-
68-
test('ingest multiple values in collect', () => {
69-
expect(myCollectionUndefined.getItemValue('1')).toBeUndefined()
70-
myCollectionUndefined.collect([
71-
{ thing: 'lol3', id: 2 },
72-
{ thing: 'lols', id: 1 },
73-
])
74-
expect(myCollectionUndefined.value.length).toBe(2)
75-
expect(myCollectionUndefined.value.length).toBe(2)
76-
})
77-
test('Does it pass the vibe check ?', () => {
78-
myCollection.collect({ thing: 'xqcL', id: 0 })
79-
expect(myCollection.getItem('0').value?.thing).toBe('xqcL')
80-
})
81-
8220
test('Watching Data', () => {
8321
myCollection.collect([
8422
{ thing: 'lol', id: 0 },

tests/state.test.ts

Lines changed: 17 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,14 @@
11
'use strict'
22
import { beforeEach, afterEach, describe, test, expect } from 'vitest'
33
import { instance, state } from '@plexusjs/core'
4-
5-
type ObjectStateExample = Partial<{
6-
a: { a?: boolean; b?: boolean }
7-
b: boolean
8-
c: { b?: boolean }
9-
}>
10-
type UnionString = 'a' | 'b' | 'c'
11-
12-
const initialValue = {
13-
boolean: true,
14-
string: 'Hello Plexus!',
15-
object: { a: { a: true, b: true }, b: true },
16-
array: [
17-
{ item: 'Hello', item2: { subitem: 'World' } },
18-
{ item: 'Goodbye', item2: { subitem: 'People' } },
19-
],
20-
null: null,
21-
}
22-
const booleanState = state(true)
23-
const stringState = state('Hello Plexus!')
24-
const objectState = state<ObjectStateExample>(initialValue.object)
25-
const arrayState = state<{ item?: string; item2?: { subitem?: string } }[]>(
26-
initialValue.array
27-
)
28-
29-
const stateWithFetchFnTest = state(() => {
30-
return 'some sort of data'
31-
})
4+
import {
5+
arrayState,
6+
booleanState,
7+
initialStateValues,
8+
objectState,
9+
stateWithFetchFnTest,
10+
stringState,
11+
} from './test-utils'
3212

3313
// TODO Disallow null as initial value
3414
beforeEach(() => {
@@ -38,63 +18,6 @@ beforeEach(() => {
3818
arrayState.reset()
3919
})
4020
describe('Testing State Function', () => {
41-
test('Can save a value', () => {
42-
const value = state(1)
43-
const value2 = state<UnionString>('a')
44-
expect(value.value).toBe(1)
45-
})
46-
47-
test('Change value and remember the old one', () => {
48-
const value = state(1)
49-
value.set(2)
50-
expect(value.value).toBe(2)
51-
expect(value.lastValue).toBe(1)
52-
})
53-
54-
test('Checking state().set()', () => {
55-
// check .set(value: object)
56-
objectState.set({ a: { b: false } })
57-
// check if the object is actually merged and children props do get overwritten
58-
expect(objectState.value.a?.b).toBe(false)
59-
})
60-
61-
test('Checking state().patch()', () => {
62-
// can the object deep merge?
63-
objectState.patch({ a: { b: false } })
64-
expect(objectState.value.a?.a).toBe(true)
65-
// check that other value is still there
66-
expect(objectState.value.b).toBe(true)
67-
// changed intended value
68-
expect(objectState.value.a?.b).toBe(false)
69-
70-
// console.log(arrayState.value)
71-
// check array deep merge
72-
arrayState.patch([{ item: 'Hello2' }])
73-
// console.log(arrayState.value)
74-
expect(arrayState.value[0].item).toBe('Hello2')
75-
expect(arrayState.value[0].item2).toStrictEqual({ subitem: 'World' })
76-
expect(arrayState.value[1].item).toBe('Goodbye')
77-
})
78-
79-
test('Checking state.watch()', () => {
80-
let callbackCalled = false
81-
82-
// can add watcher
83-
const watcherDestroyer = stringState.watch((value) => {
84-
console.log('callback called', value)
85-
callbackCalled = true
86-
})
87-
console.log(instance().runtime.engine.events.entries())
88-
stringState.set('Hello World')
89-
expect(callbackCalled).toBe(true)
90-
// can remove watcher
91-
// stringState.removeWatcher(watcherKey);
92-
watcherDestroyer()
93-
// console.log(watcherDestroyer.toString(), instance().runtime.engine.events.entries())
94-
stringState.set('new value')
95-
expect(callbackCalled).toBe(true)
96-
})
97-
9821
test('Checking state.watch()', () => {
9922
let callbackCalled = false
10023
let callback2Called = false
@@ -103,19 +26,13 @@ describe('Testing State Function', () => {
10326
console.log('callback called', value)
10427
callbackCalled = true
10528
})
106-
const watcherDestroyer2 = booleanState.watch((value) => {
107-
console.log('callback 2 called', value)
108-
callback2Called = true
109-
})
11029
// console.log(instance().runtime.engine.events.entries())
11130
stringState.set('Hello World')
11231
expect(callbackCalled).toBe(true)
11332
expect(callback2Called).toBe(false)
11433

11534
// can remove watcher
116-
// stringState.removeWatcher(watcherKey);
11735
watcherDestroyer()
118-
// console.log(watcherDestroyer.toString(), instance().runtime.engine.events.entries())
11936
stringState.set('new value')
12037
expect(callbackCalled).toBe(true)
12138
})
@@ -143,7 +60,7 @@ describe('Testing State Function', () => {
14360
test('Checking state.undo() & state.redo()', () => {
14461
objectState.set({ a: { b: false } })
14562
objectState.undo()
146-
expect(objectState.value).toStrictEqual(initialValue.object)
63+
expect(objectState.value).toStrictEqual(initialStateValues.object)
14764
objectState.redo()
14865
expect(objectState.value).toStrictEqual({ a: { b: false } })
14966
})
@@ -157,8 +74,13 @@ describe('Testing State Function', () => {
15774
console.log('1: checking', objectState.value, 'vs.', { a: { b: false } })
15875
expect(objectState.value).toStrictEqual({ a: { b: false } })
15976
objectState.undo()
160-
console.log('2: checking', objectState.value, 'vs.', initialValue.object)
161-
expect(objectState.value).toStrictEqual(initialValue.object)
77+
console.log(
78+
'2: checking',
79+
objectState.value,
80+
'vs.',
81+
initialStateValues.object
82+
)
83+
expect(objectState.value).toStrictEqual(initialStateValues.object)
16284
objectState.redo()
16385
console.log('3: checking', objectState.value, 'vs.', { a: { b: false } })
16486
expect(objectState.value).toStrictEqual({ a: { b: false } })
@@ -245,7 +167,7 @@ describe('Testing State Function', () => {
245167
// we can do some sort of calculation here
246168
return 'a new string!' + stateWithFetchFnTest.value
247169
})
248-
// awesome! But nothing should change beacuse the state isn't undefined nor did we call `fetch()`
170+
// awesome! But nothing should change because the state isn't undefined nor did we call `fetch()`
249171
expect(stateWithFetchFnTest.value).toBe('new value')
250172
// let's force a fetch...
251173
stateWithFetchFnTest.fetch()

0 commit comments

Comments
 (0)