cal/packages/lib/slugify.test.ts
Udit Takkar aa54c013f8
fix: allow dots in username (#11706)
* fix: allow dots in username

* test: added unit tests for slugify

* test: add test for username change

* tests: add test  for username and dynamic booking

* fix: type error

---------

Co-authored-by: Peer Richelsen <peeroke@gmail.com>
2023-10-23 13:37:30 +01:00

54 lines
2.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { slugify } from "./slugify";
describe("slugify", () => {
it("should convert to lowercase", () => {
expect(slugify("Hello")).toEqual("hello");
expect(slugify("HELLO")).toEqual("hello");
});
it("should convert spaces, _, +, # and any other special character to -", () => {
expect(slugify("hello there")).toEqual("hello-there");
expect(slugify("hello_there")).toEqual("hello-there");
expect(slugify("hello$there")).toEqual("hello-there");
expect(slugify("hello+there")).toEqual("hello-there");
expect(slugify("#hellothere")).toEqual("hellothere");
});
it("should keep numbers as is", () => {
expect(slugify("hellothere123")).toEqual("hellothere123");
expect(slugify("321hello there123")).toEqual("321hello-there123");
expect(slugify("hello$there")).toEqual("hello-there");
});
// So that user can freely add spaces and any other character iteratively and it get's converted to - later on.
it("should remove dashes from start and end.", () => {
expect(slugify("hello-there-")).toEqual("hello-there");
expect(slugify("hello-there_")).toEqual("hello-there");
expect(slugify("_hello-there_")).toEqual("hello-there");
expect(slugify("$hello-there_")).toEqual("hello-there");
});
it("should keep periods as is except the start and end", () => {
expect(slugify("hello.there")).toEqual("hello.there");
expect(slugify("h.e.l.l.o.t.h.e.r.e")).toEqual("h.e.l.l.o.t.h.e.r.e");
});
it("should remove consecutive periods", () => {
expect(slugify("hello...there")).toEqual("hello.there");
expect(slugify("hello....there")).toEqual("hello.there");
expect(slugify("hello..there")).toEqual("hello.there");
});
it("should remove periods from start and end", () => {
expect(slugify(".hello.there")).toEqual("hello.there");
expect(slugify(".hello.there.")).toEqual("hello.there");
expect(slugify("hellothere.")).toEqual("hellothere");
});
// This is failing, if we want to fix it, one approach is as used in getValidRhfFieldName
it.skip("should remove unicode and emoji characters", () => {
expect(slugify("Hello 📚🕯️®️ There")).toEqual("hello---------there");
expect(slugify("📚🕯️®️")).toEqual("");
});
});