summaryrefslogtreecommitdiffstats
path: root/comm/calendar/test/unit/test_relation.js
blob: 148d5a611843d61b46b5e89fd1b00cecb4d35461 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var { XPCOMUtils } = ChromeUtils.importESModule("resource://gre/modules/XPCOMUtils.sys.mjs");

XPCOMUtils.defineLazyModuleGetters(this, {
  CalEvent: "resource:///modules/CalEvent.jsm",
  CalRelation: "resource:///modules/CalRelation.jsm",
});

function run_test() {
  // Create Relation
  let relation1 = new CalRelation();

  // Create Items
  let event1 = new CalEvent();
  let event2 = new CalEvent();

  // Testing relation set/get.
  let properties = {
    relType: "PARENT",
    relId: event2.id,
  };

  for (let [property, value] of Object.entries(properties)) {
    relation1[property] = value;
    equal(relation1[property], value);
  }

  // Add relation to event
  event1.addRelation(relation1);

  // Add 2nd relation to event.
  let relation2 = new CalRelation();
  relation2.relId = "myid2";
  event1.addRelation(relation2);

  // Check the item functions
  checkRelations(event1, [relation1, relation2]);

  // modify the Relations
  modifyRelations(event1, [relation1, relation2]);

  // test icalproperty
  // eslint-disable-next-line no-unused-expressions
  relation2.icalProperty;

  test_icalprop();
}

function checkRelations(event, expRel) {
  let allRel = event.getRelations();
  equal(allRel.length, expRel.length);

  // check if all expacted relations are found
  for (let i = 0; i < expRel.length; i++) {
    ok(allRel.includes(expRel[i]));
  }

  // Check if all found relations are expected
  for (let i = 0; i < allRel.length; i++) {
    ok(expRel.includes(allRel[i]));
  }
}

function modifyRelations(event, oldRel) {
  let allRel = event.getRelations();
  let rel = allRel[0];

  // modify the properties
  rel.relType = "SIBLING";
  equal(rel.relType, "SIBLING");
  equal(rel.relType, allRel[0].relType);

  // remove one relation
  event.removeRelation(rel);
  equal(event.getRelations().length, oldRel.length - 1);

  // add one relation and remove all relations
  event.addRelation(oldRel[0]);
  event.removeAllRelations();
  equal(event.getRelations(), 0);
}

function test_icalprop() {
  let rel = new CalRelation();

  rel.relType = "SIBLING";
  rel.setParameter("X-PROP", "VAL");
  rel.relId = "value";

  let prop = rel.icalProperty;
  let propOrig = rel.icalProperty;

  equal(rel.icalString, prop.icalString);

  equal(prop.value, "value");
  equal(prop.getParameter("X-PROP"), "VAL");
  equal(prop.getParameter("RELTYPE"), "SIBLING");

  prop.value = "changed";
  prop.setParameter("RELTYPE", "changedtype");
  prop.setParameter("X-PROP", "changedxprop");

  equal(rel.relId, "value");
  equal(rel.getParameter("X-PROP"), "VAL");
  equal(rel.relType, "SIBLING");

  rel.icalProperty = prop;

  equal(rel.relId, "changed");
  equal(rel.getParameter("X-PROP"), "changedxprop");
  equal(rel.relType, "changedtype");

  rel.icalString = propOrig.icalString;

  equal(rel.relId, "value");
  equal(rel.getParameter("X-PROP"), "VAL");
  equal(rel.relType, "SIBLING");

  let rel2 = rel.clone();
  rel.icalProperty = prop;

  notEqual(rel.icalString, rel2.icalString);

  rel.deleteParameter("X-PROP");
  equal(rel.icalProperty.getParameter("X-PROP"), null);

  throws(() => {
    rel.icalString = "X-UNKNOWN:value";
  }, /Illegal value/);
}