From 7e676b0edceceef66c3d1e47852d44884208df44 Mon Sep 17 00:00:00 2001 From: bendtherules Date: Sat, 10 Aug 2019 12:58:46 +0530 Subject: [PATCH] Add testcases - utils.isConstructorFunctionComponent --- ...ls.isConstructorFunctionComponent.spec.tsx | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 test/utils.isConstructorFunctionComponent.spec.tsx diff --git a/test/utils.isConstructorFunctionComponent.spec.tsx b/test/utils.isConstructorFunctionComponent.spec.tsx new file mode 100644 index 0000000..eb477b7 --- /dev/null +++ b/test/utils.isConstructorFunctionComponent.spec.tsx @@ -0,0 +1,74 @@ +// import * as React from "react"; + +// Import stuff from src +import { isConstructorFunctionComponent } from "../src/utils"; +import { + createFunctionComponent, + createClassComponent +} from "./utils/createComponent"; + +// Import test helpers and sample components + +describe("utils", () => { + let container: HTMLDivElement; + + beforeEach(() => { + container = document.body.appendChild(document.createElement("div")); + }); + + afterEach(() => { + document.body.removeChild(container); + }); + + describe("isConstructorFunctionComponent", () => { + it("should work for any function - current limitation", () => { + const fn1 = function() {}; + + const resultPositive = isConstructorFunctionComponent(fn1 as any); + + expect(resultPositive).not.toBeFalsy(); + expect(resultPositive).toBe(true); + }); + + it("should work for function components", () => { + const fn1 = createFunctionComponent(); + + const resultPositive = isConstructorFunctionComponent(fn1); + + expect(resultPositive).not.toBeFalsy(); + expect(resultPositive).toBe(true); + }); + + it("should not work for string", () => { + const resultNegative = isConstructorFunctionComponent("div"); + + expect(resultNegative).toBeFalsy(); + expect(resultNegative).toBe(false); + }); + + it("should not work for null", () => { + const resultNegative = isConstructorFunctionComponent(null); + + expect(resultNegative).toBeFalsy(); + expect(resultNegative).toBe(false); + }); + + it("should work for any class - current limitation", () => { + const C1 = class {}; + + const resultPositive = isConstructorFunctionComponent(C1 as any); + + expect(resultPositive).not.toBeFalsy(); + expect(resultPositive).toBe(true); + }); + + it("should not work for class components", () => { + const C1 = createClassComponent("C1"); + + const resultNegative = isConstructorFunctionComponent(C1); + + expect(resultNegative).toBeFalsy(); + expect(resultNegative).toBe(false); + }); + }); +});