fixed remaining weak link tests

This commit is contained in:
Bartosz Sypytkowski 2023-06-30 13:43:28 +02:00
parent 88883a7402
commit cbdbf2ccc1

View File

@ -332,7 +332,7 @@ export const testDeepObserveMap = tc => {
/** /**
* @param {t.TestCase} tc * @param {t.TestCase} tc
*/ */
const testDeepObserveArray = tc => { //FIXME export const testDeepObserveArray = tc => { //FIXME
// test observers in a face of linked chains of values // test observers in a face of linked chains of values
const doc = new Y.Doc() const doc = new Y.Doc()
/* /*
@ -346,17 +346,30 @@ const testDeepObserveArray = tc => { //FIXME
const map = doc.getMap('map') const map = doc.getMap('map')
const array = doc.getArray('array') const array = doc.getArray('array')
/**
* @type {Array<any>}
*/
let events = []
array.observeDeep((e) => events = e)
const nested = new Y.Map() const nested = new Y.Map()
map.set('nested', nested) map.set('nested', nested)
const link = map.link('nested') const link = map.link('nested')
array.insert(0, [link]) array.insert(0, [link])
/**
* @type {Array<any>}
*/
let events = []
array.observeDeep((evts) => {
events = []
for (let e of evts) {
switch (e.constructor) {
case Y.YMapEvent:
events.push({target: e.target, keys: e.keys})
break;
case Y.YWeakLinkEvent:
events.push({target: e.target})
break;
default: throw new Error('unexpected event type ' + e.constructor)
}
}
})
// update entry in linked map // update entry in linked map
events = [] events = []
nested.set('key', 'value') nested.set('key', 'value')
@ -367,24 +380,23 @@ const testDeepObserveArray = tc => { //FIXME
nested.set('key', 'value2') nested.set('key', 'value2')
t.compare(events.length, 1) t.compare(events.length, 1)
t.compare(events[0].target, nested) t.compare(events[0].target, nested)
t.compare(events[0].keys, new Map([['key', {action:'update', oldValue: undefined}]])) t.compare(events[0].keys, new Map([['key', {action:'update', oldValue: 'value'}]]))
// delete entry in linked map // delete entry in linked map
nested.delete('key') nested.delete('key')
t.compare(events.length, 1) t.compare(events.length, 1)
t.compare(events[0].target, nested) t.compare(events[0].target, nested)
t.compare(events[0].keys, new Map([['key', {action:'delete', oldValue: undefined}]])) t.compare(events[0].keys, new Map([['key', {action:'delete', oldValue: 'value2'}]]))
// delete linked map // delete linked map
map.delete('nested') map.delete('nested')
t.compare(events.length, 1) t.compare(events.length, 1)
t.compare(events[0].target, map) t.compare(events[0].target, link)
t.compare(events[0].keys, new Map([['nested', {action:'delete', oldValue: undefined}]]))
} }
/** /**
* @param {t.TestCase} tc * @param {t.TestCase} tc
*/ */
const testMapDeepObserve = tc => { //FIXME export const testMapDeepObserve = tc => { //FIXME
const doc = new Y.Doc() const doc = new Y.Doc()
const outer = doc.getMap('outer') const outer = doc.getMap('outer')
const inner = new Y.Map() const inner = new Y.Map()
@ -394,7 +406,21 @@ const testMapDeepObserve = tc => { //FIXME
* @type {Array<any>} * @type {Array<any>}
*/ */
let events = [] let events = []
outer.observeDeep((e) => events = e) outer.observeDeep((evts) => {
events = []
for (let e of evts) {
switch (e.constructor) {
case Y.YMapEvent:
events.push({target: e.target, keys: e.keys})
break;
case Y.YWeakLinkEvent:
events.push({target: e.target})
break;
default: throw new Error('unexpected event type ' + e.constructor)
}
}
})
inner.set('key', 'value1') inner.set('key', 'value1')
t.compare(events.length, 1) t.compare(events.length, 1)
@ -405,13 +431,13 @@ const testMapDeepObserve = tc => { //FIXME
inner.set('key', 'value2') inner.set('key', 'value2')
t.compare(events.length, 1) t.compare(events.length, 1)
t.compare(events[0].target, inner) t.compare(events[0].target, inner)
t.compare(events[0].keys, new Map([['key', {action:'update', oldValue: undefined}]])) t.compare(events[0].keys, new Map([['key', {action:'update', oldValue: 'value1'}]]))
events = [] events = []
inner.delete('key') inner.delete('key')
t.compare(events.length, 1) t.compare(events.length, 1)
t.compare(events[0].target, inner) t.compare(events[0].target, inner)
t.compare(events[0].keys, new Map([['key', {action:'delete', oldValue: undefined}]])) t.compare(events[0].keys, new Map([['key', {action:'delete', oldValue: 'value2'}]]))
} }
/** /**