@applicaster/zapp-react-native-utils
Version:
Applicaster Zapp React Native utilities package
172 lines (129 loc) • 4.74 kB
JavaScript
import * as R from "ramda";
import * as stringUtils from "../index";
describe("capitalize", () => {
const { capitalize } = stringUtils;
it("capitilzes the first letter of a string", () => {
expect(capitalize("foo")).toBe("Foo");
});
});
describe("toCamelCase", () => {
const { toCamelCase } = stringUtils;
it("transforms a string to camelCase", () => {
const str1 = "foo_bar_baz";
const str2 = "foo.bar.baz";
const str3 = "foo-bar-baz";
const result = "fooBarBaz";
expect(toCamelCase(str1)).toBe(result);
expect(toCamelCase(str2)).toBe(result);
expect(toCamelCase(str3)).toBe(result);
});
});
describe("toPascalCase", () => {
const { toPascalCase } = stringUtils;
it("transforms a string to a pascal case string", () => {
const str1 = "foo_bar_baz";
const str2 = "foo.bar.baz";
const str3 = "foo-bar-baz";
const result = "FooBarBaz";
expect(toPascalCase(str1)).toBe(result);
expect(toPascalCase(str2)).toBe(result);
expect(toPascalCase(str3)).toBe(result);
});
});
describe("base64", () => {
const { to64, from64 } = stringUtils;
const str = "foo";
const base64str = Buffer.from(str).toString("base64");
it("returns the original strings when both transforms are chained", () => {
expect(R.compose(from64, to64)(str)).toEqual(str);
});
describe("to64", () => {
it("transforms a string to base 64", () => {
expect(to64(str)).toEqual(base64str);
});
});
describe("from64", () => {
it("transforms a base64 string to a plain string", () => {
expect(from64(base64str)).toEqual(str);
});
});
});
describe("inflate string", () => {
const { inflateString } = stringUtils;
it("inflates a string with the data from a provided data", () => {
const string = "this is a {{foo}} string";
const data = { foo: "templated" };
expect(inflateString({ string, data })).toBe("this is a templated string");
});
it("leaves empty space if the requested property is not found", () => {
const string = "this is a {{bar}} string";
const data = { foo: "templated" };
expect(inflateString({ string, data })).toBe("this is a string");
});
it("supports nested properties", () => {
const string = "this is a {{foo.bar}} string";
const data = { foo: { bar: "templated" } };
expect(inflateString({ string, data })).toBe("this is a templated string");
});
it("supports multiple variables", () => {
const string = "this is a {{type}} with {{characteristics}}";
const data = { type: "string", characteristics: "several vars" };
expect(inflateString({ string, data })).toBe(
"this is a string with several%20vars"
);
});
it("encodes special characters correctly", () => {
const string = "this is a string with {{specialCharacters}}";
const data = {
specialCharacters: "special characters: !@#$%^&*()-_=+[]{}|;:',./<>?",
};
expect(inflateString({ string, data })).toBe(
"this is a string with special%20characters%3A%20!%40%23%24%25%5E%26*()-_%3D%2B%5B%5D%7B%7D%7C%3B%3A'%2C.%2F%3C%3E%3F"
);
});
it("supports custom separators", () => {
const string = "this is a <% foo %> with custom <% bar %>";
const data = { foo: "string", bar: "separators" };
const separators = {
start: "<%",
end: "%>",
};
expect(inflateString({ string, separators, data })).toBe(
"this is a string with custom separators"
);
});
});
describe("objectToReadableString", () => {
const { objectToReadableString } = stringUtils;
it("transforms a object to a string with default separator", () => {
const object = { a: "test1", b: "test2", c: "test3" };
const defaultSeparator = "\r\n";
const result = `a: test1${defaultSeparator}b: test2${defaultSeparator}c: test3`;
expect(objectToReadableString(object)).toBe(result);
});
it("transforms a object to a string with custom separator", () => {
const object = { a: "test1", b: "test2", c: "test3" };
const separator = "<br />";
const result = `a: test1${separator}b: test2${separator}c: test3`;
expect(objectToReadableString(object, separator)).toBe(result);
});
});
describe("isString", () => {
const { isString } = stringUtils;
it("returns true if string", () => {
const result = isString("string");
expect(result).toEqual(true);
});
it("returns false if number", () => {
const result = isString(1);
expect(result).toEqual(false);
});
it("returns false if object", () => {
const result = isString({});
expect(result).toEqual(false);
});
it("returns false if array", () => {
const result = isString([]);
expect(result).toEqual(false);
});
});