test.web.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. import test from 'ava'
  2. import Chance from '../chance.js'
  3. import _ from 'lodash'
  4. const chance = new Chance()
  5. // chance.avatar()
  6. test('avatar() returns a legit avatar', t => {
  7. _.times(1000, () => {
  8. let avatar = chance.avatar()
  9. t.true(_.isString(avatar))
  10. t.is(avatar.split('/').length, 5)
  11. })
  12. })
  13. test('avatar() can take and ignore an invalid protocol', t => {
  14. _.times(1000, () => {
  15. let avatar = chance.avatar({ protocol: 'invalid' })
  16. t.true(_.isString(avatar))
  17. t.is(avatar.indexOf('//'), 0)
  18. })
  19. })
  20. test('avatar() can take and respect a protocol', t => {
  21. const protocols = ['http', 'https']
  22. _.times(500, () => {
  23. protocols.map((protocol) => {
  24. let avatar = chance.avatar({ protocol: protocol })
  25. t.true(_.isString(avatar))
  26. t.is(avatar.indexOf(protocol + ":"), 0)
  27. })
  28. })
  29. })
  30. test('avatar() can take and respect email', t => {
  31. _.times(1000, () => {
  32. let avatar = chance.avatar({ email: chance.email() })
  33. t.true(_.isString(avatar))
  34. t.is(avatar.split('/').length, 5)
  35. })
  36. })
  37. test('avatar() can take and ignore an invalid file extension', t => {
  38. _.times(1000, () => {
  39. let avatar = chance.avatar({ fileExtension: 'invalid' })
  40. t.true(_.isString(avatar))
  41. t.false(avatar.includes('.jpg'))
  42. t.false(avatar.includes('.png'))
  43. t.false(avatar.includes('.gif'))
  44. })
  45. })
  46. test('avatar() can take and respect a legit file extension', t => {
  47. let file_types = ['bmp', 'gif', 'jpg', 'png']
  48. _.times(250, () => {
  49. _.times(file_types.length, (index) => {
  50. let avatar = chance.avatar({ fileExtension: file_types[index] })
  51. t.true(_.isString(avatar))
  52. t.true(avatar.includes('.' + file_types[index]))
  53. })
  54. })
  55. })
  56. test('avatar() can take and ignore an invalid size', t => {
  57. _.times(1000, () => {
  58. let avatar = chance.avatar({ size: 'abc' })
  59. t.true(_.isString(avatar))
  60. t.false(avatar.includes('&s='))
  61. })
  62. })
  63. test('avatar() can take and respect a legit size', t => {
  64. _.times(1000, (index) => {
  65. let avatar = chance.avatar({ size: index + 1 })
  66. t.true(_.isString(avatar))
  67. t.true(avatar.includes('&s=' + (index + 1).toString()))
  68. })
  69. })
  70. test('avatar() can take and ignore an invalid rating', t => {
  71. _.times(1000, () => {
  72. let avatar = chance.avatar({ rating: 'invalid' })
  73. t.true(_.isString(avatar))
  74. t.false(avatar.includes('&r='))
  75. })
  76. })
  77. test('avatar() can take and respect a rating', t => {
  78. let ratings = ['g', 'pg', 'r', 'x']
  79. _.times(250, () => {
  80. _.times(ratings.length, (index) => {
  81. let avatar = chance.avatar({ rating: ratings[index] })
  82. t.true(_.isString(avatar))
  83. t.true(avatar.includes('&r=' + ratings[index]))
  84. })
  85. })
  86. })
  87. test('avatar() can take and ignore an invalid fallback image', t => {
  88. _.times(1000, () => {
  89. let avatar = chance.avatar({ fallback: 'invalid' })
  90. t.true(_.isString(avatar))
  91. t.false(avatar.includes('&d='))
  92. })
  93. })
  94. test('avatar() can take just an email', t => {
  95. _.times(1000, () => {
  96. let avatar = chance.avatar('mail@victorquinn.com')
  97. t.true(_.isString(avatar))
  98. t.false(avatar.includes('&d='))
  99. })
  100. })
  101. test('avatar() can take and respect a fallback image', t => {
  102. let fallbacks = [
  103. '404', // Return 404 if not found
  104. 'mm', // Mystery man
  105. 'identicon', // Geometric pattern based on hash
  106. 'monsterid', // A generated monster icon
  107. 'wavatar', // A generated face
  108. 'retro', // 8-bit icon
  109. 'blank' // A transparent png
  110. ]
  111. _.times(100, () => {
  112. _.times(fallbacks.length, (index) => {
  113. let avatar = chance.avatar({ fallback: fallbacks[index] })
  114. t.true(_.isString(avatar))
  115. t.true(avatar.includes('&d=' + fallbacks[index]))
  116. })
  117. })
  118. })
  119. // chance.color()
  120. test('color() returns what looks like a hex color', t => {
  121. _.times(1000, () => {
  122. let color = chance.color({ format: 'hex' })
  123. t.true(_.isString(color))
  124. t.is(color.length, 7)
  125. t.true(/#[a-z0-9]+/m.test(color))
  126. })
  127. })
  128. test('color() returns what looks like a gray scale hex color', t => {
  129. _.times(1000, () => {
  130. let color = chance.color({ format: 'hex', grayscale: true })
  131. t.true(_.isString(color))
  132. t.is(color.length, 7)
  133. t.true(/#[a-z0-9]+/m.test(color))
  134. t.is(color.slice(1, 3), color.slice(3, 5))
  135. t.is(color.slice(1, 3), color.slice(5, 7))
  136. })
  137. })
  138. test('color() returns a short hex color', t => {
  139. _.times(1000, () => {
  140. let color = chance.color({ format: 'shorthex' })
  141. t.true(_.isString(color))
  142. t.is(color.length, 4)
  143. t.true(/#[a-z0-9]+/m.test(color))
  144. })
  145. })
  146. test('color() returns what looks like a grayscale short hex color', t => {
  147. _.times(1000, () => {
  148. let color = chance.color({ format: 'shorthex', grayscale: true })
  149. t.true(_.isString(color))
  150. t.is(color.length, 4)
  151. t.is(color.slice(1, 2), color.slice(2, 3))
  152. t.is(color.slice(1, 2), color.slice(3, 4))
  153. })
  154. })
  155. test('color() returns what looks like an rgb color', t => {
  156. _.times(1000, () => {
  157. let color = chance.color({ format: 'rgb' })
  158. t.true(_.isString(color))
  159. let match = /rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)/.exec(color)
  160. t.is(match.length, 4)
  161. t.true(match[1] >= 0)
  162. t.true(match[1] <= 255)
  163. t.true(match[2] >= 0)
  164. t.true(match[2] <= 255)
  165. t.true(match[3] >= 0)
  166. t.true(match[3] <= 255)
  167. })
  168. })
  169. test('color() returns what looks like a grayscale rgb color', t => {
  170. _.times(1000, () => {
  171. let color = chance.color({ format: 'rgb', grayscale: true })
  172. t.true(_.isString(color))
  173. let match = /rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)/.exec(color)
  174. t.is(match.length, 4)
  175. t.true(match[1] >= 0)
  176. t.true(match[1] <= 255)
  177. t.is(match[1], match[2])
  178. t.is(match[1], match[3])
  179. })
  180. })
  181. test('color() returns what looks like an rgba color', t => {
  182. _.times(1000, () => {
  183. let color = chance.color({ format: 'rgba' })
  184. t.true(_.isString(color))
  185. let match = /rgba\((\d{1,3}),(\d{1,3}),(\d{1,3}),([01]\.?\d*?)\)/.exec(color)
  186. t.is(match.length, 5)
  187. t.true(match[1] >= 0)
  188. t.true(match[1] <= 255)
  189. t.true(match[2] >= 0)
  190. t.true(match[2] <= 255)
  191. t.true(match[3] >= 0)
  192. t.true(match[3] <= 255)
  193. t.true(match[4] >= 0)
  194. t.true(match[4] <= 255)
  195. })
  196. })
  197. test('color() returns what looks like a grayscale rgba color', t => {
  198. _.times(1000, () => {
  199. let color = chance.color({ format: 'rgba', grayscale: true })
  200. t.true(_.isString(color))
  201. let match = /rgba\((\d{1,3}),(\d{1,3}),(\d{1,3}),([01]\.?\d*?)\)/.exec(color)
  202. t.is(match.length, 5)
  203. t.true(match[1] >= 0)
  204. t.true(match[1] <= 255)
  205. t.true(match[2] >= 0)
  206. t.true(match[2] <= 255)
  207. t.true(match[3] >= 0)
  208. t.true(match[3] <= 255)
  209. t.true(match[4] >= 0)
  210. t.true(match[4] <= 255)
  211. t.is(match[1], match[2])
  212. t.is(match[1], match[3])
  213. t.true(match[4] >= 0)
  214. t.true(match[4] <= 1)
  215. })
  216. })
  217. test('color() 0x color works', t => {
  218. _.times(1000, () => {
  219. let color = chance.color({ format: '0x' })
  220. t.true(_.isString(color))
  221. t.is(color.length, 8)
  222. t.true(/0x[a-z0-9]+/m.test(color))
  223. })
  224. })
  225. test('color() with name returns a valid color name', t => {
  226. _.times(1000, () => {
  227. let color = chance.color({ format: 'name' })
  228. t.true(_.isString(color))
  229. })
  230. })
  231. test('color() upper case returns upper cased color', t => {
  232. _.times(1000, () => {
  233. let color = chance.color({ format: 'hex', casing: 'upper' })
  234. t.true(_.isString(color))
  235. t.is(color.length, 7)
  236. t.is(color.charAt(1).toUpperCase(), color.charAt(1))
  237. t.is(color.charAt(2).toUpperCase(), color.charAt(2))
  238. t.is(color.charAt(3).toUpperCase(), color.charAt(3))
  239. t.is(color.charAt(4).toUpperCase(), color.charAt(4))
  240. t.is(color.charAt(5).toUpperCase(), color.charAt(5))
  241. t.is(color.charAt(6).toUpperCase(), color.charAt(6))
  242. })
  243. })
  244. test('color() bogus format throws error', t => {
  245. const fn = () => chance.color({ format: 'banana' })
  246. t.throws(fn)
  247. })
  248. // chance.domain()
  249. test('domain() returns a domain', t => {
  250. _.times(1000, () => {
  251. let domain = chance.domain()
  252. t.true(_.isString(domain))
  253. t.true(domain.split('.').length > 1)
  254. })
  255. })
  256. test('domain() obeys tld, if specified', t => {
  257. _.times(1000, () => {
  258. let domain = chance.domain({ tld: 'com' })
  259. t.true(_.isString(domain))
  260. t.is(domain.split('.')[1], 'com')
  261. })
  262. })
  263. // chance.email()
  264. test('email() returns what looks like an email', t => {
  265. _.times(1000, () => {
  266. let email = chance.email()
  267. t.true(_.isString(email))
  268. t.true(email.split('@').length > 1)
  269. })
  270. })
  271. test('email() obeys domain, if specified', t => {
  272. _.times(1000, () => {
  273. let email = chance.email({ domain: 'victorquinn.com' })
  274. t.true(_.isString(email))
  275. t.is(email.split('@')[1], 'victorquinn.com')
  276. })
  277. })
  278. test('email() has a leng specified, should generate string before domain with equal length', t => {
  279. _.times(1000, () => {
  280. let email = chance.email({ length: 5 })
  281. t.is(email.split('@')[0].length, 5)
  282. })
  283. })
  284. // chance.fbid()
  285. test('fbid() returns what looks like a Facebook id', t => {
  286. _.times(1000, () => {
  287. let fbid = chance.fbid()
  288. t.true(_.isString(fbid))
  289. t.is(fbid.length, 16)
  290. })
  291. })
  292. // chance.google_analytics()
  293. test('google_analytics() returns what looks like a valid tracking code', t => {
  294. _.times(1000, () => {
  295. let tracking_code = chance.google_analytics()
  296. t.true(_.isString(tracking_code))
  297. t.is(tracking_code.length, 12)
  298. t.true(tracking_code.includes('UA-'))
  299. })
  300. })
  301. // chance.hashtag()
  302. test('hashtag() returns what looks like a hashtag', t => {
  303. _.times(1000, () => {
  304. let hashtag = chance.hashtag()
  305. t.true(_.isString(hashtag))
  306. t.true(/^\#\w+$/m.test(hashtag))
  307. })
  308. })
  309. // chance.ip()
  310. test('ip() returns what looks like an IP address', t => {
  311. _.times(1000, () => {
  312. let ip = chance.ip()
  313. t.true(_.isString(ip))
  314. t.is(ip.split('.').length, 4)
  315. t.true(/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/.test(ip))
  316. })
  317. })
  318. // chance.ipv6()
  319. test('ipv6() returns what looks like an IP address (v6)', t => {
  320. _.times(1000, () => {
  321. let ipv6 = chance.ipv6()
  322. t.true(_.isString(ipv6))
  323. t.is(ipv6.split(':').length, 8)
  324. t.true(/[a-f0-9]+:[a-f0-9]+:[a-f0-9]+:[a-f0-9]+:[a-f0-9]+:[a-f0-9]+:[a-f0-9]+:[a-f0-9]+/.test(ipv6))
  325. })
  326. })
  327. // chance.klout()
  328. test('klout() returns what looks like a legit Klout score', t => {
  329. _.times(1000, () => {
  330. let klout = chance.klout()
  331. t.true(_.isNumber(klout))
  332. t.true(klout > 0)
  333. t.true(klout <= 100)
  334. })
  335. })
  336. // chance.locale()
  337. test('locale() should create a valid two character locale with only language', t => {
  338. let locale = chance.locale()
  339. t.true(_.isString(locale))
  340. t.is(locale.length, 2)
  341. })
  342. test('locale() should create a locale with a region code', t => {
  343. let locale = chance.locale({ region: true })
  344. t.true(_.isString(locale))
  345. t.true(locale.split('-').length >= 2)
  346. })
  347. // chance.locales()
  348. test('locales() should return a list of locales', t => {
  349. let locales = chance.locales()
  350. t.true(_.isArray(locales))
  351. t.true(locales.length > 100)
  352. })
  353. test('locales() should return a list of locales', t => {
  354. let locales = chance.locales({ region: true })
  355. t.true(_.isArray(locales))
  356. t.true(locales.length > 100)
  357. })
  358. // chance.md5()
  359. test('md5() should create a hex-encoded MD5 hash of a random ASCII value when passed nothing', t => {
  360. t.is(chance.md5().length, '2063c1608d6e0baf80249c42e2be5804'.length)
  361. })
  362. test('md5() should create a hex-encoded MD5 hash of an ASCII value when passed a string', t => {
  363. t.is(chance.md5('value'), '2063c1608d6e0baf80249c42e2be5804')
  364. })
  365. test('md5() should create a hex-encoded MD5 hash of an ASCII value when passed an object', t => {
  366. t.is(chance.md5({ str: 'value' }), '2063c1608d6e0baf80249c42e2be5804')
  367. })
  368. test('md5() should create a hex-encoded MD5 hash of a UTF-8 value', t => {
  369. t.is(chance.md5('日本'), '4dbed2e657457884e67137d3514119b3')
  370. })
  371. test('md5() should create a hex-encoded HMAC-MD5 hash of an ASCII value and key', t => {
  372. t.is(chance.md5({ str: 'value', key: 'key' }), '01433efd5f16327ea4b31144572c67f6')
  373. })
  374. test('md5() should create a hex-encoded HMAC-MD5 hash of a UTF-8 value and key', t => {
  375. t.is(chance.md5({ str: '日本', key: '日本' }), 'c78b8c7357926981cc04740bd3e9d015')
  376. })
  377. test('md5() should create a raw MD5 hash of an ASCII value', t => {
  378. t.is(chance.md5({ str: 'value', key: null, raw: true }), ' c\xc1`\x8dn\x0b\xaf\x80$\x9cB\xe2\xbeX\x04')
  379. })
  380. test('md5() should create a raw MD5 hash of a UTF-8 value', t => {
  381. t.is(chance.md5({ str: '日本', key: null, raw: true }), 'M\xbe\xd2\xe6WEx\x84\xe6q7\xd3QA\x19\xb3')
  382. })
  383. test('md5() should create a raw HMAC-MD5 hash of an ASCII value', t => {
  384. t.is(chance.md5({ str: 'value', key: 'key', raw: true }), '\x01C>\xfd_\x162~\xa4\xb3\x11DW,g\xf6')
  385. })
  386. test('md5() should create a raw HMAC-MD5 hash of a UTF-8 value', t => {
  387. t.is(chance.md5({ str: '日本', key: '日本', raw: true }), '\xc7\x8b\x8csW\x92i\x81\xcc\x04t\x0b\xd3\xe9\xd0\x15')
  388. })
  389. // chance.port()
  390. test('port() should create a number in the valid port range (0 - 65535)', t => {
  391. _.times(1000, () => {
  392. let port = chance.port()
  393. t.true(_.isNumber(port))
  394. t.true(port >= 0)
  395. t.true(port <= 65535)
  396. })
  397. })
  398. // chance.semver()
  399. test('semver() works as expected', t => {
  400. _.times(1000, () => {
  401. let semver = chance.semver()
  402. t.true(_.isString(semver))
  403. t.true(/[0-9]+\.[0-9]+\.[0-9]+/.test(semver))
  404. })
  405. })
  406. test('semver() accepts a range', t => {
  407. _.times(1000, () => {
  408. let semver = chance.semver({ range: 'banana' })
  409. t.true(_.isString(semver))
  410. t.true(/^banana[0-9]+\.[0-9]+\.[0-9]+/.test(semver))
  411. })
  412. })
  413. test('semver() accepts a prerelease flag', t => {
  414. _.times(1000, () => {
  415. let semver = chance.semver({ range: 'banana' })
  416. t.true(_.isString(semver))
  417. t.true(/[0-9]+\.[0-9]+\.[0-9]+-?[dev|beta|alpha]?/.test(semver))
  418. })
  419. })
  420. // chance.tld()
  421. test('tld() returns a tld', t => {
  422. _.times(1000, () => {
  423. let tld = chance.tld()
  424. t.true(_.isString(tld))
  425. t.true(tld.length < 6)
  426. })
  427. })
  428. // chance.twitter()
  429. test('twitter() returns what looks like a Twitter handle', t => {
  430. _.times(1000, () => {
  431. let twitter = chance.twitter()
  432. t.true(_.isString(twitter))
  433. t.true(/\@[A-Za-z]+/m.test(twitter))
  434. })
  435. })
  436. // chance.url()
  437. test('url() deal with url', t => {
  438. _.times(1000, () => {
  439. let url = chance.url()
  440. t.true(_.isString(url))
  441. t.true(url.split('.').length > 1)
  442. t.true(url.split('://').length > 1)
  443. })
  444. })
  445. test('url() can take and respect a domain', t => {
  446. _.times(1000, () => {
  447. let url = chance.url({ domain: 'victorquinn.com' })
  448. t.true(_.isString(url))
  449. t.true(url.split('.').length > 1)
  450. t.true(url.split('://').length > 1)
  451. t.true(url.split('victorquinn.com').length > 1)
  452. })
  453. })
  454. test('url() can take and respect a domain prefix', t => {
  455. _.times(1000, () => {
  456. let url = chance.url({ domain_prefix: 'www' })
  457. t.true(_.isString(url))
  458. t.true(url.split('.').length > 1)
  459. t.true(url.split('://').length > 1)
  460. t.true(url.split('www').length > 1)
  461. })
  462. })
  463. test('url() can take and respect a path', t => {
  464. _.times(1000, () => {
  465. let url = chance.url({ path: 'images' })
  466. t.true(_.isString(url))
  467. t.true(url.split('.').length > 1)
  468. t.true(url.split('://').length > 1)
  469. t.true(url.split('/images').length > 1)
  470. })
  471. })
  472. test('url() can take and respect extensions', t => {
  473. _.times(1000, () => {
  474. let url = chance.url({ extensions: ['html'] })
  475. t.true(_.isString(url))
  476. t.true(url.split('.').length > 1)
  477. t.true(url.split('://').length > 1)
  478. t.not(url.indexOf('.html'), -1)
  479. })
  480. })
  481. // chance.loremPicsum()
  482. test('loremPicsum() returns loremPicsum url with default width and height', t => {
  483. _.times(1000, () => {
  484. let loremPicsumUrl = chance.loremPicsum()
  485. t.true(_.isString(loremPicsumUrl))
  486. t.true(loremPicsumUrl.split('.').length > 1)
  487. t.true(loremPicsumUrl.split('://').length > 1)
  488. t.true(loremPicsumUrl.split('picsum.photos').length > 1)
  489. t.true(loremPicsumUrl.split('/500/500').length > 1)
  490. t.true(loremPicsumUrl.split('/?random').length > 1)
  491. })
  492. })
  493. test('loremPicsum() returns loremPicsum url that respects width and height', t => {
  494. _.times(1000, () => {
  495. let width = chance.natural();
  496. let height = chance.natural();
  497. let loremPicsumUrl = chance.loremPicsum({
  498. width,
  499. height
  500. })
  501. t.true(_.isString(loremPicsumUrl))
  502. t.true(loremPicsumUrl.split('.').length > 1)
  503. t.true(loremPicsumUrl.split('://').length > 1)
  504. t.true(loremPicsumUrl.split('picsum.photos').length > 1)
  505. t.true(loremPicsumUrl.split('/' + width + '/' + height).length > 1)
  506. t.true(loremPicsumUrl.split('/?random').length > 1)
  507. })
  508. })
  509. test('loremPicsum() returns loremPicsum url that respects greyscale', t => {
  510. _.times(1000, () => {
  511. let loremPicsumUrl = chance.loremPicsum({
  512. greyscale: true
  513. })
  514. t.true(_.isString(loremPicsumUrl))
  515. t.true(loremPicsumUrl.split('.').length > 1)
  516. t.true(loremPicsumUrl.split('://').length > 1)
  517. t.true(loremPicsumUrl.split('picsum.photos').length > 1)
  518. t.true(loremPicsumUrl.split('/g/500/500').length > 1)
  519. t.true(loremPicsumUrl.split('/?random').length > 1)
  520. })
  521. })
  522. test('loremPicsum() returns loremPicsum url that respects blurred', t => {
  523. _.times(1000, () => {
  524. let loremPicsumUrl = chance.loremPicsum({
  525. blurred: true
  526. })
  527. t.true(_.isString(loremPicsumUrl))
  528. t.true(loremPicsumUrl.split('.').length > 1)
  529. t.true(loremPicsumUrl.split('://').length > 1)
  530. t.true(loremPicsumUrl.split('picsum.photos').length > 1)
  531. t.true(loremPicsumUrl.split('/500/500').length > 1)
  532. t.true(loremPicsumUrl.split('/?blur').length > 1)
  533. })
  534. })