emoji.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*jslint node: true*/
  2. /*jslint expr: true*/
  3. /*global describe, it*/
  4. "use strict";
  5. var should = require('should');
  6. var emoji = require('../index');
  7. describe("emoji.js", function () {
  8. describe("get(emoji)", function () {
  9. it("should return an emoji code", function () {
  10. var coffee = emoji.get('coffee');
  11. should.exist(coffee);
  12. coffee.should.be.exactly('☕️');
  13. });
  14. it("should support github flavored markdown emoji", function () {
  15. var coffee = emoji.get(':coffee:');
  16. should.exist(coffee);
  17. coffee.should.be.exactly('☕️');
  18. });
  19. });
  20. describe("random()", function () {
  21. it("should return a random emoji and the corresponding key", function () {
  22. var result = emoji.random();
  23. should.exist(result);
  24. should.exist(result.key);
  25. should.exist(result.emoji);
  26. result.emoji.should.be.exactly(emoji.get(result.key));
  27. });
  28. });
  29. describe("which(emoji_code)", function () {
  30. it("should return name of the emoji", function () {
  31. var coffee = emoji.which('☕️');
  32. should.exist(coffee);
  33. coffee.should.be.exactly('coffee');
  34. });
  35. it("should work for differently formed characters", function () {
  36. var umbrella = emoji.which('☔');
  37. should.exist(umbrella);
  38. umbrella.should.be.exactly('umbrella');
  39. });
  40. it("should return the same name for differently formed characters", function () {
  41. var umbrella1 = emoji.which('☔');
  42. should.exist(umbrella1);
  43. var umbrella2 = emoji.which('☔️');
  44. should.exist(umbrella2);
  45. umbrella1.should.equal(umbrella2);
  46. });
  47. });
  48. describe("emojify(str)", function () {
  49. it("should parse :emoji: in a string and replace them with the right emoji", function () {
  50. var coffee = emoji.emojify('I :heart: :coffee:! - :hushed::star::heart_eyes: ::: test : : :+1:+');
  51. should.exist(coffee);
  52. coffee.should.be.exactly('I ❤️ ☕️! - 😯⭐️😍 ::: test : : 👍+');
  53. });
  54. it("should leave unknown emoji", function () {
  55. var coffee = emoji.emojify('I :unknown_emoji: :star: :another_one:');
  56. should.exist(coffee);
  57. coffee.should.be.exactly('I :unknown_emoji: ⭐️ :another_one:');
  58. });
  59. it("should replace unknown emoji using provided cb function", function () {
  60. var coffee = emoji.emojify('I :unknown_emoji: :star: :another_one:', function(name) {
  61. return name;
  62. });
  63. should.exist(coffee);
  64. coffee.should.be.exactly('I unknown_emoji ⭐️ another_one');
  65. });
  66. });
  67. it("should return an emoji code", function () {
  68. var coffee = emoji.emoji.coffee;
  69. should.exist(coffee);
  70. coffee.should.be.exactly('☕️');
  71. });
  72. describe("search(str)", function () {
  73. it("should return partially matched emojis", function () {
  74. var matchingEmojis = emoji.search("cof");
  75. matchingEmojis.length.should.not.eql(0);
  76. matchingEmojis.forEach(function(emoji) {
  77. emoji.key.should.match(/^cof/);
  78. });
  79. });
  80. it("should only include emojies that begin with the search", function () {
  81. var matchingEmojis = emoji.search("ca");
  82. matchingEmojis.length.should.not.eql(0);
  83. matchingEmojis.forEach(function(emoji) {
  84. var index = emoji.key.indexOf("ca");
  85. index.should.be.exactly(0);
  86. });
  87. });
  88. it("should match when you include the colon", function () {
  89. var matchingEmojis = emoji.search(":c");
  90. matchingEmojis.length.should.not.eql(0);
  91. matchingEmojis.forEach(function(emoji) {
  92. var index = emoji.key.indexOf("c");
  93. index.should.be.exactly(0);
  94. });
  95. });
  96. it("should return an empty array when no matching emojis are found", function () {
  97. var matchingEmojis = emoji.search("notAnEmoji");
  98. matchingEmojis.length.should.be.exactly(0);
  99. });
  100. });
  101. });