dataObjectUtilitiesTest.js (15501B)
1 "use strict"; 2 3 describe("Zotero.DataObjectUtilities", function() { 4 describe("#patch()", function () { 5 it("should omit 'collections' if it doesn't exist", function* () { 6 var patchBase = { 7 collections: ['AAAAAAAA'] 8 }; 9 var obj = {}; 10 obj = Zotero.DataObjectUtilities.patch(patchBase, obj); 11 assert.notProperty(obj, 'collections'); 12 }) 13 14 it("should include modified 'conditions'", function* () { 15 var patchBase = { 16 name: "Search", 17 conditions: [ 18 { 19 condition: 'title', 20 operator: 'is', 21 value: 'A' 22 }, 23 { 24 condition: 'language', 25 operator: 'is', 26 value: 'en' 27 } 28 ] 29 }; 30 var obj = { 31 name: "Search", 32 conditions: [ 33 { 34 condition: 'title', 35 operator: 'is', 36 value: 'B' 37 }, 38 { 39 condition: 'language', 40 operator: 'is', 41 value: 'en' 42 } 43 ] 44 }; 45 obj = Zotero.DataObjectUtilities.patch(patchBase, obj); 46 assert.property(obj, 'conditions'); 47 assert.equal(obj.conditions[0].value, 'B'); 48 assert.equal(obj.conditions[1].value, 'en'); 49 }) 50 }) 51 52 describe("#diff()", function () { 53 // This is mostly covered by syncLocal::_reconcileChanges() tests, but we test some 54 // additional things here 55 describe("items", function () { 56 // 57 // Fields 58 // 59 describe("fields", function () { 60 it("should not show empty items as different", function* () { 61 var id1, id2, json1, json2; 62 yield Zotero.DB.executeTransaction(function* () { 63 var item = new Zotero.Item('book'); 64 id1 = yield item.save(); 65 json1 = item.toJSON(); 66 67 var item = new Zotero.Item('book'); 68 id2 = yield item.save(); 69 json2 = item.toJSON(); 70 }); 71 72 var changes = Zotero.DataObjectUtilities.diff(json1, json2); 73 assert.lengthOf(changes, 0); 74 75 yield Zotero.Items.erase([id1, id2]); 76 }) 77 78 it("should not show empty strings as different", function () { 79 var json1 = { 80 title: "" 81 }; 82 var json2 = { 83 title: "" 84 }; 85 var changes = Zotero.DataObjectUtilities.diff(json1, json2); 86 assert.lengthOf(changes, 0); 87 }) 88 89 it("should not show empty string and undefined as different", function () { 90 var json1 = { 91 title: "" 92 }; 93 var json2 = { 94 place: "" 95 }; 96 var changes = Zotero.DataObjectUtilities.diff(json1, json2); 97 assert.lengthOf(changes, 0); 98 }) 99 }) 100 101 // 102 // Creators 103 // 104 describe("creators", function () { 105 it("should not show identical creators as different", function () { 106 var json1 = { 107 creators: [ 108 { 109 name: "Center for History and New Media", 110 creatorType: "author" 111 } 112 ] 113 }; 114 var json2 = { 115 creators: [ 116 { 117 creatorType: "author", 118 name: "Center for History and New Media" 119 } 120 ] 121 }; 122 var changes = Zotero.DataObjectUtilities.diff(json1, json2); 123 assert.lengthOf(changes, 0); 124 }) 125 126 it("should not show an empty creators array and a missing one as different", function () { 127 var json1 = { 128 creators: [] 129 }; 130 var json2 = {}; 131 var changes = Zotero.DataObjectUtilities.diff(json1, json2); 132 assert.lengthOf(changes, 0); 133 134 var json1 = {}; 135 var json2 = { 136 creators: [] 137 }; 138 var changes = Zotero.DataObjectUtilities.diff(json1, json2); 139 assert.lengthOf(changes, 0); 140 141 }) 142 }) 143 144 describe("notes", function () { 145 it("should ignore sanitization changes", function* () { 146 var json1 = { 147 note: "<p>\u00a0</p>" 148 }; 149 var json2 = { 150 note: "<p> </p>" 151 }; 152 var changes = Zotero.DataObjectUtilities.diff(json1, json2); 153 assert.lengthOf(changes, 0); 154 }); 155 }); 156 157 // 158 // Relations 159 // 160 describe("relations", function () { 161 it("should not show an empty relations object and a missing one as different", function () { 162 var json1 = { 163 relations: {} 164 }; 165 var json2 = { 166 }; 167 var changes = Zotero.DataObjectUtilities.diff(json1, json2); 168 Zotero.debug(changes); 169 assert.lengthOf(changes, 0); 170 171 var json1 = {}; 172 var json2 = { 173 relations: {} 174 }; 175 var changes = Zotero.DataObjectUtilities.diff(json1, json2); 176 Zotero.debug(changes); 177 assert.lengthOf(changes, 0); 178 }) 179 }) 180 181 // 182 // Tags 183 // 184 describe("tags", function () { 185 it("should not show manual tags with or without 'type' property as different", function () { 186 var json1 = { 187 tags: [ 188 { 189 tag: "Foo" 190 } 191 ] 192 }; 193 var json2 = { 194 tags: [ 195 { 196 tag: "Foo", 197 type: 0 198 } 199 ] 200 }; 201 var changes = Zotero.DataObjectUtilities.diff(json1, json2); 202 assert.lengthOf(changes, 0); 203 }) 204 205 it("should show tags of different types as different", function () { 206 var json1 = { 207 tags: [ 208 { 209 tag: "Foo" 210 } 211 ] 212 }; 213 var json2 = { 214 tags: [ 215 { 216 tag: "Foo", 217 type: 1 218 } 219 ] 220 }; 221 var changes = Zotero.DataObjectUtilities.diff(json1, json2); 222 assert.sameDeepMembers( 223 changes, 224 [ 225 { 226 field: "tags", 227 op: "member-remove", 228 value: { 229 tag: "Foo" 230 } 231 }, 232 { 233 field: "tags", 234 op: "member-add", 235 value: { 236 tag: "Foo", 237 type: 1 238 } 239 } 240 ] 241 ); 242 }) 243 }) 244 }) 245 246 // 247 // Searches 248 // 249 // 250 // Search conditions 251 // 252 describe("searches", function () { 253 describe("conditions", function () { 254 it("should not show an empty conditions object and a missing one as different", function () { 255 var json1 = { 256 conditions: {} 257 }; 258 var json2 = { 259 }; 260 var changes = Zotero.DataObjectUtilities.diff(json1, json2); 261 Zotero.debug(changes); 262 assert.lengthOf(changes, 0); 263 264 var json1 = {}; 265 var json2 = { 266 conditions: {} 267 }; 268 var changes = Zotero.DataObjectUtilities.diff(json1, json2); 269 Zotero.debug(changes); 270 assert.lengthOf(changes, 0); 271 }) 272 273 /*it("should not show an empty conditions object and a missing one as different", function () { 274 var json1 = { 275 conditions: [] 276 }; 277 var json2 = { 278 conditions: [ 279 { 280 condition: 'title', 281 operator: 'contains', 282 value: 'test' 283 } 284 ] 285 }; 286 var changes = Zotero.DataObjectUtilities.diff(json1, json2); 287 Zotero.debug(changes); 288 assert.lengthOf(changes, 0); 289 290 var json1 = {}; 291 var json2 = { 292 conditions: {} 293 }; 294 var changes = Zotero.DataObjectUtilities.diff(json1, json2); 295 Zotero.debug(changes); 296 assert.lengthOf(changes, 0); 297 })*/ 298 }) 299 }) 300 }) 301 302 303 describe("#applyChanges()", function () { 304 // 305 // Fields 306 // 307 describe("fields", function () { 308 it("should set added/modified field values", function () { 309 var json = { 310 title: "A" 311 }; 312 var changes = [ 313 { 314 field: "title", 315 op: "add", 316 value: "B" 317 }, 318 { 319 field: "date", 320 op: "modify", 321 value: "2015-05-19" 322 } 323 ]; 324 Zotero.DataObjectUtilities.applyChanges(json, changes); 325 assert.equal(json.title, "B"); 326 assert.equal(json.date, "2015-05-19"); 327 }) 328 }) 329 330 // 331 // Collections 332 // 333 describe("collections", function () { 334 it("should add a collection", function () { 335 var json = { 336 collections: ["AAAAAAAA"] 337 }; 338 var changes = [ 339 { 340 field: "collections", 341 op: "member-add", 342 value: "BBBBBBBB" 343 } 344 ]; 345 Zotero.DataObjectUtilities.applyChanges(json, changes); 346 assert.sameMembers(json.collections, ["AAAAAAAA", "BBBBBBBB"]); 347 }) 348 349 it("should not duplicate an existing collection", function () { 350 var json = { 351 collections: ["AAAAAAAA"] 352 }; 353 var changes = [ 354 { 355 field: "collections", 356 op: "member-add", 357 value: "AAAAAAAA" 358 } 359 ]; 360 Zotero.DataObjectUtilities.applyChanges(json, changes); 361 assert.sameMembers(json.collections, ["AAAAAAAA"]); 362 assert.lengthOf(json.collections, 1); 363 }) 364 365 it("should remove a collection", function () { 366 var json = { 367 collections: ["AAAAAAAA"] 368 }; 369 var changes = [ 370 { 371 field: "collections", 372 op: "member-remove", 373 value: "AAAAAAAA" 374 } 375 ]; 376 Zotero.DataObjectUtilities.applyChanges(json, changes); 377 assert.lengthOf(json.collections, 0); 378 }) 379 }) 380 381 // 382 // Relations 383 // 384 describe("relations", function () { 385 it("should add a predicate and object to an empty relations object", function () { 386 var json = { 387 relations: {} 388 }; 389 var changes = [ 390 { 391 field: "relations", 392 op: "property-member-add", 393 value: { 394 key: "a", 395 value: "A" 396 } 397 } 398 ]; 399 Zotero.DataObjectUtilities.applyChanges(json, changes); 400 assert.sameDeepMembers([json.relations], [{ a: ["A"] }]); 401 }) 402 403 it("should add a predicate and object to a missing relations object", function () { 404 var json = {}; 405 var changes = [ 406 { 407 field: "relations", 408 op: "property-member-add", 409 value: { 410 key: "a", 411 value: "A" 412 } 413 } 414 ]; 415 Zotero.DataObjectUtilities.applyChanges(json, changes); 416 assert.sameDeepMembers([json.relations], [{ a: ["A"] }]); 417 }) 418 419 it("should add an object to an existing predicate string", function () { 420 var json = { 421 relations: { 422 a: 'A1' 423 } 424 }; 425 var changes = [ 426 { 427 field: "relations", 428 op: "property-member-add", 429 value: { 430 key: "a", 431 value: "A2" 432 } 433 } 434 ]; 435 Zotero.DataObjectUtilities.applyChanges(json, changes); 436 assert.sameDeepMembers([json.relations], [{ a: ["A1", "A2"] }]); 437 }) 438 439 it("should add an object to an existing predicate array", function () { 440 var json = { 441 relations: { 442 a: ['A1'] 443 } 444 }; 445 var changes = [ 446 { 447 field: "relations", 448 op: "property-member-add", 449 value: { 450 key: "a", 451 value: "A2" 452 } 453 } 454 ]; 455 Zotero.DataObjectUtilities.applyChanges(json, changes); 456 assert.sameDeepMembers([json.relations], [{ a: ['A1', 'A2'] }]); 457 }) 458 459 it("should ignore a removal for an missing relations object", function () { 460 var json = {}; 461 var changes = [ 462 { 463 field: "relations", 464 op: "property-member-remove", 465 value: { 466 key: "a", 467 value: "A" 468 } 469 } 470 ]; 471 Zotero.DataObjectUtilities.applyChanges(json, changes); 472 assert.notProperty(json, 'relations'); 473 }) 474 475 it("should ignore a removal for a missing relations predicate", function () { 476 var json = { 477 relations: {} 478 }; 479 var changes = [ 480 { 481 field: "relations", 482 op: "property-member-remove", 483 value: { 484 key: "a", 485 value: "A" 486 } 487 } 488 ]; 489 Zotero.DataObjectUtilities.applyChanges(json, changes); 490 assert.lengthOf(Object.keys(json.relations), 0); 491 }) 492 493 it("should ignore a removal for a missing object", function () { 494 var json = { 495 relations: { 496 a: ['A1'] 497 } 498 }; 499 var changes = [ 500 { 501 field: "relations", 502 op: "property-member-remove", 503 value: { 504 key: "a", 505 value: "A2" 506 } 507 } 508 ]; 509 Zotero.DataObjectUtilities.applyChanges(json, changes); 510 assert.sameDeepMembers([json.relations], [{ a: ['A1'] }]); 511 }) 512 513 it("should remove a predicate and object string from a relations object", function () { 514 var json = { 515 relations: { 516 a: "A" 517 } 518 }; 519 var changes = [ 520 { 521 field: "relations", 522 op: "property-member-remove", 523 value: { 524 key: "a", 525 value: "A" 526 } 527 } 528 ]; 529 Zotero.DataObjectUtilities.applyChanges(json, changes); 530 assert.lengthOf(Object.keys(json.relations), 0); 531 }) 532 533 it("should remove a predicate and object array from a relations object", function () { 534 var json = { 535 relations: { 536 a: ["A"] 537 } 538 }; 539 var changes = [ 540 { 541 field: "relations", 542 op: "property-member-remove", 543 value: { 544 key: "a", 545 value: "A" 546 } 547 } 548 ]; 549 Zotero.DataObjectUtilities.applyChanges(json, changes); 550 assert.lengthOf(Object.keys(json.relations), 0); 551 }) 552 553 it("should remove an object from an existing predicate array", function () { 554 var json = { 555 relations: { 556 a: ['A1', 'A2'] 557 } 558 }; 559 var changes = [ 560 { 561 field: "relations", 562 op: "property-member-remove", 563 value: { 564 key: "a", 565 value: "A2" 566 } 567 } 568 ]; 569 Zotero.DataObjectUtilities.applyChanges(json, changes); 570 assert.sameDeepMembers([json.relations], [{ a: ["A1"] }]); 571 }) 572 }) 573 574 // 575 // Tags 576 // 577 describe("tags", function () { 578 it("should add a tag", function () { 579 var json = { 580 tags: [ 581 { 582 tag: "A" 583 } 584 ] 585 }; 586 var changes = [ 587 { 588 field: "tags", 589 op: "member-add", 590 value: { 591 tag: "B" 592 } 593 } 594 ]; 595 Zotero.DataObjectUtilities.applyChanges(json, changes); 596 assert.sameDeepMembers( 597 json.tags, 598 [ 599 { 600 tag: "A" 601 }, 602 { 603 tag: "B" 604 } 605 ] 606 ); 607 }) 608 609 it("should not duplicate an existing tag", function () { 610 var json = { 611 tags: [ 612 { 613 tag: "A" 614 } 615 ] 616 }; 617 var changes = [ 618 { 619 field: "tags", 620 op: "member-add", 621 value: { 622 tag: "A" 623 } 624 } 625 ]; 626 Zotero.DataObjectUtilities.applyChanges(json, changes); 627 assert.sameDeepMembers( 628 json.tags, 629 [ 630 { 631 tag: "A" 632 } 633 ] 634 ); 635 assert.lengthOf(json.tags, 1); 636 }) 637 638 it("should remove a tag", function () { 639 var json = { 640 tags: [ 641 { 642 tag: "A" 643 } 644 ] 645 }; 646 var changes = [ 647 { 648 field: "tags", 649 op: "member-remove", 650 value: { 651 tag: "A" 652 } 653 } 654 ]; 655 Zotero.DataObjectUtilities.applyChanges(json, changes); 656 assert.lengthOf(json.tags, 0); 657 }) 658 }) 659 660 661 // 662 // Search conditions 663 // 664 describe("conditions", function () { 665 it("should add a condition", function () { 666 var json = { 667 conditions: [ 668 { 669 condition: "title", 670 op: "contains", 671 value: "A" 672 } 673 ] 674 }; 675 var changes = [ 676 { 677 field: "conditions", 678 op: "member-add", 679 value: { 680 condition: "title", 681 op: "contains", 682 value: "B" 683 } 684 } 685 ]; 686 Zotero.DataObjectUtilities.applyChanges(json, changes); 687 assert.sameDeepMembers( 688 json.conditions, 689 [ 690 { 691 condition: "title", 692 op: "contains", 693 value: "A" 694 }, 695 { 696 condition: "title", 697 op: "contains", 698 value: "B" 699 } 700 ] 701 ); 702 }) 703 704 it("should remove a condition", function () { 705 var json = { 706 conditions: [ 707 { 708 condition: "title", 709 op: "contains", 710 value: "A" 711 }, 712 { 713 condition: "title", 714 op: "contains", 715 value: "B" 716 } 717 ] 718 }; 719 var changes = [ 720 { 721 field: "conditions", 722 op: "member-remove", 723 value: { 724 condition: "title", 725 op: "contains", 726 value: "B" 727 } 728 } 729 ]; 730 Zotero.DataObjectUtilities.applyChanges(json, changes); 731 assert.sameDeepMembers( 732 json.conditions, 733 [ 734 { 735 condition: "title", 736 op: "contains", 737 value: "A" 738 } 739 ] 740 ); 741 }) 742 }) 743 }) 744 })