|
3 | 3 | * |
4 | 4 | * (c) 2024 Feedzai |
5 | 5 | */ |
6 | | -import { callIfExists } from "src/functions"; |
| 6 | +import { callIfExists } from "src/functions/utilities/call-if-exists"; |
7 | 7 |
|
8 | | -describe("callIfExist", () => { |
| 8 | +describe("callIfExists", () => { |
9 | 9 | it("should not throw an exception when no callback is passed", () => { |
10 | 10 | expect(callIfExists.bind(this, undefined)).not.throw(); |
11 | 11 | }); |
12 | 12 |
|
| 13 | + it("should not throw an exception when null callback is passed", () => { |
| 14 | + expect(callIfExists.bind(this, null)).not.throw(); |
| 15 | + }); |
| 16 | + |
| 17 | + it("should not throw an exception when non-function is passed", () => { |
| 18 | + // @ts-expect-error - Testing invalid input |
| 19 | + expect(callIfExists.bind(this, "not a function")).not.throw(); |
| 20 | + // @ts-expect-error - Testing invalid input |
| 21 | + expect(callIfExists.bind(this, 42)).not.throw(); |
| 22 | + // @ts-expect-error - Testing invalid input |
| 23 | + expect(callIfExists.bind(this, {})).not.throw(); |
| 24 | + }); |
| 25 | + |
13 | 26 | it("should call the function passed", () => { |
14 | 27 | const func = cy.stub(); |
15 | | - |
16 | 28 | callIfExists(func); |
17 | | - |
18 | 29 | expect(func).to.have.been.called; |
19 | 30 | }); |
| 31 | + |
| 32 | + it("should pass arguments to the callback function", () => { |
| 33 | + const func = cy.stub(); |
| 34 | + callIfExists(func, "arg1", 42, { key: "value" }); |
| 35 | + expect(func).to.have.been.calledWith("arg1", 42, { key: "value" }); |
| 36 | + }); |
| 37 | + |
| 38 | + it("should return the result of the callback function", () => { |
| 39 | + const result = callIfExists(() => "test"); |
| 40 | + expect(result).to.equal("test"); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should return undefined when callback is not a function", () => { |
| 44 | + expect(callIfExists(undefined)).to.be.undefined; |
| 45 | + expect(callIfExists(null)).to.be.undefined; |
| 46 | + // @ts-expect-error - Testing invalid input |
| 47 | + expect(callIfExists("not a function")).to.be.undefined; |
| 48 | + // @ts-expect-error - Testing invalid input |
| 49 | + expect(callIfExists(42)).to.be.undefined; |
| 50 | + // @ts-expect-error - Testing invalid input |
| 51 | + expect(callIfExists({})).to.be.undefined; |
| 52 | + }); |
| 53 | + |
| 54 | + it("should handle functions with different return types", () => { |
| 55 | + expect(callIfExists(() => 42)).to.equal(42); |
| 56 | + expect(callIfExists(() => "string")).to.equal("string"); |
| 57 | + expect(callIfExists(() => true)).to.be.true; |
| 58 | + expect(callIfExists(() => ({ key: "value" }))).to.deep.equal({ key: "value" }); |
| 59 | + expect(callIfExists(() => [1, 2, 3])).to.deep.equal([1, 2, 3]); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should handle functions with different parameter types", () => { |
| 63 | + const stringFunc = (str: string) => str.toUpperCase(); |
| 64 | + const numberFunc = (num: number) => num * 2; |
| 65 | + const objectFunc = (obj: { key: string }) => obj.key; |
| 66 | + const arrayFunc = (arr: number[]) => arr.reduce((a, b) => a + b, 0); |
| 67 | + |
| 68 | + // @ts-expect-error - Testing type compatibility |
| 69 | + expect(callIfExists(stringFunc, "test")).to.equal("TEST"); |
| 70 | + // @ts-expect-error - Testing type compatibility |
| 71 | + expect(callIfExists(numberFunc, 21)).to.equal(42); |
| 72 | + // @ts-expect-error - Testing type compatibility |
| 73 | + expect(callIfExists(objectFunc, { key: "value" })).to.equal("value"); |
| 74 | + // @ts-expect-error - Testing type compatibility |
| 75 | + expect(callIfExists(arrayFunc, [1, 2, 3])).to.equal(6); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should handle async functions", async () => { |
| 79 | + const asyncFunc = async () => "async result"; |
| 80 | + const result = await callIfExists(asyncFunc); |
| 81 | + expect(result).to.equal("async result"); |
| 82 | + }); |
| 83 | + |
| 84 | + it("should handle functions that throw errors", () => { |
| 85 | + const errorFunc = () => { |
| 86 | + throw new Error("test error"); |
| 87 | + }; |
| 88 | + expect(() => callIfExists(errorFunc)).to.throw("test error"); |
| 89 | + }); |
| 90 | + |
| 91 | + it("should handle functions with rest parameters", () => { |
| 92 | + const sum = (...numbers: number[]) => numbers.reduce((a, b) => a + b, 0); |
| 93 | + // @ts-expect-error - Testing type compatibility |
| 94 | + expect(callIfExists(sum, 1, 2, 3, 4)).to.equal(10); |
| 95 | + }); |
| 96 | + |
| 97 | + it("should handle functions with optional parameters", () => { |
| 98 | + const greet = (name: string, greeting = "Hello") => `${greeting}, ${name}!`; |
| 99 | + // @ts-expect-error - Testing type compatibility |
| 100 | + expect(callIfExists(greet, "John")).to.equal("Hello, John!"); |
| 101 | + // @ts-expect-error - Testing type compatibility |
| 102 | + expect(callIfExists(greet, "John", "Hi")).to.equal("Hi, John!"); |
| 103 | + }); |
20 | 104 | }); |
0 commit comments