smoke.test.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const fitCurve = require('../src/fit-curve');
  2. const _ = require('lodash');
  3. const expect = require('chai').expect;
  4. const verifyMatch = (expectedResult, actualResult) => {
  5. expect(actualResult).to.have.length(expectedResult.length);
  6. expectedResult.forEach(function (expectedBezierCurve, i) {
  7. var actualBezierCurve = actualResult[i];
  8. _.zip(actualBezierCurve, expectedBezierCurve).forEach(function (pairs) {
  9. expect(pairs[0][0]).to.closeTo(pairs[1][0], 1.0e-6);
  10. expect(pairs[0][1]).to.closeTo(pairs[1][1], 1.0e-6);
  11. });
  12. });
  13. };
  14. describe("Fitten curve", () => {
  15. it("should match example #1", () => {
  16. const expectedResult = [
  17. [[0, 0], [20.27317402, 20.27317402], [-1.24665147, 0], [20, 0]]
  18. ];
  19. const actualResult = fitCurve([[0, 0], [10, 10], [10, 0], [20, 0]], 50);
  20. verifyMatch(expectedResult, actualResult);
  21. });
  22. it("should match example #2", () => {
  23. const expectedResult = [
  24. [[0, 0], [20.27317402, 20.27317402], [-1.24665147, 0], [20, 0]]
  25. ];
  26. const actualResult = fitCurve([[0, 0], [10, 10], [10, 0], [20, 0], [20, 0]], 50);
  27. verifyMatch(expectedResult, actualResult);
  28. });
  29. it("should match example #3", () => {
  30. const expectedResult = [
  31. [ [ 244, 92 ],
  32. [ 284.2727272958473, 105.42424243194908 ],
  33. [ 287.98676736182495, 85 ],
  34. [ 297, 85 ]
  35. ]
  36. ];
  37. const actualResult = fitCurve([
  38. [244,92],[247,93],[251,95],[254,96],[258,97],[261,97],[265,97],[267,97],
  39. [270,97],[273,97],[281,97],[284,95],[286,94],[289,92],[291,90],[292,88],
  40. [294,86],[295,85],[296,85],[297,85]], 10);
  41. verifyMatch(expectedResult, actualResult);
  42. });
  43. it("should match example #3", () => {
  44. const expectedResult = [
  45. [[0, 0], [3.333333333333333, 3.333333333333333], [5.285954792089683, 10], [10, 10]],
  46. [[ 10, 10], [13.333333333333334, 10 ], [7.6429773960448415, 2.3570226039551585 ], [10, 0]],
  47. [[10, 0], [12.3570226, -2.3570226], [16.66666667, 0], [20, 0]]
  48. ];
  49. const actualResult = fitCurve([[0, 0], [10, 10], [10, 0], [20, 0]], 1);
  50. verifyMatch(expectedResult, actualResult);
  51. });
  52. it("shouldn't fail on perfect match", () => {
  53. const expectedResult = [
  54. [[0, 0], [6.66666666, 2.66666666], [13.33333333, 5.33333333], [20, 8]]
  55. ];
  56. const actualResult = fitCurve([[0, 0], [10, 4], [20, 8]], 0);
  57. verifyMatch(expectedResult, actualResult);
  58. });
  59. describe("when no arguments provided", () => {
  60. it("should throw a TypeError exception", () => {
  61. expect(() => fitCurve()).to.throw(TypeError, "First argument should be an array");
  62. })
  63. });
  64. describe("when one of the points doesn't conform expected format", () => {
  65. it("should throw an exception", () => {
  66. expect(() => fitCurve([[1, 1], [1]])).to.throw(Error,
  67. "Each point should be an array of numbers. Each point should have the same amount of numbers.");
  68. });
  69. });
  70. describe("when only one unique point provided", () => {
  71. it("should not throw an exception and return empty array", () => {
  72. const result = fitCurve([[1, 1], [1, 1]]);
  73. expect(result).to.eql([]);
  74. })
  75. });
  76. });