PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/test/unit/ext/pim.calendar/index.js

https://github.com/jcarty/BB10-WebWorks-Framework
JavaScript | 382 lines | 304 code | 63 blank | 15 comment | 14 complexity | 6197cff6a82963fc886acdbbd43fa952 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, MIT
  1. /*
  2. * Copyright 2012 Research In Motion Limited.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. var _apiDir = __dirname + "./../../../../ext/pim.calendar/",
  17. _libDir = __dirname + "./../../../../lib/",
  18. utils = require(_libDir + "utils"),
  19. events = require(_libDir + "event"),
  20. CalendarFindOptions = require(_apiDir + "CalendarFindOptions"),
  21. CalendarEvent = require(_apiDir + "CalendarEvent"),
  22. CalendarError = require(_apiDir + "CalendarError"),
  23. index,
  24. mockJnextObjId = 123;
  25. describe("pim.calendar/index", function () {
  26. beforeEach(function () {
  27. GLOBAL.JNEXT = {
  28. require: jasmine.createSpy("JNEXT.require").andCallFake(function () {
  29. return true;
  30. }),
  31. createObject: jasmine.createSpy("JNEXT.createObject").andCallFake(function () {
  32. return mockJnextObjId;
  33. }),
  34. invoke: jasmine.createSpy("JNEXT.invoke").andCallFake(function (id, command) {
  35. if (command.indexOf("getCalendarFolders") !== -1) {
  36. return JSON.stringify([{
  37. id: "1",
  38. accountId: "1"
  39. }]);
  40. } else if (command.indexOf("getDefaultCalendarFolder") !== -1) {
  41. return JSON.stringify({
  42. id: "1",
  43. accountId: "1"
  44. });
  45. } else if (command.indexOf("getCalendarAccounts") !== -1) {
  46. return JSON.stringify([{
  47. id: "1"
  48. }]);
  49. } else if (command.indexOf("getDefaultCalendarAccount") !== -1) {
  50. return JSON.stringify({
  51. id: "1"
  52. });
  53. } else if (command.indexOf("getEvent") !== -1) {
  54. return JSON.stringify({
  55. _success: true,
  56. event: {
  57. id: "123"
  58. }
  59. });
  60. } else {
  61. return JSON.stringify({});
  62. }
  63. }),
  64. registerEvents: jasmine.createSpy("JNEXT.registerEvent")
  65. };
  66. GLOBAL.window = {
  67. qnx: {
  68. webplatform: {
  69. device: {
  70. timezone: "America/New_York"
  71. }
  72. }
  73. }
  74. };
  75. spyOn(events, "trigger");
  76. index = require(_apiDir + "index");
  77. });
  78. afterEach(function () {
  79. GLOBAL.JNEXT = null;
  80. index = null;
  81. });
  82. it("JNEXT require/createObject/registerEvents are not called upon requiring index", function () {
  83. expect(JNEXT.require).not.toHaveBeenCalled();
  84. expect(JNEXT.createObject).not.toHaveBeenCalled();
  85. expect(JNEXT.registerEvents).not.toHaveBeenCalled();
  86. });
  87. it("find - with correct permission specified", function () {
  88. var successCb = jasmine.createSpy(),
  89. failCb = jasmine.createSpy(),
  90. findOptions = {
  91. limit: 5,
  92. detail: CalendarFindOptions.DETAIL_AGENDA
  93. },
  94. args = {
  95. _eventId: encodeURIComponent(JSON.stringify("abc")),
  96. options: encodeURIComponent(JSON.stringify(findOptions))
  97. };
  98. spyOn(utils, "hasPermission").andReturn(true);
  99. index.find(successCb, failCb, args);
  100. Object.getOwnPropertyNames(args).forEach(function (key) {
  101. args[key] = JSON.parse(decodeURIComponent(args[key]));
  102. });
  103. args["options"]["sourceTimezone"] = window.qnx.webplatform.device.timezone;
  104. expect(events.trigger).not.toHaveBeenCalled();
  105. expect(JNEXT.invoke).toHaveBeenCalledWith(mockJnextObjId, "find " + JSON.stringify(args));
  106. expect(successCb).toHaveBeenCalled();
  107. expect(failCb).not.toHaveBeenCalled();
  108. });
  109. it("find - without correct permission specified", function () {
  110. var successCb = jasmine.createSpy(),
  111. failCb = jasmine.createSpy(),
  112. findOptions = {
  113. filter: [{
  114. fieldName: CalendarFindOptions.SEARCH_FIELD_GIVEN_NAME,
  115. fieldValue: "John"
  116. }],
  117. limit: 5
  118. };
  119. spyOn(utils, "hasPermission").andReturn(false);
  120. index.find(successCb, failCb, {
  121. _eventId: encodeURIComponent(JSON.stringify("abc")),
  122. fields: encodeURIComponent(JSON.stringify(["name"])),
  123. options: encodeURIComponent(JSON.stringify(findOptions))
  124. });
  125. expect(events.trigger).toHaveBeenCalledWith(jasmine.any(String), {
  126. "result": escape(JSON.stringify({
  127. "_success": false,
  128. "code": CalendarError.PERMISSION_DENIED_ERROR
  129. }))
  130. });
  131. expect(JNEXT.invoke).not.toHaveBeenCalled();
  132. expect(successCb).toHaveBeenCalled();
  133. expect(failCb).not.toHaveBeenCalled();
  134. });
  135. it("save - with correct permission specified", function () {
  136. var successCb = jasmine.createSpy(),
  137. failCb = jasmine.createSpy(),
  138. evt = {},
  139. args = {},
  140. key;
  141. spyOn(utils, "hasPermission").andReturn(true);
  142. for (key in evt) {
  143. if (evt.hasOwnProperty(key)) {
  144. args[key] = encodeURIComponent(JSON.stringify(evt[key]));
  145. }
  146. }
  147. args["_eventId"] = encodeURIComponent(JSON.stringify("abc"));
  148. index.save(successCb, failCb, args);
  149. Object.getOwnPropertyNames(args).forEach(function (key) {
  150. args[key] = JSON.parse(decodeURIComponent(args[key]));
  151. });
  152. args["sourceTimezone"] = window.qnx.webplatform.device.timezone;
  153. args["targetTimezone"] = "";
  154. expect(events.trigger).not.toHaveBeenCalled();
  155. expect(JNEXT.invoke).toHaveBeenCalledWith(mockJnextObjId, "save " + JSON.stringify(args));
  156. expect(successCb).toHaveBeenCalled();
  157. expect(failCb).not.toHaveBeenCalled();
  158. });
  159. it("save - without correct permission specified", function () {
  160. var successCb = jasmine.createSpy(),
  161. failCb = jasmine.createSpy(),
  162. evt = new CalendarEvent({
  163. "summary": "a test",
  164. "location": "test",
  165. "start": new Date("Jan 01, 2015, 12:00"),
  166. "end": new Date("Jan 01, 2015, 12:30"),
  167. }),
  168. args = {},
  169. key;
  170. spyOn(utils, "hasPermission").andReturn(false);
  171. for (key in evt) {
  172. if (evt.hasOwnProperty(key)) {
  173. args[key] = encodeURIComponent(JSON.stringify(evt[key]));
  174. }
  175. }
  176. args["_eventId"] = encodeURIComponent(JSON.stringify("abc"));
  177. index.save(successCb, failCb, args);
  178. expect(events.trigger).toHaveBeenCalledWith(jasmine.any(String), {
  179. "result": escape(JSON.stringify({
  180. "_success": false,
  181. "code": CalendarError.PERMISSION_DENIED_ERROR
  182. }))
  183. });
  184. expect(JNEXT.invoke).not.toHaveBeenCalled();
  185. expect(successCb).toHaveBeenCalled();
  186. expect(failCb).not.toHaveBeenCalled();
  187. });
  188. it("remove - with correct permission specified", function () {
  189. var successCb = jasmine.createSpy(),
  190. failCb = jasmine.createSpy(),
  191. args = {
  192. accountId : encodeURIComponent(JSON.stringify(1)),
  193. calEventId : encodeURIComponent(JSON.stringify(2)),
  194. _eventId : encodeURIComponent(JSON.stringify("abc")),
  195. removeAll : encodeURIComponent(JSON.stringify(true))
  196. };
  197. spyOn(utils, "hasPermission").andReturn(true);
  198. index.remove(successCb, failCb, args);
  199. Object.getOwnPropertyNames(args).forEach(function (key) {
  200. args[key] = JSON.parse(decodeURIComponent(args[key]));
  201. });
  202. args["sourceTimezone"] = window.qnx.webplatform.device.timezone;
  203. expect(events.trigger).not.toHaveBeenCalled();
  204. expect(JNEXT.invoke).toHaveBeenCalledWith(mockJnextObjId, "remove " + JSON.stringify(args));
  205. expect(successCb).toHaveBeenCalled();
  206. expect(failCb).not.toHaveBeenCalled();
  207. });
  208. it("remove - without correct permission specified", function () {
  209. var successCb = jasmine.createSpy(),
  210. failCb = jasmine.createSpy();
  211. spyOn(utils, "hasPermission").andReturn(false);
  212. index.remove(successCb, failCb, {
  213. accountId : encodeURIComponent(JSON.stringify(1)),
  214. calEventId : encodeURIComponent(JSON.stringify(2)),
  215. _eventId : encodeURIComponent(JSON.stringify("abc")),
  216. removeAll : encodeURIComponent(JSON.stringify(true))
  217. });
  218. expect(events.trigger).toHaveBeenCalledWith(jasmine.any(String), {
  219. "result": escape(JSON.stringify({
  220. "_success": false,
  221. "code": CalendarError.PERMISSION_DENIED_ERROR
  222. }))
  223. });
  224. expect(JNEXT.invoke).not.toHaveBeenCalled();
  225. expect(successCb).toHaveBeenCalled();
  226. expect(failCb).not.toHaveBeenCalled();
  227. });
  228. it("getDefaultCalendarAccount - without correct permission specified", function () {
  229. var successCb = jasmine.createSpy(),
  230. failCb = jasmine.createSpy(),
  231. args = {};
  232. spyOn(utils, "hasPermission").andReturn(false);
  233. index.getDefaultCalendarAccount(successCb, failCb, args);
  234. expect(events.trigger).not.toHaveBeenCalled();
  235. expect(JNEXT.invoke).not.toHaveBeenCalled();
  236. expect(successCb).toHaveBeenCalledWith(null);
  237. expect(failCb).not.toHaveBeenCalled();
  238. });
  239. it("getDefaultCalendarAccount - with correct permission specified", function () {
  240. var successCb = jasmine.createSpy(),
  241. failCb = jasmine.createSpy(),
  242. args = {};
  243. spyOn(utils, "hasPermission").andReturn(true);
  244. index.getDefaultCalendarAccount(successCb, failCb, args);
  245. expect(events.trigger).not.toHaveBeenCalled();
  246. expect(JNEXT.invoke).toHaveBeenCalledWith(mockJnextObjId, "getDefaultCalendarAccount");
  247. expect(successCb).toHaveBeenCalledWith({
  248. id: "1"
  249. });
  250. expect(failCb).not.toHaveBeenCalled();
  251. });
  252. it("getCalendarAccounts - without correct permission specified", function () {
  253. var successCb = jasmine.createSpy(),
  254. failCb = jasmine.createSpy(),
  255. args = {};
  256. spyOn(utils, "hasPermission").andReturn(false);
  257. index.getCalendarAccounts(successCb, failCb, args);
  258. expect(events.trigger).not.toHaveBeenCalled();
  259. expect(JNEXT.invoke).not.toHaveBeenCalled();
  260. expect(successCb).toHaveBeenCalledWith(null);
  261. expect(failCb).not.toHaveBeenCalled();
  262. });
  263. it("getCalendarAccounts - with correct permission specified", function () {
  264. var successCb = jasmine.createSpy(),
  265. failCb = jasmine.createSpy(),
  266. args = {};
  267. spyOn(utils, "hasPermission").andReturn(true);
  268. index.getCalendarAccounts(successCb, failCb, args);
  269. expect(events.trigger).not.toHaveBeenCalled();
  270. expect(JNEXT.invoke).toHaveBeenCalledWith(mockJnextObjId, "getCalendarAccounts");
  271. expect(successCb).toHaveBeenCalledWith([{
  272. id: "1"
  273. }]);
  274. expect(failCb).not.toHaveBeenCalled();
  275. });
  276. it("getEvent - without correct permission specified", function () {
  277. var successCb = jasmine.createSpy(),
  278. failCb = jasmine.createSpy(),
  279. args = {
  280. eventId: encodeURIComponent(JSON.stringify("123")),
  281. accountId: encodeURIComponent(JSON.stringify("1"))
  282. };
  283. spyOn(utils, "hasPermission").andReturn(false);
  284. index.getEvent(successCb, failCb, args);
  285. expect(events.trigger).not.toHaveBeenCalled();
  286. expect(JNEXT.invoke).not.toHaveBeenCalled();
  287. expect(successCb).toHaveBeenCalledWith(null);
  288. expect(failCb).not.toHaveBeenCalled();
  289. });
  290. it("getEvent - with correct permission specified", function () {
  291. var successCb = jasmine.createSpy(),
  292. failCb = jasmine.createSpy(),
  293. args = {
  294. eventId: encodeURIComponent(JSON.stringify("123")),
  295. accountId: encodeURIComponent(JSON.stringify("1"))
  296. };
  297. spyOn(utils, "hasPermission").andReturn(true);
  298. index.getEvent(successCb, failCb, args);
  299. Object.getOwnPropertyNames(args).forEach(function (key) {
  300. args[key] = JSON.parse(decodeURIComponent(args[key]));
  301. });
  302. expect(events.trigger).not.toHaveBeenCalled();
  303. expect(JNEXT.invoke).toHaveBeenCalledWith(mockJnextObjId, "getEvent " + JSON.stringify(args));
  304. expect(successCb).toHaveBeenCalledWith({
  305. id: "123"
  306. });
  307. expect(failCb).not.toHaveBeenCalled();
  308. });
  309. it("getDefaultCalendarFolder - with correct permission specified", function () {
  310. var successCb = jasmine.createSpy(),
  311. failCb = jasmine.createSpy();
  312. spyOn(utils, "hasPermission").andReturn(true);
  313. index.getDefaultCalendarFolder(successCb, failCb, {});
  314. expect(events.trigger).not.toHaveBeenCalled();
  315. expect(JNEXT.invoke).toHaveBeenCalledWith(mockJnextObjId, "getDefaultCalendarFolder");
  316. expect(successCb).toHaveBeenCalled();
  317. expect(failCb).not.toHaveBeenCalled();
  318. });
  319. });