trigger.spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. Tinytest.addAsync('Client - Triggers - global enter triggers', function(test, next) {
  2. var rand = Random.id(), rand2 = Random.id();
  3. var log = [];
  4. var paths = ['/' + rand2, '/' + rand];
  5. var done = false;
  6. FlowRouter.route('/' + rand, {
  7. action: function(_params) {
  8. log.push(1);
  9. }
  10. });
  11. FlowRouter.route('/' + rand2, {
  12. action: function(_params) {
  13. log.push(2);
  14. }
  15. });
  16. FlowRouter.triggers.enter([function(context) {
  17. if(done) return;
  18. test.equal(context.path, paths.pop());
  19. log.push(0);
  20. }]);
  21. FlowRouter.go('/' + rand);
  22. setTimeout(function() {
  23. FlowRouter.go('/' + rand2);
  24. setTimeout(function() {
  25. test.equal(log, [0, 1, 0, 2]);
  26. done = true;
  27. setTimeout(next, 100);
  28. }, 100);
  29. }, 100);
  30. });
  31. Tinytest.addAsync('Client - Triggers - global enter triggers with "only"', function (test, next) {
  32. var rand = Random.id(), rand2 = Random.id();
  33. var log = [];
  34. var done = false;
  35. FlowRouter.route('/' + rand, {
  36. action: function(_params) {
  37. log.push(1);
  38. }
  39. });
  40. FlowRouter.route('/' + rand2, {
  41. name: 'foo',
  42. action: function(_params) {
  43. log.push(2);
  44. }
  45. });
  46. FlowRouter.triggers.enter([function(context) {
  47. if(done) return;
  48. test.equal(context.path, '/' + rand2);
  49. log.push(8);
  50. }], {only: ['foo']});
  51. FlowRouter.go('/' + rand);
  52. setTimeout(function() {
  53. FlowRouter.go('/' + rand2);
  54. setTimeout(function() {
  55. test.equal(log, [1, 8, 2]);
  56. done = true;
  57. setTimeout(next, 100);
  58. }, 100);
  59. }, 100);
  60. });
  61. Tinytest.addAsync('Client - Triggers - global enter triggers with "except"', function (test, next) {
  62. var rand = Random.id(), rand2 = Random.id();
  63. var log = [];
  64. var done = false;
  65. FlowRouter.route('/' + rand, {
  66. action: function(_params) {
  67. log.push(1);
  68. }
  69. });
  70. FlowRouter.route('/' + rand2, {
  71. name: 'foo',
  72. action: function(_params) {
  73. log.push(2);
  74. }
  75. });
  76. FlowRouter.triggers.enter([function(context) {
  77. if(done) return;
  78. test.equal(context.path, '/' + rand);
  79. log.push(8);
  80. }], {except: ['foo']});
  81. FlowRouter.go('/' + rand);
  82. setTimeout(function() {
  83. FlowRouter.go('/' + rand2);
  84. setTimeout(function() {
  85. test.equal(log, [8, 1, 2]);
  86. done = true;
  87. setTimeout(next, 100);
  88. }, 100);
  89. }, 100);
  90. });
  91. Tinytest.addAsync('Client - Triggers - global exit triggers', function (test, next) {
  92. var rand = Random.id(), rand2 = Random.id();
  93. var log = [];
  94. var done =false;
  95. FlowRouter.route('/' + rand, {
  96. action: function(_params) {
  97. log.push(1);
  98. }
  99. });
  100. FlowRouter.route('/' + rand2, {
  101. action: function(_params) {
  102. log.push(2);
  103. }
  104. });
  105. FlowRouter.go('/' + rand);
  106. FlowRouter.triggers.exit([function(context) {
  107. if(done) return;
  108. test.equal(context.path, '/' + rand);
  109. log.push(0);
  110. }]);
  111. setTimeout(function() {
  112. FlowRouter.go('/' + rand2);
  113. setTimeout(function() {
  114. test.equal(log, [1, 0, 2]);
  115. done = true;
  116. setTimeout(next, 100);
  117. }, 100);
  118. }, 100);
  119. });
  120. Tinytest.addAsync('Client - Triggers - global exit triggers with "only"', function (test, next) {
  121. var rand = Random.id(), rand2 = Random.id();
  122. var log = [];
  123. var done = false;
  124. FlowRouter.route('/' + rand, {
  125. action: function(_params) {
  126. log.push(1);
  127. }
  128. });
  129. FlowRouter.route('/' + rand2, {
  130. name: 'foo',
  131. action: function(_params) {
  132. log.push(2);
  133. }
  134. });
  135. FlowRouter.triggers.exit([function(context) {
  136. if(done) return;
  137. test.equal(context.path, '/' + rand2);
  138. log.push(8);
  139. }], {only: ['foo']});
  140. FlowRouter.go('/' + rand);
  141. setTimeout(function() {
  142. FlowRouter.go('/' + rand2);
  143. setTimeout(function() {
  144. FlowRouter.go('/' + rand);
  145. setTimeout(function() {
  146. test.equal(log, [1, 2, 8, 1]);
  147. done = true;
  148. setTimeout(next, 100);
  149. }, 100);
  150. }, 100);
  151. }, 100);
  152. });
  153. Tinytest.addAsync('Client - Triggers - global exit triggers with "except"', function (test, next) {
  154. var rand = Random.id(), rand2 = Random.id();
  155. var log = [];
  156. var done = false;
  157. FlowRouter.route('/' + rand, {
  158. action: function(_params) {
  159. log.push(1);
  160. }
  161. });
  162. FlowRouter.route('/' + rand2, {
  163. name: 'foo',
  164. action: function(_params) {
  165. log.push(2);
  166. }
  167. });
  168. FlowRouter.go('/' + rand);
  169. FlowRouter.triggers.exit([function(context) {
  170. if(done) return;
  171. test.equal(context.path, '/' + rand);
  172. log.push(9);
  173. }], {except: ['foo']});
  174. setTimeout(function() {
  175. FlowRouter.go('/' + rand2);
  176. setTimeout(function() {
  177. FlowRouter.go('/' + rand);
  178. setTimeout(function() {
  179. test.equal(log, [1, 9, 2, 1]);
  180. done = true;
  181. setTimeout(next, 100);
  182. }, 100);
  183. }, 100);
  184. }, 100);
  185. });
  186. Tinytest.addAsync('Client - Triggers - route enter triggers', function (test, next) {
  187. var rand = Random.id();
  188. var log = [];
  189. var triggerFn = function (context) {
  190. test.equal(context.path, '/' + rand);
  191. log.push(5);
  192. };
  193. FlowRouter.route('/' + rand, {
  194. triggersEnter: [triggerFn],
  195. action: function(_params) {
  196. log.push(1);
  197. }
  198. });
  199. FlowRouter.go('/' + rand);
  200. setTimeout(function() {
  201. test.equal(log, [5, 1]);
  202. setTimeout(next, 100);
  203. }, 100);
  204. });
  205. Tinytest.addAsync('Client - Triggers - router exit triggers', function (test, next) {
  206. var rand = Random.id();
  207. var log = [];
  208. var triggerFn = function (context) {
  209. test.equal(context.path, '/' + rand);
  210. log.push(6);
  211. };
  212. FlowRouter.route('/' + rand, {
  213. triggersExit: [triggerFn],
  214. action: function(_params) {
  215. log.push(1);
  216. }
  217. });
  218. FlowRouter.go('/' + rand);
  219. setTimeout(function() {
  220. FlowRouter.go('/' + Random.id());
  221. setTimeout(function() {
  222. test.equal(log, [1, 6]);
  223. setTimeout(next, 100);
  224. }, 100);
  225. }, 100);
  226. });
  227. Tinytest.addAsync('Client - Triggers - group enter triggers', function (test, next) {
  228. var rand = Random.id(), rand2 = Random.id();
  229. var log = [];
  230. var paths = ['/' + rand2, '/' + rand];
  231. var triggerFn = function (context) {
  232. test.equal(context.path, paths.pop());
  233. log.push(3);
  234. };
  235. var group = FlowRouter.group({
  236. triggersEnter: [triggerFn]
  237. });
  238. group.route('/' + rand, {
  239. action: function(_params) {
  240. log.push(1);
  241. }
  242. });
  243. group.route('/' + rand2, {
  244. action: function(_params) {
  245. log.push(2);
  246. }
  247. });
  248. FlowRouter.go('/' + rand);
  249. setTimeout(function() {
  250. FlowRouter.go('/' + rand2);
  251. setTimeout(function() {
  252. test.equal(log, [3, 1, 3, 2]);
  253. setTimeout(next, 100);
  254. }, 100);
  255. }, 100);
  256. });
  257. Tinytest.addAsync('Client - Triggers - group exit triggers', function (test, next) {
  258. var rand = Random.id(), rand2 = Random.id();
  259. var log = [];
  260. var triggerFn = function (context) {
  261. log.push(4);
  262. };
  263. var group = FlowRouter.group({
  264. triggersExit: [triggerFn]
  265. });
  266. group.route('/' + rand, {
  267. action: function(_params) {
  268. log.push(1);
  269. }
  270. });
  271. group.route('/' + rand2, {
  272. action: function(_params) {
  273. log.push(2);
  274. }
  275. });
  276. FlowRouter.go('/' + rand);
  277. setTimeout(function() {
  278. FlowRouter.go('/' + rand2);
  279. setTimeout(function() {
  280. test.equal(log, [1, 4, 2]);
  281. setTimeout(next, 100);
  282. }, 100);
  283. }, 100);
  284. });
  285. Tinytest.addAsync('Client - Triggers - redirect from enter', function(test, next) {
  286. var rand = Random.id(), rand2 = Random.id();
  287. var log = [];
  288. FlowRouter.route('/' + rand, {
  289. triggersEnter: [function(context, redirect) {
  290. redirect("/" + rand2);
  291. }, function() {
  292. throw new Error("should not execute this trigger");
  293. }],
  294. action: function(_params) {
  295. log.push(1);
  296. },
  297. name: rand
  298. });
  299. FlowRouter.route('/' + rand2, {
  300. action: function(_params) {
  301. log.push(2);
  302. },
  303. name: rand2
  304. });
  305. FlowRouter.go('/');
  306. FlowRouter.go('/' + rand);
  307. setTimeout(function() {
  308. test.equal(log, [2]);
  309. next();
  310. }, 300);
  311. });
  312. Tinytest.addAsync('Client - Triggers - redirect by routeName', function(test, next) {
  313. var rand = Random.id(), rand2 = Random.id();
  314. var log = [];
  315. FlowRouter.route('/' + rand, {
  316. name: rand,
  317. triggersEnter: [function(context, redirect) {
  318. redirect(rand2, null, {aa: "bb"});
  319. }, function() {
  320. throw new Error("should not execute this trigger");
  321. }],
  322. action: function(_params) {
  323. log.push(1);
  324. },
  325. name: rand
  326. });
  327. FlowRouter.route('/' + rand2, {
  328. name: rand2,
  329. action: function(_params, queryParams) {
  330. log.push(2);
  331. test.equal(queryParams, {aa: "bb"});
  332. },
  333. name: rand2
  334. });
  335. FlowRouter.go('/');
  336. FlowRouter.go('/' + rand);
  337. setTimeout(function() {
  338. test.equal(log, [2]);
  339. next();
  340. }, 300);
  341. });
  342. Tinytest.addAsync('Client - Triggers - redirect from exit', function(test, next) {
  343. var rand = Random.id(), rand2 = Random.id(), rand3 = Random.id();
  344. var log = [];
  345. FlowRouter.route('/' + rand, {
  346. action: function() {
  347. log.push(1);
  348. },
  349. triggersExit: [
  350. function(context, redirect) {
  351. redirect('/' + rand3);
  352. },
  353. function() {
  354. throw new Error("should not call this trigger");
  355. }
  356. ]
  357. });
  358. FlowRouter.route('/' + rand2, {
  359. action: function() {
  360. log.push(2);
  361. }
  362. });
  363. FlowRouter.route('/' + rand3, {
  364. action: function() {
  365. log.push(3);
  366. }
  367. });
  368. FlowRouter.go('/' + rand);
  369. setTimeout(function() {
  370. FlowRouter.go('/' + rand2);
  371. setTimeout(function() {
  372. test.equal(log, [1, 3]);
  373. next();
  374. }, 100);
  375. }, 100);
  376. });
  377. Tinytest.addAsync('Client - Triggers - redirect to external URL fails', function(test, next) {
  378. var rand = Random.id(), rand2 = Random.id();
  379. var log = [];
  380. // testing "http://" URLs
  381. FlowRouter.route('/' + rand, {
  382. triggersEnter: [function(context, redirect) {
  383. test.throws(function() {
  384. redirect("http://example.com/")
  385. }, "Redirects to URLs outside of the app are not supported")
  386. }],
  387. action: function(_params) {
  388. log.push(1);
  389. },
  390. name: rand
  391. });
  392. // testing "https://" URLs
  393. FlowRouter.route('/' + rand2, {
  394. triggersEnter: [function(context, redirect) {
  395. test.throws(function() {
  396. redirect("https://example.com/")
  397. })
  398. }],
  399. action: function(_params) {
  400. log.push(2);
  401. },
  402. name: rand2
  403. });
  404. FlowRouter.go('/');
  405. FlowRouter.go('/' + rand);
  406. FlowRouter.go('/' + rand2);
  407. setTimeout(function() {
  408. test.equal(log, []);
  409. next();
  410. }, 300);
  411. });
  412. Tinytest.addAsync('Client - Triggers - stop callback from enter', function(test, next) {
  413. var rand = Random.id();
  414. var log = [];
  415. FlowRouter.route('/' + rand, {
  416. triggersEnter: [function(context, redirect, stop) {
  417. log.push(10);
  418. stop();
  419. }, function() {
  420. throw new Error("should not execute this trigger");
  421. }],
  422. action: function(_params) {
  423. throw new Error("should not execute the action");
  424. }
  425. });
  426. FlowRouter.go('/');
  427. FlowRouter.go('/' + rand);
  428. setTimeout(function() {
  429. test.equal(log, [10]);
  430. next();
  431. }, 100);
  432. });
  433. Tinytest.addAsync(
  434. 'Client - Triggers - invalidate inside an autorun',
  435. function(test, next) {
  436. var rand = Random.id(), rand2 = Random.id();
  437. var log = [];
  438. var paths = ['/' + rand2, '/' + rand];
  439. var done = false;
  440. FlowRouter.route('/' + rand, {
  441. action: function(_params) {
  442. log.push(1);
  443. }
  444. });
  445. FlowRouter.route('/' + rand2, {
  446. action: function(_params) {
  447. log.push(2);
  448. }
  449. });
  450. FlowRouter.triggers.enter([function(context) {
  451. if(done) return;
  452. test.equal(context.path, paths.pop());
  453. log.push(0);
  454. }]);
  455. Tracker.autorun(function(c) {
  456. FlowRouter.go('/' + rand);
  457. });
  458. setTimeout(function() {
  459. FlowRouter.go('/' + rand2);
  460. setTimeout(function() {
  461. test.equal(log, [0, 1, 0, 2]);
  462. done = true;
  463. setTimeout(next, 100);
  464. }, 100);
  465. }, 100);
  466. });