-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from savanesoff/unit-tests
API changes, UTs, Readme
- Loading branch information
Showing
7 changed files
with
351 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,72 @@ | ||
import { screen } from "@testing-library/dom"; | ||
import "vitest-dom/extend-expect"; | ||
import Chopper from "./../src"; | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
function isNullOrUndefined(value: unknown): boolean { | ||
return value === null || value === undefined; | ||
} | ||
import { ControlProps } from "overdrag"; | ||
import { getRandomValue } from "./setup"; | ||
|
||
/** | ||
* Mock HTMLElement.offsetParent as it is not supported in JEST DOM | ||
*/ | ||
Object.defineProperty(HTMLElement.prototype, "offsetParent", { | ||
get() { | ||
// eslint-disable-next-line @typescript-eslint/no-this-alias | ||
let element = this; | ||
while ( | ||
!isNullOrUndefined(element) && | ||
(isNullOrUndefined(element.style) || | ||
isNullOrUndefined(element.style.display) || | ||
element.style.display.toLowerCase() !== "none") | ||
) { | ||
element = element.parentNode; | ||
} | ||
afterEach(() => { | ||
// Reset mock function calls | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
if (!isNullOrUndefined(element)) { | ||
return null; | ||
} | ||
describe("Constructor", () => { | ||
it(`should be defined`, () => { | ||
expect(Chopper).toBeDefined(); | ||
}); | ||
|
||
if ( | ||
!isNullOrUndefined(this.style) && | ||
!isNullOrUndefined(this.style.position) && | ||
this.style.position.toLowerCase() === "fixed" | ||
) { | ||
return null; | ||
} | ||
it("should render chopper", () => { | ||
new Chopper({ name: "Super name" }); | ||
expect(screen.queryByText("Super name")).toBeInTheDocument(); | ||
}); | ||
|
||
if ( | ||
this.tagName.toLowerCase() === "html" || | ||
this.tagName.toLowerCase() === "body" | ||
) { | ||
return null; | ||
} | ||
it('should have log level "verbose" by default', () => { | ||
const chopper = new Chopper({ name: "Super name" }); | ||
expect(chopper.getLogLevel()).toBe("verbose"); | ||
}); | ||
|
||
return this.parentNode; | ||
}, | ||
it("Should initialize super with parameters", () => { | ||
const element = document.createElement("div"); | ||
globalThis.document.body.appendChild(element); | ||
const overdragProps: Partial<ControlProps> = { | ||
element, | ||
maxContentHeight: getRandomValue(), | ||
maxContentWidth: getRandomValue(), | ||
minContentHeight: getRandomValue(), | ||
minContentWidth: getRandomValue(), | ||
snapThreshold: getRandomValue(), | ||
controlsThreshold: getRandomValue(), | ||
clickDetectionThreshold: getRandomValue(), | ||
}; | ||
const chopper = new Chopper({ name: "Super name", ...overdragProps }); | ||
expect(chopper.element).toBe(overdragProps.element); | ||
expect(chopper.maxContentHeight).toBe(overdragProps.maxContentHeight); | ||
expect(chopper.maxContentWidth).toBe(overdragProps.maxContentWidth); | ||
expect(chopper.minContentHeight).toBe(overdragProps.minContentHeight); | ||
expect(chopper.minContentWidth).toBe(overdragProps.minContentWidth); | ||
expect(chopper.snapThreshold).toBe(overdragProps.snapThreshold); | ||
expect(chopper.controlsThreshold).toBe(overdragProps.controlsThreshold); | ||
expect(chopper.clickDetectionThreshold).toBe( | ||
overdragProps.clickDetectionThreshold | ||
); | ||
}); | ||
}); | ||
|
||
export function getRandomValue(min = 0, max = 50) { | ||
return min + Math.round(Math.random() * (max - min)); | ||
} | ||
|
||
export function getRandomPixelValue(min = 0, max = 50) { | ||
return `${getRandomValue(min, max)}px`; | ||
} | ||
|
||
describe("Constructor", () => { | ||
afterEach(() => { | ||
// Reset mock function calls | ||
vi.clearAllMocks(); | ||
describe("APIs", () => { | ||
let chopper: Chopper; | ||
beforeEach(() => { | ||
chopper = new Chopper({ name: "Super name" }); | ||
}); | ||
|
||
it(`Should be defined`, () => { | ||
expect(Chopper).toBeDefined(); | ||
describe("getLogLevel", () => { | ||
it("should return log level", () => { | ||
expect(chopper.getLogLevel()).toBe("verbose"); | ||
}); | ||
}); | ||
|
||
it("Should render chopper", () => { | ||
new Chopper({ name: "Super name" }); | ||
expect(screen.getByText("Super name")).toBeInTheDocument(); | ||
describe("setLogLevel", () => { | ||
it("should set log level", () => { | ||
chopper.setLogLevel("error"); | ||
expect(chopper.getLogLevel()).toBe("error"); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.