nodeApi.mjs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* eslint no-console: 0 */
  2. /**
  3. * nodeApi.js
  4. *
  5. * Test node api utilities
  6. *
  7. * @author d98762625 [d98762625@gmail.com]
  8. * @copyright Crown Copyright 2018
  9. * @license Apache-2.0
  10. */
  11. import assert from "assert";
  12. import it from "../assertionHandler";
  13. import chef from "../../../src/node/index";
  14. import TestRegister from "../../TestRegister";
  15. TestRegister.addApiTests([
  16. it("should have some operations", () => {
  17. assert(chef);
  18. assert(chef.toBase32);
  19. assert(chef.setUnion);
  20. assert(!chef.randomFunction);
  21. }),
  22. it("should have an async/await api", async () => {
  23. try {
  24. const result = await chef.toBase32("input");
  25. assert.notEqual("something", result);
  26. } catch (e) {
  27. // shouldnt reach here
  28. assert(false);
  29. }
  30. try {
  31. const fail = chef.setUnion("1");
  32. // shouldnt get here
  33. assert(!fail || false);
  34. } catch (e) {
  35. assert(true);
  36. }
  37. }),
  38. it("should have a callback API", async () => {
  39. await chef.toBase32("something", (err, result) => {
  40. if (err) {
  41. assert(false);
  42. } else {
  43. assert.equal("ONXW2ZLUNBUW4ZY=", result);
  44. }
  45. });
  46. }),
  47. it("should handle errors in callback API", async () => {
  48. await chef.setUnion("1", (err, result) => {
  49. if (err) {
  50. assert(true);
  51. return;
  52. }
  53. assert(false);
  54. });
  55. }),
  56. it("should accept arguments in object format for operations", async () => {
  57. const result = await chef.setUnion("1 2 3 4:3 4 5 6", {
  58. itemDelimiter: " ",
  59. sampleDelimiter: ":"
  60. });
  61. assert.equal(result, "1 2 3 4 5 6");
  62. }),
  63. it("should accept just some of the optional arguments being overriden", async () => {
  64. const result = await chef.setIntersection("1 2 3 4 5\\n\\n3 4 5", {
  65. itemDelimiter: " ",
  66. });
  67. assert.equal(result, "3 4 5");
  68. }),
  69. it("should accept no override arguments and just use the default values", async () => {
  70. const result = await chef.powerSet("1,2,3");
  71. assert.equal(result, "\n3\n2\n1\n2,3\n1,3\n1,2\n1,2,3\n");
  72. })
  73. ]);