2023-08-21 17:11:47 +00:00
|
|
|
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");
|
|
|
|
});
|
|
|
|
|
2023-08-24 10:19:10 +00:00
|
|
|
it("should convert spaces, _, +, # and any other special character to -", () => {
|
2023-08-21 17:11:47 +00:00
|
|
|
expect(slugify("hello there")).toEqual("hello-there");
|
|
|
|
expect(slugify("hello_there")).toEqual("hello-there");
|
|
|
|
expect(slugify("hello$there")).toEqual("hello-there");
|
2023-08-24 10:19:10 +00:00
|
|
|
expect(slugify("hello+there")).toEqual("hello-there");
|
|
|
|
expect(slugify("#hellothere")).toEqual("hellothere");
|
2023-08-21 17:11:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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");
|
|
|
|
});
|
|
|
|
|
2023-10-23 12:37:30 +00:00
|
|
|
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");
|
|
|
|
});
|
|
|
|
|
2023-08-21 17:11:47 +00:00
|
|
|
// 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("");
|
|
|
|
});
|
|
|
|
});
|