tests.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. Tinytest.add("AccountsTemplates - addField/removeField", function(test) {
  2. // Calls after AccountsTemplates.init()
  3. AccountsTemplates._initialized = true;
  4. test.throws(function() {
  5. AccountsTemplates.addField("");
  6. }, function(err) {
  7. if (err instanceof Error && err.message === "AccountsTemplates.addField should strictly be called before AccountsTemplates.init!")
  8. return true;
  9. });
  10. test.throws(function() {
  11. AccountsTemplates.removeField("");
  12. }, function(err) {
  13. if (err instanceof Error && err.message === "AccountsTemplates.removeField should strictly be called before AccountsTemplates.init!")
  14. return true;
  15. });
  16. AccountsTemplates._initialized = false;
  17. // Trying to remove a non-existing field
  18. test.throws(function() {
  19. AccountsTemplates.removeField("foo");
  20. }, function(err) {
  21. if (err instanceof Error && err.message == "A field called foo does not exist!")
  22. return true;
  23. });
  24. // Trying to remove an existing field
  25. var email = AccountsTemplates.removeField("email");
  26. test.isUndefined(AccountsTemplates.getField("email"));
  27. // ...and puts it back in for tests re-run
  28. AccountsTemplates.addField(email);
  29. // Trying to add an already existing field
  30. test.throws(function() {
  31. var pwd = AccountsTemplates.getField("password");
  32. AccountsTemplates.addField(pwd);
  33. }, function(err) {
  34. if (err instanceof Error && err.message == "A field called password already exists!")
  35. return true;
  36. });
  37. var login = {
  38. _id: "login",
  39. displayName: "Email",
  40. type: "email"
  41. };
  42. // Successful add
  43. AccountsTemplates.addField(login);
  44. // ...and removes it for tests re-run
  45. AccountsTemplates.removeField("login");
  46. // Invalid field.type
  47. test.throws(function() {
  48. AccountsTemplates.addField({
  49. _id: "foo",
  50. displayName: "Foo",
  51. type: "bar"
  52. });
  53. }, function(err) {
  54. if (err instanceof Error && err.message == "field.type is not valid!")
  55. return true;
  56. });
  57. // Invalid minLength
  58. test.throws(function() {
  59. AccountsTemplates.addField({
  60. _id: "first-name",
  61. displayName: "First Name",
  62. type: "text",
  63. minLength: 0
  64. });
  65. }, function(err) {
  66. if (err instanceof Error && err.message == "field.minLength should be greater than zero!")
  67. return true;
  68. });
  69. // Invalid maxLength
  70. test.throws(function() {
  71. AccountsTemplates.addField({
  72. _id: "first-name",
  73. displayName: "First Name",
  74. type: "text",
  75. maxLength: 0
  76. });
  77. }, function(err) {
  78. if (err instanceof Error && err.message == "field.maxLength should be greater than zero!")
  79. return true;
  80. });
  81. // maxLength < minLength
  82. test.throws(function() {
  83. AccountsTemplates.addField({
  84. _id: "first-name",
  85. displayName: "First Name",
  86. type: "text",
  87. minLength: 2,
  88. maxLength: 1
  89. });
  90. }, function(err) {
  91. if (err instanceof Error && err.message == "field.maxLength should be greater than field.maxLength!")
  92. return true;
  93. });
  94. // Successful add
  95. var first_name = {
  96. _id: "first_name",
  97. displayName: "First Name",
  98. type: "text",
  99. minLength: 2,
  100. maxLength: 50,
  101. required: true
  102. };
  103. AccountsTemplates.addField(first_name);
  104. // Now removes it to be consistent with tests re-run
  105. AccountsTemplates.removeField("first_name");
  106. });
  107. Tinytest.add("AccountsTemplates - addFields", function(test) {
  108. // Fake uninitialized state...
  109. AccountsTemplates._initialized = false;
  110. if (Meteor.isClient) {
  111. // addFields does not exist client-side
  112. test.throws(function() {
  113. AccountsTemplates.addFields();
  114. });
  115. } else {
  116. // Not an array of objects
  117. test.throws(function() {
  118. AccountsTemplates.addFields("");
  119. }, function(err) {
  120. if (err instanceof Error && err.message === "field argument should be an array of valid field objects!")
  121. return true;
  122. });
  123. test.throws(function() {
  124. AccountsTemplates.addFields(100);
  125. }, function(err) {
  126. if (err instanceof Error && err.message === "field argument should be an array of valid field objects!")
  127. return true;
  128. });
  129. // Empty array
  130. test.throws(function() {
  131. AccountsTemplates.addFields([]);
  132. }, function(err) {
  133. if (err instanceof Error && err.message === "field argument should be an array of valid field objects!")
  134. return true;
  135. });
  136. // Successful add
  137. var first_name = {
  138. _id: "first_name",
  139. displayName: "First Name",
  140. type: "text",
  141. minLength: 2,
  142. maxLength: 50,
  143. required: true
  144. };
  145. var last_name = {
  146. _id: "last_name",
  147. displayName: "Last Name",
  148. type: "text",
  149. minLength: 2,
  150. maxLength: 100,
  151. required: false
  152. };
  153. AccountsTemplates.addFields([first_name, last_name]);
  154. // Now removes ot to be consistend with tests re-run
  155. AccountsTemplates.removeField("first_name");
  156. AccountsTemplates.removeField("last_name");
  157. }
  158. // Restores initialized state...
  159. AccountsTemplates._initialized = true;
  160. });
  161. Tinytest.add("AccountsTemplates - setState/getState", function(test) {
  162. if (Meteor.isServer) {
  163. // getState does not exist server-side
  164. test.throws(function() {
  165. AccountsTemplates.getState();
  166. });
  167. // setState does not exist server-side
  168. test.throws(function() {
  169. AccountsTemplates.setState();
  170. });
  171. } else {
  172. _.each(AccountsTemplates.STATES, function(state){
  173. AccountsTemplates.setState(state);
  174. test.equal(AccountsTemplates.getState(), state);
  175. });
  176. // Setting an invalid state should throw a Meteor.Error
  177. test.throws(function() {
  178. AccountsTemplates.setState("foo");
  179. }, function(err) {
  180. if (err instanceof Meteor.Error && err.details == "accounts-templates-core package got an invalid state value!")
  181. return true;
  182. });
  183. }
  184. });
  185. // -------------------------------------
  186. // TODO: complite the following tests...
  187. // -------------------------------------
  188. Tinytest.add("AccountsTemplates - configure", function(test) {
  189. if (Meteor.isClient) {
  190. // configure does not exist client-side
  191. test.throws(function() {
  192. AccountsTemplates.configure({});
  193. });
  194. } else {
  195. // TODO: write actual tests...
  196. }
  197. });