vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

265 lines
7.9 KiB

  1. /**
  2. * The goal here is to have a minimum-viable test case here, in order to
  3. * check changes in Validator.
  4. *
  5. * Changes in Validator should ideally be checked to see if they trigger here.
  6. *
  7. * test-console reference: https://github.com/jamesshore/test-console
  8. */
  9. var assert = require('assert');
  10. var stdout = require('test-console').stdout;
  11. var Validator = require("../lib/shared/Validator").default;
  12. // Copied from lib/network/options.js
  13. let string = 'string';
  14. let bool = 'boolean';
  15. let number = 'number';
  16. let array = 'array';
  17. let object = 'object'; // should only be in a __type__ property
  18. let dom = 'dom';
  19. let any = 'any';
  20. let allOptions = {
  21. // simple options
  22. enabled: { boolean: bool },
  23. inherit: { string: ['from', 'to', 'both'] },
  24. size: { number },
  25. filter: { 'function': 'function' },
  26. chosen: {
  27. label: { boolean: bool },
  28. edge: { 'function': 'function' },
  29. __type__: { object }
  30. },
  31. chosen2: {
  32. label: { string },
  33. __type__: { object }
  34. },
  35. // Tests with any. These have been tailored to test all paths in:
  36. // - Validator.check()
  37. // - Validator.checkFields()
  38. __any__: { string }, // Any option name allowed here, but it must be a string
  39. // NOTE: you can have as many new options as you want! IS THIS INTENTIONAL?
  40. groups: {
  41. generic: { any },
  42. __any__: { any },
  43. __type__: { object }
  44. },
  45. // TODO: add combined options, e.g.
  46. //inherit: { string: ['from', 'to', 'both'], boolean: bool },
  47. //filter: { boolean: bool, string, array, 'function': 'function' },
  48. __type__: { object }
  49. };
  50. describe('Validator', function() {
  51. function run_validator(options, check_correct, definition = undefined) {
  52. let errorFound;
  53. let output;
  54. if (definition === undefined) {
  55. definition = allOptions;
  56. }
  57. output = stdout.inspectSync(function() {
  58. errorFound = Validator.validate(options, definition);
  59. });
  60. if (check_correct) {
  61. assert(!errorFound);
  62. assert(output.length === 0, 'No error expected');
  63. } else {
  64. //console.log(output); //sometimes useful here
  65. assert(errorFound, 'Validation should have failed');
  66. assert(output.length !== 0, 'must return errors');
  67. }
  68. return output;
  69. }
  70. function testExpected(output, expectedErrors) {
  71. for (let i = 0; i < expectedErrors.length; ++i) {
  72. assert(expectedErrors[i].test(output[i]), 'Regular expression at index ' + i + ' failed');
  73. }
  74. assert(output.length === expectedErrors.length, 'Number of expected errors does not match returned errors');
  75. }
  76. it('handles regular options correctly', function(done) {
  77. // Empty options should be accepted as well
  78. run_validator({}, true);
  79. // test values for all options
  80. var options = {
  81. enabled: true,
  82. inherit: 'from',
  83. size : 123,
  84. filter : function() { return true; },
  85. chosen : {
  86. label: false,
  87. edge :function() { return true; },
  88. },
  89. chosen2: {
  90. label: "I am a string"
  91. },
  92. myNameDoesntMatter: "My type does",
  93. groups : {
  94. generic: "any type is good here",
  95. dontCareAboutName: [0,1,2,3] // Type can also be anything
  96. }
  97. };
  98. run_validator(options, true);
  99. done();
  100. });
  101. it('rejects incorrect options', function(done) {
  102. // All of the options are wrong, all should generate an error
  103. var options = {
  104. iDontExist: 42, // name is 'any' but type must be string
  105. enabled : 'boolean',
  106. inherit : 'abc',
  107. size : 'not a number',
  108. filter : 42,
  109. chosen : 'not an object',
  110. chosen2 : {
  111. label : 123,
  112. // Following test the working of Validator.getSuggestion()
  113. iDontExist: 'asdf',
  114. generic : "I'm not defined here",
  115. labe : 42, // Incomplete name
  116. labell : 123,
  117. },
  118. };
  119. var output = run_validator(options, false);
  120. // Sometimes useful: console.log(output);
  121. // Errors are in the order as the options are defined in the object
  122. let expectedErrors = [
  123. /Invalid type received for "iDontExist"\. Expected: string\. Received \[number\]/,
  124. /Invalid type received for "enabled"\. Expected: boolean\. Received \[string\]/,
  125. /Invalid option detected in "inherit"\. Allowed values are:from, to, both not/,
  126. /Invalid type received for "size"\. Expected: number\. Received \[string\]/,
  127. /Invalid type received for "filter"\. Expected: function\. Received \[number\]/,
  128. /Invalid type received for "chosen"\. Expected: object\. Received \[string\]/,
  129. /Invalid type received for "label". Expected: string. Received \[number\]/,
  130. // Expected results of Validator.getSuggestion()
  131. /Unknown option detected: "iDontExist"\. Did you mean one of these:/,
  132. /Unknown option detected: "generic"[\s\S]*Perhaps it was misplaced\? Matching option found at:/gm,
  133. /Unknown option detected: "labe"[\s\S]*Perhaps it was incomplete\? Did you mean:/gm,
  134. /Unknown option detected: "labell"\. Did you mean "label"\?/
  135. ];
  136. testExpected(output, expectedErrors);
  137. done();
  138. });
  139. /**
  140. * Explicit tests on explicit 'undefined', to be really sure it works as expected.
  141. */
  142. it('properly handles explicit `undefined`', function(done) {
  143. // Option definitions with 'undefined'
  144. let undefinedOptions = {
  145. width : { number, 'undefined': 'undefined' },
  146. undefOnly : { 'undefined': 'undefined' },
  147. colorOptions : {
  148. fill : { string },
  149. stroke : { string, 'undefined': 'undefined' },
  150. strokeWidth: { number },
  151. __type__ : { string, object, 'undefined': 'undefined' }
  152. },
  153. moreOptions : {
  154. hello : { string },
  155. world : { string, 'undefined': 'undefined' },
  156. __type__ : { object }
  157. }
  158. }
  159. //
  160. // Test good actual option values
  161. //
  162. let correct1 = {
  163. width : 42,
  164. colorOptions: 'I am a string',
  165. moreOptions : {
  166. hello: 'world',
  167. world: '!'
  168. }
  169. }
  170. var output = run_validator(correct1, true, undefinedOptions);
  171. let correct2 = {
  172. width : undefined,
  173. colorOptions: {
  174. fill : 'I am a string',
  175. stroke: 'I am a string'
  176. },
  177. moreOptions : {
  178. world: undefined
  179. }
  180. }
  181. var output = run_validator(correct2, true, undefinedOptions);
  182. let correct3 = {
  183. width : undefined,
  184. undefOnly : undefined,
  185. colorOptions: undefined
  186. }
  187. var output = run_validator(correct3, true, undefinedOptions);
  188. //
  189. // Test bad actual option values
  190. //
  191. let bad1 = {
  192. width : 'string',
  193. undefOnly : 42,
  194. colorOptions: 42,
  195. moreOptions : undefined
  196. }
  197. var output = run_validator(bad1, false, undefinedOptions);
  198. let expectedErrors = [
  199. /Invalid type received for "width"\. Expected: number, undefined\. Received \[string\]/,
  200. /Invalid type received for "undefOnly"\. Expected: undefined\. Received \[number\]/,
  201. /Invalid type received for "colorOptions"\. Expected: string, object, undefined\. Received \[number\]/,
  202. /Invalid type received for "moreOptions"\. Expected: object\. Received \[undefined\]/
  203. ];
  204. testExpected(output, expectedErrors);
  205. let bad2 = {
  206. undefOnly : 'undefined',
  207. colorOptions: {
  208. fill: undefined
  209. } ,
  210. moreOptions: {
  211. hello: undefined,
  212. world: 42
  213. }
  214. }
  215. var output = run_validator(bad2, false, undefinedOptions);
  216. let expectedErrors2= [
  217. /Invalid type received for "undefOnly"\. Expected: undefined\. Received \[string\]/,
  218. /Invalid type received for "fill"\. Expected: string\. Received \[undefined\]/,
  219. /Invalid type received for "hello"\. Expected: string\. Received \[undefined\]/,
  220. /Invalid type received for "world"\. Expected: string, undefined\. Received \[number\]/
  221. ];
  222. testExpected(output, expectedErrors2);
  223. done();
  224. });
  225. });