@@ -38,6 +38,49 @@ describe('YKeyValueLww', () => {
3838 expect ( kv . get ( 'foo' ) ) . toBe ( 'second' ) ;
3939 } ) ;
4040
41+ test ( 'bulkSet inserts all entries' , ( ) => {
42+ const ydoc = new Y . Doc ( { guid : 'test' } ) ;
43+ const yarray = ydoc . getArray < YKeyValueLwwEntry < string > > ( 'data' ) ;
44+ const kv = new YKeyValueLww ( yarray ) ;
45+
46+ kv . bulkSet ( [
47+ { key : 'foo' , val : 'bar' } ,
48+ { key : 'baz' , val : 'qux' } ,
49+ { key : 'zap' , val : 'zip' } ,
50+ ] ) ;
51+
52+ expect ( kv . get ( 'foo' ) ) . toBe ( 'bar' ) ;
53+ expect ( kv . get ( 'baz' ) ) . toBe ( 'qux' ) ;
54+ expect ( kv . get ( 'zap' ) ) . toBe ( 'zip' ) ;
55+ expect ( Array . from ( kv . entries ( ) ) ) . toHaveLength ( 3 ) ;
56+ } ) ;
57+
58+ test ( 'bulkSet updates existing entries' , ( ) => {
59+ const ydoc = new Y . Doc ( { guid : 'test' } ) ;
60+ const yarray = ydoc . getArray < YKeyValueLwwEntry < string > > ( 'data' ) ;
61+ const kv = new YKeyValueLww ( yarray ) ;
62+
63+ kv . set ( 'foo' , 'first' ) ;
64+ kv . bulkSet ( [
65+ { key : 'foo' , val : 'second' } ,
66+ { key : 'bar' , val : 'third' } ,
67+ ] ) ;
68+
69+ expect ( kv . get ( 'foo' ) ) . toBe ( 'second' ) ;
70+ expect ( kv . get ( 'bar' ) ) . toBe ( 'third' ) ;
71+ expect (
72+ Array . from ( kv . entries ( ) )
73+ . map ( ( [ key ] ) => key )
74+ . sort ( ) ,
75+ ) . toEqual ( [ 'bar' , 'foo' ] ) ;
76+ expect (
77+ yarray
78+ . toArray ( )
79+ . map ( ( entry ) => entry . key )
80+ . sort ( ) ,
81+ ) . toEqual ( [ 'bar' , 'foo' ] ) ;
82+ } ) ;
83+
4184 test ( 'delete removes value' , ( ) => {
4285 const ydoc = new Y . Doc ( { guid : 'test' } ) ;
4386 const yarray = ydoc . getArray < YKeyValueLwwEntry < string > > ( 'data' ) ;
@@ -49,6 +92,42 @@ describe('YKeyValueLww', () => {
4992 expect ( kv . has ( 'foo' ) ) . toBe ( false ) ;
5093 } ) ;
5194
95+ test ( 'bulkDelete removes all specified keys' , ( ) => {
96+ const ydoc = new Y . Doc ( { guid : 'test' } ) ;
97+ const yarray = ydoc . getArray < YKeyValueLwwEntry < string > > ( 'data' ) ;
98+ const kv = new YKeyValueLww ( yarray ) ;
99+
100+ kv . bulkSet ( [
101+ { key : 'foo' , val : 'bar' } ,
102+ { key : 'baz' , val : 'qux' } ,
103+ { key : 'zap' , val : 'zip' } ,
104+ ] ) ;
105+ kv . bulkDelete ( [ 'foo' , 'zap' ] ) ;
106+
107+ expect ( kv . get ( 'foo' ) ) . toBeUndefined ( ) ;
108+ expect ( kv . get ( 'zap' ) ) . toBeUndefined ( ) ;
109+ expect ( kv . get ( 'baz' ) ) . toBe ( 'qux' ) ;
110+ expect ( Array . from ( kv . entries ( ) ) . map ( ( [ key ] ) => key ) ) . toEqual ( [ 'baz' ] ) ;
111+ } ) ;
112+
113+ test ( 'bulkDelete is a no-op for missing keys' , ( ) => {
114+ const ydoc = new Y . Doc ( { guid : 'test' } ) ;
115+ const yarray = ydoc . getArray < YKeyValueLwwEntry < string > > ( 'data' ) ;
116+ const kv = new YKeyValueLww ( yarray ) ;
117+
118+ kv . bulkSet ( [
119+ { key : 'foo' , val : 'bar' } ,
120+ { key : 'baz' , val : 'qux' } ,
121+ ] ) ;
122+ const before = yarray . toArray ( ) ;
123+
124+ kv . bulkDelete ( [ 'missing' , 'still-missing' ] ) ;
125+
126+ expect ( kv . get ( 'foo' ) ) . toBe ( 'bar' ) ;
127+ expect ( kv . get ( 'baz' ) ) . toBe ( 'qux' ) ;
128+ expect ( yarray . toArray ( ) ) . toEqual ( before ) ;
129+ } ) ;
130+
52131 test ( 'entries have timestamp field' , ( ) => {
53132 const ydoc = new Y . Doc ( { guid : 'test' } ) ;
54133 const yarray = ydoc . getArray < YKeyValueLwwEntry < string > > ( 'data' ) ;
0 commit comments